1. Work
  2. Custom malloc() Implementation in C
  3. Docs

Custom malloc() Implementation in C

Two heap allocators in C11: a first-fit free list and a segregated free-list allocator with coalescing.

3 min read

Source on GitHub

A dual-tier low-level dynamic memory allocation subsystem written in C11. Implements both an educational first-fit allocator and an advanced segregated free-list allocator with block coalescing, boundary tags, and stress benchmarking.

Overview

Modern operating system runtimes rely on userspace memory allocators to manage heap space efficiently. This project demonstrates how malloc, free, calloc, and realloc operate beneath standard libraries by managing raw static memory pools. The repository contrasts two independent designs: a minimal first-fit allocator and an advanced high-throughput segregated allocator.

Architectures Implemented

1. Minimal Allocator (mymalloc_min)

  • Strategy: Single contiguous free list with sequential first-fit search.
  • Metadata: Header prefix storing block size and allocation state flags.
  • Use Case: Educational reference for understanding pointer arithmetic and heap header mechanics.

2. Advanced Allocator (mymalloc_adv)

  • Strategy: Segregated free lists indexed by size classes for O(1) to O(k) block discovery.
  • Coalescing: Immediate bidirectional coalescing using Knuth boundary tags (header and footer).
  • Splitting: Dynamic block splitting when requested size is smaller than the available block, preventing internal fragmentation.
  • Alignment: Strict 8-byte and 16-byte memory alignment suitable for 64-bit architectures.

Project Structure

Custom malloc() (C)/
├── include/
│   ├── mymalloc_min.h       # API interface for minimal allocator
│   └── mymalloc_adv.h       # API interface for advanced allocator
├── src/
│   ├── mymalloc_min.c       # Minimal allocator implementation
│   ├── mymalloc_adv.c       # Advanced allocator implementation
│   ├── tests_min.c          # Unit test assertions for minimal engine
│   ├── tests_adv.c          # Unit test assertions for advanced engine
│   └── tests_stress.c       # High-volume allocation and deallocation harness
├── Makefile                 # Build rules for unit tests and stress targets
└── LICENSE                  # MIT License

Requirements

  • GCC or Clang supporting C11 (-std=c11)
  • Make
  • Linux or macOS

Build and Compilation

To build both allocator binaries with their unit test runners:

make all

Build individual targets:

make min    # Builds mymalloc_min executable
make adv    # Builds mymalloc_adv executable

Clean build artifacts:

make clean

Running Tests

Unit Tests

Run the minimal allocator test suite:

./mymalloc_min

Run the advanced allocator test suite:

./mymalloc_adv

Stress Benchmarking

Build and execute high-concurrency randomized allocation cycles:

# Minimal allocator under stress
make test_min_stress
./test_min_stress

# Advanced allocator under stress
make test_adv_stress
./test_adv_stress

API Reference

void* my_malloc(size_t size);
void  my_free(void* ptr);
void* my_calloc(size_t num, size_t size);
void* my_realloc(void* ptr, size_t size);

Project Status

Complete and verified. Serves as a reference implementation for systems programming, operating systems coursework, and memory management research.

Authors and Acknowledgment

Developed by Adam Anwar as an exploration into bare-metal memory structures and custom heap runtimes.

License

MIT License. See LICENSE for terms.

Support

Submit issues, questions, or bug reports to the GitHub issue tracker: https://github.com/ADM1SH/custom-malloc---c-/issues

Roadmap

  • Done: Core architecture and baseline implementation.
  • Done: Functional verification and test coverage.
  • To do: Add thread-safe allocation using pthread mutexes
  • To do: Implement buddy allocation alternative tier

Contributing

Contributions are welcome.

  1. Fork the repository.
  2. Create a feature branch: git checkout -b feature/improvement.
  3. Commit your changes: git commit -m "feat: enhance functionality".
  4. Push to the branch: git push origin feature/improvement.
  5. Open a Pull Request.

Back to top