Skip to content

A thread-safe C implementation of malloc() that manages memory using a linked list structure. Features block reuse and mutex-based synchronization for concurrent access.

Notifications You must be signed in to change notification settings

aditya0yadav/Memory-allocator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

Custom Memory Allocator

A thread-safe implementation of a basic memory allocator in C, providing custom malloc() functionality.

Features

  • Thread-safe memory allocation using mutex locks
  • Free block reuse
  • Linked list memory block management
  • Memory alignment support

Implementation Details

The allocator uses a linked list of memory blocks, each with a header containing:

  • Block size
  • Free/occupied status
  • Pointer to next block

Key Components

struct head_t {
    size_t size;
    unsigned is_free;
    struct head_t *next;
};

Memory Management Strategy

  1. Block Reuse: Searches for existing free blocks before requesting new memory
  2. Memory Extension: Uses sbrk() to extend heap when needed
  3. Thread Safety: Implements mutex locks for concurrent access

Usage

void *ptr1 = malloc(100);  // Allocate 100 bytes
void *ptr2 = malloc(50);   // Allocate 50 bytes

Building

Compile with pthread support:

gcc -pthread allocator.c -o allocator

Limitations

  • No memory coalescing
  • No splitting of larger blocks
  • Simple first-fit allocation strategy

License

MIT License

Contributing

Pull requests welcome. Please ensure thread safety is maintained in any modifications.

About

A thread-safe C implementation of malloc() that manages memory using a linked list structure. Features block reuse and mutex-based synchronization for concurrent access.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages