coalesce.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. // FIXME What happens if we have multiple heaps and we don't know the
  16. // owner of the blocks?
  17. if(is_previous_free(ptr)) {
  18. prev = get_previous(ptr);
  19. // Check if it is a block of a fixed list
  20. fixed_list_id = map_size_to_list(heap, get_size(prev));
  21. if(fixed_list_id != -1) {
  22. // If it is, find the fixed list and remove the block
  23. current_maptable_node = heap->maptable_head;
  24. if(fixed_list_id != 0) {
  25. for(i = 1; i < fixed_list_id; i++) {
  26. current_maptable_node = current_maptable_node->next;
  27. }
  28. }
  29. remove_block(ptr, current_maptable_node->fixed_list_head);
  30. } else {
  31. // Or it is a block from the free list, so remove it
  32. // from there
  33. remove_block(ptr, heap->free_list_head);
  34. }
  35. // Set the new size
  36. // Note: the rest of the header variables will be set on free().
  37. set_size(prev, get_size(prev) + get_size(ptr) + HEADER_SIZE);
  38. return prev;
  39. } else {
  40. return ptr;
  41. }
  42. }