More strictly declare variables at start of function, for ack
authorNick Downing <nick@ndcode.org>
Sun, 21 Apr 2019 05:38:47 +0000 (15:38 +1000)
committerNick Downing <nick@ndcode.org>
Sun, 21 Apr 2019 05:40:59 +0000 (15:40 +1000)
12 files changed:
core.c
indirect_core_indirect_swap/Makefile
indirect_core_inode_swap/Makefile
indirect_core_preallocate_swap/Makefile
moveable_core_indirect_swap/Makefile
moveable_core_inode_swap/Makefile
moveable_core_preallocate_swap/Makefile
pp.sh
process.c
process_test_gen.c
process_test_run.c
swap.c

diff --git a/core.c b/core.c
index f9dd489..beb8f86 100644 (file)
--- a/core.c
+++ b/core.c
@@ -25,8 +25,16 @@ int victim_core_blocks;
 #endif
 
 static void core_move(struct pool_item *item, int new_base) {
-  int base = item->base;
-  int blocks = item->limit - base;
+  int base, blocks;
+#ifdef INDIRECT_CORE
+  int i;
+#else
+  struct process *process;
+  long size;
+#endif
+
+  base = item->base;
+  blocks = item->limit - base;
   printf(
     "core_move [%d,%d) to [%d,%d)\n",
     base,
@@ -36,7 +44,7 @@ static void core_move(struct pool_item *item, int new_base) {
   );
   assert(new_base <= base || new_base >= base + blocks);
 #ifdef INDIRECT_CORE
-  for (int i = 0; i < blocks; ++i) {
+  for (i = 0; i < blocks; ++i) {
     core_table_mem[new_base + i] = core_table_mem[base + i];
 #ifndef NDEBUG
     core_table_mem[base + i] = 0x55555555;
@@ -44,9 +52,9 @@ static void core_move(struct pool_item *item, int new_base) {
   }
 #else
   // see if incomplete last block, if so don't copy extra part
-  struct process *process =
+  process =
     (struct process *)((char *)item - offsetof(struct process, core_item));
-  long size = (long)blocks << BLOCK_SHIFT;
+  size = (long)blocks << BLOCK_SHIFT;
   if (size > process->size)
     size = process->size;
 
@@ -57,8 +65,17 @@ static void core_move(struct pool_item *item, int new_base) {
 
 #ifdef MOVEABLE_CORE
 static void core_move_up(struct pool_item *item, int new_limit) {
-  int limit = item->limit;
-  int blocks = limit - item->base;
+  int limit, blocks;
+#ifdef INDIRECT_CORE
+  int i;
+#else
+  long limit1, new_limit1, size;
+  struct process *process;
+  long trim;
+#endif
+
+  limit = item->limit;
+  blocks = limit - item->base;
   printf(
     "core_move_up [%d,%d) to [%d,%d)\n",
     limit - blocks,
@@ -69,7 +86,7 @@ static void core_move_up(struct pool_item *item, int new_limit) {
   assert(new_limit >= limit || new_limit <= limit - blocks);
 #ifdef INDIRECT_CORE
   blocks = -blocks;
-  for (int i = -1; i >= blocks; --i) {
+  for (i = -1; i >= blocks; --i) {
     core_table_mem[new_limit + i] = core_table_mem[limit + i];
 #ifndef NDEBUG
     core_table_mem[limit + i] = 0x55555555;
@@ -77,14 +94,14 @@ static void core_move_up(struct pool_item *item, int new_limit) {
   }
 #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;
+  limit1 = (long)limit << BLOCK_SHIFT;
+  new_limit1 = (long)new_limit << BLOCK_SHIFT;
+  size = (long)blocks << BLOCK_SHIFT;
 
   // see if incomplete last block, if so don't copy extra part
-  struct process *process =
+  process =
     (struct process *)((char *)item - offsetof(struct process, core_item));
-  long trim = size - process->size;
+  trim = size - process->size;
   if (trim > 0) {
     limit1 -= trim;
     new_limit1 -= trim;
index 6cedec7..8696d8b 100644 (file)
@@ -9,4 +9,4 @@ core.o: core.c core.h pool.h
 swap.o: swap.c swap.h pool.h
 
 clean:
-       rm -f process_test_run
+       rm -f *.o process_test_run
index 68692d9..cd08d43 100644 (file)
@@ -9,4 +9,4 @@ core.o: core.c core.h pool.h
 fuzix_fs.o: fuzix_fs.c fuzix_fs.h
 
 clean:
-       rm -f process_test_run
+       rm -f *.o process_test_run
index 6cedec7..8696d8b 100644 (file)
@@ -9,4 +9,4 @@ core.o: core.c core.h pool.h
 swap.o: swap.c swap.h pool.h
 
 clean:
-       rm -f process_test_run
+       rm -f *.o process_test_run
index 6cedec7..8696d8b 100644 (file)
@@ -9,4 +9,4 @@ core.o: core.c core.h pool.h
 swap.o: swap.c swap.h pool.h
 
 clean:
-       rm -f process_test_run
+       rm -f *.o process_test_run
index 68692d9..cd08d43 100644 (file)
@@ -9,4 +9,4 @@ core.o: core.c core.h pool.h
 fuzix_fs.o: fuzix_fs.c fuzix_fs.h
 
 clean:
-       rm -f process_test_run
+       rm -f *.o process_test_run
index 6cedec7..8696d8b 100644 (file)
@@ -9,4 +9,4 @@ core.o: core.c core.h pool.h
 swap.o: swap.c swap.h pool.h
 
 clean:
-       rm -f process_test_run
+       rm -f *.o process_test_run
diff --git a/pp.sh b/pp.sh
index 36186c7..2c06480 100755 (executable)
--- a/pp.sh
+++ b/pp.sh
@@ -1,6 +1,6 @@
 #!/bin/sh -e
 mkdir --parents $1
-sed -e 's/^#include </$include </; s/^#include "rassert\.h"/$include "rassert.h"/; s/^#define DISK_/$define DISK_/; s/^#define BLOCK_/$define BLOCK_/; s/^#define UCP/$define UCP/' -i *.[ch]
+sed -e 's/^#include </$include </; s/^#include "rassert\.h"/$include "rassert.h"/; s/^#define DISK_/$define DISK_/; s/^#define BLOCK_/$define BLOCK_/; s/^#define UCP/$define UCP/;s/^#define TRANSFER_/$define TRANSFER_/' -i *.[ch]
 for i in \
 core.c \
 core.h \
index f22bff1..a98c809 100644 (file)
--- a/process.c
+++ b/process.c
@@ -54,6 +54,9 @@ long estimate_size(long size) {
 #endif
 
 void process_init(int n, int spare) {
+#ifndef NDEBUG
+  int i;
+#endif
 #if defined(INODE_SWAP) && defined(PREALLOCATE_CORE)
   long core_table_avail;
 #endif
@@ -62,7 +65,7 @@ void process_init(int n, int spare) {
   rassert(processes);
   n_processes = n;
 #ifndef NDEBUG
-  for (int i = 0; i < n_processes; ++i)
+  for (i = 0; i < n_processes; ++i)
     processes[i].size = -1L;
 #endif
 
@@ -106,8 +109,12 @@ static void do_swap_out(int swap_out) {
   int blocks, swap_base, core_base;
 #endif
   long size;
-#if defined(INDIRECT_CORE) || defined(INDIRECT_SWAP)
-  int i, j;
+#ifdef INODE_SWAP
+#ifdef INDIRECT_CORE
+  int i, j, core_block;
+#endif
+#elif defined(INDIRECT_CORE) || defined(INDIRECT_SWAP)
+  int i, j, core_block, swap_block;
 #endif
 
   // loop entry code for the case of an existing victim
@@ -213,7 +220,7 @@ static void do_swap_out(int swap_out) {
       j = (int)(size >> BLOCK_SHIFT);
  printf("blocks");
       for (i = 0; i < j; ++i) {
-        int core_block = core_base + i;
+        core_block = core_base + i;
 #ifdef INDIRECT_CORE
         core_block = core_table_mem[core_block];
  printf(" %d", core_block);
@@ -226,7 +233,7 @@ static void do_swap_out(int swap_out) {
           BLOCK_SIZE
         );
 #else
-        int swap_block = swap_base + i;
+        swap_block = swap_base + i;
 #ifdef INDIRECT_SWAP
         swap_block = swap_table_mem[swap_block];
 #ifdef INDIRECT_CORE
@@ -244,7 +251,7 @@ static void do_swap_out(int swap_out) {
       }
       j = (int)size & (BLOCK_SIZE - 1);
       if (j) {
-        int core_block = core_base + i;
+        core_block = core_base + i;
 #ifdef INDIRECT_CORE
         core_block = core_table_mem[core_block];
  printf(" %d", core_block);
@@ -257,7 +264,7 @@ static void do_swap_out(int swap_out) {
           j
         );
 #else
-        int swap_block = swap_base + i;
+        swap_block = swap_base + i;
 #ifdef INDIRECT_SWAP
         swap_block = swap_table_mem[swap_block];
 #ifdef INDIRECT_CORE
@@ -316,6 +323,7 @@ static void do_swap_out(int swap_out) {
 bool process_alloc(struct process *process, long size) {
 #ifdef INODE_SWAP
   long estimated_size;
+  int i;
 #endif
   int blocks, swap_out;
 
@@ -339,7 +347,7 @@ bool process_alloc(struct process *process, long size) {
   process->swap_inode->c_node.i_mode = F_REG;   /* For the time being */
   process->swap_inode->c_node.i_nlink = 0;
   process->swap_inode->c_node.i_size = 0;
-  for (int i = 0; i < 20; ++i)
+  for (i = 0; i < 20; ++i)
     process->swap_inode->c_node.i_addr[i] = 0;
   wr_inode(process->swap_inode);
 
@@ -407,7 +415,7 @@ bool process_realloc(struct process *process, long size) {
 #ifdef INODE_SWAP
   long estimated_size, old_estimated_size, estimated_size_change;
 #endif
-  int blocks, old_blocks, swap_out;
+  int blocks, old_blocks, blocks_change, swap_out;
 
   // must be already allocated
   assert(process->size != -1L);
@@ -425,11 +433,11 @@ bool process_realloc(struct process *process, long size) {
 
   blocks = (int)((size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT);
   old_blocks = (int)((process->size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT);
-  int blocks_change = blocks - old_blocks;
+  blocks_change = blocks - old_blocks;
 #else 
   blocks = (int)((size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT);
   old_blocks = (int)((process->size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT);
-  int blocks_change = blocks - old_blocks;
+  blocks_change = blocks - old_blocks;
   if (process_avail < blocks_change)
     return false;
 #endif
index 6dbf00f..dd40e94 100644 (file)
@@ -4,8 +4,8 @@
 #include <string.h>
 #include "rassert.h"
 
-#define BLOCK_SIZE 0x1000
-#define BLOCK_SHIFT 12
+#define BLOCK_SIZE 0x10 //0x1000
+#define BLOCK_SHIFT 4 //12
 
 int rand_int(int n) {
   return (int)((long long)rand() * n / (RAND_MAX + 1LL));
index b58aef8..0445783 100644 (file)
@@ -22,15 +22,18 @@ uint8_t *swap_block_mem;
 #endif
 
 void core_hash_init(int process, long base, long size, long offset) {
+  long i, addr;
+  long long hash;
+
  //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;
+  for (i = 0L; i < size; ++i) {
+    addr = base + i;
 #ifdef INDIRECT_CORE
     addr =
       (core_table_mem[addr >> BLOCK_SHIFT] << BLOCK_SHIFT) |
       (addr & (BLOCK_SIZE - 1));
 #endif
-    long long hash = process * 17 + (i + offset) * 29;
+    hash = process * 17 + (i + offset) * 29;
     hash = (hash & 0xffffffffLL) + (hash >> 32);
     hash = (hash & 0xffffLL) + (hash >> 16);
     hash = (hash & 0xffLL) + (hash >> 8);
@@ -41,15 +44,18 @@ void core_hash_init(int process, long base, long size, long offset) {
 }
 
 void core_hash_verify(int process, long base, long size, long offset) {
+  long i, addr;
+  long long hash;
+
  //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;
+  for (i = 0L; i < size; ++i) {
+    addr = base + i;
 #ifdef INDIRECT_CORE
     addr =
       (core_table_mem[addr >> BLOCK_SHIFT] << BLOCK_SHIFT) |
       (addr & (BLOCK_SIZE - 1));
 #endif
-    long long hash = process * 17 + (i + offset) * 29;
+    hash = process * 17 + (i + offset) * 29;
     hash = (hash & 0xffffffffLL) + (hash >> 32);
     hash = (hash & 0xffffLL) + (hash >> 16);
     hash = (hash & 0xffLL) + (hash >> 8);
@@ -61,24 +67,27 @@ void core_hash_verify(int process, long base, long size, long offset) {
 
 #ifdef INODE_SWAP
 void swap_hash_verify(int process, struct cinode *inode) {
-  long count;
+  long offset, count;
+  uint8_t buf[TRANSFER_SIZE];
+  int i;
+  long long hash;
+
   for (
-    long offset = 0;
+    offset = 0;
     (count = (long)inode->c_node.i_size - offset) > 0;
     offset += count
   ) {
     if (count > TRANSFER_SIZE)
       count = TRANSFER_SIZE;
 
-    uint8_t buf[TRANSFER_SIZE];
     udata.u_base = (char *)buf;
     udata.u_count = count;
     udata.u_offset = offset;
     udata.u_error = 0;
     rassert(readi(inode) == count && udata.u_error == 0);
 
-    for (int i = 0; i < count; ++i) {
-      long long hash = process * 17 + (i + offset) * 29;
+    for (i = 0; i < count; ++i) {
+      hash = process * 17 + (i + offset) * 29;
       hash = (hash & 0xffffffffLL) + (hash >> 32);
       hash = (hash & 0xffffLL) + (hash >> 16);
       hash = (hash & 0xffLL) + (hash >> 8);
@@ -89,15 +98,18 @@ void swap_hash_verify(int process, struct cinode *inode) {
 }
 #else
 void swap_hash_verify(int process, long base, long size) {
+  long i, addr;
+  long long hash;
+
  //printf("swap_hash_verify %d %ld(%d) %ld(%d)\n", process, base, (int)(base >> BLOCK_SHIFT), size, (int)((size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT));
-  for (long i = 0L; i < size; ++i) {
-    long addr = base + i;
+  for (i = 0L; i < size; ++i) {
+    addr = base + i;
 #ifdef INDIRECT_SWAP
     addr =
       (swap_table_mem[addr >> BLOCK_SHIFT] << BLOCK_SHIFT) |
       (addr & (BLOCK_SIZE - 1));
 #endif
-    long long hash = process * 17 + i * 29;
+    hash = process * 17 + i * 29;
     hash = (hash & 0xffffffffLL) + (hash >> 32);
     hash = (hash & 0xffffLL) + (hash >> 16);
     hash = (hash & 0xffLL) + (hash >> 8);
@@ -110,8 +122,10 @@ void swap_hash_verify(int process, long base, long size) {
 
 #ifndef INDIRECT_CORE
 void core_copy(long src_base, long dest_base, long size) {
+  long i;
+
  //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) {
+  for (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;
@@ -120,9 +134,11 @@ void core_copy(long src_base, long dest_base, long size) {
 }
 
 void core_copy_up(long src_limit, long dest_limit, long size) {
+  long i;
+
  //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) {
+  for (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;
@@ -138,12 +154,13 @@ void swap_read(
   long core_base,
   long size
 ) {
+  long i, count;
+
  //printf("swap_read %d %ld %ld %ld\n", inode->c_num, offset, core_base, size);
-  for (long i = 0; i < size; ++i)
+  for (i = 0; i < size; ++i)
     rassert(core_block_mem[core_base + i] == 0xaa);
 
-  long count;
-  for (long i = 0; (count = size - i) > 0; i += count) {
+  for (i = 0; (count = size - i) > 0; i += count) {
     if (count > TRANSFER_SIZE)
       count = TRANSFER_SIZE;
  
@@ -161,9 +178,10 @@ void swap_write(
   long core_base,
   long size
 ) {
+  long i, count;
+
  //printf("swap_write %d %ld %ld %ld\n", inode->c_num, offset, core_base, size);
-  long count;
-  for (long i = 0; (count = size - i) > 0; i += count) {
+  for (i = 0; (count = size - i) > 0; i += count) {
     if (count > TRANSFER_SIZE)
       count = TRANSFER_SIZE;
  
@@ -179,8 +197,10 @@ void swap_write(
 #else
 #ifndef INDIRECT_SWAP
 void swap_copy(long src_base, long dest_base, long size) {
+  long i;
+
  //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) {
+  for (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;
@@ -189,9 +209,11 @@ void swap_copy(long src_base, long dest_base, long size) {
 }
 
 void swap_copy_up(long src_limit, long dest_limit, long size) {
+  long i;
+
  //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) {
+  for (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;
@@ -201,16 +223,20 @@ void swap_copy_up(long src_limit, long dest_limit, long size) {
 #endif
 
 void core_to_swap_copy(long src_base, long dest_base, long size) {
+  long i;
+
  //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)
+  for (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) {
+  long i;
+
  //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)
+  for (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);
@@ -218,6 +244,35 @@ void swap_to_core_copy(long src_base, long dest_base, long size) {
 #endif
 
 int main(int argc, char **argv) {
+  int n_processes;
+  int n_core_blocks;
+#ifdef INODE_SWAP
+  char *fs_path;
+#else
+  int n_swap_blocks;
+#endif
+  int spare;
+#ifdef INDIRECT_CORE
+  int core_table_blocks;
+#endif
+#ifdef INDIRECT_SWAP
+  int swap_table_blocks;
+#endif
+#if defined(INDIRECT_CORE) || defined(INDIRECT_SWAP)
+  int i, j, k;
+#else
+  int i, j;
+#endif
+  char buf[256];
+  int process;
+  bool success, result;
+  long old_size, size, actual_old_size;
+  int old_blocks, blocks, actual_old_blocks;
+  int swap_base, swap_blocks;
+  long swap_size;
+  int core_base, core_blocks;
+  long core_size;
+
   if (argc < 5) {
     printf(
       "usage: %s n_processes n_core_blocks "
@@ -239,21 +294,21 @@ int main(int argc, char **argv) {
     );
     exit(EXIT_FAILURE);
   }
-  int n_processes = atoi(argv[1]);
-  int n_core_blocks = atoi(argv[2]);
+  n_processes = atoi(argv[1]);
+  n_core_blocks = atoi(argv[2]);
 #ifdef INODE_SWAP
-  char *fs_path = argv[3];
+  fs_path = argv[3];
 #else
-  int n_swap_blocks = atoi(argv[3]);
+  n_swap_blocks = atoi(argv[3]);
 #endif
-  int spare = atoi(argv[4]);
+  spare = atoi(argv[4]);
 #if defined(INDIRECT_CORE) && defined(INDIRECT_SWAP)
-  int core_table_blocks = argc >= 6 ? atoi(argv[5]) : n_core_blocks;
-  int swap_table_blocks = argc >= 7 ? atoi(argv[6]) : n_swap_blocks;
+  core_table_blocks = argc >= 6 ? atoi(argv[5]) : n_core_blocks;
+  swap_table_blocks = argc >= 7 ? atoi(argv[6]) : n_swap_blocks;
 #elif defined(INDIRECT_CORE)
-  int core_table_blocks = argc >= 6 ? atoi(argv[5]) : n_core_blocks;
+  core_table_blocks = argc >= 6 ? atoi(argv[5]) : n_core_blocks;
 #elif defined(INDIRECT_SWAP)
-  int swap_table_blocks = argc >= 6 ? atoi(argv[5]) : n_swap_blocks;
+  swap_table_blocks = argc >= 6 ? atoi(argv[5]) : n_swap_blocks;
 #endif
 
 #ifdef INDIRECT_CORE
@@ -281,12 +336,11 @@ int main(int argc, char **argv) {
 #endif
 
   process_init(n_processes, spare);
-  for (int i = 0; i < n_processes; ++i)
+  for (i = 0; i < n_processes; ++i)
     processes[i].size = -1L;
 
   while (true) {
  //printf("avail %d %d(%d) %d(%d)\n", process_avail, core_avail(), core_table.avail, swap_avail(), swap_table.avail);
-    char buf[256];
     switch (scanf("%s", buf)) {
     case -1:
       goto done;
@@ -296,12 +350,9 @@ int main(int argc, char **argv) {
       rassert(false);
     }
     if (strcmp(buf, "alloc") == 0) {
-      int process;
-      long size;
       rassert(scanf("%d %ld %s", &process, &size, buf) == 3);
       rassert(process >= 0 && process < n_processes);
       rassert(size >= 0);
-      bool success;
       if (strcmp(buf, "false") == 0)
         success = false;
       else if (strcmp(buf, "true") == 0)
@@ -315,7 +366,7 @@ int main(int argc, char **argv) {
         success ? "true" : "false"
       );
       rassert(processes[process].size == -1L);
-      bool result = process_alloc(processes + process, size);
+      result = process_alloc(processes + process, size);
       printf(
         "... %s\n",
         result == success ?
@@ -329,8 +380,8 @@ int main(int argc, char **argv) {
           processes[process].size = -1L;
         }
         else {
-          int core_base = processes[process].core_item.base;
-          int blocks = (int)((size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT);
+          core_base = processes[process].core_item.base;
+          blocks = (int)((size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT);
           assert(
             processes[process].core_item.limit ==
               core_base + blocks
@@ -349,13 +400,10 @@ int main(int argc, char **argv) {
       }
     }
     else if (strcmp(buf, "realloc") == 0) {
-      int process;
-      long old_size, size;
       rassert(scanf("%d %ld %ld %s", &process, &old_size, &size, buf) == 4);
       rassert(process >= 0 && process < n_processes);
       rassert(old_size >= 0);
       rassert(size >= 0);
-      bool success;
       if (strcmp(buf, "false") == 0)
         success = false;
       else if (strcmp(buf, "true") == 0)
@@ -372,15 +420,15 @@ int main(int argc, char **argv) {
       if (processes[process].size == -1L)
         printf("... not allocated, ignore\n");
       else {
-        int core_base = processes[process].core_item.base;
-        long actual_old_size = processes[process].size;
-        int actual_old_blocks =
+        core_base = processes[process].core_item.base;
+        actual_old_size = processes[process].size;
+        actual_old_blocks =
           (int)((actual_old_size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT);
         rassert(
           processes[process].core_item.limit ==
             core_base + actual_old_blocks
         );
-        int old_blocks = (int)((old_size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT);
+        old_blocks = (int)((old_size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT);
         if (actual_old_blocks != old_blocks) {
           printf(
             "... old blocks %d, should be %d\n",
@@ -400,7 +448,7 @@ int main(int argc, char **argv) {
           actual_old_size - size,
           size
         );
-        bool result = process_realloc(processes + process, size);
+        result = process_realloc(processes + process, size);
         printf(
           "... %s\n",
           result == success ?
@@ -414,7 +462,7 @@ int main(int argc, char **argv) {
           }
           else {
             core_base = processes[process].core_item.base;
-            int blocks = (int)((size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT);
+            blocks = (int)((size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT);
             rassert(
               processes[process].core_item.limit ==
                 core_base + blocks
@@ -435,7 +483,6 @@ int main(int argc, char **argv) {
       }
     }
     else if (strcmp(buf, "run") == 0) {
-      int process;
       rassert(scanf("%d", &process) == 1);
       rassert(process >= 0 && process < n_processes);
       printf("run %d\n", process);
@@ -447,8 +494,6 @@ int main(int argc, char **argv) {
       }
     } 
     else if (strcmp(buf, "free") == 0) {
-      int process;
-      long old_size;
       rassert(scanf("%d %ld", &process, &old_size) == 2);
       rassert(process >= 0 && process < n_processes);
       rassert(old_size >= 0);
@@ -456,9 +501,9 @@ int main(int argc, char **argv) {
       if (processes[process].size == -1L)
         printf("... not allocated, ignore\n");
       else {
-        int actual_old_blocks =
+        actual_old_blocks =
           (int)((processes[process].size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT);
-        int old_blocks =
+        old_blocks =
           (int)((old_size + (BLOCK_SIZE - 1)) >> BLOCK_SHIFT);
         if (actual_old_blocks != old_blocks) {
           printf(
@@ -468,10 +513,6 @@ int main(int argc, char **argv) {
           );
           rassert(actual_old_blocks <= old_blocks);
         }
-        int swap_base, swap_blocks;
-        long swap_size;
-        int core_base, core_blocks;
-        long core_size;
         if (processes[process].lru_item.prev != NULL) { // fully in core
           assert(processes + process != victim);
           swap_base = -1;
@@ -546,14 +587,10 @@ int main(int argc, char **argv) {
 
 done:
   printf("final state:\n");
-  for (int i = 0; i < n_processes; ++i) {
+  for (i = 0; i < n_processes; ++i) {
     if (processes[i].size == -1L)
       printf("process %d: not allocated\n", i);
     else {
-      int swap_base, swap_blocks;
-      long swap_size;
-      int core_base, core_blocks;
-      long core_size;
       if (processes[i].lru_item.prev != NULL) { // fully in core
         assert(processes + i != victim);
         swap_base = -1;
@@ -639,42 +676,38 @@ done:
   }
 
 #ifdef INDIRECT_CORE
-  for (int i = 0; i < core_table_blocks; ++i)
+  for (i = 0; i < core_table_blocks; ++i)
     rassert(core_table_mem[i] == 0x55555555);
   {
-    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;
+    k = n_core_blocks >> 3;
+    for (j = 0; j < k; ++j)
+      rassert(core_block_bitmap[j] == 0xff);
+    k = n_core_blocks & 7;
     if (k)
       rassert(core_block_bitmap[j] == (uint8_t)~(-1 << k));
   }
 #endif
-  {
-    int j = n_core_blocks << BLOCK_SHIFT;
-    for (int i = 0; i < j; ++i)
-      rassert(core_block_mem[i] == 0xaa);
-  }
+  j = n_core_blocks << BLOCK_SHIFT;
+  for (i = 0; i < j; ++i)
+    rassert(core_block_mem[i] == 0xaa);
 #ifdef INODE_SWAP
   printf("free inodes %d blocks %d\n", fs_tab[0].s_tinode, fs_tab[0].s_tfree);
 #else
 #ifdef INDIRECT_SWAP
-  for (int i = 0; i < swap_table_blocks; ++i)
+  for (i = 0; i < swap_table_blocks; ++i)
     rassert(swap_table_mem[i] == 0x55555555);
   {
-    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;
+    k = n_swap_blocks >> 3;
+    for (j = 0; j < k; ++j)
+      rassert(swap_block_bitmap[j] == 0xff);
+    k = n_swap_blocks & 7;
     if (k)
       rassert(swap_block_bitmap[j] == (uint8_t)~(-1 << k));
   }
 #endif
-  {
-    int j = n_swap_blocks << BLOCK_SHIFT;
-    for (int i = 0; i < j; ++i)
-      rassert(swap_block_mem[i] == 0xaa);
-  }
+  j = n_swap_blocks << BLOCK_SHIFT;
+  for (i = 0; i < j; ++i)
+    rassert(swap_block_mem[i] == 0xaa);
 #endif
 
   return 0;
diff --git a/swap.c b/swap.c
index 20b76ae..27b4e0e 100644 (file)
--- a/swap.c
+++ b/swap.c
@@ -25,8 +25,15 @@ int victim_swap_blocks;
 #endif
 
 static void swap_move(struct pool_item *item, int new_base) {
-  int base = item->base;
-  int blocks = item->limit - base;
+  int base, blocks;
+#ifdef INDIRECT_SWAP
+  int i;
+#else
+  long size;
+#endif
+
+  base = item->base;
+  blocks = item->limit - base;
   printf(
     "swap_move [%d,%d) to [%d,%d)\n",
     base,
@@ -36,7 +43,7 @@ static void swap_move(struct pool_item *item, int new_base) {
   );
   assert(new_base <= base || new_base >= base + blocks);
 #ifdef INDIRECT_SWAP
-  for (int i = 0; i < blocks; ++i) {
+  for (i = 0; i < blocks; ++i) {
     swap_table_mem[new_base + i] = swap_table_mem[base + i];
 #ifndef NDEBUG
     swap_table_mem[base + i] = 0x55555555;
@@ -46,7 +53,7 @@ static void swap_move(struct pool_item *item, int new_base) {
   // 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;
+  size = (long)blocks << BLOCK_SHIFT;
   if (size > process->size)
     size = process->size;
 
@@ -57,8 +64,17 @@ static void swap_move(struct pool_item *item, int new_base) {
 
 #ifdef MOVEABLE_SWAP
 static void swap_move_up(struct pool_item *item, int new_limit) {
-  int limit = item->limit;
-  int blocks = limit - item->base;
+  int limit, blocks;
+#ifdef INDIRECT_SWAP
+  int i;
+#else
+  long limit1, new_limit1, size;
+  struct process *process;
+  long trim;
+#endif
+
+  limit = item->limit;
+  blocks = limit - item->base;
   printf(
     "swap_move_up [%d,%d) to [%d,%d)\n",
     limit - blocks,
@@ -69,7 +85,7 @@ static void swap_move_up(struct pool_item *item, int new_limit) {
   assert(new_limit >= limit || new_limit <= limit - blocks);
 #ifdef INDIRECT_SWAP
   blocks = -blocks;
-  for (int i = -1; i >= blocks; --i) {
+  for (i = -1; i >= blocks; --i) {
     swap_table_mem[new_limit + i] = swap_table_mem[limit + i];
 #ifndef NDEBUG
     swap_table_mem[limit + i] = 0x55555555;
@@ -77,14 +93,14 @@ static void swap_move_up(struct pool_item *item, int new_limit) {
   }
 #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;
+  limit1 = (long)limit << BLOCK_SHIFT;
+  new_limit1 = (long)new_limit << BLOCK_SHIFT;
+  size = (long)blocks << BLOCK_SHIFT;
 
   // see if incomplete last block, if so don't copy extra part
-  struct process *process =
+  process =
     (struct process *)((char *)item - offsetof(struct process, swap_item));
-  long trim = size - process->size;
+  trim = size - process->size;
   if (trim > 0) {
     limit1 -= trim;
     new_limit1 -= trim;