This project has three core functions:
my_malloc— allocates a block of memory from a fixed-size heap;my_free— returns a previously allocated block back to the heap;my_realloc— resizes an existing allocation;
The heap is a fixed-size byte array. It is divided into fixed-size blocks of 16 bytes each. A bitmap tracks which blocks are free or occupied: each bit in the bitmap corresponds to one block, where 0 means free and 1 means used.
When my_malloc is called, the allocator scans the bitmap using a First Fit strategy: it looks for the first run of consecutive free blocks large enough to satisfy the request, marks them as used, and returns a pointer to that region of the heap.
Each allocation stores a small header (a size_t) just before the user data, recording how many blocks that allocation is using. my_free and my_realloc read this header to know exactly how much space to release or move.
When my_free is called, the allocator locates the corresponding blocks in the bitmap and clears their bits, making them available again.
my_realloc is built on top of my_malloc and my_free: it allocates a new region, copies the old data across, and frees the original block.
chmod +x build.sh
./build.sh
./bin/memalloc