Custom Heap Allocator

A customly made heap allocator in C. This allocator is a completion of the basic free and malloc function. This also has the double-free problem solved. It uses best fit for the memory allocation.

Overview

This project is a custom-built allocator written entirely in C. It mimics the behavior memory allocation in C. It does not yet support calloc or realloc. It can allocate any variable since it uses void pointers. The vmalloc function returns a pointer to the memory allocated.

Features

Demo

Here's a quick demo of heap allocator in action:

Will Add Later (youtube video)

Sample Code


struct block_header* best_fit(struct block_header* header, size_t size){
    struct block_header* current = header;
    struct block_header* goal = NULL;
    size_t current_size;
                        
    while(current->size_status != VM_ENDMARK){
        current_size = BLKSZ(current);
            int isallocated = current->size_status & VM_BUSY;
                        
            if(current_size == size && !isallocated){
                return current;
            } else if(current_size > size && (goal == NULL || BLKSZ(goal) > current_size) && !isallocated){
                goal = current;
            }
        current = (struct block_header*)((char*)current + current_size);
    }
                    
    return goal;
}
            

Explore the Code

Check out the full project on GitHub, or download it directly.

What I Learned

This project strengthened my understanding of how C allocate memory. I learned memory allocation works and how block splitting and merging works. . It strengthened my grasp of pointer arithmetic, data structures, and systems-level debugging.