Custom dynamic memory allocator implemented in C, simulating a heap manager that uses segregated free lists to track free and allocated memory blocks.
The allocator supports heap initialization, allocation, deallocation, writing, reading, and memory statistics reporting.
This project implements a simplified version of a memory management subsystem similar to those used in operating systems or runtime allocators.
The heap is divided into multiple free lists, each responsible for blocks of different sizes (powers of two).
Each allocation or deallocation dynamically updates these lists, keeping detailed metrics about fragmentation, memory usage, and block counts.
- Initializes the heap structure with multiple free lists.
- Each list handles blocks of size
2^(i+3)bytes. - Tracks start address, total memory, and fragmentation metadata.
- Allocates the smallest possible block that can satisfy the request.
- If necessary, splits larger blocks (fragmentation tracking enabled).
- Allocated blocks are tracked in a doubly linked list.
- Frees a specific block based on its start address.
- Reinserts the freed block into the appropriate free list.
- Handles invalid or double frees safely.
- Supports writing strings across multiple contiguous blocks.
- Prevents buffer overflows via contiguous block validation.
- Reading prints the data starting from a given address.
- Prints a detailed report of current heap status:
- Total / allocated / free memory
- Block distribution per list
- Fragmentation statistics
- Allocated block addresses and sizes
- Frees all allocated structures and resets state cleanly.
doubly_linked_list_t– used for both free lists and the allocated listinfo_t– stores each block’s address, size, and data pointerlist_t– represents the full heap metadata, containing:- Array of free lists
- Statistics (allocations, frees, fragmentation, etc.)
INIT_HEAP
MALLOC 128
WRITE 0x400 "Hello Allocator" 16
READ 0x400 16
FREE 0x400
DUMP_MEMORY
DESTROY_HEAP
- Implemented a full memory allocator with segregated free lists
- Practiced pointer arithmetic and dynamic structure management
- Designed algorithms for block fragmentation and merging
- Simulated system-level memory management behavior in user space