Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

39 Commits

Repository files navigation

🧠 ft_malloc

custom memory allocator written in C, inspired by the behavior of the libc malloc, free, and realloc.

it uses dynamic memory management using low-level system calls such as mmap and munmap, with a focus on performance && memory efficiency.


🚀 Features

  • custom implementations of:
    • malloc
    • free
    • realloc
  • memory zones:
    • TINY allocations
    • SMALL allocations
    • LARGE allocations
  • page-based memory management using mmap
  • lazy allocation strategy (no full pre-initialization)
  • thread safety w/ pthread_mutex

architecture

memory is divided into zones:

TypeSize RangeStorage Strategy
TINYsmall allocations <= 128pre-mapped zones
SMALLmedium allocations <= 1024pre-mapped zones
LARGElarge allocations > 1024one mmap per allocation

📦 zone layout

each zone contains:

[t_page][t_block][data][t_block][data]...
  • t_page → metadata: whole zone
  • t_block → metadata: for each allocation
  • data → user raw memory

⚙️ allocation Strategy

🔹 lazy initialization

instead of pre-creating all blocks:

  • 1 large free block is created initially, blocks are split on demand during each allocation

significantly reduces:

  • page reclaims
  • memory overhead
  • initialization cost
  • & avoids touching unused memory pages (lazy paging)

🔹 block Splitting

allocating:

[FREE BLOCK]
↓ split
[ALLOCATED][FREE REMAINDER]

🔹 free Strategy

  • block are returned to the free list of corresponding page -> minimizes page reclaims
  • no immediate munmap for TINY/SMALL zones
  • munmap is used only for LARGE allocations

🧪 testing

example test:

while (i<1024)
{
addr=malloc(1024);
addr[0] =42;
free(addr);
i++;
}
char*addr1; char*addr3; addr1= (char*)malloc(16*M); strcpy(addr1, "Bonjours\n"); print(addr1); addr3= (char*)realloc(addr1, 128*M); addr3[127*M] =42; print(addr3); 

🛠 Build

make

this produces (library):

libft_malloc.dylib (macOS)
libft_malloc.so (Linux)

▶️ USAGE

macOS

export DYLD_INSERT_LIBRARIES=./libft_malloc.dylib
export DYLD_FORCE_FLAT_NAMESPACE=1
./your_program

linux

export LD_PRELOAD=./libft_malloc.so
./your_program

debug

inspect allocations with:

show_alloc_mem();

test my malloc performance with:

/urs/bin/time -l ./your_test

possible improvements

  • block-coalescing (no advanced coalescing of adjacent free blocks)
  • Better fit strategies (best-fit / segregated lists)

About

UNIX project implementing libc malloc with [mmap(), munmap()]

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages