coalesce.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /*
  2. * Copyright 2011 Institute of Communication and Computer Systems (ICCS)
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. #include "coalesce.h"
  18. #include "block_header.h"
  19. #include "other.h"
  20. #include "dmm_config.h"
  21. void * coalesce(allocator_t *allocator, heap_t *heap, void *ptr, size_t size) {
  22. void *prev;
  23. #ifdef WITH_FIXED_LISTS
  24. int fixed_list_id, i;
  25. #endif /* WITH_FIXED_LISTS */
  26. maptable_node_t *current_maptable_node;
  27. prev = get_dlprevious(ptr);
  28. #ifdef WITH_FIXED_LISTS
  29. // Check if it is a block of a fixed list
  30. fixed_list_id = map_size_to_list(heap, get_size(prev));
  31. if(fixed_list_id != -1) {
  32. // If it is, find the fixed list and remove the block
  33. current_maptable_node = heap->maptable_head;
  34. if(fixed_list_id != 0) {
  35. for(i = 1; i < fixed_list_id; i++) {
  36. current_maptable_node = current_maptable_node->next;
  37. }
  38. }
  39. remove_block(&ptr, &current_maptable_node->fixed_list_head);
  40. }
  41. #endif /* WITH_FIXED_LISTS */
  42. // Set the new size
  43. // Note: the rest of the header variables will be set on free().
  44. set_size_and_free(allocator, prev, size);
  45. /* If the current block is the allocator's border pointer, update the
  46. * latter to point to the previous block.
  47. */
  48. if(allocator->border_ptr == ptr)
  49. {
  50. allocator->border_ptr = prev;
  51. }
  52. return prev;
  53. }