pmalloc Hero title graphic

pmalloc

An minimal malloc style heap memory manager for embedded systems

By Tom Cully Updated

Around 4 years ago I dusted off some the auld embedded chops for a competition project - an embedded VM called Mihini - and was very surprised that there didn't seem to be a simple, minimal, well documented malloc compatible library on Github.

I knocked one together. At the time the implementation worked well enough, a few issues were discovered, a couple serious. So for late 2026 I've blown the cobwebs off it for laughs - bugs fixed, CI, coverage, code quality, etc.

https://github.com/tomdionysus/pmalloc

Just copy in pmalloc.h and pmalloc.c, and use it like malloc:

#include "pmalloc.h"

static pmalloc_t heap;
static unsigned char memory[16384];  // or a fixed address and size on your board

int main(void) {
  pmalloc_init(&heap);
  pmalloc_addblock(&heap, memory, sizeof(memory));

  char *buffer = pmalloc_malloc(&heap, 256);
  if (buffer == NULL) return 1;  // out of memory

  char *bigger = pmalloc_realloc(&heap, buffer, 512);
  if (bigger != NULL) buffer = bigger;  // on failure, buffer is still valid

  pmalloc_free(&heap, buffer);
  return 0;
}

Like a lot of my stuff, pretty niche - but an embedded memory manager in under 800 bytes on a Cortex might be of use to someone somewhere.