Remove the reservation of spare space from pool object, core and swap modules
authorNick Downing <nick@ndcode.org>
Sat, 23 Mar 2019 04:44:49 +0000 (15:44 +1100)
committerNick Downing <nick@ndcode.org>
Sat, 23 Mar 2019 04:44:49 +0000 (15:44 +1100)
12 files changed:
core.c
core.h
n.sh
o.sh
pool.c
pool.h
pool_test_run.c
process.c
process.h
process_test_run.c
swap.c
swap.h

diff --git a/core.c b/core.c
index 04a06b5..8f293bc 100644 (file)
--- a/core.c
+++ b/core.c
@@ -67,16 +67,15 @@ static void core_move_up(struct pool_item *item, int new_limit) {
 #endif
 
 #ifdef INDIRECT_CORE
-void core_init(int table_size, int table_spare, int n_blocks)
+void core_init(int table_size, int n_blocks)
 #else
-void core_init(int table_size, int table_spare)
+void core_init(int table_size)
 #endif
 {
   pool_init(
     &core_table,
     0,
     table_size,
-    table_spare,
     core_move,
 #ifdef MOVEABLE_CORE
     core_move_up
diff --git a/core.h b/core.h
index 71a2fb7..a2ec2e5 100644 (file)
--- a/core.h
+++ b/core.h
@@ -4,8 +4,8 @@
 #include <stdint.h>
 #include "pool.h"
 
-#define INDIRECT_CORE 1
-//#define MOVEABLE_CORE 1
+//#define INDIRECT_CORE 1
+#define MOVEABLE_CORE 1
 
 #ifdef MOVEABLE_CORE
 #define core_table_alloc(item, size) \
@@ -40,11 +40,11 @@ extern int *core_table_mem;
 extern int *core_block_mem;
 
 #ifdef INDIRECT_CORE
-void core_init(int n_table, int table_spare, int n_blocks);
+void core_init(int n_table, int n_blocks);
 bool core_block_alloc(int *blocks, int size);
 void core_block_free(int *blocks, int size);
 #else
-void core_init(int n_table, int table_spare);
+void core_init(int n_table);
 #endif
 
 #endif
diff --git a/n.sh b/n.sh
index 0807437..77af046 100755 (executable)
--- a/n.sh
+++ b/n.sh
@@ -1,10 +1,10 @@
 #!/bin/sh
 
 echo "generate test script"
-./pool_test_gen 64 240 256 16 >pool_test.txt
+./pool_test_gen 64 256 1024 16 >pool_test.txt
 
 echo "run test script, non-moveable"
-./pool_test_run 64 256 16 <pool_test.txt
+./pool_test_run 64 256 <pool_test.txt
 
 echo "run test script, moveable"
-./pool_test_run 64 256 16 1 <pool_test.txt
+./pool_test_run 64 256 1 <pool_test.txt
diff --git a/o.sh b/o.sh
index a7f24bc..eb85203 100755 (executable)
--- a/o.sh
+++ b/o.sh
@@ -2,16 +2,16 @@
 
 echo "generate test script"
 # preallocated core, possible preallocated swap (not indirect):
-#./process_test_gen 16 48 65536 32 >process_test.txt
+#./process_test_gen 16 48 1024 32 >process_test.txt
 # non preallocated core, preallocated swap (not indirect):
-#./process_test_gen 16 176 65536 32 >process_test.txt
+#./process_test_gen 16 176 1024 32 >process_test.txt
 # non preallocated core, non preallocated swap (or indirect swap):
-./process_test_gen 16 240 65536 32 >process_test.txt
+./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 0 192 0 16 <process_test.txt
+./process_test_run 16 64 192 16 <process_test.txt
 # non moveable swap (must be preallocated, may be indirect):
-#./process_test_run 16 64 0 384 0 16 192 <process_test.txt
+#./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 0 384 0 16 64 192 <process_test.txt
+#./process_test_run 16 384 384 16 64 192 <process_test.txt
diff --git a/pool.c b/pool.c
index 13825a7..9743443 100644 (file)
--- a/pool.c
+++ b/pool.c
@@ -12,7 +12,7 @@ void check_invariants(struct pool_head *head) {
   int avail;
   struct pool_item *p;
 
-  avail = head->item.base - head->item.limit - head->spare;
+  avail = head->item.base - head->item.limit;
   for (p = head->item.next; p != &head->item; p = p->next) {
     assert(p->prev->next == p && p->next->prev == p);
     assert(p->base >= p->prev->limit && p->limit <= p->next->base);
@@ -26,7 +26,6 @@ void pool_init(
   struct pool_head *head,
   int base,
   int limit,
-  int spare,
   void (*move)(struct pool_item *item, int new_base),
   void (*move_up)(struct pool_item *item, int new_limit)
 ) {
@@ -34,8 +33,7 @@ void pool_init(
   head->item.next = &head->item;
   head->item.base = limit; // note: swapped
   head->item.limit = base; // note: swapped
-  head->avail = limit - base - spare;
-  head->spare = spare;
+  head->avail = limit - base;
   head->move = move;
   head->move_up = move_up;
  check_invariants(head);
@@ -56,7 +54,6 @@ bool pool_alloc(
   avail = head->avail - size;
   if (avail < 0)
     return false;
-  avail += head->spare;
 
   // first-fit algorithm
   q = &head->item;
@@ -108,7 +105,6 @@ bool pool_realloc(
   avail = head->avail - size_change;
   if (avail < 0)
     return false;
-  avail += head->spare;
 
   // first-fit algorithm, until our own list entry
   q = &head->item;
@@ -203,7 +199,6 @@ bool pool_alloc_moveable(
   avail = head->avail - size;
   if (avail < 0)
     return false;
-  avail += head->spare;
 
   // first-fit algorithm
   q = &head->item;
@@ -279,7 +274,6 @@ bool pool_realloc_moveable(
   avail = head->avail - size_change;
   if (avail < 0)
     return false;
-  avail += head->spare;
  
   // first-fit algorithm, until our own list entry
   q = &head->item;
diff --git a/pool.h b/pool.h
index 58bc425..d1f6fa7 100644 (file)
--- a/pool.h
+++ b/pool.h
@@ -13,7 +13,6 @@ struct pool_item {
 struct pool_head {
   struct pool_item item;
   int avail;
-  int spare;
   void (*move)(struct pool_item *item, int new_base);
   void (*move_up)(struct pool_item *item, int new_limit);
 };
@@ -22,7 +21,6 @@ void pool_init(
   struct pool_head *head,
   int base,
   int limit,
-  int spare,
   void (*move)(struct pool_item *item, int new_base),
   void (*move_up)(struct pool_item *item, int new_limit)
 );
index 8105127..3e016ee 100644 (file)
@@ -43,17 +43,16 @@ void move_up(struct pool_item *item, int new_limit) {
 }
 
 int main(int argc, char **argv) {
-  if (argc < 4) {
-    printf("usage: %s n_items pool_size spare [moveable]\n", argv[0]);
+  if (argc < 3) {
+    printf("usage: %s n_items pool_size [moveable]\n", argv[0]);
     exit(EXIT_FAILURE);
   }
   int n_items = atoi(argv[1]);
   int pool_size = atoi(argv[2]);
-  int spare = atoi(argv[3]);
-  bool moveable = argc >= 5 ? (bool)atoi(argv[4]) : false;
+  bool moveable = argc >= 4 ? (bool)atoi(argv[3]) : false;
 
   struct pool_head head;
-  pool_init(&head, 0, pool_size, spare, move, move_up);
+  pool_init(&head, 0, pool_size, move, move_up);
 
   struct pool_item *items = malloc(n_items * sizeof(struct pool_item));
   rassert(items);
index b2f6985..0f9e6b5 100644 (file)
--- a/process.c
+++ b/process.c
@@ -35,40 +35,22 @@ static void check_invariants() {
     n_core_blocks +
 #else
     core_table.item.base -
-    core_table.item.limit -
-    core_table.spare +
+    core_table.item.limit +
 #endif
 #ifdef INDIRECT_SWAP
     n_swap_blocks -
 #else
     swap_table.item.base -
     swap_table.item.limit -
-    swap_table.spare -
 #endif
     process_spare;
 #if defined(PREALLOCATE_CORE)
-  if (
-    avail >
-      core_table.item.base -
-      core_table.item.limit -
-      core_table.spare
-  )
-    avail =
-      core_table.item.base -
-      core_table.item.limit -
-      core_table.spare;
+  if (avail > core_table.item.base - core_table.item.limit)
+    avail = core_table.item.base - core_table.item.limit;
 #endif
 #if defined(PREALLOCATE_SWAP)
-  if (
-    avail >
-      swap_table.item.base -
-      swap_table.item.limit -
-      swap_table.spare
-  )
-    avail =
-      swap_table.item.base -
-      swap_table.item.limit -
-      swap_table.spare;
+  if (avail > swap_table.item.base - swap_table.item.limit)
+    avail = swap_table.item.base - swap_table.item.limit;
 #endif
   if (victim == NULL) {
 #ifdef PREALLOCATE_CORE
index f24fc14..7893db3 100644 (file)
--- a/process.h
+++ b/process.h
@@ -3,8 +3,8 @@
 
 #include "pool.h"
 
-#define PREALLOCATE_CORE 1
-#define PREALLOCATE_SWAP 1
+//#define PREALLOCATE_CORE 1
+//#define PREALLOCATE_SWAP 1
 
 struct lru_item {
   struct lru_item *prev;
index c631c46..c5c4770 100644 (file)
@@ -8,49 +8,47 @@
 #include "swap.h"
 
 int main(int argc, char **argv) {
-  if (argc < 7) {
+  if (argc < 5) {
 #if defined(INDIRECT_CORE) && defined(INDIRECT_SWAP)
-    printf("usage: %s n_processes core_table_size core_table_spare swap_table_size swap_table_spare spare [n_core_blocks [n_swap_blocks]]\n", argv[0]);
+    printf("usage: %s n_processes core_table_size swap_table_size spare [n_core_blocks [n_swap_blocks]]\n", argv[0]);
 #elif defined(INDIRECT_CORE)
-    printf("usage: %s n_processes core_table_size core_table_spare swap_table_size swap_table_spare spare [n_core_blocks]\n", argv[0]);
+    printf("usage: %s n_processes core_table_size swap_table_size spare [n_core_blocks]\n", argv[0]);
 #elif defined(INDIRECT_SWAP)
-    printf("usage: %s n_processes core_table_size core_table_spare swap_table_size swap_table_spare spare [n_swap_blocks]\n", argv[0]);
+    printf("usage: %s n_processes core_table_size swap_table_size spare [n_swap_blocks]\n", argv[0]);
 #else
-    printf("usage: %s n_processes core_table_size core_table_spare swap_table_size swap_table_spare spare\n", argv[0]);
+    printf("usage: %s n_processes core_table_size swap_table_size spare\n", argv[0]);
 #endif
     exit(EXIT_FAILURE);
   }
   int n_processes = atoi(argv[1]);
   int core_table_size = atoi(argv[2]);
-  int core_table_spare = atoi(argv[3]);
-  int swap_table_size = atoi(argv[4]);
-  int swap_table_spare = atoi(argv[5]);
-  int spare = atoi(argv[6]);
+  int swap_table_size = atoi(argv[3]);
+  int spare = atoi(argv[4]);
 #if defined(INDIRECT_CORE) && defined(INDIRECT_SWAP)
-  int n_core_blocks = argc >= 8 ? atoi(argv[7]) : core_table_size;
-  int n_swap_blocks = argc >= 9 ? atoi(argv[8]) : swap_table_size;
+  int n_core_blocks = argc >= 6 ? atoi(argv[5]) : core_table_size;
+  int n_swap_blocks = argc >= 7 ? atoi(argv[6]) : swap_table_size;
 #elif defined(INDIRECT_CORE)
-  int n_core_blocks = argc >= 8 ? atoi(argv[7]) : core_table_size;
+  int n_core_blocks = argc >= 6 ? atoi(argv[5]) : core_table_size;
 #elif defined(INDIRECT_SWAP)
-  int n_swap_blocks = argc >= 8 ? atoi(argv[7]) : swap_table_size;
+  int n_swap_blocks = argc >= 6 ? atoi(argv[5]) : swap_table_size;
 #endif
 
 #ifdef INDIRECT_CORE
-  core_init(core_table_size, core_table_spare, n_core_blocks);
+  core_init(core_table_size, n_core_blocks);
 #else
-  core_init(core_table_size, core_table_spare);
+  core_init(core_table_size);
 #endif
 #ifdef INDIRECT_SWAP
-  swap_init(swap_table_size, swap_table_spare, n_swap_blocks);
+  swap_init(swap_table_size, n_swap_blocks);
 #else
-  swap_init(swap_table_size, swap_table_spare);
+  swap_init(swap_table_size);
 #endif
   process_init(n_processes, spare);
   for (int i = 0; i < n_processes; ++i)
     processes[i].size = -1;
 
   while (true) {
- printf("avail %d %d(%d) %d(%d)\n", process_avail, core_avail(), core_table.avail, swap_avail(), swap_table.avail);
//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:
@@ -99,7 +97,6 @@ int main(int argc, char **argv) {
 #else
             int core_block = core_base + i;
 #endif
- printf("%d\n", core_block);
             rassert(core_block_mem[core_block] == 0xaaaaaaaa);
             core_block_mem[core_block] = (int)hash;
           }
diff --git a/swap.c b/swap.c
index 574385d..bca5611 100644 (file)
--- a/swap.c
+++ b/swap.c
@@ -78,16 +78,15 @@ static void swap_move_up(struct pool_item *item, int new_limit) {
 #endif
 
 #ifdef INDIRECT_SWAP
-void swap_init(int table_size, int table_spare, int n_blocks)
+void swap_init(int table_size, int n_blocks)
 #else
-void swap_init(int table_size, int table_spare)
+void swap_init(int table_size)
 #endif
 {
   pool_init(
     &swap_table,
     ~table_size,
     -1,
-    table_spare,
     swap_move,
 #ifdef MOVEABLE_SWAP
     swap_move_up
diff --git a/swap.h b/swap.h
index 4944851..dc2acc2 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) \
@@ -40,11 +40,11 @@ extern int *swap_table_mem;
 extern int *swap_block_mem;
 
 #ifdef INDIRECT_SWAP
-void swap_init(int n_table, int table_spare, int n_blocks);
+void swap_init(int n_table, int n_blocks);
 bool swap_block_alloc(int *blocks, int size);
 void swap_block_free(int *blocks, int size);
 #else
-void swap_init(int n_table, int table_spare);
+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);