Make n_blocks the primary size and table_size only given if indirect (was the other...
authorNick Downing <nick@ndcode.org>
Sat, 23 Mar 2019 21:46:23 +0000 (08:46 +1100)
committerNick Downing <nick@ndcode.org>
Sat, 23 Mar 2019 22:53:32 +0000 (09:53 +1100)
core.c
o.sh
process.c
process.h
process_test_run.c
swap.c
swap.h

diff --git a/core.c b/core.c
index bb7d10c..388881b 100644 (file)
--- a/core.c
+++ b/core.c
@@ -67,15 +67,19 @@ static void core_move_up(struct pool_item *item, int new_limit) {
 #endif
 
 #ifdef INDIRECT_CORE
-void core_init(int table_size, int n_blocks)
+void core_init(int n_blocks, int table_size)
 #else
-void core_init(int table_size)
+void core_init(int n_blocks)
 #endif
 {
   pool_init(
     &core_table,
     0,
+#ifdef INDIRECT_CORE
     table_size,
+#else
+    n_blocks,
+#endif
     core_move,
 #ifdef MOVEABLE_CORE
     core_move_up
@@ -85,6 +89,10 @@ void core_init(int table_size)
   );
 
 #ifdef INDIRECT_CORE
+  core_table_mem = malloc(table_size * sizeof(int));
+  rassert(core_table_mem);
+  memset(core_table_mem, 0x55, table_size * sizeof(int));
+
   n_core_blocks = n_blocks;
   n_core_block_bitmap = (n_core_blocks + 7) >> 3;
   core_block_bitmap = malloc(n_core_block_bitmap);
@@ -94,19 +102,11 @@ void core_init(int table_size)
     core_block_bitmap[n_core_blocks >> 3] = ~masks[n_core_blocks & 7];
   core_block_next = 0;
   core_block_avail = n_core_blocks;
-
-  core_table_mem = malloc(table_size * sizeof(int));
-  rassert(core_table_mem);
-  memset(core_table_mem, 0x55, table_size * sizeof(int));
+#endif
 
   core_block_mem = malloc(n_blocks * sizeof(int));
   rassert(core_block_mem);
   memset(core_block_mem, 0xaa, n_blocks * sizeof(int));
-#else
-  core_block_mem = malloc(table_size * sizeof(int));
-  rassert(core_block_mem);
-  memset(core_block_mem, 0xaa, table_size * sizeof(int));
-#endif
 }
 
 #ifdef INDIRECT_CORE
diff --git a/o.sh b/o.sh
index eb85203..ef53fbb 100755 (executable)
--- a/o.sh
+++ b/o.sh
@@ -9,9 +9,4 @@ echo "generate test script"
 ./process_test_gen 16 240 1024 32 >process_test.txt
 
 echo "run test script"
-# moveable swap (useable with any configuration):
-./process_test_run 16 64 192 16 <process_test.txt
-# non moveable swap (must be preallocated, may be indirect):
-#./process_test_run 16 64 384 16 192 <process_test.txt
-# non moveable core and swap (must be preallocated, may be indirect):
-#./process_test_run 16 384 384 16 64 192 <process_test.txt
+./process_test_run 16 64 192 16 384 384 <process_test.txt
index 0f9e6b5..6753c09 100644 (file)
--- a/process.c
+++ b/process.c
@@ -641,14 +641,14 @@ void process_free(struct process *process) {
     process->lru_item.next->prev = process->lru_item.prev;
 
 #ifdef PREALLOCATE_CORE
-#ifdef INDIRECT_SWAP
+#ifdef INDIRECT_CORE
     core_block_free(
       core_table_mem + process->core_item.base,
       process->core_item.limit - process->core_item.base 
     );
 #endif
 #else
-#ifdef INDIRECT_SWAP
+#ifdef INDIRECT_CORE
     core_block_free(
       core_table_mem + process->pool_item.base,
       process->pool_item.limit - process->pool_item.base 
index 7893db3..cc9e735 100644 (file)
--- a/process.h
+++ b/process.h
@@ -4,7 +4,7 @@
 #include "pool.h"
 
 //#define PREALLOCATE_CORE 1
-//#define PREALLOCATE_SWAP 1
+#define PREALLOCATE_SWAP 1
 
 struct lru_item {
   struct lru_item *prev;
index 62855b2..2a0301e 100644 (file)
 int main(int argc, char **argv) {
   if (argc < 5) {
 #if defined(INDIRECT_CORE) && defined(INDIRECT_SWAP)
-    printf("usage: %s n_processes core_table_size swap_table_size spare [n_core_blocks [n_swap_blocks]]\n", argv[0]);
+    printf("usage: %s n_processes n_core_blocks n_swap_blocks spare [core_table_size [swap_table_size]]\n", argv[0]);
 #elif defined(INDIRECT_CORE)
-    printf("usage: %s n_processes core_table_size swap_table_size spare [n_core_blocks]\n", argv[0]);
+    printf("usage: %s n_processes n_core_blocks n_swap_blocks spare [core_table_size]\n", argv[0]);
 #elif defined(INDIRECT_SWAP)
-    printf("usage: %s n_processes core_table_size swap_table_size spare [n_swap_blocks]\n", argv[0]);
+    printf("usage: %s n_processes n_core_blocks n_swap_blocks spare [swap_table_size]\n", argv[0]);
 #else
-    printf("usage: %s n_processes core_table_size swap_table_size spare\n", argv[0]);
+    printf("usage: %s n_processes n_core_blocks n_swap_blocks spare\n", argv[0]);
 #endif
     exit(EXIT_FAILURE);
   }
   int n_processes = atoi(argv[1]);
-  int core_table_size = atoi(argv[2]);
-  int swap_table_size = atoi(argv[3]);
+  int n_core_blocks = atoi(argv[2]);
+  int n_swap_blocks = atoi(argv[3]);
   int spare = atoi(argv[4]);
 #if defined(INDIRECT_CORE) && defined(INDIRECT_SWAP)
-  int n_core_blocks = argc >= 6 ? atoi(argv[5]) : core_table_size;
-  int n_swap_blocks = argc >= 7 ? atoi(argv[6]) : swap_table_size;
+  int core_table_size = argc >= 6 ? atoi(argv[5]) : n_core_blocks;
+  int swap_table_size = argc >= 7 ? atoi(argv[6]) : n_swap_blocks;
 #elif defined(INDIRECT_CORE)
-  int n_core_blocks = argc >= 6 ? atoi(argv[5]) : core_table_size;
+  int core_table_size = argc >= 6 ? atoi(argv[5]) : n_core_blocks;
 #elif defined(INDIRECT_SWAP)
-  int n_swap_blocks = argc >= 6 ? atoi(argv[5]) : swap_table_size;
+  int swap_table_size = argc >= 6 ? atoi(argv[5]) : n_swap_blocks;
 #endif
 
 #ifdef INDIRECT_CORE
-  core_init(core_table_size, n_core_blocks);
+  core_init(n_core_blocks, core_table_size);
 #else
-  core_init(core_table_size);
+  core_init(n_core_blocks);
 #endif
 #ifdef INDIRECT_SWAP
-  swap_init(swap_table_size, n_swap_blocks);
+  swap_init(n_swap_blocks, swap_table_size);
 #else
-  swap_init(swap_table_size);
+  swap_init(n_swap_blocks);
 #endif
   process_init(n_processes, spare);
   for (int i = 0; i < n_processes; ++i)
@@ -319,22 +319,43 @@ done:
         swap_base + swap_size
       );
       core_hash_verify(i, core_base, 0, core_size);
+#ifdef INDIRECT_CORE
+      core_block_free(core_table_mem + core_base, core_size);
+#endif
       swap_hash_verify(i, swap_base, 0, swap_size, core_size);
+#ifdef INDIRECT_SWAP
+      swap_block_free(swap_table_mem + swap_base, swap_size);
+#endif
     }
   }
 
-  for (int i = 0; i < core_table.item.base; ++i)
 #ifdef INDIRECT_CORE
+  for (int i = 0; i < core_table_size; ++i)
     rassert(core_table_mem[i] == 0x55555555);
-  for (int i = 0; i < n_core_blocks; ++i)
+  {
+    int j = n_core_blocks >> 3;
+    for (int i = 0; i < j; ++i)
+      rassert(core_block_bitmap[i] == 0xff);
+    int k = n_core_blocks & 7;
+    if (k)
+      rassert(core_block_bitmap[j] == (uint8_t)~(-1 << k));
+  }
 #endif
+  for (int i = 0; i < n_core_blocks; ++i)
     rassert(core_block_mem[i] == 0xaaaaaaaa);
-  int swap_limit = ~swap_table.item.base;
-  for (int i = 0; i < swap_limit; ++i)
 #ifdef INDIRECT_SWAP
+  for (int i = 0; i < swap_table_size; ++i)
     rassert(swap_table_mem[i] == 0x55555555);
-  for (int i = 0; i < n_swap_blocks; ++i)
+  {
+    int j = n_swap_blocks >> 3;
+    for (int i = 0; i < j; ++i)
+      rassert(swap_block_bitmap[i] == 0xff);
+    int k = n_swap_blocks & 7;
+    if (k)
+      rassert(swap_block_bitmap[j] == (uint8_t)~(-1 << k));
+  }
 #endif
+  for (int i = 0; i < n_swap_blocks; ++i)
     rassert(swap_block_mem[i] == 0xaaaaaaaa);
 
   return 0;
diff --git a/swap.c b/swap.c
index 5e4630e..5c96a47 100644 (file)
--- a/swap.c
+++ b/swap.c
@@ -78,14 +78,18 @@ static void swap_move_up(struct pool_item *item, int new_limit) {
 #endif
 
 #ifdef INDIRECT_SWAP
-void swap_init(int table_size, int n_blocks)
+void swap_init(int n_blocks, int table_size)
 #else
-void swap_init(int table_size)
+void swap_init(int n_blocks)
 #endif
 {
   pool_init(
     &swap_table,
+#ifdef INDIRECT_SWAP
     ~table_size,
+#else
+    ~n_blocks,
+#endif
     -1,
     swap_move,
 #ifdef MOVEABLE_SWAP
@@ -96,6 +100,10 @@ void swap_init(int table_size)
   );
 
 #ifdef INDIRECT_SWAP
+  swap_table_mem = malloc(table_size * sizeof(int));
+  rassert(swap_table_mem);
+  memset(swap_table_mem, 0x55, table_size * sizeof(int));
+
   n_swap_blocks = n_blocks;
   n_swap_block_bitmap = (n_swap_blocks + 7) >> 3;
   swap_block_bitmap = malloc(n_swap_block_bitmap);
@@ -105,19 +113,11 @@ void swap_init(int table_size)
     swap_block_bitmap[n_swap_blocks >> 3] = ~masks[n_swap_blocks & 7];
   swap_block_next = 0;
   swap_block_avail = n_swap_blocks;
-
-  swap_table_mem = malloc(table_size * sizeof(int));
-  rassert(swap_table_mem);
-  memset(swap_table_mem, 0x55, table_size * sizeof(int));
+#endif
 
   swap_block_mem = malloc(n_blocks * sizeof(int));
   rassert(swap_block_mem);
   memset(swap_block_mem, 0xaa, n_blocks * sizeof(int));
-#else
-  swap_block_mem = malloc(table_size * sizeof(int));
-  rassert(swap_block_mem);
-  memset(swap_block_mem, 0xaa, table_size * sizeof(int));
-#endif
 }
 
 #ifdef INDIRECT_SWAP
diff --git a/swap.h b/swap.h
index a24e18b..f3c8627 100644 (file)
--- a/swap.h
+++ b/swap.h
@@ -4,8 +4,8 @@
 #include <stdint.h>
 #include "pool.h"
 
-//#define INDIRECT_SWAP 1
-#define MOVEABLE_SWAP 1
+#define INDIRECT_SWAP 1
+//#define MOVEABLE_SWAP 1
 
 #ifdef MOVEABLE_SWAP
 #define swap_table_alloc(item, size) \