Mini Malloc Memory Management System

๐Ÿ—๏ธ Architecture Overview

This mini malloc implementation uses a multi-arena system to reduce lock contention in multi-threaded environments.

Arena 0

Block 1 (64B)
Block 2 (128B)
Block 3 (32B)

Arena 1

Block 1 (256B)
Block 2 (64B)

Arena 2

Block 1 (512B)

Arena 3

MMAP Block (128KB)
Used Block (sbrk)
Free Block
MMAP Block (โ‰ฅ64KB)

๐Ÿงต Thread to Arena Mapping

Thread 1

ID: 12345

Arena: 12345 % 8 = 1

Thread 2

ID: 67890

Arena: 67890 % 8 = 2

Thread 3

ID: 54321

Arena: 54321 % 8 = 1

๐Ÿ“Š Function Workflows

malloc(size_t size) Flow

1
Size Alignment: Round up size to 8-byte boundary
size = ALIGN8(size)
2
Arena Selection: Get arena based on thread ID
arena_idx = pthread_self() % NUM_ARENAS
3
Size Check: If size โ‰ฅ 64KB, use mmap directly
if (size >= MMAP_THRESHOLD) goto mmap_path
4
Search Free Blocks: First-fit search in arena
find_free_block(&last, size, arena)
5
Block Found: Split if necessary, mark as used
No Block: Request new space via sbrk()

free(void *ptr) Flow

1
Get Block Metadata: ptr - sizeof(block_meta)
block_meta *b = (block_meta *)ptr - 1
2
Validate Block: Check if block is valid
3
MMAP Check: If mmap block, unmap directly
if (b->use_mmap) munmap(b, total_size)
4
Mark Free: Set free flag and coalesce
b->free = 1; coalesce(b)

realloc(void *ptr, size_t size) Flow

1
Edge Cases:
โ€ข If ptr == NULL โ†’ malloc(size)
โ€ข If size == 0 โ†’ free(ptr), return NULL
2
Same Size or Smaller: If current size โ‰ฅ new size
Split block if beneficial, return same pointer
3
Try In-Place Expansion: Check if next block is free
Coalesce and reuse if total size is sufficient
4
Fallback: malloc(new_size) + memcpy + free(old)

calloc(size_t nmemb, size_t size) Flow

1
Overflow Check: Detect integer overflow
__builtin_mul_overflow(nmemb, size, &total)
2
Allocate: Call malloc(total_size)
3
Zero Memory: memset(ptr, 0, total_size)

๐ŸŽฎ Interactive Simulation

๐Ÿงต Current Thread Info

Active Thread: 1
Target Arena: 1
Arena Calculation: 1 % 8 = 1

๐Ÿ“ Operation Log

System initialized with 8 arenas...

๐Ÿ“Š Statistics

Total Allocations: 0

Active Blocks: 0

Free Blocks: 0

MMAP Blocks: 0

Total Memory: 0 bytes

Fragmentation: 0%

๐Ÿ—บ๏ธ Memory Layout

๐Ÿ“š Stack
Thread stacks, local variables, function calls
๐Ÿ—บ๏ธ MMAP Region
Large allocations (โ‰ฅ64KB), memory-mapped files, libraries
๐Ÿ—๏ธ Heap (sbrk)
Arena-managed blocks, small-medium allocations (<64KB)

๐Ÿ”ง Block Structure

Block Metadata (32 bytes)
โ€ข size_t size (8 bytes)
โ€ข block_meta *next (8 bytes)
โ€ข block_meta *prev (8 bytes)
โ€ข uint8_t free (1 byte)
โ€ข uint8_t use_mmap (1 byte)
โ€ข uint8_t arena_idx (1 byte)
โ€ข padding (5 bytes)
User Payload
Available memory returned to user

๐Ÿงต Multi-Threading Benefits

Arena Striping: Each thread is assigned to an arena based on thread ID modulo number of arenas (8).

Reduced Lock Contention: Threads in different arenas can allocate/free simultaneously.

Cache Locality: Related allocations from the same thread are likely in the same arena.

MMAP Bypass: Large allocations (โ‰ฅ64KB) bypass arena system entirely for better performance.