mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 10:15:57 +01:00
mmap anonymous: configurable size
This commit is contained in:
26
README.adoc
26
README.adoc
@@ -12539,20 +12539,36 @@ link:userland/c/malloc.c[]: `malloc` hello world: allocate two ints and use them
|
||||
|
||||
LInux 5.1 / glibc 2.29 implements it with the <<mmap,`mmap` system call>>.
|
||||
|
||||
===== malloc out o fmemory
|
||||
===== malloc maximum size
|
||||
|
||||
Test how much memory Linux lets us allocate:
|
||||
Test how much memory Linux lets us allocate by doubling a buffer with `realloc` until it fails:
|
||||
|
||||
....
|
||||
./run --userland userland/c/out_of_memory.c
|
||||
./run --userland userland/c/malloc_max.c
|
||||
....
|
||||
|
||||
Source: link:userland/c/out_of_memory.c[]
|
||||
Source: link:userland/c/malloc_max.c[]
|
||||
|
||||
Outcome at c03d5d18ea971ae85d008101528d84c2ff25eb27 on Ubuntu 19.04 <<p51>> host: prints up to `0x1000000000` (64GiB).
|
||||
Outcome at c03d5d18ea971ae85d008101528d84c2ff25eb27 on Ubuntu 19.04 <<p51>> host (16GiB RAM): prints up to `0x1000000000` (64GiB).
|
||||
|
||||
TODO dive into source code.
|
||||
|
||||
TODO: if we do direct <<malloc>> allocations with link:userland/c/malloc.c[] or <<mmap>> with link:userland/linux/mmap_anonymous.c[], then the limit was smaller than 64GiB!
|
||||
|
||||
These work:
|
||||
|
||||
....
|
||||
./userland/c/malloc.out 0x100000000
|
||||
./userland/linux/mmap_anonymous.out 0x100000000
|
||||
....
|
||||
|
||||
which is `4Gib * sizeof(int) == 16GiB`, but these fail at 32GiB:
|
||||
|
||||
....
|
||||
./userland/c/malloc.out 0x200000000
|
||||
./userland/linux/mmap_anonymous.out 0x200000000
|
||||
....
|
||||
|
||||
Bibliography: https://stackoverflow.com/questions/2798330/maximum-memory-which-malloc-can-allocate
|
||||
|
||||
==== GCC C extensions
|
||||
|
||||
@@ -473,7 +473,7 @@ path_properties_tuples = (
|
||||
'file_write_read.c': {'baremetal': False},
|
||||
'getchar.c': {'interactive': True},
|
||||
'infinite_loop.c': {'more_than_1s': True},
|
||||
'out_of_memory.c': {'disrupts_system': True},
|
||||
'malloc_max.c': {'disrupts_system': True},
|
||||
'return1.c': {'exit_status': 1},
|
||||
'return2.c': {'exit_status': 2},
|
||||
}
|
||||
|
||||
@@ -4,11 +4,20 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int main(void) {
|
||||
int main(int argc, char **argv) {
|
||||
int *is;
|
||||
size_t nbytes = 2 * sizeof(*is);
|
||||
size_t nbytes, nints;
|
||||
|
||||
/* Allocate 2 ints. Note that unlike traditional stack arrays (non-VLA)
|
||||
/* Decide how many ints to allocate. */
|
||||
if (argc < 2) {
|
||||
nints = 2;
|
||||
} else {
|
||||
nints = strtoull(argv[1], NULL, 0);
|
||||
}
|
||||
nbytes = nints * sizeof(*is);
|
||||
|
||||
/* Allocate the ints.
|
||||
* Note that unlike traditional stack arrays (non-VLA)
|
||||
* this value does not have to be determined at compile time! */
|
||||
is = malloc(nbytes);
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
/* https://cirosantilli.com/linux-kernel-module-cheat#malloc */
|
||||
/* https://cirosantilli.com/linux-kernel-module-cheat#malloc-maximum-size */
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -5,11 +5,20 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/mman.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int main(void) {
|
||||
int main(int argc, char **argv) {
|
||||
int *is;
|
||||
size_t nbytes = 2 * sizeof(*is);
|
||||
size_t nbytes, nints;
|
||||
|
||||
/* Decide how many ints to allocate. */
|
||||
if (argc < 2) {
|
||||
nints = 2;
|
||||
} else {
|
||||
nints = strtoull(argv[1], NULL, 0);
|
||||
}
|
||||
nbytes = nints * sizeof(*is);
|
||||
|
||||
/* Allocate 2 ints. */
|
||||
is = mmap(
|
||||
|
||||
Reference in New Issue
Block a user