i am just working on my Heap Manager.
I am implementing it with a simple linked list an want to know your opinion about my code. Also I don't know if it is secure implemented and i have to test it too, but it should work.
Here is the existing code of my Heap Manager:
Code: Select all
/* Create a new heap */
heap_t *create_new_heap(unsigned int start, unsigned int end, unsigned int max, unsigned int supervisor, unsigned int readonly)
{
heap_t *new_heap = NULL;
heap_block_t *block = NULL;
/* Need to do some security checks right here */
/* place new_heap at start */
new_heap = (heap_t *)start;
memset(new_heap, 0, sizeof(heap_t));
/* place one element behind the heap */
block = (heap_block_t *)start + sizeof(heap_t);
memset(block, 0, sizeof(heap_block_t));
/* Save new start position */
start += sizeof(heap_t);
start += sizeof(heap_block_t);
/* set default heap management informations */
new_heap->start_address = start;
new_heap->end_address = end;
new_heap->max_address = max;
new_heap->supervisor = (supervisor)?true:false;
new_heap->readonly = (readonly)?true:false;
/* Initialise Heap Management List with one big free block */
new_heap->first_block = block;
new_heap->last_block = block;
/* Initialise the first big block */
block->used = false;
block->size = end - start;
/* At last return the new heap */
return(new_heap)
}
/*
* Function searches the heap for a free block
*/
void *heap_alloc(unsigned int size, heap_t *heap)
{
heap_block_t *block = NULL;
heap_block_t *new_block = NULL;
unsigned int local_size = 0;
/* Check wheather heap is allocated or not */
if(heap == NULL)
return(NULL);
/* Check for a proper initialisation of heap */
if(heap->first_block == NULL && heap->last_block == NULL)
return(NULL);
/* Don't allocate a block with 0 bytes */
if(size == 0)
return(NULL);
/* loop through heap and check each element which is free if one free block
* is greater then the wanted size, split it */
for(block = heap->first_block, local_size = block->size;
block != NULL;
local_size += (sizeof(heap_block_t) + block->size), block = block->next)
{
if(block->used)
continue;
if(block->size < size)
continue;
/* Something like this will never happen ;) */
if(block->size == size)
{
/* Set block as used */
block->used = true;
/* return the address */
return(heap->start_address + local_size);
}
else
{
/* Save the address of the new block */
new_block = (heap_block_t *)(heap->start_address + local_size) + sizeof(heap_bock_t) + size;
/* Attach new block to the list */
heap->last_block->next = new_block;
heap->last_block = new_block;
/* Save block Informations */
new_block->used = false;
new_block->size = block->size - size;
return(heap->start_address + local_size);
}
}
return(NULL);
}
So now there is the Heap Management Structure and the structure for a block:
Code: Select all
typedef struct heap_block heap_block_t;
typedef struct heap_block
{
bool used;
unsigned int size;
heap_block_t *next;
} heap_block_t;
typedef struct heap
{
unsigned int start_address;
unsigned int end_address;
unsigned int max_address;
bool supervisor;
bool readonly;
heap_block_t *first_block;
heap_block_t *last_block;
} heap_t;
Cheers Christian