I have read in Advanced Unix Programming (and also in a few other books) that Linux malloc()
uses the Linux system call sbrk()
to request memory from the operating system.
I am looking at the glibc malloc.c
code and I can see many mentions of sbrk()
in the comments, but not referred to directly in the code.
How/where is sbrk()
referred to/used when malloc()
requests memory from the OS?
(This could be a general misunderstanding on my part of how system calls are made from the C runtime library. If so, I would be interested to know how they are made??)
Glibc's malloc.c requests more memory by calling the function stored in the
__morecore
global function pointer (the call actually uses the macroMORECORE
which expands to__morecore
). By default, this holds the address of function__default_morecore
, which is defined in morecore.c. This function callssbrk
.Note that some
malloc
implementations may usemmap
to get more memory instead ofsbrk
.