-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Free space is mapped by size and by offset, so the smallest free block top most of the heap is returned. Allocations and deletions are done in constant low time. Fragmentation is no problem, the heap won't overflow.
heap_handle_t heap_create(size_t offset, size_t size);
A small header get's stored right at the beginning:
typedef struct
{
size_t free;
size_t used;
size_t size;
size_t free_block;
block_map_t map_free;
}heap_t;
void* heap_alloc(heap_handle_t heap, size_t size);
If there is a gap big enough, it is found in the map. Else a new heap_block is created at the end of the heap.
The size and a flag if it is free are stored twice at the head and the foot. This is neccessary to combine free blocks next to each other.
void heap_free(heap_handle_t heap, void* buf);
The block is added to the map and marked free. If there are free blocks left or right of it, those get removed from the map and a combined block is added.
Free blocks are sorted by size and by offset.
When allocating from the map, the smallest gap top most of the heap is returned.
https://github.com/svenbieg/Clusters
Adding a free block to the map can cause an allocation, the opposite when removing. I've made it by caching releases in free blocks, and by making allocations passive without moving anything in the map.
I've spent my whole holidays on this memory-manager last year. I still can't give You any guarantee on my code, but i think it is perfect now.
Best regards,
Sven Bieg
Copyright 2024, Sven Bieg (svenbieg@web.de)