coalesce.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include "coalesce.h"
  2. #include "block_header.h"
  3. #include "other.h"
  4. void * coalesce(void *ptr, heap_t *heap) {
  5. void *prev;
  6. int fixed_list_id, i;
  7. maptable_node_t *current_maptable_node;
  8. // If there is a negative value on max_coalesce_size, then don't do
  9. // anything.
  10. // FIXME To be moved in custom_free()
  11. if(heap->dmm_knobs.max_coalesce_size < 0) {
  12. return ptr;
  13. }
  14. // Try to coalesce with the previous memory block
  15. if(is_previous_free(ptr)) {
  16. prev = get_previous(ptr);
  17. // Check if it is a block of a fixed list
  18. fixed_list_id = map_size_to_list(heap, get_size(prev));
  19. if(fixed_list_id != -1) {
  20. // If it is, find the fixed list and remove the block
  21. current_maptable_node = heap->maptable_head;
  22. if(fixed_list_id != 0) {
  23. for(i = 1; i < fixed_list_id; i++) {
  24. current_maptable_node =
  25. current_maptable_node->next;
  26. }
  27. }
  28. remove_block(ptr, current_maptable_node->fixed_list_head);
  29. } else {
  30. // Or it is a block from the free list, so remove it
  31. // from there
  32. remove_block(ptr, heap->free_list_head);
  33. }
  34. // Set the new size
  35. // Note: the rest of the header variables will be set on free().
  36. set_size(prev, get_size(prev) + get_size(ptr) + HEADER_SIZE);
  37. return prev;
  38. } else {
  39. return ptr;
  40. }
  41. }