I am writing a custom memory allocator in my program and trying to better understand what is considered allocated vs unallocated memory. I am told that for a basic, "naive" sbrk()
memory allocator, calls to sbrk()
must provide a size aligned to (a multiple of) 16 bytes. This means that if I need to allocate for example, 5 bytes of memory, the operation (5 + (16-1)) & ~(16-1)) is applied, which rounds up to 16 in this case. If the size requested were 17 instead of 5, then it would round to 32.
This means that we are getting back from the operating system more bytes than the user requested for the sake of alignment. My question is, are the 11 bytes (in the case of the first example) or 15 bytes (in the case of the second example) considered "allocated" or not? In a proper implementation of a memory allocator, could the user actually use more than the requested bytes between the requested size and the 16 byte boundary? If not, how is this enforced?