Block Splitting
1 / 5
malloc(50) finds 1000-byte block
Without split: 950 bytes wasted!
Internal fragmentation = waste inside blocks
Before: [hdr | 1000 free ]
After: [hdr|50 used][hdr|~926 free ]
Split large blocks when allocating small amounts.
#define MIN_BLOCK_SIZE (sizeof(header) + 16)
// Only split if remainder is useful
remaining = block->size - aligned_request;
can_split = remaining >= MIN_BLOCK_SIZE;
new_block = (block_header_t*)(
(char*)block + sizeof(header) + aligned_size
);
Don't forget alignment!