search_algorithms.c 7.2 KB

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