Skip to content

Small implementation of malloc and free. Library that interacts with the operating system to perform heap management on behalf of a user

Notifications You must be signed in to change notification settings

keepsake200/malloc-replica

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 

Repository files navigation

malloc-replica

A small implementation of malloc, free, calloc, and realloc in C.
This project recreates core heap-management behavior by maintaining metadata blocks in a linked list and requesting memory from the operating system using sbrk().

The allocator supports multiple fit strategies, handles alignment, and tracks heap statistics to help visualize allocator behavior over time.


Features / Requirements Modeled

Heap management

  • Grows the heap on demand using sbrk()
  • Maintains a linked list of heap blocks containing metadata:
    • block size
    • free/allocated status
    • pointer to the next block

Allocation behavior

  • 4-byte alignment for all requested allocations
  • Locates a usable free block using a selectable fit strategy
  • Splits free blocks when there is enough leftover space to form a valid new block
  • Reuses freed blocks instead of always growing the heap

Freeing and coalescing

  • Marks blocks as free when free() is called
  • Coalesces adjacent free blocks to reduce fragmentation

Supported allocation strategies (compile-time)

  • First Fit
  • Best Fit
  • Worst Fit
  • Next Fit

Statistics and reporting

Automatically prints heap statistics at program exit (registered with atexit()), including:

  • number of mallocs
  • number of frees
  • number of reuses
  • number of heap grows
  • number of block splits
  • number of coalesces
  • number of blocks tracked
  • total requested bytes
  • maximum heap size reached

About

Small implementation of malloc and free. Library that interacts with the operating system to perform heap management on behalf of a user

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages