/*
* Custom memory allocator for embedded systems.
* Features:
* - Fixed-size memory pool.
* - First-fit allocation with block splitting.
* - Address-ordered free list with coalescing (merges adjacent free blocks).
* - 8-byte alignment (configurable).
* - Low overhead: ~16 bytes per block header (size_t + pointer).
* - No dependencies on libc malloc/free.
* - Suitable for limited resources (e.g., 1KB to 64KB pools).
* - O(n) time where n is number of free blocks (efficient for small pools).
*
* Usage:
* allocator_t alloc;
* allocator_init(&alloc, pool_buffer, sizeof(pool_buffer));
* void *p = allocator_malloc(&alloc, 100);
* allocator_free(&alloc, p);
*
* Limitations:
* - Fixed pool size (out-of-memory if exhausted).
* - No support for > pool_size allocations.
* - No thread-safety (add mutex if needed).
* - Minimal error checking (add assertions/magic numbers if paranoid).
*/
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#define ALLOCATOR_ALIGNMENT 8U
#define ALIGN_MASK (ALLOCATOR_ALIGNMENT - 1U)
#define ALIGN_UP(size) (((size) + ALIGN_MASK) & ~ALIGN_MASK)
typedef struct block_header {
size_t size; // Total block size INCLUDING this header (aligned).
struct block_header *next; // Next free block (NULL or valid free block ptr for used).
} block_header_t;
typedef struct {
block_header_t *free_list;
uint8_t *pool_start;
size_t pool_size;
} allocator_t;
/*
* Initialize the allocator with a fixed pool.
* pool: Pre-allocated buffer (must be ALIGNMENT-aligned for best results).
* size: Size of the pool in bytes (should be reasonable, e.g., 1024-65536).
*/
void allocator_init(allocator_t *alloc, uint8_t *pool, size_t size) {
alloc->pool_start = pool;
alloc->pool_size = ALIGN_UP(size);
alloc->free_list = (block_header_t *)pool;
alloc->free_list->size = alloc->pool_size;
alloc->free_list->next = NULL;
}
/*
* Allocate 'size' bytes (rounded up to alignment).
* Returns pointer to user data (header is hidden before it), or NULL on failure.
*/
void *allocator_malloc(allocator_t *alloc, size_t size) {
if (size == 0) {
return NULL;
}
// Total block size: user size + header, aligned.
size_t block_size = ALIGN_UP(size + sizeof(block_header_t));
if (block_size > alloc->pool_size) {
return NULL;
}
block_header_t **prev = &alloc->free_list;
block_header_t *curr = alloc->free_list;
while (curr != NULL) {
if (curr->size >= block_size) {
// Remove from free list.
*prev = curr->next;
if (curr->size >= block_size + sizeof(block_header_t)) {
// Split: create remainder free block.
block_header_t *remainder = (block_header_t *)
((uint8_t *)curr + block_size);
remainder->size = curr->size - block_size;
remainder->next = alloc->free_list;
alloc->free_list = remainder;
}
// Return pointer to payload (after header).
return (uint8_t *)curr + sizeof(block_header_t);
}
prev = &curr->next;
curr = curr->next;
}
return NULL; // No suitable block found.
}
/*
* Free a previously allocated pointer.
* Merges with adjacent free blocks to reduce fragmentation.
*/
void allocator_free(allocator_t *alloc, void *ptr) {
if (ptr == NULL) {
return;
}
block_header_t *to_free = (block_header_t *)((uint8_t *)ptr - sizeof(block_header_t));
uintptr_t block_start = (uintptr_t)to_free;
uintptr_t block_end = block_start + to_free->size;
// Ignore invalid frees (basic sanity check).
if (block_start < (uintptr_t)alloc->pool_start ||
block_end > (uintptr_t)alloc->pool_start + alloc->pool_size) {
return;
}
// Find insertion point in address-sorted free list.
block_header_t **curr_ptr = &alloc->free_list;
block_header_t *prev_free = NULL;
while (*curr_ptr != NULL && (uintptr_t)*curr_ptr < block_start) {
prev_free = *curr_ptr;
curr_ptr = &(*curr_ptr)->next;
}
// Insert freed block.
to_free->next = *curr_ptr;
*curr_ptr = to_free;
// Coalesce with previous free block if adjacent.
if (prev_free != NULL &&
(uintptr_t)prev_free + prev_free->size == block_start) {
// Merge into previous.
prev_free->size += to_free->size;
prev_free->next = to_free->next;
// Now check if we can also merge with next.
block_header_t *next_free = prev_free->next;
if (next_free != NULL &&
(uintptr_t)prev_free + prev_free->size == (uintptr_t)next_free) {
prev_free->size += next_free->size;
prev_free->next = next_free->next;
}
} else {
// No previous merge: check next free block.
block_header_t *next_free = to_free->next;
if (next_free != NULL && block_end == (uintptr_t)next_free) {
to_free->size += next_free->size;
to_free->next = next_free->next;
}
}
}
/*
* Optional: Get bytes remaining free.
*/
size_t allocator_free_bytes(allocator_t *alloc) {
size_t free = 0;
block_header_t *curr = alloc->free_list;
while (curr != NULL) {
free += curr->size;
curr = curr->next;
}
return free;
}
/* Example usage:
*
* #define EXAMPLE_POOL_SIZE 4096
* static uint8_t example_pool[EXAMPLE_POOL_SIZE];
*
* int main(void) {
* allocator_t alloc;
* allocator_init(&alloc, example_pool, EXAMPLE_POOL_SIZE);
*
* void *p1 = allocator_malloc(&alloc, 100);
* void *p2 = allocator_malloc(&alloc, 200);
* allocator_free(&alloc, p1);
* void *p3 = allocator_malloc(&alloc, 150); // May reuse coalesced space
*
* // ... etc.
* }
*/
This allocator is lightweight, efficient for embedded use, and handles fragmentation well through coalescing. Header overhead is minimal (~12-16 bytes on 32/64-bit systems). Test thoroughly in your environment, and consider adding a magic number in the header for corruption detection if needed (e.g., uint32_t magic; after size). For multi-threaded use, wrap calls in a mutex.