This project demonstrates a custom implementation of a dynamic memory allocator using the sbrk system call. It manages memory allocation and deallocation with a best-fit strategy, a linked-list-based free list, and block splitting/coalescing mechanisms.
- Uses
sbrkto request memory from the operating system. - Implements a best-fit strategy to find the most suitable free block for allocation.
- Splits larger blocks into smaller usable and free blocks to optimize space usage.
- Coalesces adjacent free blocks during deallocation to avoid fragmentation.
- Text: Stores the program's executable instructions.
- Data: Contains global and static variables.
- Heap: Dynamic memory managed by
malloc,calloc, andfree. It grows upward as more memory is allocated. - Unallocated Memory: Space between the program break (managed with
sbrk) and the stack. - Stack: Stores local variables and function call information. It grows downward.
The free list tracks unused memory blocks that are available for future allocations. Each block contains metadata and pointers for a doubly linked list.
- Fields:
size: Size of the memory block.prv: Pointer to the previous block.nxt: Pointer to the next block.
Operations:
- Add newly freed blocks back to the free list.
- Merge adjacent free blocks during coalescing.
This flowchart outlines the steps to allocate memory:
- Check the free list for a suitable block:
- If a block of the required size is found:
- If it's larger than the requested size, split it into two blocks.
- Use the required part and leave the remaining part in the free list.
- If no suitable block is found, request more memory using
sbrk(size).
- If a block of the required size is found:
- Return the allocated memory to the caller.


