Separate out block allocation bitmap stuff into a reuseable module block_pool.c
authorNick Downing <nick@ndcode.org>
Thu, 9 May 2019 12:06:06 +0000 (22:06 +1000)
committerNick Downing <nick@ndcode.org>
Thu, 9 May 2019 12:06:06 +0000 (22:06 +1000)
20 files changed:
Makefile
block_pool.c [new file with mode: 0644]
block_pool.h [new file with mode: 0644]
core.c
core.h
gen.sh
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
o.sh
pool.c
pp.sh
process.c
process.h
process_test_run.c
swap.c
swap.h

index 1f74582..ac390d8 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -20,13 +20,14 @@ process_test_gen: process_test_gen.o
 
 process_test_gen.o: process_test_gen.c
 
-process_test_run: process_test_run.o process.o core.o fuzix_fs_or_swap.o pool.o
+process_test_run: process_test_run.o process.o core.o fuzix_fs_or_swap.o block_pool.o pool.o
        ${CC} ${CFLAGS} -o $@ $^
 
 process_test_run.o: process_test_run.c process.h core.h swap.h pool.h fuzix_fs.h util.h
 process.o: process.c process.h core.h swap.h pool.h fuzix_fs.h
 core.o: core.c core.h pool.h
 fuzix_fs_or_swap.o: fuzix_fs_or_swap.c fuzix_fs.c swap.h core.h pool.h fuzix_fs.h
+block_pool.o: block_pool.c block_pool.h
 
 ucp.o: ucp.c fuzix_fs.h
 fuzix_fs.o: fuzix_fs.c fuzix_fs.h
diff --git a/block_pool.c b/block_pool.c
new file mode 100644 (file)
index 0000000..c0a0edb
--- /dev/null
@@ -0,0 +1,88 @@
+#include <stdlib.h>
+#include <string.h>
+#include "block_pool.h"
+#include "rassert.h"
+
+static uint8_t masks[8] = {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80};
+static uint8_t bits[8] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80};
+
+void block_pool_init(struct block_pool *block_pool, int n_blocks) {
+  block_pool->n_blocks = n_blocks;
+  block_pool->n_bitmap = (n_blocks + 7) >> 3;
+  block_pool->bitmap = malloc(block_pool->n_bitmap);
+  rassert(block_pool->bitmap);
+  memset(block_pool->bitmap, 0xff, block_pool->n_bitmap);
+  if (n_blocks & 7)
+    block_pool->bitmap[n_blocks >> 3] =
+      ~masks[n_blocks & 7];
+  block_pool->next = 0;
+  block_pool->avail = n_blocks;
+}
+
+bool block_pool_alloc(struct block_pool *block_pool, int *table, int size) {
+  int i, j, k;
+  uint8_t c;
+
+  if (block_pool->avail < size)
+    return false;
+
+  for (i = 0; i < size; ++i) {
+    j = block_pool->next >> 3;
+    c = block_pool->bitmap[j] & masks[block_pool->next & 7];
+    goto loop_entry;
+    for (; j < block_pool->n_bitmap; ++j) {
+      c = block_pool->bitmap[j];
+    loop_entry:
+      if (c) goto found;
+    }
+#if 1
+    for (j = 0; ; ++j) {
+      assert(j <= block_pool->next >> 3);
+      c = block_pool->bitmap[j];
+      if (c) goto found;
+    }
+#else
+    k = (block_pool->next >> 3) + 1;
+    for (j = 0; j < k; ++j) {
+      c = block_pool->bitmap[j];
+      if (c) goto found;
+    }
+    core_block_free(table, i);
+    return false;
+#endif
+
+  found:
+    for (k = 0; (c & 1) == 0; ++k)
+      c >>= 1;
+    block_pool->bitmap[j] &= ~bits[k];
+    block_pool->next = (j << 3) | k;
+
+#ifndef NDEBUG // makes us remove the following line in gen.sh as appropriate
+    assert(table[i] == 0x55555555);
+#endif
+    table[i] = block_pool->next++;
+    if (block_pool->next >= block_pool->n_blocks)
+      block_pool->next = 0;
+  }
+
+  block_pool->avail -= size;
+  return true;
+}
+
+void block_pool_free(struct block_pool *block_pool, int *table, int size) {
+  int i, j, k;
+
+  for (i = 0; i < size; ++i) {
+    j = table[i];
+    assert(j >= 0 && j < block_pool->n_blocks);
+    k = j & 7;
+    j >>= 3;
+    assert((block_pool->bitmap[j] & bits[k]) == 0);
+    block_pool->bitmap[j] |= bits[k];
+#ifndef NDEBUG
+    table[i] = 0x55555555;
+#endif
+  }
+
+  block_pool->avail += size;
+}
diff --git a/block_pool.h b/block_pool.h
new file mode 100644 (file)
index 0000000..fe2794c
--- /dev/null
@@ -0,0 +1,19 @@
+#ifndef _BLOCK_POOL_H
+#define _BLOCK_POOL_H 1
+
+#include <stdbool.h>
+#include <stdint.h>
+
+struct block_pool {
+  int n_blocks;
+  int n_bitmap;
+  uint8_t *bitmap;
+  int next;
+  int avail;
+};
+
+void block_pool_init(struct block_pool *block_pool, int n_blocks);
+bool block_pool_alloc(struct block_pool *block_pool, int *blocks, int size);
+void block_pool_free(struct block_pool *block_pool, int *blocks, int size);
+
+#endif
diff --git a/core.c b/core.c
index 8d837e2..ab704e5 100644 (file)
--- a/core.c
+++ b/core.c
@@ -9,15 +9,7 @@
 struct pool_head core_table;
 #ifdef INDIRECT_CORE
 int *core_table_mem;
-
-static uint8_t masks[8] = {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80};
-static uint8_t bits[8] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80};
-
-int n_core_blocks;
-int n_core_block_bitmap;
-uint8_t *core_block_bitmap;
-int core_block_next;
-int core_block_avail;
+struct block_pool core_block_pool;
 #endif
 
 #ifdef PREALLOCATE_CORE
@@ -141,84 +133,6 @@ void core_init(int n_blocks)
   rassert(core_table_mem);
   memset(core_table_mem, 0x55, table_size * sizeof(int));
 
-  n_core_blocks = n_blocks;
-  n_core_block_bitmap = (n_core_blocks + 7) >> 3;
-  core_block_bitmap = malloc(n_core_block_bitmap);
-  rassert(core_block_bitmap);
-  memset(core_block_bitmap, 0xff, n_core_block_bitmap);
-  if (n_core_blocks & 7)
-    core_block_bitmap[n_core_blocks >> 3] = ~masks[n_core_blocks & 7];
-  core_block_next = 0;
-  core_block_avail = n_core_blocks;
-#endif
-}
-
-#ifdef INDIRECT_CORE
-bool core_block_alloc(int *table, int size) {
-  int i, j, k;
-  uint8_t c;
-
-  if (core_block_avail < size)
-    return false;
-
-  for (i = 0; i < size; ++i) {
-    j = core_block_next >> 3;
-    c = core_block_bitmap[j] & masks[core_block_next & 7];
-    goto loop_entry;
-    for (; j < n_core_block_bitmap; ++j) {
-      c = core_block_bitmap[j];
-    loop_entry:
-      if (c) goto found;
-    }
-#if 1
-    for (j = 0; ; ++j) {
-      assert(j <= core_block_next >> 3);
-      c = core_block_bitmap[j];
-      if (c) goto found;
-    }
-#else
-    k = (core_block_next >> 3) + 1;
-    for (j = 0; j < k; ++j) {
-      c = core_block_bitmap[j];
-      if (c) goto found;
-    }
-    core_block_free(table, i);
-    return false;
-#endif
-
-  found:
-    for (k = 0; (c & 1) == 0; ++k)
-      c >>= 1;
-    core_block_bitmap[j] &= ~bits[k];
-    core_block_next = (j << 3) | k;
-
-#ifndef NDEBUG // makes us remove the following line in gen.sh as appropriate
-    assert(table[i] == 0x55555555);
+  block_pool_init(&core_block_pool, n_blocks);
 #endif
-    table[i] = core_block_next++;
-    if (core_block_next >= n_core_blocks)
-      core_block_next = 0;
-  }
-
-  core_block_avail -= size;
-  return true;
 }
-
-void core_block_free(int *table, int size) {
-  int i, j, k;
-
-  for (i = 0; i < size; ++i) {
-    j = table[i];
-    assert(j >= 0 && j < n_core_blocks);
-    k = j & 7;
-    j >>= 3;
-    assert((core_block_bitmap[j] & bits[k]) == 0);
-    core_block_bitmap[j] |= bits[k];
-#ifndef NDEBUG
-    table[i] = 0x55555555;
-#endif
-  }
-
-  core_block_avail += size;
-}
-#endif
diff --git a/core.h b/core.h
index f3a7ac9..da3128a 100644 (file)
--- a/core.h
+++ b/core.h
@@ -1,15 +1,18 @@
 #ifndef _CORE_H
 #define _CORE_H 1
 
-#include <stdint.h>
-#include "pool.h"
-
 //#define PREALLOCATE_CORE 1
 //#define INDIRECT_CORE 1
 #define MOVEABLE_CORE 1
 
+#include <stdint.h>
 #ifdef INDIRECT_CORE
-#define CORE_AVAIL core_block_avail
+#include "block_pool.h"
+#endif
+#include "pool.h"
+
+#ifdef INDIRECT_CORE
+#define CORE_AVAIL core_block_pool.avail
 #else
 #define CORE_AVAIL core_table.avail
 #endif
 extern struct pool_head core_table;
 #ifdef INDIRECT_CORE
 extern int *core_table_mem;
-
-extern int n_core_blocks;
-int n_core_block_bitmap;
-extern uint8_t *core_block_bitmap;
-extern int core_block_next;
-extern int core_block_avail;
+extern struct block_pool core_block_pool;
 #endif
 
 #ifdef PREALLOCATE_CORE
@@ -31,8 +29,6 @@ extern int victim_core_blocks;
 
 #ifdef INDIRECT_CORE
 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);
 
diff --git a/gen.sh b/gen.sh
index 8c84b68..2023a40 100755 (executable)
--- a/gen.sh
+++ b/gen.sh
@@ -37,6 +37,7 @@ sed -e 's/^\/\/#define MOVEABLE_CORE 1/#define MOVEABLE_CORE 1/' -i core.h
 sed -e 's/^#define INODE_SWAP 1/\/\/#define INODE_SWAP 1/' -i process.h
 sed -e 's/^#define INDIRECT_SWAP 1/\/\/#define INDIRECT_SWAP 1/' -i swap.h
 ./pp.sh moveable_core_preallocate_swap $1
+rm moveable_core_preallocate_swap/block_pool.[ch]
 cp rassert.h moveable_core_preallocate_swap
 
 sed -e 's/^\/\/#define INDIRECT_SWAP 1/#define INDIRECT_SWAP 1/' -i swap.h
@@ -45,5 +46,5 @@ cp rassert.h moveable_core_indirect_swap
 
 sed -e 's/^\/\/#define INODE_SWAP 1/#define INODE_SWAP 1/' -i process.h
 ./pp.sh moveable_core_inode_swap $1
-rm moveable_core_inode_swap/swap.[ch]
+rm moveable_core_inode_swap/block_pool.[ch] moveable_core_inode_swap/swap.[ch]
 cp rassert.h fuzix_fs.[ch] util.[ch] moveable_core_inode_swap
index 8696d8b..43180aa 100644 (file)
@@ -1,12 +1,14 @@
 CFLAGS=-g -Wall
 
-process_test_run: process_test_run.o process.o core.o swap.o pool.o
+process_test_run: process_test_run.o process.o core.o swap.o block_pool.o pool.o
        ${CC} ${CFLAGS} -o $@ $^
 
 process_test_run.o: process_test_run.c process.h core.h swap.h pool.h
 process.o: process.c process.h core.h swap.h pool.h
 core.o: core.c core.h pool.h
 swap.o: swap.c swap.h pool.h
+block_pool.o: block_pool.c block_pool.h
+pool.o: pool.c pool.h
 
 clean:
        rm -f *.o process_test_run
index cd08d43..e9d3588 100644 (file)
@@ -1,12 +1,14 @@
 CFLAGS=-g -Wall
 
-process_test_run: process_test_run.o process.o core.o fuzix_fs.o util.o pool.o
+process_test_run: process_test_run.o process.o core.o fuzix_fs.o util.o block_pool.o pool.o
        ${CC} ${CFLAGS} -o $@ $^
 
 process_test_run.o: process_test_run.c process.h core.h fuzix_fs.h util.h pool.h
 process.o: process.c process.h core.h fuzix_fs.h util.h pool.h
 core.o: core.c core.h pool.h
 fuzix_fs.o: fuzix_fs.c fuzix_fs.h
+block_pool.o: block_pool.c block_pool.h
+pool.o: pool.c pool.h
 
 clean:
        rm -f *.o process_test_run
index 8696d8b..43180aa 100644 (file)
@@ -1,12 +1,14 @@
 CFLAGS=-g -Wall
 
-process_test_run: process_test_run.o process.o core.o swap.o pool.o
+process_test_run: process_test_run.o process.o core.o swap.o block_pool.o pool.o
        ${CC} ${CFLAGS} -o $@ $^
 
 process_test_run.o: process_test_run.c process.h core.h swap.h pool.h
 process.o: process.c process.h core.h swap.h pool.h
 core.o: core.c core.h pool.h
 swap.o: swap.c swap.h pool.h
+block_pool.o: block_pool.c block_pool.h
+pool.o: pool.c pool.h
 
 clean:
        rm -f *.o process_test_run
index 8696d8b..06e4155 100644 (file)
@@ -1,12 +1,14 @@
 CFLAGS=-g -Wall
 
-process_test_run: process_test_run.o process.o core.o swap.o pool.o
+process_test_run: process_test_run.o process.o core.o swap.o block_pool.o pool.o
        ${CC} ${CFLAGS} -o $@ $^
 
 process_test_run.o: process_test_run.c process.h core.h swap.h pool.h
 process.o: process.c process.h core.h swap.h pool.h
 core.o: core.c core.h pool.h
 swap.o: swap.c swap.h pool.h
-
+block_pool.o: block_pool.c block_pool.h
+pool.o: pool.c pool.h
 clean:
        rm -f *.o process_test_run
index cd08d43..8c1770f 100644 (file)
@@ -7,6 +7,7 @@ process_test_run.o: process_test_run.c process.h core.h fuzix_fs.h util.h pool.h
 process.o: process.c process.h core.h fuzix_fs.h util.h pool.h
 core.o: core.c core.h pool.h
 fuzix_fs.o: fuzix_fs.c fuzix_fs.h
+pool.c: pool.o pool.h
 
 clean:
        rm -f *.o process_test_run
index 8696d8b..56ff7c8 100644 (file)
@@ -7,6 +7,7 @@ process_test_run.o: process_test_run.c process.h core.h swap.h pool.h
 process.o: process.c process.h core.h swap.h pool.h
 core.o: core.c core.h pool.h
 swap.o: swap.c swap.h pool.h
+pool.o: pool.c pool.h
 
 clean:
        rm -f *.o process_test_run
diff --git a/o.sh b/o.sh
index 7635678..88524fa 100755 (executable)
--- a/o.sh
+++ b/o.sh
@@ -4,8 +4,8 @@ echo "generate test script"
 ./process_test_gen 16 248 1024 32 >process_test.txt
 
 echo "run test script"
-#./process_test_run 16 64 192 8 384 384 <process_test.txt
-rm -f fs.bin
-# need 1536 x 0.5 kbyte for 192 x 4 = 768 kbytes, plus 1 x 0.5 kbyte root dir
-./mkfs fs.bin 63 1600
-./process_test_run 16 64 fs.bin 8 384 384 <process_test.txt
+./process_test_run 16 64 192 8 384 384 <process_test.txt
+#rm -f fs.bin
+## need 1536 x 0.5 kbyte for 192 x 4 = 768 kbytes, plus 1 x 0.5 kbyte root dir
+#./mkfs fs.bin 63 1600
+#./process_test_run 16 64 fs.bin 8 384 384 <process_test.txt
diff --git a/pool.c b/pool.c
index 79e995f..ad6ac18 100644 (file)
--- a/pool.c
+++ b/pool.c
@@ -5,7 +5,9 @@
 #include "pool.h"
 
 #define MOVEABLE_POOL 1
+#ifdef MOVEABLE_POOL
 #define TRY_BLOCKER 1
+#endif
 
 #ifndef NDEBUG
 void check_invariants(struct pool_head *head) {
@@ -53,7 +55,7 @@ bool pool_alloc(
   int new_base, new_limit, avail;
   bool dir;
   struct pool_item *p, *q;
-#if defined(MOVEABLE_POOL) && defined(TRY_BLOCKER)
+#ifdef TRY_BLOCKER
   struct pool_item *r, *target_item;
   int target_size;
 #endif
@@ -81,7 +83,7 @@ bool pool_alloc(
       new_limit = item->limit - gap;
       new_base = new_limit - size;
     }
-#if !defined(MOVEABLE_POOL) || !defined(TRY_BLOCKER)
+#ifndef TRY_BLOCKER
     if (new_base >= item->prev->limit && new_limit <= item->next->base)
       goto resize;
 #endif
@@ -104,7 +106,7 @@ bool pool_alloc(
     p = &head->item;
     q = &head->item;
 
-#if defined(MOVEABLE_POOL) && defined(TRY_BLOCKER)
+#ifdef TRY_BLOCKER
 blocker_test:
     // enter here when either blocker has changed
     target_item = item;
@@ -175,7 +177,7 @@ blocker_test:
 
       // see if target fits in current gap 
       gap = q->base - p->limit;
-#if defined(MOVEABLE_POOL) && defined(TRY_BLOCKER)
+#ifdef TRY_BLOCKER
       if (target_size <= gap) {
  //printf("target\n");
         if (target_item == item)
diff --git a/pp.sh b/pp.sh
index 9cd2b57..4a7d584 100755 (executable)
--- a/pp.sh
+++ b/pp.sh
@@ -2,6 +2,8 @@
 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/; s/^#define TRANSFER_/$define TRANSFER_/; s/^#define POOL_ALLOC_MODE_/$define POOL_ALLOC_MODE_/' -i *.[ch]
 for i in \
+block_pool.c \
+block_pool.h \
 core.c \
 core.h \
 pool.c \
index f5f9943..0a55749 100644 (file)
--- a/process.c
+++ b/process.c
@@ -260,7 +260,9 @@ static bool do_swap_out(int swap_out) {
 
       // transfer data to swap
 #ifdef INDIRECT_SWAP
-      rassert(swap_block_alloc(swap_table_mem + swap_base, blocks));
+      rassert(
+        block_pool_alloc(&swap_block_pool, swap_table_mem + swap_base, blocks)
+      );
 #endif
 #ifdef INODE_SWAP
  printf("write core [%d,%d) to swap [%d,%d)\n", core_base, core_base + blocks, victim_swap_blocks, victim_swap_blocks + blocks);
@@ -350,7 +352,7 @@ static bool do_swap_out(int swap_out) {
  printf("\n");
 #endif
 #ifdef INDIRECT_CORE
-      core_block_free(core_table_mem + core_base, blocks);
+      block_pool_free(&core_block_pool, core_table_mem + core_base, blocks);
 #endif
       victim_swap_blocks += blocks;
 
@@ -485,7 +487,8 @@ bool process_alloc(struct process *process, long size) {
 #ifdef INDIRECT_CORE
   // populate new table with physical blocks
   rassert(
-    core_block_alloc(
+    block_pool_alloc(
+      &core_block_pool,
       core_table_mem + process->core_item.base,
       blocks
     )
@@ -616,13 +619,15 @@ bool process_realloc(struct process *process, long size) {
   // populate new table with physical blocks or discard them
   if (blocks_change >= 0)
     rassert(
-      core_block_alloc(
+      block_pool_alloc(
+        &core_block_pool,
         core_table_mem + process->core_item.limit - blocks_change,
         blocks_change
       )
     );
   else
-    core_block_free(
+    block_pool_free(
+      &core_block_pool,
       core_table_mem + process->core_item.limit,
       -blocks_change
     );
@@ -796,7 +801,7 @@ void process_run(struct process *process) {
 
       // transfer data to core
 #ifdef INDIRECT_CORE
-      core_block_alloc(core_table_mem + core_base, blocks);
+      block_pool_alloc(&core_block_pool, core_table_mem + core_base, blocks);
 #endif
 #ifdef INODE_SWAP
  printf("read swap [%d,%d) to core [%d,%d)\n", process_swap_blocks, process_swap_blocks + blocks, core_base, core_base + blocks);
@@ -885,7 +890,7 @@ void process_run(struct process *process) {
  printf("\n");
 #endif
 #ifdef INDIRECT_SWAP
-      swap_block_free(swap_table_mem + swap_base, blocks);
+      block_pool_free(&swap_block_pool, swap_table_mem + swap_base, blocks);
 #endif
     } while (process_swap_blocks);
 
@@ -933,7 +938,8 @@ void process_free(struct process *process) {
 
 #ifdef INDIRECT_CORE
     int core_base = process->core_item.base;
-    core_block_free(
+    block_pool_free(
+      &core_block_pool,
       core_table_mem + core_base,
       process->core_item.limit - core_base
     );
@@ -949,7 +955,11 @@ void process_free(struct process *process) {
 #ifndef PREALLOCATE_SWAP
     victim_swap_blocks = victim->swap_item.limit - swap_base;
 #endif
-    swap_block_free(swap_table_mem + swap_base, victim_swap_blocks);
+    block_pool_free(
+      &swap_block_pool,
+      swap_table_mem + swap_base,
+      victim_swap_blocks
+    );
 #endif
 #if !defined(INODE_SWAP) && !defined(PREALLOCATE_SWAP)
     pool_free(&swap_table, &victim->swap_item);
@@ -972,7 +982,11 @@ void process_free(struct process *process) {
     core_base = victim->core_item.base;
     victim_core_blocks = victim->core_item.limit - core_base;
 #endif
-    core_block_free(core_table_mem + core_base, victim_core_blocks);
+    block_pool_free(
+      &core_block_pool,
+      core_table_mem + core_base,
+      victim_core_blocks
+    );
 #endif
 #ifndef PREALLOCATE_CORE
     pool_free(&core_table, &victim->core_item);
@@ -983,7 +997,8 @@ void process_free(struct process *process) {
     // fully in swap, remove from swap pool
 #ifdef INDIRECT_SWAP
     int swap_base = process->swap_item.base;
-    swap_block_free(
+    block_pool_free(
+      &swap_block_pool,
       swap_table_mem + swap_base,
       process->swap_item.limit - swap_base
     );
index 0fe70db..d63a317 100644 (file)
--- a/process.h
+++ b/process.h
@@ -1,16 +1,18 @@
 #ifndef _PROCESS_H
 #define _PROCESS_H 1
 
-#include "pool.h"
-
 #define BLOCK_SIZE 0x1000
 #define BLOCK_SHIFT 12
 
 #define INODE_SWAP 1
+#ifdef INODE_SWAP
 #define DISK_BLOCK_SIZE 0x200
 #define DISK_BLOCK_SHIFT 9
 #define DISK_INDIRECT_SIZE 0x100
 #define DISK_INDIRECT_SHIFT 8
+#endif
+
+#include "pool.h"
 
 struct lru_item {
   struct lru_item *prev;
index b193c80..31c0f22 100644 (file)
@@ -647,7 +647,11 @@ done:
         swap_size
       );
 #ifdef INDIRECT_CORE
-      core_block_free(core_table_mem + core_base, core_blocks);
+      block_pool_free(
+        &core_block_pool,
+        core_table_mem + core_base,
+        core_blocks
+      );
 #endif
       swap_hash_verify(
         i,
@@ -662,7 +666,11 @@ done:
  //printf("deref inode %d\n", processes[i].swap_inode->c_num);
       i_deref(processes[i].swap_inode);
 #elif defined(INDIRECT_SWAP)
-      swap_block_free(swap_table_mem + swap_base, swap_blocks);
+      block_pool_free(
+        &swap_block_pool,
+        swap_table_mem + swap_base,
+        swap_blocks
+      );
 #endif
     }
   }
@@ -673,12 +681,12 @@ done:
     rassert(core_table_mem[i] == 0x55555555);
 #endif
   {
-    k = n_core_blocks >> 3;
+    k = core_block_pool.n_blocks >> 3;
     for (j = 0; j < k; ++j)
-      rassert(core_block_bitmap[j] == 0xff);
-    k = n_core_blocks & 7;
+      rassert(core_block_pool.bitmap[j] == 0xff);
+    k = core_block_pool.n_blocks & 7;
     if (k)
-      rassert(core_block_bitmap[j] == (uint8_t)~(-1 << k));
+      rassert(core_block_pool.bitmap[j] == (uint8_t)~(-1 << k));
   }
 #endif
   j = n_core_blocks << BLOCK_SHIFT;
@@ -693,12 +701,12 @@ done:
     rassert(swap_table_mem[i] == 0x55555555);
 #endif
   {
-    k = n_swap_blocks >> 3;
+    k = swap_block_pool.n_blocks >> 3;
     for (j = 0; j < k; ++j)
-      rassert(swap_block_bitmap[j] == 0xff);
-    k = n_swap_blocks & 7;
+      rassert(swap_block_pool.bitmap[j] == 0xff);
+    k = swap_block_pool.n_blocks & 7;
     if (k)
-      rassert(swap_block_bitmap[j] == (uint8_t)~(-1 << k));
+      rassert(swap_block_pool.bitmap[j] == (uint8_t)~(-1 << k));
   }
 #endif
   j = n_swap_blocks << BLOCK_SHIFT;
diff --git a/swap.c b/swap.c
index d76d238..33a411b 100644 (file)
--- a/swap.c
+++ b/swap.c
@@ -9,15 +9,7 @@
 struct pool_head swap_table;
 #ifdef INDIRECT_SWAP
 int *swap_table_mem;
-
-static uint8_t masks[8] = {0xff, 0xfe, 0xfc, 0xf8, 0xf0, 0xe0, 0xc0, 0x80};
-static uint8_t bits[8] = {1, 2, 4, 8, 0x10, 0x20, 0x40, 0x80};
-
-int n_swap_blocks;
-int n_swap_block_bitmap;
-uint8_t *swap_block_bitmap;
-int swap_block_next;
-int swap_block_avail;
+struct block_pool swap_block_pool;
 #endif
 
 #ifdef PREALLOCATE_SWAP
@@ -29,6 +21,7 @@ static void swap_move(struct pool_item *item, int new_base) {
 #ifdef INDIRECT_SWAP
   int i;
 #else
+  struct process *process;
   long size;
 #endif
 
@@ -51,7 +44,7 @@ static void swap_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, swap_item));
   size = (long)blocks << BLOCK_SHIFT;
   if (size > process->size)
@@ -140,84 +133,6 @@ void swap_init(int n_blocks)
   rassert(swap_table_mem);
   memset(swap_table_mem, 0x55, table_size * sizeof(int));
 
-  n_swap_blocks = n_blocks;
-  n_swap_block_bitmap = (n_swap_blocks + 7) >> 3;
-  swap_block_bitmap = malloc(n_swap_block_bitmap);
-  rassert(swap_block_bitmap);
-  memset(swap_block_bitmap, 0xff, n_swap_block_bitmap);
-  if (n_swap_blocks & 7)
-    swap_block_bitmap[n_swap_blocks >> 3] = ~masks[n_swap_blocks & 7];
-  swap_block_next = 0;
-  swap_block_avail = n_swap_blocks;
-#endif
-}
-
-#ifdef INDIRECT_SWAP
-bool swap_block_alloc(int *table, int size) {
-  int i, j, k;
-  uint8_t c;
-
-  if (swap_block_avail < size)
-    return false;
-
-  for (i = 0; i < size; ++i) {
-    j = swap_block_next >> 3;
-    c = swap_block_bitmap[j] & masks[swap_block_next & 7];
-    goto loop_entry;
-    for (; j < n_swap_block_bitmap; ++j) {
-      c = swap_block_bitmap[j];
-    loop_entry:
-      if (c) goto found;
-    }
-#if 1
-    for (j = 0; ; ++j) {
-      assert(j <= swap_block_next >> 3);
-      c = swap_block_bitmap[j];
-      if (c) goto found;
-    }
-#else
-    k = (swap_block_next >> 3) + 1;
-    for (j = 0; j < k; ++j) {
-      c = swap_block_bitmap[j];
-      if (c) goto found;
-    }
-    swap_block_free(table, i);
-    return false;
-#endif
-
-  found:
-    for (k = 0; (c & 1) == 0; ++k)
-      c >>= 1;
-    swap_block_bitmap[j] &= ~bits[k];
-    swap_block_next = (j << 3) | k;
-
-#ifndef NDEBUG // makes us remove the following line in gen.sh as appropriate
-    assert(table[i] == 0x55555555);
-#endif
-    table[i] = swap_block_next++;
-    if (swap_block_next >= n_swap_blocks)
-      swap_block_next = 0;
-  }
-
-  swap_block_avail -= size;
-  return true;
-}
-
-void swap_block_free(int *table, int size) {
-  int i, j, k;
-
-  for (i = 0; i < size; ++i) {
-    j = table[i];
-    assert(j >= 0 && j < n_swap_blocks);
-    k = j & 7;
-    j >>= 3;
-    assert((swap_block_bitmap[j] & bits[k]) == 0);
-    swap_block_bitmap[j] |= bits[k];
-#ifndef NDEBUG
-    table[i] = 0x55555555;
+  block_pool_init(&swap_block_pool, n_blocks);
 #endif
-  }
-
-  swap_block_avail += size;
 }
-#endif
diff --git a/swap.h b/swap.h
index 104e425..87aeb6d 100644 (file)
--- a/swap.h
+++ b/swap.h
@@ -1,15 +1,18 @@
 #ifndef _SWAP_H
 #define _SWAP_H 1
 
-#include <stdint.h>
-#include "pool.h"
-
 #define PREALLOCATE_SWAP 1
 #define INDIRECT_SWAP 1
 //#define MOVEABLE_SWAP 1
 
+#include <stdint.h>
 #ifdef INDIRECT_SWAP
-#define SWAP_AVAIL swap_block_avail
+#include "block_pool.h"
+#endif
+#include "pool.h"
+
+#ifdef INDIRECT_SWAP
+#define SWAP_AVAIL swap_block_pool.avail
 #else
 #define SWAP_AVAIL swap_table.avail
 #endif
 extern struct pool_head swap_table;
 #ifdef INDIRECT_SWAP
 extern int *swap_table_mem;
-
-extern int n_swap_blocks;
-int n_swap_block_bitmap;
-extern uint8_t *swap_block_bitmap;
-extern int swap_block_next;
-extern int swap_block_avail;
+extern struct block_pool swap_block_pool;
 #endif
 
 #ifdef PREALLOCATE_SWAP
@@ -31,8 +29,6 @@ extern int victim_swap_blocks;
 
 #ifdef INDIRECT_SWAP
 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);