A multi-process safe memory allocator using POSIX shared memory.
- Process-shared memory allocation using
shm_open+mmap - Thread-safe with robust mutex
- Offset-based pointers for cross-process compatibility
- 16-byte aligned allocations
- Coalescing of free blocks
make # build library and tests
make debug # build with debug output
make clean # clean build files#include "memalloc.h"
// First process: create shared memory
shm_init();
// Other processes: attach to existing
shm_attach();
// Allocate memory
void *p = shm_malloc(100);
void *q = shm_calloc(10, sizeof(int));
p = shm_realloc(p, 200);
// Free memory
shm_free(p);
shm_free(q);
// Cleanup
shm_detach(); // unmap from this process
shm_destroy(); // destroy shared memoryshm_init()- Create and initialize shared memoryshm_attach()- Attach to existing shared memoryshm_detach()- Detach from shared memoryshm_destroy()- Destroy shared memory
shm_malloc(size)- Allocate memoryshm_calloc(n, size)- Allocate zeroed memoryshm_realloc(ptr, size)- Resize allocationshm_free(ptr)- Free memory
shm_validate()- Check heap integrity (returns 0 on success)shm_dump()- Print heap stateshm_stats(total, used, free)- Get memory statisticsshm_usable_size(ptr)- Get usable size of allocation
make run-tests # run all testsIndividual tests (in bin/):
test_basic- Basic alloc/freetest_stress- Multi-process stress testtest_realloc- Realloc behaviortest_alignment- 16-byte alignmenttest_edge- Edge cases (NULL, zero size)test_independent- Independent process attachtest_debug- Debug tools
MemAlloc/
├── include/
│ └── memalloc.h # Public header
├── src/
│ └── memalloc.c # Implementation
├── tests/
│ ├── test_basic.c
│ ├── test_stress.c
│ └── ...
├── bin/ # Compiled test binaries
├── Makefile
└── README.md