Heap Fragmentation Analyzer
Walk an allocator's block list and report fragmentation: total free bytes, the largest contiguous free block, and a fragmentation ratio that tells you when a large allocation will fail despite plenty of free memory.
Constraints
- Blocks form a singly linked list with a size and a used flag.
- Adjacent free blocks count as one contiguous run.
- Fragmentation ratio is 1 minus largest_free over total_free.
- A fully free heap reports a ratio of 0.
- An empty heap must not divide by zero.
- No allocation during the walk.
1#include <cstddef>2 3struct Block { size_t size; bool used; Block* next; };4 5struct HeapStats { size_t total_free; size_t largest_free; double fragmentation; };6 7HeapStats analyze(const Block* head);