Subroutinize the hashing initialization and verification, to avoid duplication
authorNick Downing <nick@ndcode.org>
Sat, 23 Mar 2019 06:22:24 +0000 (17:22 +1100)
committerNick Downing <nick@ndcode.org>
Sat, 23 Mar 2019 06:22:24 +0000 (17:22 +1100)
core.c
core.h
pool_test_run.c
process_test_run.c
swap.c
swap.h

diff --git a/core.c b/core.c
index 8f293bc..bb7d10c 100644 (file)
--- a/core.c
+++ b/core.c
@@ -176,3 +176,33 @@ void core_block_free(int *table, int size) {
   core_block_avail += size;
 }
 #endif
+
+void core_hash_init(int process, int core_base, int base, int limit) {
+  for (int i = base; i < limit; ++i) {
+    long long hash = process * 17 + i * 29;
+    hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
+    hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
+#ifdef INDIRECT_CORE
+    int core_block = core_table_mem[core_base + i];
+#else
+    int core_block = core_base + i;
+#endif
+    rassert(core_block_mem[core_block] == 0xaaaaaaaa);
+    core_block_mem[core_block] = (int)hash;
+  }
+}
+
+void core_hash_verify(int process, int core_base, int base, int limit) {
+  for (int i = base; i < limit; ++i) {
+    long long hash = process * 17 + i * 29;
+    hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
+    hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
+#ifdef INDIRECT_CORE
+    int core_block = core_table_mem[core_base + i];
+#else
+    int core_block = core_base + i;
+#endif
+    rassert(core_block_mem[core_block] == (int)hash);
+    core_block_mem[core_block] = 0xaaaaaaaa;
+  }
+}
diff --git a/core.h b/core.h
index a2ec2e5..2b333bc 100644 (file)
--- a/core.h
+++ b/core.h
@@ -46,5 +46,7 @@ void core_block_free(int *blocks, int size);
 #else
 void core_init(int n_table);
 #endif
+void core_hash_init(int process, int core_base, int base, int limit);
+void core_hash_verify(int process, int core_base, int base, int limit);
 
 #endif
index 639aae6..637769c 100644 (file)
@@ -42,6 +42,26 @@ void move_up(struct pool_item *item, int new_limit) {
   }
 }
 
+void hash_init(int item, int item_base, int base, int limit) {
+  for (int i = base; i < limit; ++i) {
+    long long hash = item * 17 + i * 29;
+    hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
+    hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
+    rassert(mem[item_base + i] == 0xaaaaaaaa);
+    mem[item_base + i] = (int)hash;
+  }
+}
+
+void hash_verify(int item, int item_base, int base, int limit) {
+  for (int i = base; i < limit; ++i) {
+    long long hash = item * 17 + i * 29;
+    hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
+    hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
+    rassert(mem[item_base + i] == (int)hash);
+    mem[item_base + i] = 0xaaaaaaaa;
+  }
+}
+
 int main(int argc, char **argv) {
   if (argc < 3) {
     printf("usage: %s n_items pool_size [moveable]\n", argv[0]);
@@ -111,13 +131,7 @@ int main(int argc, char **argv) {
           int base = items[item].base;
           rassert(items[item].limit == base + size); 
  printf("new region [%d,%d)\n", base, base + size);
-          for (int i = 0; i < size; ++i) {
-            rassert(mem[base + i] == 0xaaaaaaaa);
-            long long hash = item * 17 + i * 29;
-            hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
-            hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
-            mem[base + i] = (int)hash;
-          }
+          hash_init(item, base, 0, size);
         }
       }
     }
@@ -155,13 +169,7 @@ int main(int argc, char **argv) {
           rassert(actual_old_size <= old_size);
         }
  printf("old region [%d,%d)\n", base, base + actual_old_size);
-        for (int i = size; i < actual_old_size; ++i) {
-          long long hash = item * 17 + i * 29;
-          hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
-          hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
-          rassert(mem[base + i] == (int)hash);
-          mem[base + i] = 0xaaaaaaaa;
-        }
+        hash_verify(item, base, size, actual_old_size);
         bool result =
           moveable ?
             pool_realloc_moveable(&head, items + item, size) :
@@ -181,13 +189,7 @@ int main(int argc, char **argv) {
             base = items[item].base;
             rassert(items[item].limit == base + size);
  printf("new region [%d,%d)\n", base, base + size);
-            for (int i = actual_old_size; i < size; ++i) {
-              rassert(mem[base + i] == 0xaaaaaaaa);
-              long long hash = item * 17 + i * 29;
-              hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
-              hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
-              mem[base + i] = (int)hash;
-            }
+            hash_init(item, base, actual_old_size, size);
           }
         }
       }
@@ -216,13 +218,7 @@ int main(int argc, char **argv) {
           rassert(actual_old_size <= old_size);
         }
  printf("old region [%d,%d)\n", base, base + actual_old_size);
-        for (int i = 0; i < actual_old_size; ++i) {
-          long long hash = item * 17 + i * 29;
-          hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
-          hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
-          rassert(mem[base + i] == (int)hash);
-          mem[base + i] = 0xaaaaaaaa;
-        }
+        hash_verify(item, base, 0, actual_old_size);
         pool_free(&head, items + item);
         items[item].prev = NULL;
         printf("... ok\n");
@@ -241,13 +237,7 @@ done:
       int base = items[i].base;
       int size = items[i].limit - base;
       printf("item %d: region [%d,%d)\n", i, base, base + size);
-      for (int j = 0; j < size; ++j) {
-        long long hash = i * 17 + j * 29;
-        hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
-        hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
-        rassert(mem[base + j] == (int)hash);
-        mem[base + j] = 0xaaaaaaaa;
-      }
+      hash_verify(i, base, 0, size);
     }
   }
 
index 5e7aa27..62855b2 100644 (file)
@@ -99,18 +99,7 @@ int main(int argc, char **argv) {
           assert(processes[process].pool_item.limit == core_base + size);
 #endif
  printf("new core [%d,%d)\n", core_base, core_base + size);
-          for (int i = 0; i < size; ++i) {
-            long long hash = process * 17 + i * 29;
-            hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
-            hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
-#ifdef INDIRECT_CORE
-            int core_block = core_table_mem[core_base + i];
-#else
-            int core_block = core_base + i;
-#endif
-            rassert(core_block_mem[core_block] == 0xaaaaaaaa);
-            core_block_mem[core_block] = (int)hash;
-          }
+          core_hash_init(process, core_base, 0, size);
 #ifdef PREALLOCATE_SWAP
  printf("new swap [%d,%d)\n", ~processes[process].swap_item.limit, ~processes[process].swap_item.base);
 #endif
@@ -166,18 +155,7 @@ int main(int argc, char **argv) {
 #else
  printf("old core [%d,%d)\n", core_base, core_base + actual_old_size);
 #endif
-        for (int i = size; i < actual_old_size; ++i) {
-          long long hash = process * 17 + i * 29;
-          hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
-          hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
-#ifdef INDIRECT_CORE
-          int core_block = core_table_mem[core_base + i];
-#else
-          int core_block = core_base + i;
-#endif
-          rassert(core_block_mem[core_block] == (int)hash);
-          core_block_mem[core_block] = 0xaaaaaaaa;
-        }
+        core_hash_verify(process, core_base, size, actual_old_size);
         bool result = process_realloc(processes + process, size);
         printf(
           "... %s\n",
@@ -203,18 +181,7 @@ int main(int argc, char **argv) {
 #else
  printf("new core [%d,%d)\n", core_base, core_base + size);
 #endif
-            for (int i = actual_old_size; i < size; ++i) {
-              long long hash = process * 17 + i * 29;
-              hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
-              hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
-#ifdef INDIRECT_CORE
-              int core_block = core_table_mem[core_base + i];
-#else
-              int core_block = core_base + i;
-#endif
-              rassert(core_block_mem[core_block] == 0xaaaaaaaa);
-              core_block_mem[core_block] = (int)hash;
-            }
+            core_hash_init(process, core_base, actual_old_size, size);
           }
         }
       }
@@ -285,33 +252,11 @@ int main(int argc, char **argv) {
             swap_limit - victim_swap_item.base :
             !in_core ? swap_limit - processes[process].pool_item.base : 0;
 #endif
+        rassert(core_size + swap_size == actual_old_size);
         int swap_base = ~swap_limit;
  printf("old core [%d,%d) swap [%d,%d)\n", core_base, core_base + core_size, swap_base, swap_base + swap_size);
-        for (int i = 0; i < core_size; ++i) {
-          long long hash = process * 17 + i * 29;
-          hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
-          hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
-#ifdef INDIRECT_CORE
-          int core_block = core_table_mem[core_base + i];
-#else
-          int core_block = core_base + i;
-#endif
-          rassert(core_block_mem[core_block] == (int)hash);
-          core_block_mem[core_block] = 0xaaaaaaaa;
-        }
-        for (int i = 0; i < swap_size; ++i) {
-          long long hash = process * 17 + (core_size + i) * 29;
-          hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
-          hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
-#ifdef INDIRECT_SWAP
-          int swap_block = swap_table_mem[swap_base + i];
-#else
-          int swap_block = swap_base + i;
-#endif
-          rassert(swap_block_mem[swap_block] == (int)hash);
-          swap_block_mem[swap_block] = 0xaaaaaaaa;
-        }
-        rassert(core_size + swap_size == actual_old_size);
+        core_hash_verify(process, core_base, 0, core_size);
+        swap_hash_verify(process, swap_base, 0, swap_size, core_size);
         process_free(processes + process);
         processes[process].size = -1;
         printf("... ok\n");
@@ -363,6 +308,7 @@ done:
           swap_limit - victim_swap_item.base :
           !in_core ? swap_limit - processes[i].pool_item.base : 0;
 #endif
+      rassert(core_size + swap_size == processes[i].size);
       int swap_base = ~swap_limit;
       printf(
         "process %d: core [%d,%d) swap [%d,%d)\n",
@@ -372,32 +318,8 @@ done:
         swap_base,
         swap_base + swap_size
       );
-      for (int j = 0; j < core_size; ++j) {
-        long long hash = i * 17 + j * 29;
-        hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
-        hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
-#ifdef INDIRECT_CORE
-        int core_block = core_table_mem[core_base + j];
-        core_table_mem[core_base + j] = 0x55555555;
-#else
-        int core_block = core_base + j;
-#endif
-        rassert(core_block_mem[core_block] == (int)hash);
-        core_block_mem[core_block] = 0xaaaaaaaa;
-      }
-      for (int j = 0; j < swap_size; ++j) {
-        long long hash = i * 17 + (core_size + j) * 29;
-        hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
-        hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
-#ifdef INDIRECT_SWAP
-        int swap_block = swap_table_mem[swap_base + j];
-        swap_table_mem[swap_base + j] = 0x55555555;
-#else
-        int swap_block = swap_base + j;
-#endif
-        rassert(swap_block_mem[swap_block] == (int)hash);
-        swap_block_mem[swap_block] = 0xaaaaaaaa;
-      }
+      core_hash_verify(i, core_base, 0, core_size);
+      swap_hash_verify(i, swap_base, 0, swap_size, core_size);
     }
   }
 
diff --git a/swap.c b/swap.c
index bca5611..5e4630e 100644 (file)
--- a/swap.c
+++ b/swap.c
@@ -244,3 +244,24 @@ void swap_write(int swap_base, int core_base, int size) {
  printf("\n");
 #endif
 }
+
+void swap_hash_verify(
+  int process,
+  int swap_base,
+  int base,
+  int limit,
+  int offset
+) {
+  for (int i = base; i < limit; ++i) {
+    long long hash = process * 17 + (i + offset) * 29;
+    hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
+    hash = (hash & 0xffffffffffffffffLL) + (hash >> 32);
+#ifdef INDIRECT_SWAP
+    int swap_block = swap_table_mem[swap_base + i];
+#else
+    int swap_block = swap_base + i;
+#endif
+    rassert(swap_block_mem[swap_block] == (int)hash);
+    swap_block_mem[swap_block] = 0xaaaaaaaa;
+  }
+}
diff --git a/swap.h b/swap.h
index dc2acc2..a24e18b 100644 (file)
--- a/swap.h
+++ b/swap.h
@@ -48,5 +48,12 @@ void swap_init(int n_table);
 #endif
 void swap_read(int swap_base, int core_base, int size);
 void swap_write(int swap_base, int core_base, int size);
+void swap_hash_verify(
+  int process,
+  int swap_base,
+  int base,
+  int limit,
+  int offset
+);
 
 #endif