best.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright 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/config.h>
  18. #include "freelist/fitting/best.h"
  19. #include "freelist/ordering_policy.h"
  20. #include "freelist/block_header_funcs.h"
  21. /**
  22. * \details In order to remove a block from a singly linked list, we need to
  23. * keep track of the previous block as well: The previous block must point to
  24. * the current block's next block once the current one is removed.
  25. * Normally the best fit search alogrithm would have to traverse the whole list
  26. * in order to find the best block. However, a check is being performed each
  27. * time a new best candidate is found, so that we stop once a perfect block is
  28. * found.
  29. */
  30. block_header_t * fl_best_fit(freelist_rb_t *raw_block, size_t requested_size) {
  31. block_header_t *current_block, *previous_block;
  32. block_header_t *best_block, *best_previous_block;
  33. size_t best_size, block_size;
  34. previous_block = NULL;
  35. best_previous_block = NULL;
  36. best_block = NULL;
  37. best_size = (size_t) -1; /* SIZE_MAX */
  38. SLIST_FOREACH(current_block, &raw_block->fl_head, pointers) {
  39. block_size = get_size(current_block);
  40. if(block_size >= requested_size) {
  41. if(block_size < best_size) {
  42. best_block = current_block;
  43. best_size = block_size;
  44. best_previous_block = previous_block;
  45. /* If the requested size is found, there is no need to keep
  46. * searching for a better sized block */
  47. if(best_size == requested_size) {
  48. break;
  49. }
  50. }
  51. }
  52. previous_block = current_block;
  53. }
  54. /* Remove the block from the list
  55. * Note: no need to iterate through the list as the block is already found
  56. */
  57. if(best_block != NULL) {
  58. if((raw_block->fl_head).slh_first == best_block) {
  59. REMOVE_FSLLIST_HEAD(raw_block);
  60. } else {
  61. #ifdef FIFO_ORDERED
  62. if(raw_block->fl_tail == best_block) {
  63. raw_block->fl_tail = best_previous_block;
  64. }
  65. #endif /* FIFO_ORDERED */
  66. best_previous_block->pointers.sle_next =
  67. best_block->pointers.sle_next;
  68. }
  69. }
  70. return best_block;
  71. }