</>EmbeddedInterview

Implement a variable-size static heap allocator with splitting, coalescing and alignment.

HardCoding: Memory & String Functions

Short answer: Reserve a static aligned heap. Maintain a sorted list of blocks (offset, size, free flag). Allocate using first fit, split a larger block, and merge adjacent free blocks on release. This educational allocator keeps metadata outside the heap and allocates byte buffers in aligned units.

#include <stddef.h>
#include <stdbool.h>

#define ALIGN _Alignof(max_align_t)
#define UNITS 64
#define MAX_BLOCKS UNITS

_Alignas(max_align_t) static unsigned char heap[UNITS * ALIGN];

struct block {
    size_t off, size; /* measured in alignment units */
    bool free;
};
static struct block b[MAX_BLOCKS] = {{0, UNITS, true}};
static size_t nb = 1;

void *heap_alloc(size_t bytes)
{
    if (!bytes || bytes > sizeof heap) return NULL;
    size_t need = (bytes + ALIGN - 1) / ALIGN;

    for (size_t i = 0; i < nb; i++) {
        if (!b[i].free || b[i].size < need) continue;

        if (b[i].size > need && nb < MAX_BLOCKS) {
            for (size_t j = nb; j > i + 1; j--)
                b[j] = b[j-1];
            b[i+1] = (struct block){b[i].off + need,
                                   b[i].size - need, true};
            b[i].size = need;
            nb++;
        }
        b[i].free = false;
        return heap + b[i].off * ALIGN;
    }
    return NULL;
}

bool heap_free(void *p)
{
    if (!p) return true;
    size_t i;
    for (i = 0; i < nb; i++)
        if (p == heap + b[i].off * ALIGN) break;
    if (i == nb || b[i].free) return false;
    b[i].free = true;

    for (size_t j = 0; j + 1 < nb;) {
        if (b[j].free && b[j+1].free) {
            b[j].size += b[j+1].size;
            for (size_t k = j+1; k+1 < nb; k++)
                b[k] = b[k+1];
            nb--;
        } else j++;
    }
    return true;
}

Example: With 16-byte alignment, requesting 20 bytes reserves two units (32 bytes). Freeing adjacent allocations merges their blocks and reduces external fragmentation. The aligned heap base and unit-sized offsets guarantee fundamental alignment; for an explicitly 64-byte-aligned byte pool, use _Alignas(64) and 64-byte units on a platform that supports that alignment.

Allocation is O(B) for B blocks; freeing can be O(B²) due to metadata shifts. The allocator rejects foreign pointers and detectable double frees, but cannot detect stale pointers after an address is reused. It is not a conforming general-purpose malloc replacement (the backing array has a declared type); use for byte buffers or platform-supported storage. No concurrent or ISR safety is implied; add synchronization if required.