This project includes allocate_memory
and free_memory
functions similar to the standard library's malloc
and free
. Before allocating memory, the allocator must be initialized with initialize_memory_allocator
.
- Memory is allocated in static memory (not the heap)
- This is not a true Buddy System implementation1
initialize_memory_allocator();
// Allocating memory for 5 int elements
int *a = allocate_memory(sizeof(int) * 5);
// Initializing dynamic array with data
for (int i = 0; i < 5; i++)
{
a[i] = i;
}
// Printing dynamic array values
for (int i = 0; i < 5; i++)
{
printf("Value at index %d: %d\n", i, a[i]);
}
// Freeing after use
free_memory(a);
The following code example would produce a binary tree structure as shown below:
// in this example
// sizeof(BlockHeader) = 24
// MEMORY_POOL_SIZE = 1024
initialize_memory_allocator();
int *a = allocate_memory(214);
The actual block size in this implementation is smaller than expected because each block must include not only the free memory space but also the metadata of the BlockHeader struct2. This metadata is essential for navigating the binary tree.