Skip to content

ashlesh-t/myandmonlymalloc

Repository files navigation

MyAndOnlyMalloc

A custom page-backed memory allocator written from scratch in C.

This allocator:

  • Does NOT use malloc internally
  • Uses OS pages via mmap (through pages.c)
  • Implements an Arena-based memory model
  • Uses segregated free bins
  • Uses block headers for metadata
  • Supports block splitting

High-Level Architecture


OS (mmap)
│
▼
+------------------------------+

| Allocator                                              |
| ------------------------------------------------------ |
| Arena                                                  |
| ├── memory (heap start)                                |
| ├── total_size                                         |
| └── bins[NUM_BINS]                                     |
| +------------------------------+                       |
|                                                        |
| ▼                                                      |
| +----------------------------------------------------+ |
| Arena Memory                                           |
|                                                        |
| [BlockHeader][User Data] [BlockHeader][User Data]      |
| [Free Block]        [Allocated Block]                  |
|                                                        |
| +----------------------------------------------------+ |


Memory Layout

When allocator_create() runs:


+-------------------+
| Allocator struct  |
+-------------------+
| Arena Memory      |  ← contiguous page-backed region
|                   |
| [BlockHeader]     | ← initial free block
|      ...          |
|                   |
+-------------------+


🏗 Block Structure

Each memory block looks like:


+---------------------+

| BlockHeader             |
| ----------------------- |
| size                    |
| next (if free)          |
| free flag               |
| +---------------------+ |
| User Data               |
| (returned pointer)      |
| +---------------------+ |

User only sees:


ptr → User Data

But internally:


BlockHeader = ptr - sizeof(BlockHeader)


Bins (Segregated Free Lists)

We maintain:


bins[0] → small blocks (≤ 32 bytes)
bins[1] → ≤ 64 bytes
bins[2] → ≤ 128 bytes
...
bins[7] → large blocks

Each bin is a linked list of free blocks.


bins[i]
│
▼
[Block] → [Block] → [Block]


Allocation Flow

  1. Align requested size
  2. Add header size
  3. Find suitable bin
  4. Search block
  5. Remove block from bin
  6. Split if large enough
  7. Return user pointer

Flow:


allocator_alloc(size)
↓
find_suitable_block()
↓
split_block()
↓
return ptr


Free Flow


allocator_free(ptr)
↓
BlockHeader = ptr - header
↓
mark free
↓
insert into bin

Currently:

  • No coalescing yet
  • No GC yet
  • No trees yet

What Makes This Serious?

✔ Page-backed memory
✔ No internal malloc usage
✔ Metadata headers
✔ Segregated bins
✔ Splitting logic

This is the foundation of real-world allocators.


Next Planned Upgrades

  • Tree-based large block management
  • Multiple arenas (thread-local)
  • Garbage Collector
  • Slab allocator for tiny objects

Author: Ashlesha T
Project: MyAndOnlyMalloc


---

About

-Not YET-

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors