#include "coalesce.h" #include "block_header.h" #include "other.h" void * coalesce(void *ptr, heap_t *heap) { void *prev; int fixed_list_id, i; maptable_node_t *current_maptable_node; // If there is a negative value on max_coalesce_size, then don't do // anything. // FIXME To be moved in custom_free() if(heap->dmm_knobs.max_coalesce_size < 0) { return ptr; } // Try to coalesce with the previous memory block if(is_previous_free(ptr)) { prev = get_previous(ptr); // Check if it is a block of a fixed list fixed_list_id = map_size_to_list(heap, get_size(prev)); if(fixed_list_id != -1) { // If it is, find the fixed list and remove the block current_maptable_node = heap->maptable_head; if(fixed_list_id != 0) { for(i = 1; i < fixed_list_id; i++) { current_maptable_node = current_maptable_node->next; } } remove_block(ptr, current_maptable_node->fixed_list_head); } else { // Or it is a block from the free list, so remove it // from there remove_block(ptr, heap->free_list_head); } // Set the new size // Note: the rest of the header variables will be set on free(). set_size(prev, get_size(prev) + get_size(ptr) + HEADER_SIZE); return prev; } else { return ptr; } }