AltMem is a custom user-space memory allocator implemented in C. It serves as an educational and practical demonstration of heap memory management, showcasing how standard library functions like malloc and free operate under the hood using OS-level system calls.
- 8-Byte Alignment: Ensures all allocated blocks are aligned to 8-byte boundaries for CPU access efficiency.
- Doubly-Linked List: Uses a metadata header (
block_meta_t) prepended to each block to manage free and allocated blocks. - System Call Integration: Dynamically requests heap memory from the operating system using
sbrkfor small allocations andmmapfor large allocations. - Basic Memory Operations: Exposes functions for allocation and deallocation (
alt_mallocandalt_free). - Best-Fit Block Reuse: Traverses the block list using a Best-Fit strategy to find the smallest free block that satisfies the allocation request, minimizing wasted space.
- Block Splitting: Splits a reused free block if it is larger than the requested size plus metadata, reserving the remaining space for future allocations to prevent internal fragmentation.
- Coalescing (Block Merging): Automatically merges adjacent free blocks (both forward and backward) during
alt_freeto combat memory fragmentation. mmapSupport: Large allocations (>= 128 KiB) bypass the customsbrkheap and are allocated directly from the OS using anonymous virtual memory mapping (mmap). When freed, these blocks are immediately released back to the OS usingmunmap.- Automatic Leak Detection: Includes a built-in leak detector (
alt_check_leaks) registered via__attribute__((destructor))that automatically fires when the program exits, scanning the allocation linked list and reporting any unfreed block sizes and addresses tostderr.
- alt_mem.h: Public header declaring the allocator's API.
- alt_mem.c: Core implementation including internal metadata structures, block tracking, and allocation logic.
- main.c: Simple test harness to demonstrate allocator operations.
Allocates size bytes of memory.
- Returns: A pointer to the user payload space, or
NULLif allocation fails. - Details: Aligns the requested size to an 8-byte multiple. If the size is less than 128 KiB, it searches for a best-fit free block, splitting it if possible; otherwise, it extends the heap using
sbrk. For allocations of 128 KiB or larger, it requests memory from the OS usingmmap.
Frees the memory space pointed to by ptr.
- Details: Retrieves the metadata block preceding the user pointer. If the block was allocated via
sbrk(marked asSTATUS_ALLOC), it marks its status asSTATUS_FREEand coalesces adjacent free blocks. If it was allocated viammap(marked asSTATUS_MAPPED), it unmaps the memory usingmunmapand updates the block list. IfptrisNULL, no action is taken.
Allocates zero-initialized memory for an array of nmemb elements, each of size bytes.
- Returns: A pointer to the zero-initialized user payload space, or
NULLif allocation fails (including due to multiplication overflow). - Details: Computes the total size as
nmemb * sizeand checks for integer overflow. Aligns the size to an 8-byte multiple. If the size is less than 128 KiB, it allocates memory usingsbrk(applying best-fit search, block splitting, or heap extension) and zeroes the payload memory withmemset. For size 128 KiB or larger, it requests zero-initialized memory from the OS usingmmap.
Resizes the memory block pointed to by ptr to size bytes.
- Returns: A pointer to the resized memory block, or
NULLif resizing fails. - Details:
- Edge Cases: If
ptrisNULL, it acts asalt_malloc(size). Ifsizeis 0, it acts asalt_free(ptr)and returnsNULL. - Mmapped Blocks: Allocates a new block, copies the data (minimum of old/new sizes), and frees the original mapped block.
- Sbrk Blocks:
- Shrinking: If the requested size is smaller, it shrinks in-place. If the freed space is large enough to contain block metadata plus aligned payload, the block is split, and the remainder is freed and coalesced forward.
- Growing: It checks if the adjacent next block in memory is free. If so, and the combined size is sufficient, it grows in-place by coalescing with that next block (splitting any excess remainder). If growing in-place is not possible, it allocates a new block, copies the data, and frees the old block.
- Edge Cases: If
To compile and run the included test suite, follow these steps:
-
Compile:
gcc alt_mem.c main.c -o allocator_test
-
Execute:
./allocator_test
The allocator is fully implemented and complete. It supports:
- Best-fit block reuse and splitting for heap allocations (
sbrk). - Automatic coalescing (merging) of adjacent free blocks to prevent fragmentation.
- Transparent
mmapredirection for allocations larger than 128 KiB. - Complete C standard library equivalent memory allocator API including:
alt_malloc,alt_free,alt_calloc, andalt_realloc. - Automatic leak detection running via a destructor at program exit.