I was reading through a presentation on the implementation of malloc, and on slide 7 it suggests storing a regions size and availability in a single word to save space. The alternative is to use two words, which is wasteful as the availability bit only needs to be 0 or 1.
This is the given explanation:
- If blocks are aligned, low-order address bits are always 0
- Why store an always-0 bit?
- Use it as allocated/free flag! When reading size word, must mask out this bit
http://courses.engr.illinois.edu/cs241/sp2012/lectures/09-malloc.pdf
But I’m not really understanding how this works and how it could be implemented in C. Why is one bit of the size integer always 0?
This is the key to understanding what it going on. Many CPUs require that multibyte primitive values be stored at addresses divisible by the number of bytes in the primitive: 16-bit primitives need to be stored at even addresses; 32-bit
ints need to be stored at addresses divisible by four, and so on. An attempt to access anintthrough a pointer that corresponds to an odd address results in a bus error.In systems like that
mallocmust always return an address suitable for storing any primitive supported by the given CPU. Therefore, if CPU supports 32-bit integers, all addresses returned bymallocmust be divisible by4. Such addresses are said to be aligned. To comply,mallocimplementations pad sizes blocks requested by the program by 0 to 3 bytes at the end to have length divisible by4. As a consequence of this decision, the last two bits of an address of an aligned block will always be zero. An implementation ofmalloccan use these bits for its own purposes, as long as they are “masked out” before returning the result to callers.