| 1234567891011121314151617181920212223242526272829303132333435363738394041 |
- #include <stdio.h>
- #include "coalesce.h"
- #include "block_header.h"
- #include "other.h"
- void * coalesce(void *ptr, heap_t *heap, allocator_t *allocator) {
- void *prev;
- int fixed_list_id, i;
- maptable_node_t *current_maptable_node;
- 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);
- }
- // 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);
- /* If the current block is the allocator's border pointer, update the
- * latter to point to the previous block.
- */
- if(allocator->border_ptr == ptr)
- {
- allocator->border_ptr = prev;
- }
- return prev;
- }
|