split.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 "block_header.h"
  18. #include "other.h"
  19. #include "split.h"
  20. #include "dmm_config.h"
  21. void split(allocator_t *allocator, heap_t *heap, void *ptr,
  22. size_t req_size) {
  23. void *new_block;
  24. int fixed_list_id, i;
  25. size_t new_block_size;
  26. maptable_node_t *current_maptable_node;
  27. new_block = (void *) ((char *) ptr + req_size + HEADER_SIZE);
  28. new_block_size = get_size(ptr) - req_size - HEADER_SIZE;
  29. /* Resize the previous, to be used block */
  30. set_size_and_used(ptr, req_size);
  31. set_size_and_free(new_block, new_block_size);
  32. set_previous_size_availability(new_block, get_size_availability(ptr));
  33. #ifdef WITH_FIXED_LISTS
  34. /* FIXME code from custom_free, some refactoring maybe?
  35. * Be careful, custom_free also does coalescing, we don't need that
  36. */
  37. /* Check if the block could be put in a fixed list */
  38. fixed_list_id = map_size_to_list(heap, new_block_size);
  39. if(fixed_list_id != -1) { /* put it in the right fixed list */
  40. current_maptable_node = heap->maptable_head;
  41. if(fixed_list_id != 0) {
  42. for(i = 1; i < fixed_list_id; i++) {
  43. current_maptable_node = current_maptable_node->next;
  44. }
  45. }
  46. push_block(&new_block, &current_maptable_node->fixed_list_head);
  47. } else { /* put it in the free list */
  48. #endif /* WITH_FIXED_LISTS */
  49. push_block(&new_block, &heap->free_list_head);
  50. #ifdef WITH_FIXED_LISTS
  51. }
  52. #endif /* WITH_FIXED_LISTS */
  53. #ifdef WITH_OWNERSHIP
  54. set_owner(new_block, heap);
  55. #endif /* WITH_OWNERSHIP */
  56. if(allocator->border_ptr == ptr) {
  57. allocator->border_ptr = new_block;
  58. }
  59. }