ソースを参照

Refactor split()

Ioannis Koutras 13 年 前
コミット
f1e797ed12
共有2 個のファイルを変更した26 個の追加26 個の削除を含む
  1. 1 25
      src/custom_malloc.c
  2. 25 1
      src/split.c

+ 1 - 25
src/custom_malloc.c

@@ -44,10 +44,6 @@
 
 void * custom_ahmalloc(allocator_t* allocator, heap_t* heap, size_t size) {
     void *ptr;
-#if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
-    size_t new_size;
-    size_t min_split_size;
-#endif /* (SPLITTING_FIXED) || (SPLITTING_VARIABLE) */
 
     ptr = NULL;
 
@@ -84,27 +80,7 @@ void * custom_ahmalloc(allocator_t* allocator, heap_t* heap, size_t size) {
 
         /* Try to split */
 #if defined (SPLITTING_FIXED) || defined (SPLITTING_VARIABLE)
-        /* Check what would be the size of the new block if we split the current
-         * one.
-         * Note: new_size is a size_t, so compare first in order to prevent an
-         * underflow.
-         */
-        if(get_size(ptr) > size + HEADER_SIZE) {
-            new_size = get_size(ptr) - size - HEADER_SIZE;
-        } else {
-            new_size = 0;
-        }
-
-#ifdef SPLITTING_FIXED
-        min_split_size = MIN_SPLITTING_SIZE;
-#endif /* SPLITTING_FIXED */
-#ifdef SPLITTING_VARIABLE
-        min_split_size = heap->dmm_knobs.min_split_size;
-#endif /* SPLITTING_VARIABLE */
-
-        if(new_size >= min_split_size) {
-            split(allocator, heap, ptr, size);
-        }
+        split(allocator, heap, ptr, size);
 #endif /* (SPLITTING_FIXED) || (SPLITTING_VARIABLE) */
 
         mark_used(allocator, ptr);

+ 25 - 1
src/split.c

@@ -22,6 +22,8 @@
 
 void split(allocator_t *allocator, heap_t *heap, void *ptr,
         size_t req_size) {
+    size_t new_size;
+    size_t min_split_size;
     void *new_block;
     size_t new_block_size;
 #ifdef COALESCE_AFTER_SPLIT
@@ -34,7 +36,29 @@ void split(allocator_t *allocator, heap_t *heap, void *ptr,
     maptable_node_t *current_maptable_node;
 #endif /* WITH_FIXED_LISTS */
 
-    new_block = (void *) ((char *) ptr + req_size + HEADER_SIZE);
+    /* Check what would be the size of the new block if we split the current
+     * one.
+     * Note: new_size is a size_t, so compare first in order to prevent an
+     * underflow.
+     */
+    if(get_size(ptr) > req_size + HEADER_SIZE) {
+        new_size = get_size(ptr) - req_size - HEADER_SIZE;
+    } else {
+        new_size = 0;
+    }
+
+#ifdef SPLITTING_FIXED
+    min_split_size = MIN_SPLITTING_SIZE;
+#endif /* SPLITTING_FIXED */
+#ifdef SPLITTING_VARIABLE
+    min_split_size = heap->dmm_knobs.min_split_size;
+#endif /* SPLITTING_VARIABLE */
+
+    if(new_size < min_split_size) {
+        return;
+    }
+
+    new_block = (void *)((char *)ptr + req_size + HEADER_SIZE);
     new_block_size = get_size(ptr) - req_size - HEADER_SIZE;
 
     /* Resize the previous, to be used block */