Block Splitting

1 / 5

The Problem

malloc(50) finds 1000-byte block
Without split: 950 bytes wasted!

Internal fragmentation = waste inside blocks

2 / 5

The Solution

Before: [hdr | 1000 free            ]

After:  [hdr|50 used][hdr|~926 free ]

Split large blocks when allocating small amounts.

3 / 5

When to Split

#define MIN_BLOCK_SIZE (sizeof(header) + 16)

// Only split if remainder is useful
remaining = block->size - aligned_request;
can_split = remaining >= MIN_BLOCK_SIZE;
4 / 5

Split Pointer Math

new_block = (block_header_t*)(
    (char*)block + sizeof(header) + aligned_size
);

Don't forget alignment!

5 / 5
Use arrow keys or click edges to navigate. Press H to toggle help, F for fullscreen.