src/kernel/mm/kheap.c
/**
 * This is used to dynamically change the memory allocation of a previously allocated memory. 
 * In other words, if the memory previously allocated with the help of malloc or calloc is insufficient, 
 * realloc can be used to dynamically re-allocate memory.
 * 
 * @param ptr Pointer to reallocate
 * @param newSize New size
 * 
 * @return New pointer or NULL
 */
This is used to dynamically change the memory allocation of a previously allocated memory.
In other words, if the memory previously allocated with the help of malloc or calloc is insufficient,
realloc can be used to dynamically re-allocate memory.

@param ptr Pointer to reallocate
@param newSize New size

@return New pointer or NULL
This is used to dynamically change the memory allocation of a previously allocated memory . In other words if the memory previously allocated with the help of malloc or calloc is insufficient realloc can be used to dynamically re - allocate memory . @param ptr Pointer to reallocate @param newSize New size @return New pointer or NULL
void *krealloc(void *ptr, uint32_t newSize) {
    kheapHeader *block = ptr - sizeof(kheapHeader);
    kheapHeader *tmp = _kheapFirst;
    if (newSize <= block->size)
        return NULL;
    while(tmp->next != NULL) {
        if (block + block->size == tmp) {
            block->size += (newSize - block->size);
            kheapHeader *new_block = tmp + (newSize - block->size);
            new_block->prev = tmp->prev;
            new_block->next = tmp->next;
            tmp->prev->next = new_block;
            tmp->next->prev = new_block;            
            return block;
        }
        tmp = tmp->next;
    }
    if (block + block->size == tmp) {
        block->size += (newSize - block->size);
        kheapHeader *new_block = tmp + (newSize - block->size);
        new_block->prev = tmp->prev;
        new_block->next = tmp->next;
        tmp->prev->next = new_block;
        tmp->next->prev = new_block;            
        return block;
    }
    uint32_t new_ptr = kmalloc(newSize);
    if (!new_ptr)
        return NULL;
    memcpy(new_ptr, ptr, block->size);
    kfree(block);
    return new_ptr;
}
void * krealloc ( void * ptr , uint32_t newSize ) { kheapHeader * block = ptr - sizeof ( kheapHeader ) ; kheapHeader * tmp = _kheapFirst ; if ( newSize <= block -> size ) return NULL ; while ( tmp -> next != NULL ) { if ( block + block -> size == tmp ) { block -> size += ( newSize - block -> size ) ; kheapHeader * new_block = tmp + ( newSize - block -> size ) ; new_block -> prev = tmp -> prev ; new_block -> next = tmp -> next ; tmp -> prev -> next = new_block ; tmp -> next -> prev = new_block ; return block ; } tmp = tmp -> next ; } if ( block + block -> size == tmp ) { block -> size += ( newSize - block -> size ) ; kheapHeader * new_block = tmp + ( newSize - block -> size ) ; new_block -> prev = tmp -> prev ; new_block -> next = tmp -> next ; tmp -> prev -> next = new_block ; tmp -> next -> prev = new_block ; return block ; } uint32_t new_ptr = kmalloc ( newSize ) ; if ( ! new_ptr ) return NULL ; memcpy ( new_ptr , ptr , block -> size ) ; kfree ( block ) ; return new_ptr ; }
This is used to dynamically change the memory allocation of a previously allocated memory.
This is used to dynamically change the memory allocation of a previously allocated memory .
// This is a contiguous chunk of memory // Set up the new shifted block  // Last block // This is a contiguous chunk of memory // Set up the new shifted block  // Otherwise find an entire new block and copy everything