Managing the Heap
Each
Unix process owns a specific memory region called
heap
,
which is used to satisfy the process’s dynamic
memory requests. The start_brk and
brk fields of the memory descriptor delimit the
starting and ending addresses, respectively, of that region.
The following C library functions can be used by the process to request and release dynamic memory:
-
malloc(size) Requests
sizebytes of dynamic memory; if the allocation succeeds, it returns the linear address of the first memory location.-
calloc(n,size) Requests an array consisting of
nelements of sizesize; if the allocation succeeds, it initializes the array components to 0 and returns the linear address of the first element.-
free(addr) Releases the memory region allocated by
malloc( )orcalloc( )that has an initial address ofaddr.-
brk(addr) Modifies the size of the heap directly; the
addrparameter specifies the new value ofcurrent->mm->brk, and the return value is the new ending address of the memory region (the process must check whether it coincides with the requestedaddrvalue).-
sbrk(incr) Is similar to
brk( ), except that theincrparameter specifies the increment or decrement of the heap size in bytes.
The brk( ) function differs from the other
functions listed because it is the only one implemented as a system
call. All the other functions are implemented in the C library by
using brk( ) and mmap( ).
When a process in User Mode invokes the brk( )
system call, the kernel executes the sys_brk(addr) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access