coalesce.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include <stdio.h>
  2. #include "coalesce.h"
  3. #include "block_header.h"
  4. #include "other.h"
  5. void * coalesce(void *ptr, heap_t *heap, allocator_t *allocator) {
  6. void *prev;
  7. int fixed_list_id, i;
  8. maptable_node_t *current_maptable_node;
  9. prev = get_previous(ptr);
  10. // Check if it is a block of a fixed list
  11. fixed_list_id = map_size_to_list(heap, get_size(prev));
  12. if(fixed_list_id != -1) {
  13. // If it is, find the fixed list and remove the block
  14. current_maptable_node = heap->maptable_head;
  15. if(fixed_list_id != 0) {
  16. for(i = 1; i < fixed_list_id; i++) {
  17. current_maptable_node = current_maptable_node->next;
  18. }
  19. }
  20. remove_block(ptr, current_maptable_node->fixed_list_head);
  21. }
  22. // Set the new size
  23. // Note: the rest of the header variables will be set on free().
  24. set_size(prev, get_size(prev) + get_size(ptr) + HEADER_SIZE);
  25. /* If the current block is the allocator's border pointer, update the
  26. * latter to point to the previous block.
  27. */
  28. if(allocator->border_ptr == ptr)
  29. {
  30. allocator->border_ptr = prev;
  31. }
  32. return prev;
  33. }