Tidy up process_alloc() / process_realloc() allocating/freeing physical blocks
authorNick Downing <nick@ndcode.org>
Sun, 2 Jun 2019 01:55:28 +0000 (11:55 +1000)
committerNick Downing <nick@ndcode.org>
Sun, 2 Jun 2019 02:10:25 +0000 (12:10 +1000)
core.h
o.sh
process.c
swap.h

diff --git a/core.h b/core.h
index 19600b5..165474b 100644 (file)
--- a/core.h
+++ b/core.h
@@ -1,7 +1,7 @@
 #ifndef _CORE_H
 #define _CORE_H 1
 
-//#define INDIRECT_CORE 1
+#define INDIRECT_CORE 1
 
 #include <stdint.h>
 #ifdef INDIRECT_CORE
diff --git a/o.sh b/o.sh
index 5cec47d..5fca29f 100755 (executable)
--- a/o.sh
+++ b/o.sh
@@ -1,7 +1,7 @@
 #!/bin/sh
 
 echo "generate test script"
-./process_test_gen 16 248 4096 32 true true >process_test.txt
+./process_test_gen 16 248 4096 32 false false >process_test.txt
 
 echo "run test script"
 ./process_test_run 16 64 192 8 384 384 <process_test.txt
index 5ddb523..8599e94 100644 (file)
--- a/process.c
+++ b/process.c
@@ -274,27 +274,29 @@ bool process_alloc(struct process *process, int para, int paras) {
   )
     return false;
 
-  // free up as much core as we need to
 #ifndef INDIRECT_CORE
+  // free up as much core as we need to
   rassert(do_swap_out(blocks - core_table.avail));
-#else /* INDIRECT_CORE */
-  rassert(do_swap_out(blocks - core_block_pool.avail));
-#endif /* INDIRECT_CORE */
 
   // allocate core and possible swap
-#ifndef INDIRECT_CORE
   rassert(
     pool_alloc(
+      &core_table,
+      &process->core_item,
+      blocks,
+      POOL_ALLOC_MODE_MOVEABLE
+    )
+  );
 #else /* INDIRECT_CORE */
+  // free up as much core as we need to
+  rassert(do_swap_out(blocks - core_block_pool.avail));
+
+  // allocate core and possible swap
   if (
     !pool_alloc(
-#endif /* INDIRECT_CORE */
       &core_table,
       &process->core_item,
       blocks,
-#ifndef INDIRECT_CORE
-      POOL_ALLOC_MODE_MOVEABLE
-#else /* INDIRECT_CORE */
       0
     )
   ) {
@@ -302,15 +304,15 @@ bool process_alloc(struct process *process, int para, int paras) {
     return false;
   }
 
-  // populate new table with physical blocks
+  // populate physical blocks as needed
   rassert(
     block_pool_alloc(
       &core_block_pool,
       core_table_mem + process->core_item.base,
       blocks
-#endif /* INDIRECT_CORE */
     )
   );
+#endif /* INDIRECT_CORE */
 
   // insert at head of LRU list
   process->lru_item.prev = &lru_head;
@@ -369,33 +371,31 @@ bool process_realloc(struct process *process, int paras, bool dir) {
     return false;
   }
 
-  // free up as much core as we need to
 #ifndef INDIRECT_CORE
+  // free up as much core as we need to
   rassert(do_swap_out(blocks_change - core_table.avail));
-#else /* INDIRECT_CORE */
-  rassert(do_swap_out(blocks_change - core_block_pool.avail));
-#endif /* INDIRECT_CORE */
 
   // reallocate core and possible swap
-#ifndef INDIRECT_CORE
   rassert(
     pool_alloc(
+      &core_table,
+      &process->core_item,
+      blocks,
+      dir | (POOL_ALLOC_MODE_MOVEABLE | POOL_ALLOC_MODE_REALLOC)
+    )
+  );
 #else /* INDIRECT_CORE */
+  // free up as much core as we need to
+  rassert(do_swap_out(blocks_change - core_block_pool.avail));
+
+  // reallocate core and possible swap
   if (
     !pool_alloc(
-#endif /* INDIRECT_CORE */
       &core_table,
       &process->core_item,
       blocks,
-#ifndef INDIRECT_CORE
-      dir | (POOL_ALLOC_MODE_MOVEABLE | POOL_ALLOC_MODE_REALLOC)
-#else /* INDIRECT_CORE */
       dir | POOL_ALLOC_MODE_REALLOC
-#endif /* INDIRECT_CORE */
     )
-#ifndef INDIRECT_CORE
-  );
-#else /* INDIRECT_CORE */
   ) {
     pool_free(&swap_table, &process->swap_item);
     rassert(
@@ -409,21 +409,30 @@ bool process_realloc(struct process *process, int paras, bool dir) {
     return false;
   }
 
-  // populate new table with physical blocks or discard them
-  if (blocks_change >= 0)
+  if (blocks_change < 0)
+    // discard physical blocks as needed
+    block_pool_free(
+      &core_block_pool,
+      core_table_mem + (
+        dir ?
+          process->core_item.base + blocks_change :
+          process->core_item.limit
+      ),
+      -blocks_change
+    );
+  else
+    // populate physical blocks as needed
     rassert(
       block_pool_alloc(
         &core_block_pool,
-        core_table_mem + process->core_item.limit - blocks_change,
+        core_table_mem + (
+          dir ?
+            process->core_item.base :
+            process->core_item.limit - blocks_change
+        ),
         blocks_change
       )
     );
-  else
-    block_pool_free(
-      &core_block_pool,
-      core_table_mem + process->core_item.limit,
-      -blocks_change
-    );
 #endif /* INDIRECT_CORE */
 
   // track total allocation
diff --git a/swap.h b/swap.h
index 9b9a640..b751427 100644 (file)
--- a/swap.h
+++ b/swap.h
@@ -1,7 +1,7 @@
 #ifndef _SWAP_H
 #define _SWAP_H 1
 
-#define INDIRECT_SWAP 1
+//#define INDIRECT_SWAP 1
 
 #include <stdint.h>
 #ifdef INDIRECT_SWAP