How / where is sbrk used in malloc.c?

I read in Advanced Unix Programming (and also in several other books) that Linux malloc()uses the Linux system call sbrk()to request memory from the operating system.

I am looking at glibc code malloc.c, and I can see many mentions sbrk()in the comments, but not mentioned directly in the code.

How / where is it sbrk()referenced / used when it malloc()requests memory from the OS?

(This may be a general misunderstanding as to how system calls are made from the C runtime library. If so, would I be interested to know how they are made?)

+4
source share
1 answer

Glibc malloc.c requests more memory by calling a function stored in the global function pointer __morecore(the call actually uses a macro MORECOREthat extends to __morecore). By default, this address contains the address of the function __default_morecore, which is defined in morecore.c . This function calls sbrk.

Note that some implementations mallocmay use mmapto get more memory instead sbrk.

+6
source

All Articles