Every post about memory allocation seems to either explain how
mmap is used or how
sbrk was used, without regard for how these can be contextualized with the heap.
I have gathered that the heap plays an almost insignificant role in memory allocations -- in fact, I am not sure what it does :D, and I request that someone dis-confuse me.
This is what I understand:
1) When memory is malloced, the end of the uninitialized data segment, the BSS, is expanded. This expansion (which moves an address
x to, say,
x-n) takes place as a result of a call to
sbrk. In this model, memory is allocated into the
n bytes (assuming that each address is in correspondence with one byte) that
sbrk decreased the position of the BSS segment header by. This model is now obsolete. Some define the heap to be the space that is the aggregate of all such expansions. Others do not -- in the latter case, what does the heap do?
2) In modern memory allocation schemes, a heap does exist (for what reason, again, I am not sure). To allocate memory,
malloc internally uses
mmap to store data into memory regions that are a collection of pages. These memory regions are independent of the heap.
For old sytems: If memory allocations are stored into address space that is obtained after increasing the offset of the end BSS, then does a heap serve any purpose?
For newer systems: Granted that mmap is mainly used for memory allocations, then what purpose does the heap serve?
In both cases, does the heap really do anything useful?