Move core_block_mem and swap_block_mem into pool_test_run.c and make core/swap memory...
authorNick Downing <nick@ndcode.org>
Fri, 5 Apr 2019 23:11:14 +0000 (10:11 +1100)
committerNick Downing <nick@ndcode.org>
Sat, 6 Apr 2019 03:00:07 +0000 (14:00 +1100)
core.c
core.h
process_test_run.c
swap.c
swap.h

diff --git a/core.c b/core.c
index 3c5fb24..21ac014 100644 (file)
--- a/core.c
+++ b/core.c
@@ -1,11 +1,12 @@
+#include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include "core.h"
+#include "process.h"
 #include "rassert.h"
 
 struct pool_head core_table;
-
 #ifdef INDIRECT_CORE
 int *core_table_mem;
 
@@ -18,67 +19,77 @@ uint8_t *core_block_bitmap;
 int core_block_next;
 int core_block_avail;
 #endif
-uint8_t *core_block_mem;
 
 static void core_move(struct pool_item *item, int new_base) {
   int base = item->base;
-  int size = item->limit - base;
+  int blocks = item->limit - base;
   printf(
     "core_move [%d,%d) to [%d,%d)\n",
     base,
-    base + size,
+    base + blocks,
     new_base,
-    new_base + size
+    new_base + blocks
   );
-  assert(new_base <= base || new_base >= base + size);
-  for (int i = 0; i < size; ++i) {
+  assert(new_base <= base || new_base >= base + blocks);
 #ifdef INDIRECT_CORE
+  for (int i = 0; i < blocks; ++i) {
     core_table_mem[new_base + i] = core_table_mem[base + i];
+#ifndef NDEBUG
     core_table_mem[base + i] = 0x55555555;
-#else
-    memcpy(
-      core_block_mem + (new_base + i) * BLOCK_SIZE,
-      core_block_mem + (base + i) * BLOCK_SIZE,
-      BLOCK_SIZE
-    );
-    memset(
-      core_block_mem + (base + i) * BLOCK_SIZE,
-      0xaa,
-      BLOCK_SIZE
-    );
 #endif
   }
+#else
+  // see if incomplete last block, if so don't copy extra part
+  struct process *process =
+    (struct process *)((char *)item - offsetof(struct process, core_item));
+  long size = (long)blocks << BLOCK_SHIFT;
+  if (size > process->size)
+    size = process->size;
+
+  // copy by abstract routine
+  core_copy((long)base << BLOCK_SHIFT, (long)new_base << BLOCK_SHIFT, size);
+#endif
 }
 
 #ifdef MOVEABLE_CORE
 static void core_move_up(struct pool_item *item, int new_limit) {
   int limit = item->limit;
-  int neg_size = item->base - limit;
+  int blocks = limit - item->base;
   printf(
     "core_move_up [%d,%d) to [%d,%d)\n",
-    limit + neg_size,
+    limit - blocks,
     limit,
-    new_limit + neg_size,
+    new_limit - blocks,
     new_limit
   );
-  assert(new_limit >= limit || new_limit <= limit + neg_size);
-  for (int i = -1; i >= neg_size; --i) {
+  assert(new_limit >= limit || new_limit <= limit - blocks);
 #ifdef INDIRECT_CORE
+  blocks = -blocks;
+  for (int i = -1; i >= blocks; --i) {
     core_table_mem[new_limit + i] = core_table_mem[limit + i];
+#ifndef NDEBUG
     core_table_mem[limit + i] = 0x55555555;
-#else
-    memcpy(
-      core_block_mem + (new_limit + i) * BLOCK_SIZE,
-      core_block_mem + (limit + i) * BLOCK_SIZE,
-      BLOCK_SIZE
-    );
-    memset(
-      core_block_mem + (limit + i) * BLOCK_SIZE,
-      0xaa,
-      BLOCK_SIZE
-    );
 #endif
   }
+#else
+  // set up transfer parameters
+  long limit1 = (long)limit << BLOCK_SHIFT;
+  long new_limit1 = (long)new_limit << BLOCK_SHIFT;
+  long size = (long)blocks << BLOCK_SHIFT;
+
+  // see if incomplete last block, if so don't copy extra part
+  struct process *process =
+    (struct process *)((char *)item - offsetof(struct process, core_item));
+  long trim = size - process->size;
+  if (trim > 0) {
+    limit1 -= trim;
+    new_limit1 -= trim;
+    size = process->size;
+  }
+
+  // copy by abstract routine
+  core_copy_up(limit1, new_limit1, size);
+#endif
 }
 #endif
 
@@ -119,10 +130,6 @@ void core_init(int n_blocks)
   core_block_next = 0;
   core_block_avail = n_core_blocks;
 #endif
-
-  core_block_mem = malloc(n_blocks * BLOCK_SIZE);
-  rassert(core_block_mem);
-  memset(core_block_mem, 0xaa, n_blocks * BLOCK_SIZE);
 }
 
 #ifdef INDIRECT_CORE
diff --git a/core.h b/core.h
index d7403a3..21125a0 100644 (file)
--- a/core.h
+++ b/core.h
@@ -33,7 +33,6 @@
 #endif
 
 extern struct pool_head core_table;
-
 #ifdef INDIRECT_CORE
 extern int *core_table_mem;
 
@@ -43,7 +42,6 @@ extern uint8_t *core_block_bitmap;
 extern int core_block_next;
 extern int core_block_avail;
 #endif
-extern uint8_t *core_block_mem;
 
 #ifdef INDIRECT_CORE
 void core_init(int n_table, int n_blocks);
@@ -51,6 +49,10 @@ bool core_block_alloc(int *blocks, int size);
 void core_block_free(int *blocks, int size);
 #else
 void core_init(int n_table);
+
+// abstract
+void core_copy(long src_base, long dest_base, long size);
+void core_copy_up(long src_limit, long dest_limit, long size);
 #endif
 
 #endif
index 57dac14..091842f 100644 (file)
@@ -7,9 +7,13 @@
 #include "rassert.h"
 #include "swap.h"
 
+uint8_t *core_block_mem;
+uint8_t *swap_block_mem;
+
 void core_hash_init(int process, long base, long size, long offset) {
-  for (long i = 0; i < size; ++i) {
-    int addr = base + i;
+ //printf("core_hash_init %d %ld(%d) %ld(%d) %ld(%d)\n", process, base, (int)(base >> BLOCK_SHIFT), size, (int)((size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT), offset, (int)(offset >> BLOCK_SHIFT));
+  for (long i = 0L; i < size; ++i) {
+    long addr = base + i;
 #ifdef INDIRECT_CORE
     addr =
       (core_table_mem[addr >> BLOCK_SHIFT] << BLOCK_SHIFT) |
@@ -20,14 +24,16 @@ void core_hash_init(int process, long base, long size, long offset) {
     hash = (hash & 0xffffLL) + (hash >> 16);
     hash = (hash & 0xffLL) + (hash >> 8);
     hash = (hash & 0xffLL) + (hash >> 8);
+ if (core_block_mem[addr] != 0xaa) printf("%ld\n", addr);
     rassert(core_block_mem[addr] == 0xaa);
     core_block_mem[addr] = (uint8_t)hash;
   }
 }
 
 void core_hash_verify(int process, long base, long size, long offset) {
-  for (long i = 0; i < size; ++i) {
-    int addr = base + i;
+ //printf("core_hash_verify %d %ld(%d) %ld(%d) %ld(%d)\n", process, base, (int)(base >> BLOCK_SHIFT), size, (int)((size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT), offset, (int)(offset >> BLOCK_SHIFT));
+  for (long i = 0L; i < size; ++i) {
+    long addr = base + i;
 #ifdef INDIRECT_CORE
     addr =
       (core_table_mem[addr >> BLOCK_SHIFT] << BLOCK_SHIFT) |
@@ -44,8 +50,9 @@ void core_hash_verify(int process, long base, long size, long offset) {
 }
 
 void swap_hash_verify(int process, long base, long size, long offset) {
-  for (long i = 0; i < size; ++i) {
-    int addr = base + i;
+ //printf("swap_hash_verify %d %ld(%d) %ld(%d) %ld(%d)\n", process, base, (int)(base >> BLOCK_SHIFT), size, (int)((size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT), offset, (int)(offset >> BLOCK_SHIFT));
+  for (long i = 0L; i < size; ++i) {
+    long addr = base + i;
 #ifdef INDIRECT_SWAP
     addr =
       (swap_table_mem[addr >> BLOCK_SHIFT] << BLOCK_SHIFT) |
@@ -61,6 +68,68 @@ void swap_hash_verify(int process, long base, long size, long offset) {
   }
 }
 
+#ifndef INDIRECT_CORE
+void core_copy(long src_base, long dest_base, long size) {
+ //printf("core_copy %ld(%d) %ld(%d) %ld(%d)\n", src_base, (int)(src_base >> BLOCK_SHIFT), dest_base, (int)(dest_base >> BLOCK_SHIFT), size, (int)((size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT));
+  for (long i = 0L; i < size; ++i) {
+    core_block_mem[dest_base + i] = core_block_mem[src_base + i];
+#ifndef NDEBUG
+    core_block_mem[src_base + i] = 0xaa;
+#endif
+  }
+}
+
+void core_copy_up(long src_limit, long dest_limit, long size) {
+ //printf("core_copy_up %ld(%d) %ld(%d) %ld(%d)\n", src_limit, (int)(src_limit >> BLOCK_SHIFT), dest_limit, (int)(dest_limit >> BLOCK_SHIFT), size, (int)((size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT));
+  size = -size;
+  for (long i = -1L; i >= size; --i) {
+    core_block_mem[dest_limit + i] = core_block_mem[src_limit + i];
+#ifndef NDEBUG
+    core_block_mem[src_limit + i] = 0xaa;
+#endif
+  }
+}
+#endif
+
+#ifndef INDIRECT_SWAP
+void swap_copy(long src_base, long dest_base, long size) {
+ //printf("swap_copy %ld(%d) %ld(%d) %ld(%d)\n", src_base, (int)(src_base >> BLOCK_SHIFT), dest_base, (int)(dest_base >> BLOCK_SHIFT), size, (int)((size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT));
+  for (long i = 0; i < size; ++i) {
+    swap_block_mem[dest_base + i] = swap_block_mem[src_base + i];
+#ifndef NDEBUG
+    swap_block_mem[src_base + i] = 0x55;
+#endif
+  }
+}
+
+void swap_copy_up(long src_limit, long dest_limit, long size) {
+ //printf("swap_copy_up %ld(%d) %ld(%d) %ld(%d)\n", src_limit, (int)(src_limit >> BLOCK_SHIFT), dest_limit, (int)(dest_limit >> BLOCK_SHIFT), size, (int)((size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT));
+  size = -size;
+  for (long i = -1; i >= size; --i) {
+    swap_block_mem[dest_limit + i] = swap_block_mem[src_limit + i];
+#ifndef NDEBUG
+    swap_block_mem[src_limit + i] = 0x55;
+#endif
+  }
+}
+#endif
+
+void core_to_swap_copy(long src_base, long dest_base, long size) {
+ //printf("core_to_swap_copy %ld(%d) %ld(%d) %ld(%d)\n", src_base, (int)(src_base >> BLOCK_SHIFT), dest_base, (int)(dest_base >> BLOCK_SHIFT), size, (int)((size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT));
+  for (long i = 0; i < size; ++i)
+    assert(swap_block_mem[dest_base + i] == 0xaa);
+  memcpy(swap_block_mem + dest_base, core_block_mem + src_base, size);
+  memset(core_block_mem + src_base, 0xaa, size);
+}
+
+void swap_to_core_copy(long src_base, long dest_base, long size) {
+ //printf("swap_to_core_copy %ld(%d) %ld(%d) %ld(%d)\n", src_base, (int)(src_base >> BLOCK_SHIFT), dest_base, (int)(dest_base >> BLOCK_SHIFT), size, (int)((size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT));
+  for (long i = 0; i < size; ++i)
+    assert(core_block_mem[dest_base + i] == 0xaa);
+  memcpy(core_block_mem + dest_base, swap_block_mem + src_base, size);
+  memset(swap_block_mem + src_base, 0xaa, size);
+}
+
 int main(int argc, char **argv) {
   if (argc < 5) {
 #if defined(INDIRECT_CORE) && defined(INDIRECT_SWAP)
@@ -86,12 +155,19 @@ int main(int argc, char **argv) {
 #elif defined(INDIRECT_SWAP)
   int swap_table_blocks = argc >= 6 ? atoi(argv[5]) : n_swap_blocks;
 #endif
+  swap_block_mem = malloc((long)n_swap_blocks << BLOCK_SHIFT);
+  rassert(swap_block_mem);
+  memset(swap_block_mem, 0xaa, (long)n_swap_blocks << BLOCK_SHIFT);
 
 #ifdef INDIRECT_CORE
   core_init(n_core_blocks, core_table_blocks);
 #else
   core_init(n_core_blocks);
 #endif
+  core_block_mem = malloc((long)n_core_blocks << BLOCK_SHIFT);
+  rassert(core_block_mem);
+  memset(core_block_mem, 0xaa, (long)n_core_blocks << BLOCK_SHIFT);
+
 #ifdef INDIRECT_SWAP
   swap_init(n_swap_blocks, swap_table_blocks);
 #else
diff --git a/swap.c b/swap.c
index 2cfe598..50f8e88 100644 (file)
--- a/swap.c
+++ b/swap.c
@@ -1,12 +1,13 @@
+#include <stddef.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include "core.h"
+#include "process.h"
 #include "swap.h"
 #include "rassert.h"
 
 struct pool_head swap_table;
-
 #ifdef INDIRECT_SWAP
 int *swap_table_mem;
 
@@ -19,67 +20,77 @@ uint8_t *swap_block_bitmap;
 int swap_block_next;
 int swap_block_avail;
 #endif
-uint8_t *swap_block_mem;
 
 static void swap_move(struct pool_item *item, int new_base) {
   int base = item->base;
-  int size = item->limit - base;
+  int blocks = item->limit - base;
   printf(
     "swap_move [%d,%d) to [%d,%d)\n",
     base,
-    base + size,
+    base + blocks,
     new_base,
-    new_base + size
+    new_base + blocks
   );
-  assert(new_base <= base || new_base >= base + size);
-  for (int i = 0; i < size; ++i) {
+  assert(new_base <= base || new_base >= base + blocks);
 #ifdef INDIRECT_SWAP
+  for (int i = 0; i < blocks; ++i) {
     swap_table_mem[new_base + i] = swap_table_mem[base + i];
+#ifndef NDEBUG
     swap_table_mem[base + i] = 0x55555555;
-#else
-    memcpy(
-      swap_block_mem + (new_base + i) * BLOCK_SIZE,
-      swap_block_mem + (base + i) * BLOCK_SIZE,
-      BLOCK_SIZE
-    );
-    memset(
-      swap_block_mem + (base + i) * BLOCK_SIZE,
-      0xaa,
-      BLOCK_SIZE
-    );
 #endif
   }
+#else
+  // see if incomplete last block, if so don't copy extra part
+  struct process *process =
+    (struct process *)((char *)item - offsetof(struct process, swap_item));
+  long size = (long)blocks << BLOCK_SHIFT;
+  if (size > process->size)
+    size = process->size;
+
+  // copy by abstract routine
+  swap_copy((long)base << BLOCK_SHIFT, (long)new_base << BLOCK_SHIFT, size);
+#endif
 }
 
 #ifdef MOVEABLE_SWAP
 static void swap_move_up(struct pool_item *item, int new_limit) {
   int limit = item->limit;
-  int neg_size = item->base - limit;
+  int blocks = limit - item->base;
   printf(
     "swap_move_up [%d,%d) to [%d,%d)\n",
-    limit + neg_size,
+    limit - blocks,
     limit,
-    new_limit + neg_size,
+    new_limit - blocks,
     new_limit
   );
-  assert(new_limit >= limit || new_limit <= limit + neg_size);
-  for (int i = -1; i >= neg_size; --i) {
+  assert(new_limit >= limit || new_limit <= limit - blocks);
 #ifdef INDIRECT_SWAP
+  blocks = -blocks;
+  for (int i = -1; i >= blocks; --i) {
     swap_table_mem[new_limit + i] = swap_table_mem[limit + i];
+#ifndef NDEBUG
     swap_table_mem[limit + i] = 0x55555555;
-#else
-    memcpy(
-      swap_block_mem + (new_limit + i) * BLOCK_SIZE,
-      swap_block_mem + (limit + i) * BLOCK_SIZE,
-      BLOCK_SIZE
-    );
-    memset(
-      swap_block_mem + (limit + i) * BLOCK_SIZE,
-      0xaa,
-      BLOCK_SIZE
-    );
 #endif
   }
+#else
+  // set up transfer parameters
+  long limit1 = (long)limit << BLOCK_SHIFT;
+  long new_limit1 = (long)new_limit << BLOCK_SHIFT;
+  long size = (long)blocks << BLOCK_SHIFT;
+
+  // see if incomplete last block, if so don't copy extra part
+  struct process *process =
+    (struct process *)((char *)item - offsetof(struct process, swap_item));
+  long trim = size - process->size;
+  if (trim > 0) {
+    limit1 -= trim;
+    new_limit1 -= trim;
+    size = process->size;
+  }
+
+  // copy by abstract routine
+  swap_copy_up(limit1, new_limit1, size);
+#endif
 }
 #endif
 
@@ -120,10 +131,6 @@ void swap_init(int n_blocks)
   swap_block_next = 0;
   swap_block_avail = n_swap_blocks;
 #endif
-
-  swap_block_mem = malloc(n_blocks * BLOCK_SIZE);
-  rassert(swap_block_mem);
-  memset(swap_block_mem, 0xaa, n_blocks * BLOCK_SIZE);
 }
 
 #ifdef INDIRECT_SWAP
@@ -207,7 +214,6 @@ void swap_read(int swap_base, int core_base, long size) {
     swap_block = swap_table_mem[swap_block];
  printf(" %d", swap_block);
 #endif
-    long swap_addr = (long)swap_block << BLOCK_SHIFT;
     int core_block = core_base + i;
 #ifdef INDIRECT_CORE
     core_block = core_table_mem[core_block];
@@ -217,17 +223,17 @@ void swap_read(int swap_base, int core_base, long size) {
  printf(" %d", core_block);
 #endif
 #endif
-    long core_addr = (long)core_block << BLOCK_SHIFT;
-    int count = size < BLOCK_SIZE ? (int)size : BLOCK_SIZE;
-    size -= count;
-    for (int j = 0; j < count; ++j)
-      assert(core_block_mem[core_addr + j] == 0xaa);
-    memcpy(core_block_mem + core_addr, swap_block_mem + swap_addr, count);
-    memset(swap_block_mem + swap_addr, 0xaa, count);
- if (count < BLOCK_SIZE) {
+    long count = size < BLOCK_SIZE ? size : BLOCK_SIZE;
+#if defined(INDIRECT_CORE) || defined(INDIRECT_SWAP)
+ if (count < BLOCK_SIZE)
   putchar('*');
-  assert(swap_block_mem[swap_addr + count] == 0xaa);
- }
+#endif
+    swap_to_core_copy(
+      (long)swap_block << BLOCK_SHIFT,
+      (long)core_block << BLOCK_SHIFT,
+      count
+    );
+    size -= count;
   }
 #if defined(INDIRECT_CORE) || defined(INDIRECT_SWAP)
  printf("\n");
@@ -246,7 +252,6 @@ void swap_write(int swap_base, int core_base, long size) {
     core_block = core_table_mem[core_block];
  printf(" %d", core_block);
 #endif
-    long core_addr = (long)core_block << BLOCK_SHIFT;
     int swap_block = swap_base + i;
 #ifdef INDIRECT_SWAP
     swap_block = swap_table_mem[swap_block];
@@ -256,17 +261,17 @@ void swap_write(int swap_base, int core_base, long size) {
  printf(" %d", swap_block);
 #endif
 #endif
-    long swap_addr = (long)swap_block << BLOCK_SHIFT;
-    int count = size < BLOCK_SIZE ? (int)size : BLOCK_SIZE;
-    size -= count;
-    for (int j = 0; j < count; ++j)
-      assert(swap_block_mem[swap_addr + j] == 0xaa);
-    memcpy(swap_block_mem + swap_addr, core_block_mem + core_addr, count);
-    memset(core_block_mem + core_addr, 0xaa, count);
- if (count < BLOCK_SIZE) {
+    long count = size < BLOCK_SIZE ? size : BLOCK_SIZE;
+#if defined(INDIRECT_CORE) || defined(INDIRECT_SWAP)
+ if (count < BLOCK_SIZE)
   putchar('*');
-  assert(core_block_mem[core_addr + count] == 0xaa);
- }
+#endif
+    core_to_swap_copy(
+      (long)core_block << BLOCK_SHIFT,
+      (long)swap_block << BLOCK_SHIFT,
+      count
+    );
+    size -= count;
   }
 #if defined(INDIRECT_CORE) || defined(INDIRECT_SWAP)
  printf("\n");
diff --git a/swap.h b/swap.h
index 894ced1..2d1d0b3 100644 (file)
--- a/swap.h
+++ b/swap.h
@@ -4,7 +4,7 @@
 #include <stdint.h>
 #include "pool.h"
 
-#define INDIRECT_SWAP 1
+//#define INDIRECT_SWAP 1
 //#define MOVEABLE_SWAP 1
 
 #ifdef MOVEABLE_SWAP
@@ -31,7 +31,6 @@
 #endif
 
 extern struct pool_head swap_table;
-
 #ifdef INDIRECT_SWAP
 extern int *swap_table_mem;
 
@@ -41,7 +40,6 @@ extern uint8_t *swap_block_bitmap;
 extern int swap_block_next;
 extern int swap_block_avail;
 #endif
-extern uint8_t *swap_block_mem;
 
 #ifdef INDIRECT_SWAP
 void swap_init(int n_table, int n_blocks);
@@ -49,8 +47,17 @@ bool swap_block_alloc(int *blocks, int size);
 void swap_block_free(int *blocks, int size);
 #else
 void swap_init(int n_table);
+
+// abstract
+void swap_copy(long src_base, long dest_base, long size);
+void swap_copy_up(long src_limit, long dest_limit, long size);
 #endif
+
 void swap_read(int swap_base, int core_base, long size);
 void swap_write(int swap_base, int core_base, long size);
 
+// abstract
+void core_to_swap_copy(long src_base, long dest_base, long size);
+void swap_to_core_copy(long src_base, long dest_base, long size);
+
 #endif