Skip to content

infernosalex/MemAlloc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MemAlloc — Shared Memory Allocator

GitHub

A multi-process safe memory allocator using POSIX shared memory.

Features

  • 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

Build

make        # build library and tests
make debug  # build with debug output
make clean  # clean build files

Usage

#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 memory

API

Lifecycle

  • shm_init() - Create and initialize shared memory
  • shm_attach() - Attach to existing shared memory
  • shm_detach() - Detach from shared memory
  • shm_destroy() - Destroy shared memory

Allocation

  • shm_malloc(size) - Allocate memory
  • shm_calloc(n, size) - Allocate zeroed memory
  • shm_realloc(ptr, size) - Resize allocation
  • shm_free(ptr) - Free memory

Debug

  • shm_validate() - Check heap integrity (returns 0 on success)
  • shm_dump() - Print heap state
  • shm_stats(total, used, free) - Get memory statistics
  • shm_usable_size(ptr) - Get usable size of allocation

Tests

make run-tests  # run all tests

Individual tests (in bin/):

  • test_basic - Basic alloc/free
  • test_stress - Multi-process stress test
  • test_realloc - Realloc behavior
  • test_alignment - 16-byte alignment
  • test_edge - Edge cases (NULL, zero size)
  • test_independent - Independent process attach
  • test_debug - Debug tools

Project Structure

MemAlloc/
├── include/
│   └── memalloc.h      # Public header
├── src/
│   └── memalloc.c      # Implementation
├── tests/
│   ├── test_basic.c
│   ├── test_stress.c
│   └── ...
├── bin/                # Compiled test binaries
├── Makefile
└── README.md

About

Thread-safe memory library for concurrent allocation, reallocation, and deallocation.

Topics

Resources

Stars

Watchers

Forks

Contributors