Browse Source

Fixed best_fit_on_freelist() for doubly-linked lists.

Ioannis Koutras 13 years ago
parent
commit
d8f75e89b3
1 changed files with 9 additions and 4 deletions
  1. 9 4
      src/linked_lists/search_algorithms.c

+ 9 - 4
src/linked_lists/search_algorithms.c

@@ -62,17 +62,20 @@ void * search_on_fixed(heap_t * heap, size_t requested_size) {
 void * best_fit_on_freelist(heap_t *heap, size_t requested_size) {
     void *current_block, *previous_block;
     void *best_block, *best_previous_block;
-    size_t best_size;
+    size_t best_size, block_size;
 
+    current_block = NULL;
     previous_block = NULL;
     best_previous_block = NULL;
     best_block = NULL;
     best_size = (size_t) -1; /* SIZE_MAX */
+    block_size = 0;
 
     for(current_block = heap->free_list_head; current_block != NULL;
             current_block = get_next(current_block)) {
-        if(get_size(current_block) >= requested_size) {
-            if(get_size(current_block) < best_size) {
+        block_size = get_size(current_block);
+        if(block_size >= requested_size) {
+            if(block_size < best_size) {
                 best_block = current_block;
                 best_size = get_size(current_block);
                 best_previous_block = previous_block;
@@ -99,7 +102,9 @@ void * best_fit_on_freelist(heap_t *heap, size_t requested_size) {
         } else {
             set_next(best_previous_block, get_next(best_block));
 #ifdef BLOCKS_IN_DLL
-            set_previous(get_next(best_block), best_previous_block);
+            if(get_next(best_block) != NULL) {
+                set_previous(get_next(best_block), best_previous_block);
+            }
 #endif /* BLOCKS_IN_DLL */
         }
     }