custom_malloc.c 5.0 KB

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