I’m writing my own memory allocator. If my notion is right, there is kernel’s addres space from zero to 1-GB, and user-space from 1 to 4, that contains different sections like: code, stack, data, heap and others.
- Is it right, that heap section’s size and position can’t be changed during the program execution?
- How can I get the size and the position?
- All I need to do, after getting heap area, is allocate the memory at my discretion, isn’t I?
Why do you worry about this?
If you’re writing an allocator a-la libc, use
sbrkand/ormmapto reserve memory from the kernel. You do not need to worry about where the heap is placed with neither of those system calls.If you want to instrument libc’s
malloc/calloc/realloc, things are even simpler – just wrap them in your allocator functions.Yes, the allocator effectively manages the heap by requesting memory from the kernel. Typically, as is the case with
brk, its position lies after the end of the data segment, and it grows at increasing addresses (or allocates in multiples of page size with mmap, etc.)The allocator manages the size; the position of the heap is not relevant as far as the allocator is concerned, but it’s at the position of knowing it.
The allocator effectively requests memory from the kernel. Once it has that memory, it can distribute it to programs however it deems fit.