src/kernel/mm/kheap.c
/**
 * This is used to dynamically de-allocate the memory.
 * The memory allocated using functions malloc() and calloc() is not de-allocated on their own. 
 * Hence the free() method is used, whenever the dynamic memory allocation takes place. 
 * It helps to reduce wastage of memory by freeing it.
 * 
 * @param addr Address to free.
 * 
 * @return The freed address or NULL.
 */
This is used to dynamically de-allocate the memory.
The memory allocated using functions malloc() and calloc() is not de-allocated on their own.
Hence the free() method is used, whenever the dynamic memory allocation takes place.
It helps to reduce wastage of memory by freeing it.

@param addr Address to free.

@return The freed address or NULL.
This is used to dynamically de - allocate the memory . The memory allocated using functions malloc () and calloc () is not de - allocated on their own . Hence the free () method is used whenever the dynamic memory allocation takes place . It helps to reduce wastage of memory by freeing it . @param addr Address to free . @return The freed address or NULL .
void *kfree(void *addr) {
    kheapHeader *free_block = (uint32_t)addr - sizeof(kheapHeader);
    kheapHeader *tmp = _kheapFirst;
    while (tmp->next != NULL) {
        if ((free_block + free_block->size) == tmp) {
            tmp->size += free_block->size;
            tmp = free_block;   
            return tmp;
        } else if (free_block->next == tmp) {
            free_block->prev = tmp->prev;
            tmp->prev = free_block;
            return free_block;
        } else if ((tmp + tmp->size) == free_block) {
            tmp->size += free_block->size;
            return tmp;
        } else if (free_block->prev <= tmp) {
            free_block->next = tmp->prev;
            tmp->next = free_block;
            return free_block;
        }
        tmp = tmp->next;
    }
    if ((free_block + free_block->size) == tmp) {
        tmp->size += free_block->size;
        tmp = free_block;   
        return tmp;
    } else if (free_block->next == tmp) {
        free_block->prev = tmp->prev;
        tmp->prev = free_block;
        return free_block;
    } else if ((tmp + tmp->size) == free_block) {
        tmp->size += free_block->size;
        return tmp;
    } else if (free_block->prev <= tmp) {
        free_block->next = tmp->prev;
        tmp->next = free_block;
        return free_block;
    }
    return NULL;
}
void * kfree ( void * addr ) { kheapHeader * free_block = ( uint32_t ) addr - sizeof ( kheapHeader ) ; kheapHeader * tmp = _kheapFirst ; while ( tmp -> next != NULL ) { if ( ( free_block + free_block -> size ) == tmp ) { tmp -> size += free_block -> size ; tmp = free_block ; return tmp ; } else if ( free_block -> next == tmp ) { free_block -> prev = tmp -> prev ; tmp -> prev = free_block ; return free_block ; } else if ( ( tmp + tmp -> size ) == free_block ) { tmp -> size += free_block -> size ; return tmp ; } else if ( free_block -> prev <= tmp ) { free_block -> next = tmp -> prev ; tmp -> next = free_block ; return free_block ; } tmp = tmp -> next ; } if ( ( free_block + free_block -> size ) == tmp ) { tmp -> size += free_block -> size ; tmp = free_block ; return tmp ; } else if ( free_block -> next == tmp ) { free_block -> prev = tmp -> prev ; tmp -> prev = free_block ; return free_block ; } else if ( ( tmp + tmp -> size ) == free_block ) { tmp -> size += free_block -> size ; return tmp ; } else if ( free_block -> prev <= tmp ) { free_block -> next = tmp -> prev ; tmp -> next = free_block ; return free_block ; } return NULL ; }
This is used to dynamically de-allocate the memory.
This is used to dynamically de - allocate the memory .
// They're contiguous (free_block is behind), merge the blocks // TODO: VERIFY THIS // Just add the block // They're contiguous (free_block is in the front), merge the blocks // Just add the block // For the last block // They're contiguous (free_block is behind), merge the blocks // TODO: VERIFY THIS // Just add the block // They're contiguous (free_block is in the front), merge the blocks // Just add the block