Skip to content

Dynamic memory allocator implemented in C using segregated free lists. Supports block splitting, merging, and fragmentation tracking with interactive heap commands

Notifications You must be signed in to change notification settings

marinaa13/segregated-free-list-allocator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 

Repository files navigation

Segregated Free List Allocator

Custom dynamic memory allocator implemented in C, simulating a heap manager that uses segregated free lists to track free and allocated memory blocks.
The allocator supports heap initialization, allocation, deallocation, writing, reading, and memory statistics reporting.


Overview

This project implements a simplified version of a memory management subsystem similar to those used in operating systems or runtime allocators.
The heap is divided into multiple free lists, each responsible for blocks of different sizes (powers of two).
Each allocation or deallocation dynamically updates these lists, keeping detailed metrics about fragmentation, memory usage, and block counts.


Features

1. Heap Initialization

  • Initializes the heap structure with multiple free lists.
  • Each list handles blocks of size 2^(i+3) bytes.
  • Tracks start address, total memory, and fragmentation metadata.

2. Memory Allocation (MALLOC)

  • Allocates the smallest possible block that can satisfy the request.
  • If necessary, splits larger blocks (fragmentation tracking enabled).
  • Allocated blocks are tracked in a doubly linked list.

3. Memory Deallocation (FREE)

  • Frees a specific block based on its start address.
  • Reinserts the freed block into the appropriate free list.
  • Handles invalid or double frees safely.

4. Writing & Reading Data (WRITE / READ)

  • Supports writing strings across multiple contiguous blocks.
  • Prevents buffer overflows via contiguous block validation.
  • Reading prints the data starting from a given address.

5. Memory Dump (DUMP_MEMORY)

  • Prints a detailed report of current heap status:
    • Total / allocated / free memory
    • Block distribution per list
    • Fragmentation statistics
    • Allocated block addresses and sizes

6. Heap Destruction (DESTROY_HEAP)

  • Frees all allocated structures and resets state cleanly.

Data Structures

  • doubly_linked_list_t – used for both free lists and the allocated list
  • info_t – stores each block’s address, size, and data pointer
  • list_t – represents the full heap metadata, containing:
    • Array of free lists
    • Statistics (allocations, frees, fragmentation, etc.)

Example Commands

INIT_HEAP 
MALLOC 128 
WRITE 0x400 "Hello Allocator" 16
READ 0x400 16
FREE 0x400
DUMP_MEMORY
DESTROY_HEAP

Learning Outcomes

  • Implemented a full memory allocator with segregated free lists
  • Practiced pointer arithmetic and dynamic structure management
  • Designed algorithms for block fragmentation and merging
  • Simulated system-level memory management behavior in user space

About

Dynamic memory allocator implemented in C using segregated free lists. Supports block splitting, merging, and fragmentation tracking with interactive heap commands

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published