Skip to content

jdias2019/bitmap-memory-allocator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bitmap-based memory allocator

What it does

This project has three core functions:

  • my_malloc — allocates a block of memory from a fixed-size heap;
  • my_free — returns a previously allocated block back to the heap;
  • my_realloc — resizes an existing allocation;

How it works

The heap is a fixed-size byte array. It is divided into fixed-size blocks of 16 bytes each. A bitmap tracks which blocks are free or occupied: each bit in the bitmap corresponds to one block, where 0 means free and 1 means used.

When my_malloc is called, the allocator scans the bitmap using a First Fit strategy: it looks for the first run of consecutive free blocks large enough to satisfy the request, marks them as used, and returns a pointer to that region of the heap.

Each allocation stores a small header (a size_t) just before the user data, recording how many blocks that allocation is using. my_free and my_realloc read this header to know exactly how much space to release or move.

When my_free is called, the allocator locates the corresponding blocks in the bitmap and clears their bits, making them available again.

my_realloc is built on top of my_malloc and my_free: it allocates a new region, copies the old data across, and frees the original block.

Build and run

chmod +x build.sh
./build.sh
./bin/memalloc

About

simple memory allocator algorithm (bitmap)

Topics

Resources

License

Stars

Watchers

Forks

Contributors