ソースを参照

Minor fixes and comments on coalesce()

Ioannis Koutras 13 年 前
コミット
0de79edee3
共有2 個のファイルを変更した13 個の追加10 個の削除を含む
  1. 2 1
      private-include/coalesce.h
  2. 11 9
      src/coalesce.c

+ 2 - 1
private-include/coalesce.h

@@ -21,7 +21,8 @@
 #include <dmmlib/heap.h>
 
 /**
- * Merges a memory block with its previous one if the latter one is free.
+ * Tries to merges a memory block with its previous one and/or the next one.
+ * Returns true if it is not required to add the coalesced block to any pool.
  *
  * @param ptr The memory block to be checked.
  * @param heap The heap of the memory block.

+ 11 - 9
src/coalesce.c

@@ -112,17 +112,18 @@ bool coalesce(allocator_t *allocator, heap_t *heap, void *ptr) {
 #endif /* COUNT_HOPS */
         }
 #endif /* WITH_FIXED_LISTS */
-        /* Reset the previous block size */
-        set_size_and_free(allocator, previous_block, three_blocks_size);
         /* Remove the next block from any freelist */
         remove_block_from_lists(&next_block, heap);
         /* Update border pointer if the next block was the border pointer */
         if(allocator->border_ptr == next_block) {
-            allocator->border_ptr = ptr;
+            allocator->border_ptr = previous_block;
         }
+        /* Reset the previous block size */
+        set_size_and_free(allocator, previous_block, three_blocks_size);
         return true;
     }
 
+    /* Check if Previous + Current is ok */
     if(previous_current_size <= max_coal_size) {
         /* If previous block is on a fixed list, remove it */
 #ifdef WITH_FIXED_LISTS
@@ -143,21 +144,22 @@ bool coalesce(allocator_t *allocator, heap_t *heap, void *ptr) {
 #endif /* COUNT_HOPS */
         }
 #endif /* WITH_FIXED_LISTS */
-        /* Reset the previous block size */
-        set_size_and_free(allocator, ptr, previous_current_size);
         /* Update border pointer if the current block was the border pointer */
-        if(allocator->border_ptr == next_block) {
-            allocator->border_ptr = ptr;
+        if(allocator->border_ptr == ptr) {
+            allocator->border_ptr = previous_block;
         }
+        /* Reset the previous block size */
+        set_size_and_free(allocator, ptr, previous_current_size);
         return true;
     }
-
+ 
+    /* Check if Current + Next is ok */
     if(current_next_size <= max_coal_size) {
-        set_size_and_free(allocator, ptr, current_next_size);
         remove_block_from_lists(&next_block, heap);
         if(allocator->border_ptr == next_block) {
             allocator->border_ptr = ptr;
         }
+        set_size_and_free(allocator, ptr, current_next_size);
         return false;
     }