A thread-safe implementation of a basic memory allocator in C, providing custom malloc()
functionality.
- Thread-safe memory allocation using mutex locks
- Free block reuse
- Linked list memory block management
- Memory alignment support
The allocator uses a linked list of memory blocks, each with a header containing:
- Block size
- Free/occupied status
- Pointer to next block
struct head_t {
size_t size;
unsigned is_free;
struct head_t *next;
};
- Block Reuse: Searches for existing free blocks before requesting new memory
- Memory Extension: Uses
sbrk()
to extend heap when needed - Thread Safety: Implements mutex locks for concurrent access
void *ptr1 = malloc(100); // Allocate 100 bytes
void *ptr2 = malloc(50); // Allocate 50 bytes
Compile with pthread support:
gcc -pthread allocator.c -o allocator
- No memory coalescing
- No splitting of larger blocks
- Simple first-fit allocation strategy
MIT License
Pull requests welcome. Please ensure thread safety is maintained in any modifications.