How/where is sbrk used within malloc.c?

2.6k views Asked by At

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??)

1

There are 1 answers

4
interjay On

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

Note that some malloc implementations may use mmap to get more memory instead of sbrk.