custom_malloc.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 <dmmlib/dmmlib.h>
  18. #include "dmm_config.h"
  19. #ifdef HAVE_LOCKS
  20. #include "posix_lock.h"
  21. #endif /* HAVE_LOCKS */
  22. #if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
  23. #include "split.h"
  24. #endif /* SPLITTING_FIXED || SPLITTING_VARIABLE */
  25. #include "other.h"
  26. #include "linked_lists/linked_lists.h"
  27. #include "linked_lists/search_algorithms.h"
  28. #include "sys_alloc.h"
  29. #include "block_header.h"
  30. #ifdef WITH_ADAPTIVITY
  31. #include "dmm_adaptor.h"
  32. #endif /* WITH_ADAPTIVITY */
  33. #if defined (BEST_FIT)
  34. #define search_on_free(size) best_fit_on_freelist(heap, size)
  35. #elif defined (GOOD_FIT)
  36. #define search_on_free(size) good_fit_on_freelist(heap, size)
  37. #elif defined (EXACT_FIT)
  38. #define search_on_free(size) exact_fit_on_freelist(heap, size)
  39. #elif defined (FIRST_FIT)
  40. #define search_on_free(size) first_fit_on_freelist(heap, size)
  41. #endif /* BEST_FIT */
  42. void * custom_ahmalloc(allocator_t* allocator, heap_t* heap, size_t size) {
  43. void *ptr;
  44. #if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
  45. size_t new_size;
  46. size_t min_split_size;
  47. #endif /* (SPLITTING_FIXED) || (SPLITTING_VARIABLE) */
  48. ptr = NULL;
  49. #ifdef HAVE_LOCKS
  50. posix_lock(heap);
  51. #endif /* HAVE_LOCKS */
  52. #ifdef COUNT_HOPS
  53. /* Initialize the hops counter */
  54. heap->dmm_stats.total_hops = 0;
  55. #endif /* COUNT_HOPS */
  56. #ifdef WITH_FIXED_LISTS
  57. ptr = search_on_fixed(heap, req_padding(size));
  58. if(ptr == NULL) {
  59. #endif /* WITH_FIXED_LISTS */
  60. #ifdef ALLOC_VAR_FIT
  61. ptr = (*allocator->search_policy)(heap, req_padding(size));
  62. #elif defined (HEAP_VAR_FIT)
  63. ptr = (*heap->dmm_knobs.search_policy)(heap, req_padding(size));
  64. #else /* ALLOC_VAR_FIT */
  65. ptr = search_on_free(size);
  66. #endif /* ALLOC_VAR_FIT */
  67. #ifdef WITH_FIXED_LISTS
  68. }
  69. #endif /* WITH_FIXED_LISTS */
  70. if(ptr != NULL) {
  71. #ifdef REQUEST_SIZE_INFO
  72. set_requested_size(ptr, size);
  73. #endif /* REQUEST_SIZE_INFO */
  74. /* Try to split */
  75. #if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
  76. /* Check what would be the size of the new block if we split the current
  77. * one.
  78. * Note: new_size is a size_t, so compare first in order to prevent an
  79. * underflow.
  80. */
  81. if(get_size(ptr) > size + HEADER_SIZE) {
  82. new_size = get_size(ptr) - size - HEADER_SIZE;
  83. } else {
  84. new_size = 0;
  85. }
  86. #ifdef SPLITTING_FIXED
  87. min_split_size = MIN_SPLITTING_SIZE;
  88. #endif /* SPLITTING_FIXED */
  89. #ifdef SPLITTING_VARIABLE
  90. min_split_size = heap->dmm_knobs.min_split_size;
  91. #endif /* SPLITTING_VARIABLE */
  92. if(new_size >= min_split_size) {
  93. split(allocator, heap, ptr, size);
  94. }
  95. #endif /* (SPLITTING_FIXED) || (SPLITTING_VARIABLE) */
  96. mark_used(allocator, ptr);
  97. #ifdef FUTURE_FEATURES
  98. /* Update the used blocks list */
  99. push_block(&ptr, &heap->used_blocks_head);
  100. #endif /* FUTURE_FEATURES */
  101. #ifdef WITH_STATS
  102. /* Update statistics */
  103. #ifdef REQUEST_SIZE_INFO
  104. heap->dmm_stats.mem_requested += size;
  105. #endif /* REQUEST_SIZE_INFO */
  106. heap->dmm_stats.mem_used += HEADER_SIZE + get_size(ptr);
  107. heap->dmm_stats.live_objects += 1;
  108. heap->dmm_stats.num_malloc += 1;
  109. #endif /* WITH_STATS */
  110. /* FIXME To be refactored - END */
  111. } else {
  112. ptr = sys_alloc(allocator, heap, size);
  113. }
  114. #ifdef WITH_ADAPTIVITY
  115. /* Refresh the state of the heap allocator if a certain number of
  116. * malloc's has been served already
  117. */
  118. /* TODO Define 50 as a constant */
  119. if(heap->dmm_stats.num_malloc % 50) {
  120. malloc_state_refresh(heap);
  121. }
  122. #endif /* WITH_ADAPTIVITY */
  123. #ifdef HAVE_LOCKS
  124. posix_unlock(heap);
  125. #endif /* HAVE_LOCKS */
  126. return ptr;
  127. }