Simplify the calling convention of the hash init and verify routines
authorNick Downing <nick@ndcode.org>
Thu, 4 Apr 2019 09:21:18 +0000 (20:21 +1100)
committerNick Downing <nick@ndcode.org>
Thu, 4 Apr 2019 09:40:04 +0000 (20:40 +1100)
n.sh
pool_test_run.c
process_test_run.c

diff --git a/n.sh b/n.sh
index 9c38ecb..18ad7bb 100755 (executable)
--- a/n.sh
+++ b/n.sh
@@ -1,7 +1,7 @@
 #!/bin/sh
 
 echo "generate test script"
-./pool_test_gen 64 256 1024 16 false >pool_test.txt
+./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
index 27561c2..07bf41c 100644 (file)
@@ -42,23 +42,23 @@ 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;
+void hash_init(int item, int base, int size, int offset) {
+  for (int i = 0; i < size; ++i) {
+    long long hash = item * 17 + (i + offset) * 29;
     hash = (hash & 0xffffffffLL) + (hash >> 32);
     hash = (hash & 0xffffffffLL) + (hash >> 32);
-    rassert(mem[item_base + i] == 0xaaaaaaaa);
-    mem[item_base + i] = (int)hash;
+    rassert(mem[base + i] == 0xaaaaaaaa);
+    mem[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;
+void hash_verify(int item, int base, int size, int offset) {
+  for (int i = 0; i < size; ++i) {
+    long long hash = item * 17 + (i + offset) * 29;
     hash = (hash & 0xffffffffLL) + (hash >> 32);
     hash = (hash & 0xffffffffLL) + (hash >> 32);
-    rassert(mem[item_base + i] == (int)hash);
-    mem[item_base + i] = 0xaaaaaaaa;
+    rassert(mem[base + i] == (int)hash);
+    mem[base + i] = 0xaaaaaaaa;
   }
 }
 
@@ -131,7 +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);
-          hash_init(item, base, 0, size);
+          hash_init(item, base, size, 0);
         }
       }
     }
@@ -169,7 +169,12 @@ int main(int argc, char **argv) {
           rassert(actual_old_size <= old_size);
         }
  printf("old region [%d,%d)\n", base, base + actual_old_size);
-        hash_verify(item, base, size, actual_old_size);
+        hash_verify(
+          item,
+          base + size,
+          actual_old_size - size,
+          size
+        );
         bool result =
           moveable ?
             pool_realloc_moveable(&head, items + item, size) :
@@ -189,7 +194,12 @@ 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);
-            hash_init(item, base, actual_old_size, size);
+            hash_init(
+              item,
+              base + actual_old_size,
+              size - actual_old_size,
+              actual_old_size
+            );
           }
         }
       }
@@ -228,7 +238,7 @@ int main(int argc, char **argv) {
           rassert(actual_old_size <= old_size);
         }
  printf("old region [%d,%d)\n", base, base + actual_old_size);
-        hash_verify(item, base, 0, actual_old_size - size);
+        hash_verify(item, base, actual_old_size - size, 0);
         bool result =
           moveable ?
             pool_realloc_base_moveable(&head, items + item, size) :
@@ -248,13 +258,21 @@ 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);
-            hash_verify(
-              item,
-              base + size - actual_old_size,
-              size >= actual_old_size ? 0 : actual_old_size - size,
-              actual_old_size
-            );
-            hash_init(item, base, 0, size);
+            if (size >= actual_old_size)
+              hash_verify(
+                item,
+                base + size - actual_old_size,
+                actual_old_size,
+                0
+              );
+            else
+              hash_verify(
+                item,
+                base,
+                size,
+                actual_old_size - size
+              );
+            hash_init(item, base, size, 0);
           }
         }
       }
@@ -283,7 +301,7 @@ int main(int argc, char **argv) {
           rassert(actual_old_size <= old_size);
         }
  printf("old region [%d,%d)\n", base, base + actual_old_size);
-        hash_verify(item, base, 0, actual_old_size);
+        hash_verify(item, base, actual_old_size, 0);
         pool_free(&head, items + item);
         items[item].prev = NULL;
         printf("... ok\n");
@@ -302,7 +320,7 @@ done:
       int base = items[i].base;
       int size = items[i].limit - base;
       printf("item %d: region [%d,%d)\n", i, base, base + size);
-      hash_verify(i, base, 0, size);
+      hash_verify(i, base, size, 0);
     }
   }
 
index e052745..8c66859 100644 (file)
@@ -7,15 +7,15 @@
 #include "rassert.h"
 #include "swap.h"
 
-void core_hash_init(int process, int core_base, int base, int limit) {
-  for (int i = base; i < limit; ++i) {
+void core_hash_init(int process, int base, int blocks, int offset) {
+  for (int i = 0; i < blocks; ++i) {
 #ifdef INDIRECT_CORE
-    int core_block = core_table_mem[core_base + i];
+    int core_block = core_table_mem[base + i];
 #else
-    int core_block = core_base + i;
+    int core_block = base + i;
 #endif
     for (int j = 0; j < BLOCK_SIZE; ++j) {
-      long long hash = process * 17 + i * 29 + j * 37;
+      long long hash = process * 17 + (i + offset) * 29 + j * 37;
       hash = (hash & 0xffffffffLL) + (hash >> 32);
       hash = (hash & 0xffffLL) + (hash >> 16);
       hash = (hash & 0xffLL) + (hash >> 8);
@@ -26,15 +26,15 @@ 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) {
-  for (int i = base; i < limit; ++i) {
+void core_hash_verify(int process, int base, int blocks, int offset) {
+  for (int i = 0; i < blocks; ++i) {
 #ifdef INDIRECT_CORE
-    int core_block = core_table_mem[core_base + i];
+    int core_block = core_table_mem[base + i];
 #else
-    int core_block = core_base + i;
+    int core_block = base + i;
 #endif
     for (int j = 0; j < BLOCK_SIZE; ++j) {
-      long long hash = process * 17 + i * 29 + j * 37;
+      long long hash = process * 17 + (i + offset) * 29 + j * 37;
       hash = (hash & 0xffffffffLL) + (hash >> 32);
       hash = (hash & 0xffffLL) + (hash >> 16);
       hash = (hash & 0xffLL) + (hash >> 8);
@@ -49,15 +49,15 @@ void core_hash_verify(int process, int core_base, int base, int limit) {
   }
 }
 
-void swap_hash_verify(int process, int swap_base, int base, int limit) {
-  for (int i = base; i < limit; ++i) {
+void swap_hash_verify(int process, int base, int blocks, int offset) {
+  for (int i = 0; i < blocks; ++i) {
 #ifdef INDIRECT_SWAP
-    int swap_block = swap_table_mem[swap_base + i];
+    int swap_block = swap_table_mem[base + i];
 #else
-    int swap_block = swap_base + i;
+    int swap_block = base + i;
 #endif
     for (int j = 0; j < BLOCK_SIZE; ++j) {
-      long long hash = process * 17 + i * 29 + j * 37;
+      long long hash = process * 17 + (i + offset) * 29 + j * 37;
       hash = (hash & 0xffffffffLL) + (hash >> 32);
       hash = (hash & 0xffffLL) + (hash >> 16);
       hash = (hash & 0xffLL) + (hash >> 8);
@@ -164,7 +164,7 @@ int main(int argc, char **argv) {
               core_base + blocks
           );
  printf("new core [%d,%d)\n", core_base, core_base + blocks);
-          core_hash_init(process, core_base, 0, blocks);
+          core_hash_init(process, core_base, blocks, 0);
 #ifdef PREALLOCATE_SWAP
  printf("new swap [%d,%d)\n", PER_PROCESS_SWAP_ITEM(processes + process).base, PER_PROCESS_SWAP_ITEM(processes + process).limit);
 #endif
@@ -217,7 +217,12 @@ int main(int argc, char **argv) {
  printf("old core [%d,%d)\n", core_base, core_base + actual_old_blocks);
 #endif
         int blocks = (int)((size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT);
-        core_hash_verify(process, core_base, blocks, actual_old_blocks);
+        core_hash_verify(
+          process,
+          core_base + blocks,
+          actual_old_blocks - blocks,
+          blocks
+        );
         bool result = process_realloc(processes + process, size);
         printf(
           "... %s\n",
@@ -241,7 +246,12 @@ int main(int argc, char **argv) {
 #else
  printf("new core [%d,%d)\n", core_base, core_base + blocks);
 #endif
-            core_hash_init(process, core_base, actual_old_blocks, blocks);
+            core_hash_init(
+              process,
+              core_base + actual_old_blocks,
+              blocks - actual_old_blocks,
+              actual_old_blocks
+            );
           }
         }
       }
@@ -307,13 +317,8 @@ int main(int argc, char **argv) {
         rassert(core_blocks + swap_blocks == actual_old_blocks);
  printf("old core [%d,%d) swap [%d,%d)\n", core_base, core_base + core_blocks, swap_base, swap_base + swap_blocks);
  fflush(stdout);
-        core_hash_verify(
-          process,
-          core_base - swap_blocks,
-          swap_blocks,
-          swap_blocks + core_blocks
-        );
-        swap_hash_verify(process, swap_base, 0, swap_blocks);
+        core_hash_verify(process, core_base, core_blocks, swap_blocks);
+        swap_hash_verify(process, swap_base, swap_blocks, 0);
         process_free(processes + process);
         processes[process].size = -1L;
         printf("... ok\n");
@@ -361,16 +366,11 @@ done:
         swap_base,
         swap_base + swap_blocks
       );
-      core_hash_verify(
-        i,
-        core_base - swap_blocks,
-        swap_blocks,
-        swap_blocks + core_blocks
-      );
+      core_hash_verify(i, core_base, core_blocks, swap_blocks);
 #ifdef INDIRECT_CORE
       core_block_free(core_table_mem + core_base, core_blocks);
 #endif
-      swap_hash_verify(i, swap_base, 0, swap_blocks);
+      swap_hash_verify(i, swap_base, swap_blocks, 0);
 #ifdef INDIRECT_SWAP
       swap_block_free(swap_table_mem + swap_base, swap_blocks);
 #endif