./run --userland userland/c/malloc_max.c+
diff --git a/index.html b/index.html index a7ff123..abc34b7 100644 --- a/index.html +++ b/index.html @@ -1210,7 +1210,11 @@ body.book #toc,body.book #preamble,body.book h1.sect0,body.book .sect1>h2{page-b
Allocate memory! Vs using the stack: https://stackoverflow.com/questions/4584089/what-is-the-function-of-the-push-pop-instructions-used-on-registers-in-x86-ass/33583134#33583134
userland/c/malloc.c: malloc hello world: allocate two ints and use them.
userland/c/out_of_memory.c: test how much memory Linux lets us allocate
-LInux 5.1 / glibc 2.29 implements it with the mmap system call.
Test how much memory Linux lets us allocate by doubling a buffer with realloc until it fails:
./run --userland userland/c/malloc_max.c+
Source: userland/c/malloc_max.c
+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 userland/c/malloc.c or mmap with 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+
The mmap system call allows advanced memory operations:
-userland/posix/mmap_file.c: memory mapped file example
-The mmap system call allows advanced memory operations.
mmap is notably used to implement the malloc ANSI C function, replacing the previously used break system call.
@@ -20774,12 +20810,39 @@ git -C "$(./getvar qemu_source_dir)" checkout -Linux adds has several POSIX extension flags to it.
Basic mmap example, do the same as userland/c/malloc.c, but with mmap.
Example: userland/linux/mmap_anonymous.c[]
+In POSIX 7 mmap always maps to a file.
+If we add the MAP_ANONYMOUS Linux extension however, this is not required, and mmap can be used to allocate memory like malloc.
+Memory mapped file example: userland/posix/mmap_file.c
+The example creates a file, mmaps to it, writes to maped memory, and then closes the file.
+We then read the file and confirm it was written to.
+Example: userland/glibc/brk.c
+Example: userland/linux/brk.c
The example allocates two ints and uses them, and then deallocates back.