3 ms·
But that malloc is implemented in C lib doesn’t mean that the question and the answers are wrong. Eventually system specific calls have to occur. The question i
by acqq 7y ago
But that malloc is implemented in C lib doesn’t mean that the question and the answers are wrong. Eventually system specific calls have to occur. The question is very specific, asking what happens in Linux when one makes a malloc call requesting a big allocation:
“does it actually create 1 GiB worth of swap pages? Or does it mprotect the address range and create the pages when you actually touch them like mmap does?”
And how it is implemented can be seen, as an example, here:
https://sources.debian.org/src/glibc/2.28-10/malloc/malloc.c/ https://sources.debian.org/src/glibc/2.28-10/malloc/malloc.c...
Note both mprotect and mmap references.
- temac 7y agoThe question is only semi-specific and the result depends on a ton of variables, like: which libc do you use; which kernel (maybe a patched linux kernel like on Android, don't know if they change anything regarding memory alloc though) kernel configuration do you use (esp. in regard with overcommit), do you have any other additional tuning, etc. > does it actually create 1 GiB worth of swap pages Create? No; probably never. > does it mprotect the address range and create the pages when you actually touch them like mmap does? Both mmap and mprotect are syscalls, and syscalls do not (directly) call other syscalls. So mmap does not do that. And malloc can't because it does not run anymore after it has returned. On top of all of that, malloc(insanely_big_value) can still fail immediately by returning NULL even if you have overcommit activated. Granted, 1GB is probably never an "insanely_big_value"? Not even sure on 32 bits systems though.
- acqq 7y agoAsking "in Linux" obviously didn't mean "on Android" and also not "in Linux after I patch the kernel configuration." The question was clear, the answer on the site was clear, by the answer here to which I responded was by somebody who I suspect simply hasn't read what was written there.
- saagarjha 7y agoYou can write a malloc that performs no system calls and hands out chunks of the data segment you’ve set aside (e.g. by creating a large array in your program). But I think it’s clear in context that the question is talking about one of the standard Linux memory allocators, probably Glibc’s, which will brk/mmap from the OS and hand that out.