CCareaga/heap_allocator logo

CCareaga/heap_allocator

Free

A simple heap memory allocator in ~200 lines.

FreeFree tier
Type
Open Source

About CCareaga/heap_allocator

SHMALL (Simple Heap Memory ALLocator) is a minimalistic heap memory allocator written in C, designed for educational purposes, particularly for those new to OS development. The implementation consists of approximately 230 lines of code, providing a simple yet functional version of malloc and free. It features binning with doubly-linked lists based on chunk size, coalescing of freed chunks, and a quick best-fit allocation strategy enabled by sorted free lists. The allocator supports easy expansion and contraction via a 'wilderness' chunk. The repository includes a commented source file and a demo program to illustrate usage.

Key Features

Binning uses doubly-linked lists based on size
Coalescing of freed chunks to reduce fragmentation
Quick best-fit allocation due to sorted free lists
Easy heap expansion and contraction via wilderness chunk
Very small codebase: ~230 lines (heap and linked-list)
Commented source code for learning

Pros & Cons

Pros
  • Extremely simple and easy to understand implementation
  • Includes coalescing and best-fit for reduced fragmentation
  • Small codebase allows quick study
  • Commented version provided for clarity
  • Easily extensible with custom binning functions
Cons
  • Not designed for production use; lacks concurrency support and optimization
  • Insertion into bins is O(n) due to sorting, not O(1)
  • Limited to single-heap, single-thread scenarios
  • Requires pre-allocated memory region (uses malloc in demo)

Best For

Educational tool for learning heap memory allocation internalsHobby OS development where a minimal allocator is neededUnderstanding how malloc and free functions work

FAQ

What is SHMALL?
SHMALL stands for Simple Heap Memory ALLocator. It is a minimal heap allocator written in C, intended for educational use and hobby OS development.
How many lines of code is the allocator?
The allocator is about 230 lines of code, including the heap and linked-list functionality.
What features does it have?
It uses binning with doubly-linked lists based on chunk size, coalesces freed chunks, and implements quick best-fit allocation due to sorted free lists. It also supports easy expansion and contraction.
How is the heap initialized?
The heap must be provided a block of memory. In the demo, malloc supplies that memory. The init_heap function sets up one large chunk with a header and footer.
Is this allocator production-ready?
No, it is designed for educational purposes and lacks features like concurrency support, performance optimizations, and robustness for production environments.