search_algorithms.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 and set the next block as the new head.
  25. */
  26. void * search_on_fixed(heap_t * heap, size_t requested_size) {
  27. maptable_node_t *node;
  28. void *ptr;
  29. node = heap->maptable_head;
  30. ptr = NULL;
  31. while(node) {
  32. if(node->size == requested_size) {
  33. ptr = node->fixed_list_head;
  34. if(ptr != NULL) {
  35. node->fixed_list_head = get_next(ptr);
  36. #ifdef BLOCKS_IN_DLL
  37. set_previous(node->fixed_list_head, NULL);
  38. #endif /* BLOCKS_IN_DLL */
  39. }
  40. break;
  41. }
  42. node = node->next;
  43. }
  44. return ptr;
  45. }
  46. #endif /* WITH_FIXED_LISTS */
  47. /**
  48. * \details In order to remove a block from a singly linked list, we need to
  49. * keep track of the previous block as well: The previous block must point to
  50. * the current block's next block once the current one is removed.
  51. * Normally the best fit search alogrithm would have to traverse the whole list
  52. * in order to find the best block. However, a check is being performed each
  53. * time a new best candidate is found, so that we stop once a perfect block is
  54. * found.
  55. */
  56. void * best_fit_on_freelist(heap_t *heap, size_t requested_size) {
  57. void *current_block, *previous_block;
  58. void *best_block, *best_previous_block;
  59. size_t best_size;
  60. previous_block = NULL;
  61. best_previous_block = NULL;
  62. best_block = NULL;
  63. best_size = (size_t) -1; /* SIZE_MAX */
  64. for(current_block = heap->free_list_head; current_block != NULL;
  65. current_block = get_next(current_block)) {
  66. if(get_size(current_block) >= requested_size) {
  67. if(get_size(current_block) < best_size) {
  68. best_block = current_block;
  69. best_size = get_size(current_block);
  70. best_previous_block = previous_block;
  71. /* If the requested size is found, there is no need to keep
  72. * searching for a better sized block */
  73. if(best_size == requested_size) {
  74. break;
  75. }
  76. }
  77. }
  78. previous_block = current_block;
  79. }
  80. /* Remove the block from the list */
  81. /* Note: remove_block() is not used because the block is already found */
  82. if(best_block != NULL) {
  83. if(best_block == heap->free_list_head) {
  84. heap->free_list_head = get_next(best_block);
  85. #ifdef BLOCKS_IN_DLL
  86. if(heap->free_list_head != NULL) {
  87. set_previous(heap->free_list_head, NULL);
  88. }
  89. #endif /* BLOCKS_IN_DLL */
  90. } else {
  91. set_next(best_previous_block, get_next(best_block));
  92. #ifdef BLOCKS_IN_DLL
  93. set_previous(get_next(best_block), best_previous_block);
  94. #endif /* BLOCKS_IN_DLL */
  95. }
  96. }
  97. return best_block;
  98. }
  99. /**
  100. * \details In order to remove a block from a singly linked list, we need to
  101. * keep track of the previous block as well: The previous block must point to
  102. * the current block's next block once the current one is removed.
  103. */
  104. void * exact_fit_on_freelist(heap_t *heap, size_t requested_size) {
  105. void *current_block, *previous_block, *ptr;
  106. previous_block = NULL;
  107. ptr = NULL;
  108. for(current_block = heap->free_list_head; current_block != NULL;
  109. current_block = get_next(current_block)) {
  110. if(get_size(current_block) == requested_size) {
  111. if(current_block == heap->free_list_head) {
  112. heap->free_list_head = get_next(current_block);
  113. #ifdef BLOCKS_IN_DLL
  114. set_previous(heap->free_list_head, NULL);
  115. #endif /* BLOCKS_IN_DLL */
  116. } else {
  117. set_next(previous_block, get_next(current_block));
  118. #ifdef BLOCKS_IN_DLL
  119. set_previous(get_next(current_block), previous_block);
  120. #endif /* BLOCKS_IN_DLL */
  121. }
  122. ptr = current_block;
  123. break;
  124. }
  125. previous_block = current_block;
  126. }
  127. return ptr;
  128. }
  129. /**
  130. * \details In order to remove a block from a singly linked list, we need to
  131. * keep track of the previous block as well: The previous block must point to
  132. * the current block's next block once the current one is removed.
  133. */
  134. void * first_fit_on_freelist(heap_t *heap, size_t requested_size) {
  135. void *current_block, *previous_block, *ptr;
  136. previous_block = NULL;
  137. ptr = NULL;
  138. for(current_block = heap->free_list_head; current_block != NULL;
  139. current_block = get_next(current_block)) {
  140. if(get_size(current_block) >= requested_size) {
  141. if(current_block == heap->free_list_head) {
  142. heap->free_list_head = get_next(current_block);
  143. #ifdef BLOCKS_IN_DLL
  144. set_previous(heap->free_list_head, NULL);
  145. #endif /* BLOCKS_IN_DLL */
  146. } else {
  147. set_next(previous_block, get_next(current_block));
  148. #ifdef BLOCKS_IN_DLL
  149. set_previous(get_next(current_block), previous_block);
  150. #endif /* BLOCKS_IN_DLL */
  151. }
  152. ptr = current_block;
  153. break;
  154. }
  155. previous_block = current_block;
  156. }
  157. return ptr;
  158. }