kernel/src/allocator/allocator.c
void heap_free(heap_t * heap, void * p) {
    if (p == NULL) { return; }


    bin_t * list;
    footer_t *new_foot, *old_foot;

    node_t * head = (node_t *)((char *)p - offset);

#ifdef MEM_DEBUG
    heap->bytes_allocated -= head->size;
    TRACE("[MEM DEBUG] FREE %i bytes", head->size);
#endif

    if (head == (node_t *)(uintptr_t)heap->start) {
        head->hole = 1;
        add_node(heap->bins[get_bin_index(head->size)], head);
        return;
    }

    node_t * next = (node_t *)((char *)get_foot(head) + sizeof(footer_t));
    footer_t * f = (footer_t *)((char *)head - sizeof(footer_t));
    node_t * prev = f->header;

    if (prev->hole) {
        list = heap->bins[get_bin_index(prev->size)];
        remove_node(list, prev);

        prev->size += overhead + head->size;
        new_foot = get_foot(head);
        new_foot->header = prev;

        head = prev;
    }

    if (next->hole) {
        list = heap->bins[get_bin_index(next->size)];
        remove_node(list, next);

        head->size += overhead + next->size;

        old_foot = get_foot(next);
        old_foot->header = 0;
        next->size = 0;
        next->hole = 0;

        new_foot = get_foot(head);
        new_foot->header = head;
    }

    head->hole = 1;
    add_node(heap->bins[get_bin_index(head->size)], head);
}
// ========================================================
// this is the free function of the heap, it takes the
// heap struct pointer and the pointer provided by the
// heap_alloc function. the given chunk will be possibly
// coalesced  and then placed in the correct bin
// ========================================================
this is the free function of the heap, it takes the
heap struct pointer and the pointer provided by the
heap_alloc function. the given chunk will be possibly
coalesced  and then placed in the correct bin
this is the free function of the heap it takes the heap struct pointer and the pointer provided by the heap_alloc function . the given chunk will be possibly coalesced and then placed in the correct bin
void heap_free(heap_t * heap, void * p) {
    if (p == NULL) { return; }
    bin_t * list;
    footer_t *new_foot, *old_foot;
    node_t * head = (node_t *)((char *)p - offset);
#ifdef MEM_DEBUG
    heap->bytes_allocated -= head->size;
    TRACE("[MEM DEBUG] FREE %i bytes", head->size);
#endif
    if (head == (node_t *)(uintptr_t)heap->start) {
        head->hole = 1;
        add_node(heap->bins[get_bin_index(head->size)], head);
        return;
    }
    node_t * next = (node_t *)((char *)get_foot(head) + sizeof(footer_t));
    footer_t * f = (footer_t *)((char *)head - sizeof(footer_t));
    node_t * prev = f->header;
    if (prev->hole) {
        list = heap->bins[get_bin_index(prev->size)];
        remove_node(list, prev);
        prev->size += overhead + head->size;
        new_foot = get_foot(head);
        new_foot->header = prev;
        head = prev;
    }
    if (next->hole) {
        list = heap->bins[get_bin_index(next->size)];
        remove_node(list, next);
        head->size += overhead + next->size;
        old_foot = get_foot(next);
        old_foot->header = 0;
        next->size = 0;
        next->hole = 0;
        new_foot = get_foot(head);
        new_foot->header = head;
    }
    head->hole = 1;
    add_node(heap->bins[get_bin_index(head->size)], head);
}
void heap_free ( heap_t * heap , void * p ) { if ( p == NULL ) { return ; } bin_t * list ; footer_t * new_foot , * old_foot ; node_t * head = ( node_t * ) ( ( char * ) p - offset ) ; #ifdef MEM_DEBUG heap -> bytes_allocated -= head -> size ; TRACE ( " " , head -> size ) ; #endif if ( head == ( node_t * ) ( uintptr_t ) heap -> start ) { head -> hole = 1 ; add_node ( heap -> bins [ get_bin_index ( head -> size ) ] , head ) ; return ; } node_t * next = ( node_t * ) ( ( char * ) get_foot ( head ) + sizeof ( footer_t ) ) ; footer_t * f = ( footer_t * ) ( ( char * ) head - sizeof ( footer_t ) ) ; node_t * prev = f -> header ; if ( prev -> hole ) { list = heap -> bins [ get_bin_index ( prev -> size ) ] ; remove_node ( list , prev ) ; prev -> size += overhead + head -> size ; new_foot = get_foot ( head ) ; new_foot -> header = prev ; head = prev ; } if ( next -> hole ) { list = heap -> bins [ get_bin_index ( next -> size ) ] ; remove_node ( list , next ) ; head -> size += overhead + next -> size ; old_foot = get_foot ( next ) ; old_foot -> header = 0 ; next -> size = 0 ; next -> hole = 0 ; new_foot = get_foot ( head ) ; new_foot -> header = head ; } head -> hole = 1 ; add_node ( heap -> bins [ get_bin_index ( head -> size ) ] , head ) ; }
this is the free function of the heap, it takes the
heap struct pointer and the pointer provided by the
heap_alloc function.
this is the free function of the heap it takes the heap struct pointer and the pointer provided by the heap_alloc function .