A simple yet powerful dynamic memory management system that mimics the behavior of malloc
, free
, calloc
, and realloc
, tailored for both small and large memory allocations.
- Custom
malloc
,free
,calloc
,realloc
functions using data structures - Memory coalescing to reduce fragmentation
- Block splitting for efficient space use
- Separate strategies for small & large allocations
Include the header and link the implementation file:
#include "osmem.h"
Then use the custom allocation functions just like standard ones:
void *ptr = os_malloc(64);
os_free(ptr);
Allocates a block of memory of the specified size.
Uses internal data structures to track and deliver usable payloads.
Frees the memory block pointed to by
ptr
.
Handles bothsbrk
-based andmmap
-based memory allocations.
Allocates memory for an array and initializes it to zero.
Perfect for ensuring clean, zeroed memory.
Resizes a previously allocated block.
Handles all edge cases like expansion, shrinking, or creating new blocks.
- Uses
sbrk
syscall for linear heap expansion. - Preallocates a chunk on first use.
- Finds suitable free blocks or expands the heap.
- Uses
mmap
syscall for direct mapping of large memory blocks.
Adjacent free blocks are merged to prevent fragmentation.
Large blocks are split into smaller parts when possible.
βββ osmem.h # Header file with function declarations
βββ osmem.c # Implementation of the memory allocator
βββ README.md # You are here!