Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

ShauryaaSharma/Custom-Malloc-Memory-Allocator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

5 Commits

Repository files navigation

Custom-Malloc-Memory-Allocator

A custom memory allocator built in C that implements malloc and free using a doubly linked list. The project manages heap memory with sbrk/brk, supports block splitting and merging, heap expansion/shrinking, and basic thread safety, while exploring low-level memory management and OS internals.

Custom Memory Allocator in C

A custom implementation of malloc() and free() built from scratch in C to explore low-level memory management, heap allocation, and operating system internals.

This project manually manages heap memory using sbrk() and brk(), maintains memory blocks through a doubly linked list, and demonstrates how memory allocators split, merge, expand, and shrink heap regions dynamically.


Features

  • Custom malloc and free implementation
  • Heap management using sbrk() and brk()
  • Doubly linked list based block management
  • Best-fit allocation strategy
  • Block splitting during allocation
  • Block merging (coalescing) during free
  • Heap expansion and shrinking
  • Basic thread safety using a simple lock
  • Memory/page inspection utilities
  • Automated tests using assert

Memory Layout

+----------------------+
| Allocator Header |
|----------------------|
| magic bytes |
| lock |
| amount of blocks |
| amount of pages |
+----------------------+
+----------------------+
| Block Header |
|----------------------|
| marker |
| prev pointer |
| next pointer |
| length |
| in_use flag |
+----------------------+
| User Memory |
+----------------------+

How It Works

Allocation (an_malloc)

  • Searches for the smallest available free block
  • Splits blocks if extra space remains
  • Expands the heap using sbrk() if required
  • Returns a pointer to usable memory

Free (an_free)

  • Marks the block as free
  • Merges adjacent free blocks
  • Shrinks the heap when possible
  • Clears released memory

Concepts Explored

  • Heap memory management
  • Program break (brk / sbrk)
  • Virtual memory pages
  • Pointer arithmetic
  • Raw memory traversal
  • Linked lists
  • Memory fragmentation
  • Thread synchronization
  • Linux process memory maps

Project Structure

allocator.c
│
├── an_malloc() -> custom malloc implementation
├── an_free() -> custom free implementation
├── add_used_block() -> allocation logic
├── reduce_heap_size_if_possible()
├── Heap/Page Experiments
└── Automated Tests

Build & Run

Compile

gcc allocator.c -o allocator

Run

./allocator

Example Usage

char *ptr = (char *)an_malloc(100);
ptr[0] = 'A';
an_free(ptr);

Tests Included

  • Basic allocation tests
  • Large allocation tests
  • Block splitting tests
  • Block merging tests
  • Heap growth/shrinking tests
  • Complex allocation/free scenarios

Limitations

This allocator is designed for educational purposes and is not production-ready.

Missing features include:

  • Proper memory alignment
  • calloc
  • realloc
  • Advanced synchronization primitives
  • Double-free protection
  • Optimized allocation bins
  • Metadata protection

Learning Goals

This project was built to better understand:

  • How malloc() and free() work internally
  • How operating systems manage heap memory
  • Low-level systems programming concepts
  • Memory safety and fragmentation
  • Runtime allocator design

About

A custom memory allocator built in C that implements malloc and free using a doubly linked list. The project manages heap memory with sbrk/brk, supports block splitting and merging, heap expansion/shrinking, and basic thread safety, while exploring low-level memory management and OS internals.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

Contributors

Languages

AltStyle によって変換されたページ (->オリジナル) /