search_algorithms.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 <stdio.h>
  18. #include "linked_lists/search_algorithms.h"
  19. #include "block_header.h"
  20. #ifdef WITH_FIXED_LISTS
  21. /**
  22. * \details The maptable of the heap is being traversed sequentially while
  23. * searching for a size equal of the requested size. If one is found, then we
  24. * return the head of this list (provided that it is not null) and set the next
  25. * block as the new head.
  26. */
  27. void * search_on_fixed(heap_t * heap, size_t requested_size) {
  28. maptable_node_t *node;
  29. void *ptr;
  30. node = heap->maptable_head;
  31. ptr = NULL;
  32. while(node) {
  33. #ifdef COUNT_HOPS
  34. heap->dmm_stats.total_hops++;
  35. #endif /* COUNT_HOPS */
  36. if(node->size == requested_size) {
  37. if(node->fixed_list_head != NULL) {
  38. ptr = node->fixed_list_head;
  39. node->fixed_list_head = get_next(ptr);
  40. #ifdef BLOCKS_IN_DLL
  41. set_previous(node->fixed_list_head, NULL);
  42. #endif /* BLOCKS_IN_DLL */
  43. }
  44. break;
  45. }
  46. node = node->next;
  47. }
  48. return ptr;
  49. }
  50. #endif /* WITH_FIXED_LISTS */
  51. /**
  52. * \details In order to remove a block from a singly linked list, we need to
  53. * keep track of the previous block as well: The previous block must point to
  54. * the current block's next block once the current one is removed.
  55. * Normally the best fit search alogrithm would have to traverse the whole list
  56. * in order to find the best block. However, a check is being performed each
  57. * time a new best candidate is found, so that we stop once a perfect block is
  58. * found.
  59. */
  60. void * best_fit_on_freelist(heap_t *heap, size_t requested_size) {
  61. void *current_block, *previous_block;
  62. void *best_block, *best_previous_block;
  63. size_t best_size, block_size;
  64. current_block = NULL;
  65. previous_block = NULL;
  66. best_previous_block = NULL;
  67. best_block = NULL;
  68. best_size = (size_t) -1; /* SIZE_MAX */
  69. block_size = 0;
  70. for(current_block = heap->free_list_head; current_block != NULL;
  71. current_block = get_next(current_block)) {
  72. #ifdef COUNT_HOPS
  73. heap->dmm_stats.total_hops++;
  74. #endif /* COUNT_HOPS */
  75. block_size = get_size(current_block);
  76. if(block_size >= requested_size) {
  77. if(block_size < best_size) {
  78. best_block = current_block;
  79. best_size = get_size(current_block);
  80. best_previous_block = previous_block;
  81. /* If the requested size is found, there is no need to keep
  82. * searching for a better sized block */
  83. if(best_size == requested_size) {
  84. break;
  85. }
  86. }
  87. }
  88. previous_block = current_block;
  89. }
  90. /* Remove the block from the list */
  91. /* Note: remove_block() is not used because the block is already found */
  92. if(best_block != NULL) {
  93. if(best_block == heap->free_list_head) {
  94. heap->free_list_head = get_next(best_block);
  95. #ifdef BLOCKS_IN_DLL
  96. if(heap->free_list_head != NULL) {
  97. set_previous(heap->free_list_head, NULL);
  98. }
  99. #endif /* BLOCKS_IN_DLL */
  100. } else {
  101. set_next(best_previous_block, get_next(best_block));
  102. #ifdef BLOCKS_IN_DLL
  103. if(get_next(best_block) != NULL) {
  104. set_previous(get_next(best_block), best_previous_block);
  105. }
  106. #endif /* BLOCKS_IN_DLL */
  107. }
  108. }
  109. return best_block;
  110. }
  111. #ifdef GOOD_FIT
  112. void * good_fit_on_freelist(heap_t *heap, size_t requested_size) {
  113. void *current_block, *previous_block;
  114. void *best_block, *best_previous_block;
  115. size_t best_size, block_size;
  116. current_block = NULL;
  117. previous_block = NULL;
  118. best_previous_block = NULL;
  119. best_block = NULL;
  120. best_size = (size_t) -1; /* SIZE_MAX */
  121. block_size = 0;
  122. for(current_block = heap->free_list_head; current_block != NULL;
  123. current_block = get_next(current_block)) {
  124. #ifdef COUNT_HOPS
  125. heap->dmm_stats.total_hops++;
  126. #endif /* COUNT_HOPS */
  127. block_size = get_size(current_block);
  128. if(block_size >= requested_size) {
  129. if(block_size < best_size) {
  130. best_block = current_block;
  131. best_size = get_size(current_block);
  132. best_previous_block = previous_block;
  133. /* If the block size fits the relaxed requirements, then there
  134. * is no need to keep searching for a better sized block */
  135. if(heap->dmm_knobs.fit_percentage * best_size <= requested_size) {
  136. break;
  137. }
  138. }
  139. }
  140. previous_block = current_block;
  141. }
  142. /* Remove the block from the list */
  143. /* Note: remove_block() is not used because the block is already found */
  144. if(best_block != NULL) {
  145. if(best_block == heap->free_list_head) {
  146. heap->free_list_head = get_next(best_block);
  147. #ifdef BLOCKS_IN_DLL
  148. if(heap->free_list_head != NULL) {
  149. set_previous(heap->free_list_head, NULL);
  150. }
  151. #endif /* BLOCKS_IN_DLL */
  152. } else {
  153. set_next(best_previous_block, get_next(best_block));
  154. #ifdef BLOCKS_IN_DLL
  155. if(get_next(best_block) != NULL) {
  156. set_previous(get_next(best_block), best_previous_block);
  157. }
  158. #endif /* BLOCKS_IN_DLL */
  159. }
  160. }
  161. return best_block;
  162. }
  163. #endif /* GOOD_FIT */
  164. /**
  165. * \details In order to remove a block from a singly linked list, we need to
  166. * keep track of the previous block as well: The previous block must point to
  167. * the current block's next block once the current one is removed.
  168. */
  169. void * exact_fit_on_freelist(heap_t *heap, size_t requested_size) {
  170. void *current_block, *previous_block, *ptr;
  171. previous_block = NULL;
  172. ptr = NULL;
  173. for(current_block = heap->free_list_head; current_block != NULL;
  174. current_block = get_next(current_block)) {
  175. #ifdef COUNT_HOPS
  176. heap->dmm_stats.total_hops++;
  177. #endif /* COUNT_HOPS */
  178. if(get_size(current_block) == requested_size) {
  179. if(current_block == heap->free_list_head) {
  180. heap->free_list_head = get_next(current_block);
  181. #ifdef BLOCKS_IN_DLL
  182. set_previous(heap->free_list_head, NULL);
  183. #endif /* BLOCKS_IN_DLL */
  184. } else {
  185. set_next(previous_block, get_next(current_block));
  186. #ifdef BLOCKS_IN_DLL
  187. set_previous(get_next(current_block), previous_block);
  188. #endif /* BLOCKS_IN_DLL */
  189. }
  190. ptr = current_block;
  191. break;
  192. }
  193. previous_block = current_block;
  194. }
  195. return ptr;
  196. }
  197. /**
  198. * \details In order to remove a block from a singly linked list, we need to
  199. * keep track of the previous block as well: The previous block must point to
  200. * the current block's next block once the current one is removed.
  201. */
  202. void * first_fit_on_freelist(heap_t *heap, size_t requested_size) {
  203. void *current_block, *previous_block, *ptr;
  204. previous_block = NULL;
  205. ptr = NULL;
  206. for(current_block = heap->free_list_head; current_block != NULL;
  207. current_block = get_next(current_block)) {
  208. #ifdef COUNT_HOPS
  209. heap->dmm_stats.total_hops++;
  210. #endif /* COUNT_HOPS */
  211. if(get_size(current_block) >= requested_size) {
  212. if(current_block == heap->free_list_head) {
  213. heap->free_list_head = get_next(current_block);
  214. #ifdef BLOCKS_IN_DLL
  215. set_previous(heap->free_list_head, NULL);
  216. #endif /* BLOCKS_IN_DLL */
  217. } else {
  218. set_next(previous_block, get_next(current_block));
  219. #ifdef BLOCKS_IN_DLL
  220. set_previous(get_next(current_block), previous_block);
  221. #endif /* BLOCKS_IN_DLL */
  222. }
  223. ptr = current_block;
  224. break;
  225. }
  226. previous_block = current_block;
  227. }
  228. return ptr;
  229. }