Change clunky swap allocation stuff to use a normal realloc, but suppress copy
authorNick Downing <nick@ndcode.org>
Sat, 8 Jun 2019 05:56:35 +0000 (15:56 +1000)
committerNick Downing <nick@ndcode.org>
Sat, 8 Jun 2019 05:56:35 +0000 (15:56 +1000)
process.c
swap.c

index 1851d75..4c7656c 100644 (file)
--- a/process.c
+++ b/process.c
@@ -284,6 +284,13 @@ bool process_realloc(
   process_calc(process, &calc);
   assert(calc.in_core_block == calc.block_limit);
 
+  // for now, not allowed to resize both ends at once
+  // (in case we free physical blocks then fail to resize)
+  assert(
+    para_base == process->para_base ||
+      para_limit == process->para_limit
+  );
+
   // check blocks
   old_blocks = calc.block_limit - calc.block_base;
   block_base = para_base >> BLOCK_PARAS_SHIFT;
@@ -293,13 +300,17 @@ bool process_realloc(
   if (process_avail < blocks_change)
     return false;
 
-  // this is a bit clunky, we want realloc without copying old contents,
-  // so we free, try to allocate larger, if that fails, allocate original
-  pool_free(&swap_table, &process->swap_item);
-  if (!pool_alloc(&swap_table, &process->swap_item, blocks, 0)) {
-    rassert(pool_alloc(&swap_table, &process->swap_item, old_blocks, 0));
+  // reallocate swap
+  if (
+    !pool_alloc(
+      &swap_table,
+      &process->swap_item,
+      blocks,
+      dir | POOL_ALLOC_MODE_REALLOC,
+      0 //calc.block_base - block_base
+    )
+  )
     return false;
-  }
 
 #ifndef INDIRECT_CORE
   // free up as much core as we need to
@@ -338,14 +349,15 @@ bool process_realloc(
       calc.block_base - block_base
     )
   ) {
-    assert(blocks_change >= 0);
-    pool_free(&swap_table, &process->swap_item);
+    // note: if we get to here we are resizing larger,
+    // so we can't have freed any physical blocks above
     rassert(
       pool_alloc(
         &swap_table,
         &process->swap_item,
         old_blocks,
-        0
+        dir | POOL_ALLOC_MODE_REALLOC,
+        0 //calc.block_base - block_base
       )
     );
     return false;
diff --git a/swap.c b/swap.c
index ff42f2c..76ee837 100644 (file)
--- a/swap.c
+++ b/swap.c
@@ -11,8 +11,10 @@ struct pool_head swap_table;
 #ifdef INDIRECT_SWAP
 int *swap_table_mem;
 struct block_pool swap_block_pool;
+#endif /* INDIRECT_SWAP */
 
 static void swap_move(struct pool_item *item, int new_base) {
+#if 0 // only called for block under realloc, whose contents are ignored
   int base, blocks;
   int i;
 
@@ -25,8 +27,26 @@ static void swap_move(struct pool_item *item, int new_base) {
     swap_table_mem[new_base + i] = swap_table_mem[base + i];
     swap_table_mem[base + i] = 0x55555555;
   }
+#endif
+}
+
+static void swap_move_up(struct pool_item *item, int new_base) {
+#if 0 // only called for block under realloc, whose contents are ignored
+  int base, blocks;
+  int i;
+
+  limit = item->limit;
+  blocks = limit - item->base;
+ printf("swap_move_up [%d,%d) to [%d,%d)\n", limit - blocks, limit, new_limit - blocks, new_limit);
+  assert(new_limit >= limit || new_limit <= limit - blocks);
+
+  blocks = -blocks;
+  for (i = -1; i >= blocks; --i) {
+    swap_table_mem[new_limit + i] = swap_table_mem[limit + i];
+    swap_table_mem[limit + i] = 0x55555555;
+  }
+#endif
 }
-#endif /* INDIRECT_SWAP */
 
 #ifndef INDIRECT_SWAP
 void swap_init(int n_blocks)
@@ -39,12 +59,11 @@ void swap_init(int n_blocks, int table_size)
     0,
 #ifndef INDIRECT_SWAP
     n_blocks,
-    NULL,
 #else /* INDIRECT_SWAP */
     table_size,
-    swap_move,
 #endif /* INDIRECT_SWAP */
-    NULL
+    swap_move,
+    swap_move_up
   );
 
 #ifdef INDIRECT_SWAP