Simplify pool_alloc() calling convention so that offset is always specified for POOL_...
authorNick Downing <nick@ndcode.org>
Sat, 8 Jun 2019 05:39:07 +0000 (15:39 +1000)
committerNick Downing <nick@ndcode.org>
Sat, 8 Jun 2019 05:39:07 +0000 (15:39 +1000)
n.sh
pool.c
pool.h
pool_test_run.c
process.c

diff --git a/n.sh b/n.sh
index 8609ed8..b1675ce 100755 (executable)
--- a/n.sh
+++ b/n.sh
@@ -3,14 +3,5 @@
 echo "generate test script"
 ./pool_test_gen 64 256 1024 16 true >pool_test.txt
 
-#echo "run test script, non-moveable"
-#./pool_test_run 64 256 <pool_test.txt
-
-#echo "run test script, last-fit, non-moveable"
-#./pool_test_run 64 256 true <pool_test.txt
-
-echo "run test script, moveable"
+echo "run test script"
 ./pool_test_run 64 256 false true <pool_test.txt
-
-#echo "run test script, last-fit, moveable"
-#./pool_test_run 64 256 true true <pool_test.txt
diff --git a/pool.c b/pool.c
index 36aa53d..c884c6d 100644 (file)
--- a/pool.c
+++ b/pool.c
@@ -69,20 +69,10 @@ bool pool_alloc(
     size_change += item->base - item->limit;
 
     // calculate new spot if block is extended not moved
-    gap = 0;
-    if (mode & POOL_ALLOC_MODE_REALLOC_OFFSET) {
-      va_start(ap, mode);
-      gap = va_arg(ap, int);
-      va_end(ap);
-    }
-    if ((mode & POOL_ALLOC_MODE_LAST_FIT) == 0) {
-      new_base = item->base - gap;
-      new_limit = new_base + size;
-    }
-    else {
-      new_limit = item->limit - gap;
-      new_base = new_limit - size;
-    }
+    va_start(ap, mode);
+    new_base = item->base - va_arg(ap, int);
+    va_end(ap);
+    new_limit = new_base + size;
 #ifndef TRY_BLOCKER
     if (new_base >= item->prev->limit && new_limit <= item->next->base)
       goto resize;
@@ -289,36 +279,21 @@ found:
   if (mode & POOL_ALLOC_MODE_REALLOC) {
     // move ourself into found spot
     // (throw away truncated parts before calling move routine)
-
-    // in certain cases there can't be truncation, and we can also
-    // guarantee the overlap direction, so prefer the move routine
-    // for given direction (user doesn't need to provide other one)
-    switch (mode) {
-    case POOL_ALLOC_MODE_REALLOC:
-      assert(head->move);
-      head->move(item, new_base);
-      break;
-    case POOL_ALLOC_MODE_LAST_FIT | POOL_ALLOC_MODE_REALLOC:
-      assert(head->move_up);
-      head->move_up(item, new_limit);
-      break;
-    default:
-      assert(head->move && head->move_up);
-      if (base_offset < 0) {
-        item->base -= base_offset;
-        base_offset = 0;
-      }
-      if (limit_offset > 0) {
-        item->limit -= limit_offset;
-        limit_offset = 0;
-      }
-      base_offset += new_base;
-      gap = base_offset - item->base; // amount to move by
-      if (gap < 0)
-        head->move(item, base_offset);
-      else if (gap > 0)
-        head->move_up(item, limit_offset + new_limit);
+    assert(head->move && head->move_up);
+    if (base_offset < 0) {
+      item->base -= base_offset;
+      base_offset = 0;
+    }
+    if (limit_offset > 0) {
+      item->limit -= limit_offset;
+      limit_offset = 0;
     }
+    base_offset += new_base;
+    gap = base_offset - item->base; // amount to move by
+    if (gap < 0)
+      head->move(item, base_offset);
+    else if (gap > 0)
+      head->move_up(item, limit_offset + new_limit);
 
     // remove from list
     item->prev->next = item->next;
diff --git a/pool.h b/pool.h
index dd90100..f6dbb7a 100644 (file)
--- a/pool.h
+++ b/pool.h
@@ -20,7 +20,6 @@ struct pool_head {
 #define POOL_ALLOC_MODE_LAST_FIT 1
 #define POOL_ALLOC_MODE_MOVEABLE 2
 #define POOL_ALLOC_MODE_REALLOC 4
-#define POOL_ALLOC_MODE_REALLOC_OFFSET 8
 
 void pool_init(
   struct pool_head *head,
index 1c137f7..e5e752e 100644 (file)
@@ -174,7 +174,8 @@ int main(int argc, char **argv) {
           &head,
           items + item,
           size,
-          mode | POOL_ALLOC_MODE_REALLOC
+          mode | POOL_ALLOC_MODE_REALLOC,
+          0
         );
         printf(
           "... %s\n",
@@ -190,7 +191,8 @@ int main(int argc, char **argv) {
                 &head,
                 items + item,
                 actual_old_size,
-                mode | POOL_ALLOC_MODE_REALLOC
+                mode | POOL_ALLOC_MODE_REALLOC,
+                0
               )
             );
           }
@@ -250,7 +252,8 @@ int main(int argc, char **argv) {
           mode ^ (
             POOL_ALLOC_MODE_LAST_FIT |
             POOL_ALLOC_MODE_REALLOC
-          )
+          ),
+          size - actual_old_size
         );
         printf(
           "... %s\n",
@@ -269,7 +272,8 @@ int main(int argc, char **argv) {
                 mode ^ (
                   POOL_ALLOC_MODE_LAST_FIT |
                   POOL_ALLOC_MODE_REALLOC
-                )
+                ),
+                actual_old_size - size
               )
             );
           }
index b5d5fc5..1851d75 100644 (file)
--- a/process.c
+++ b/process.c
@@ -168,7 +168,8 @@ static bool do_swap_out(int swap_out) {
           &core_table,
           &process->core_item,
           calc.in_core_block - calc.block_base,
-          POOL_ALLOC_MODE_MOVEABLE | POOL_ALLOC_MODE_REALLOC
+          POOL_ALLOC_MODE_MOVEABLE | POOL_ALLOC_MODE_REALLOC,
+          0
         )
       );
 #endif /* ! INDIRECT_CORE */
@@ -310,7 +311,8 @@ bool process_realloc(
       &core_table,
       &process->core_item,
       blocks,
-      dir | (POOL_ALLOC_MODE_MOVEABLE | POOL_ALLOC_MODE_REALLOC)
+      dir | (POOL_ALLOC_MODE_MOVEABLE | POOL_ALLOC_MODE_REALLOC),
+      calc.block_base - block_base
     )
   );
 #else /* INDIRECT_CORE */
@@ -332,7 +334,8 @@ bool process_realloc(
       &core_table,
       &process->core_item,
       blocks,
-      dir | POOL_ALLOC_MODE_REALLOC
+      dir | POOL_ALLOC_MODE_REALLOC,
+      calc.block_base - block_base
     )
   ) {
     assert(blocks_change >= 0);
@@ -421,7 +424,8 @@ void process_run(struct process *process) {
         calc.in_core_block - calc.block_base + blocks,
         process->flags & PROCESS_FLAGS_CORE_ITEM ?
           POOL_ALLOC_MODE_MOVEABLE | POOL_ALLOC_MODE_REALLOC :
-          POOL_ALLOC_MODE_MOVEABLE
+          POOL_ALLOC_MODE_MOVEABLE,
+        0
       )
     );
     process->flags |= PROCESS_FLAGS_CORE_ITEM;