pool_alloc_moveable(&core_table, item, size)
#define core_table_realloc(item, size) \
pool_realloc_moveable(&core_table, item, size)
+#define core_table_realloc_base(item, size) \
+ pool_realloc_base_moveable(&core_table, item, size)
#else
#define core_table_alloc(item, size) \
pool_alloc(&core_table, item, size)
#define core_table_realloc(item, size) \
pool_realloc(&core_table, item, size)
+#define core_table_realloc_base(item, size) \
+ pool_realloc_base(&core_table, item, size)
#endif
#define core_table_free(item) pool_free(&core_table, item)
while (true) {
p = q;
q = p->next;
- if (p == item) {
+ if (q == item) {
assert(item->limit - size >= item->prev->limit);
goto resize;
}
// loop entry code for the case of an existing victim
if (swap_out > 0) {
if (victim) {
- // calculate amount to swap
+ // calculate amount to swap out
#ifndef PREALLOCATE_CORE
victim_core_size = victim_core_item.limit - victim_core_item.base;
#endif
#else
pool_move_item(&victim->pool_item, &victim_core_item);
victim_core_size = victim_core_item.limit - victim_core_item.base;
+ assert(victim_core_size == victim->size);
#endif
- // calculate amount to swap
+ // calculate amount to swap out
size = swap_out < victim_core_size ? swap_out : victim_core_size;
printf("new victim %d, swap out %d of %d\n", (int)(victim - processes), size, victim_core_size);
loop_entry:
// transfer data to swap
#ifdef PREALLOCATE_SWAP
- swap_base = ~(victim->swap_item.base + victim_swap_size);
+ swap_base = victim->swap_item.base + victim_swap_size - size;
#else
- swap_base = ~victim_swap_item.limit;
+ swap_base = victim_swap_item.limit - size;
#endif
#ifdef PREALLOCATE_CORE
- core_base = victim->core_item.base + victim_core_size - size;
+ core_base = victim->core_item.base + victim_swap_size - size;
#else
- core_base = victim_core_item.limit - size;
+ core_base = victim_core_item.base;
#endif
#ifdef INDIRECT_SWAP
rassert(swap_block_alloc(swap_table_mem + swap_base, size));
if (victim_core_size) {
#ifndef PREALLOCATE_CORE
// no, reduce core allocation, using dedicated core item
- rassert(core_table_realloc(&victim_core_item, victim_core_size));
+ rassert(core_table_realloc_base(&victim_core_item, victim_core_size));
#endif
// as an optimization, skip the calculation of swap_out -= size
)
);
else
- {
core_block_free(
#ifdef PREALLOCATE_CORE
core_table_mem + process->core_item.limit,
#endif
-size_change
);
- }
#endif
// track total allocation
#else
pool_move_item(&process->pool_item, &process_swap_item);
process_swap_size = process_swap_item.limit - process_swap_item.base;
+ assert(process_swap_size == process->size);
#endif
- // calculate amount to swap
+ // calculate amounts to swap out then in
size = process_swap_size;
swap_out = size - core_avail();
+#if !defined(PREALLOCATE_SWAP) || defined(INDIRECT_SWAP)
if (swap_out > swap_avail()) {
size += swap_avail() - swap_out;
swap_out = swap_avail();
}
+#endif
// free up as much core as we can
- assert(swap_out <= swap_avail());
do_swap_out(swap_out);
// add to core pool, using dedicated core item
loop_entry_partial:
#endif
- // calculate how much to swap
+ // calculate amounts to swap out then in
size = process_swap_size;
swap_out = size - core_avail();
+#if !defined(PREALLOCATE_SWAP) || defined(INDIRECT_SWAP)
if (swap_out > swap_avail()) {
size += swap_avail() - swap_out;
swap_out = swap_avail();
}
+#endif
// free up as much core as we can
do_swap_out(swap_out);
// increase core allocation
process_core_size += size;
#ifndef PREALLOCATE_CORE
- rassert(core_table_realloc(&process_core_item, process_core_size));
+ rassert(core_table_realloc_base(&process_core_item, process_core_size));
#endif
loop_entry_full:
// transfer data to core
#ifdef PREALLOCATE_SWAP
- swap_base = ~(process->swap_item.base + process_swap_size);
+ swap_base = process->swap_item.base + process_swap_size - size;
#else
- swap_base = ~process_swap_item.limit;
+ swap_base = process_swap_item.limit - size;
#endif
#ifdef PREALLOCATE_CORE
- core_base = process->core_item.base + process_core_size - size;
+ core_base = process->core_item.base + process_swap_size - size;
#else
- core_base = process_core_item.limit - size;
+ core_base = process_core_item.base;
#endif
#ifdef INDIRECT_CORE
core_block_alloc(core_table_mem + core_base, size);
#ifdef PREALLOCATE_CORE
#ifdef INDIRECT_CORE
core_block_free(
- core_table_mem + process->core_item.base,
+ core_table_mem + process->core_item.base + victim_swap_size,
victim_core_size
);
#endif
#ifdef PREALLOCATE_SWAP
#ifdef INDIRECT_SWAP
swap_block_free(
- swap_table_mem + ~(process->swap_item.base + victim_swap_size),
+ swap_table_mem + process->swap_item.base,
victim_swap_size
);
#endif
#else
#ifdef INDIRECT_SWAP
swap_block_free(
- swap_table_mem + ~victim_swap_item.limit,
+ swap_table_mem + victim_swap_item.base,
victim_swap_item.limit - victim_swap_item.base
);
#endif
#ifdef PREALLOCATE_SWAP
#ifdef INDIRECT_SWAP
swap_block_free(
- swap_table_mem + ~(process->swap_item.limit),
+ swap_table_mem + process->swap_item.base,
process->swap_item.limit - process->swap_item.base
);
#endif
#else
#ifdef INDIRECT_SWAP
swap_block_free(
- swap_table_mem + ~(process->pool_item.limit),
+ swap_table_mem + process->pool_item.base,
process->pool_item.limit - process->pool_item.base
);
#endif
printf("new core [%d,%d)\n", core_base, core_base + size);
core_hash_init(process, core_base, 0, size);
#ifdef PREALLOCATE_SWAP
- printf("new swap [%d,%d)\n", ~processes[process].swap_item.limit, ~processes[process].swap_item.base);
+ printf("new swap [%d,%d)\n", processes[process].swap_item.base, processes[process].swap_item.limit);
#endif
}
}
rassert(actual_old_size <= old_size);
}
#ifdef PREALLOCATE_SWAP
- printf("old core [%d,%d) swap [%d,%d)\n", core_base, core_base + actual_old_size, ~processes[process].swap_item.limit, ~processes[process].swap_item.base);
+ printf("old core [%d,%d) swap [%d,%d)\n", core_base, core_base + actual_old_size, processes[process].swap_item.base, processes[process].swap_item.limit);
#else
printf("old core [%d,%d)\n", core_base, core_base + actual_old_size);
#endif
rassert(processes[process].pool_item.limit == core_base + size);
#endif
#ifdef PREALLOCATE_SWAP
- printf("new core [%d,%d) swap [%d,%d)\n", core_base, core_base + size, ~processes[process].swap_item.limit, ~processes[process].swap_item.base);
+ printf("new core [%d,%d) swap [%d,%d)\n", core_base, core_base + size, processes[process].swap_item.base, processes[process].swap_item.limit);
#else
printf("new core [%d,%d)\n", core_base, core_base + size);
#endif
}
bool is_victim = processes + process == victim;
bool in_core = processes[process].lru_item.prev != NULL;
+#ifdef PREALLOCATE_SWAP
+ int swap_size =
+ is_victim ?
+ victim_swap_size :
+ !in_core ? processes[process].size : 0;
+ int swap_base =
+ processes[process].swap_item.base;
+#else
+ int swap_base =
+ is_victim ?
+ victim_swap_item.base :
+ !in_core ? processes[process].pool_item.base : -1;
+ int swap_size =
+ is_victim ?
+ victim_swap_item.limit - swap_base :
+ !in_core ? processes[process].pool_item.limit - swap_base : 0;
+#endif
#ifdef PREALLOCATE_CORE
int core_size =
is_victim ?
victim_core_size :
in_core ? processes[process].size : 0;
int core_base =
- processes[process].core_item.base;
+ processes[process].core_item.base + swap_size;
#else
int core_base =
is_victim ?
is_victim ?
victim_core_item.limit - core_base :
in_core ? processes[process].pool_item.limit - core_base : 0;
-#endif
-#ifdef PREALLOCATE_SWAP
- int swap_size =
- is_victim ?
- victim_swap_size :
- !in_core ? processes[process].size : 0;
- int swap_limit =
- processes[process].swap_item.base + swap_size;
-#else
- int swap_limit =
- is_victim ?
- victim_swap_item.limit :
- !in_core ? processes[process].pool_item.limit : 0;
- int swap_size =
- is_victim ?
- swap_limit - victim_swap_item.base :
- !in_core ? swap_limit - processes[process].pool_item.base : 0;
#endif
rassert(core_size + swap_size == actual_old_size);
- int swap_base = ~swap_limit;
printf("old core [%d,%d) swap [%d,%d)\n", core_base, core_base + core_size, swap_base, swap_base + swap_size);
- core_hash_verify(process, core_base, 0, core_size);
- swap_hash_verify(process, swap_base, 0, swap_size, core_size);
+ fflush(stdout);
+ core_hash_verify(process, core_base - swap_size, swap_size, swap_size + core_size);
+ swap_hash_verify(process, swap_base, 0, swap_size);
process_free(processes + process);
processes[process].size = -1;
printf("... ok\n");
else {
bool is_victim = processes + i == victim;
bool in_core = processes[i].lru_item.prev != NULL;
+#ifdef PREALLOCATE_SWAP
+ int swap_size =
+ is_victim ?
+ victim_swap_size :
+ !in_core ? processes[i].size : 0;
+ int swap_base =
+ processes[i].swap_item.base;
+#else
+ int swap_base =
+ is_victim ?
+ victim_swap_item.base :
+ !in_core ? processes[i].pool_item.base : -1;
+ int swap_size =
+ is_victim ?
+ victim_swap_item.limit - swap_base :
+ !in_core ? processes[i].pool_item.limit - swap_base : 0;
+#endif
#ifdef PREALLOCATE_CORE
int core_size =
is_victim ?
victim_core_size :
in_core ? processes[i].size : 0;
int core_base =
- processes[i].core_item.base;
+ processes[i].core_item.base + swap_size;
#else
int core_base =
is_victim ?
is_victim ?
victim_core_item.limit - core_base :
in_core ? processes[i].pool_item.limit - core_base : 0;
-#endif
-#ifdef PREALLOCATE_SWAP
- int swap_size =
- is_victim ?
- victim_swap_size :
- !in_core ? processes[i].size : 0;
- int swap_limit =
- processes[i].swap_item.base + swap_size;
-#else
- int swap_limit =
- is_victim ?
- victim_swap_item.limit :
- !in_core ? processes[i].pool_item.limit : 0;
- int swap_size =
- is_victim ?
- swap_limit - victim_swap_item.base :
- !in_core ? swap_limit - processes[i].pool_item.base : 0;
#endif
rassert(core_size + swap_size == processes[i].size);
- int swap_base = ~swap_limit;
printf(
"process %d: core [%d,%d) swap [%d,%d)\n",
i,
swap_base,
swap_base + swap_size
);
- core_hash_verify(i, core_base, 0, core_size);
+ core_hash_verify(i, core_base - swap_size, swap_size, swap_size + core_size);
#ifdef INDIRECT_CORE
core_block_free(core_table_mem + core_base, core_size);
#endif
- swap_hash_verify(i, swap_base, 0, swap_size, core_size);
+ swap_hash_verify(i, swap_base, 0, swap_size);
#ifdef INDIRECT_SWAP
swap_block_free(swap_table_mem + swap_base, swap_size);
#endif
#endif
uint8_t *swap_block_mem;
-// swap address native wrt. pool and complemented wrt. backing store,
-// means that swap_move() moves upward in backing store, do backward
static void swap_move(struct pool_item *item, int new_base) {
int base = item->base;
- int neg_size = base - item->limit;
-
- int limit = ~base;
- int new_limit = ~new_base;
+ int size = item->limit - base;
printf(
"swap_move [%d,%d) to [%d,%d)\n",
- limit + neg_size,
- limit,
- new_limit + neg_size,
- new_limit
+ base,
+ base + size,
+ new_base,
+ new_base + size
);
- assert(new_limit >= limit || new_limit <= limit + neg_size);
- for (int i = -1; i >= neg_size; --i) {
+ assert(new_base <= base || new_base >= base + size);
+ for (int i = 0; i < size; ++i) {
#ifdef INDIRECT_SWAP
- swap_table_mem[new_limit + i] = swap_table_mem[limit + i];
- swap_table_mem[limit + i] = 0x55555555;
+ swap_table_mem[new_base + i] = swap_table_mem[base + i];
+ swap_table_mem[base + i] = 0x55555555;
#else
memcpy(
- swap_block_mem + (new_limit + i) * BLOCK_SIZE,
- swap_block_mem + (limit + i) * BLOCK_SIZE,
+ swap_block_mem + (new_base + i) * BLOCK_SIZE,
+ swap_block_mem + (base + i) * BLOCK_SIZE,
BLOCK_SIZE
);
memset(
- swap_block_mem + (limit + i) * BLOCK_SIZE,
+ swap_block_mem + (base + i) * BLOCK_SIZE,
0xaa,
BLOCK_SIZE
);
}
#ifdef MOVEABLE_SWAP
-// swap address native wrt. pool and complemented wrt. backing store,
-// means that swap_move_up() moves downward in backing store, do forward
static void swap_move_up(struct pool_item *item, int new_limit) {
int limit = item->limit;
- int size = limit - item->base;
-
- int base = ~limit;
- int new_base = ~new_limit;
+ int neg_size = item->base - limit;
printf(
"swap_move_up [%d,%d) to [%d,%d)\n",
- base,
- base + size,
- new_base,
- new_base + size
+ limit + neg_size,
+ limit,
+ new_limit + neg_size,
+ new_limit
);
- assert(new_base <= base || new_base >= base + size);
- for (int i = 0; i < size; ++i) {
+ assert(new_limit >= limit || new_limit <= limit + neg_size);
+ for (int i = -1; i >= neg_size; --i) {
#ifdef INDIRECT_SWAP
- swap_table_mem[new_base + i] = swap_table_mem[base + i];
- swap_table_mem[base + i] = 0x55555555;
+ swap_table_mem[new_limit + i] = swap_table_mem[limit + i];
+ swap_table_mem[limit + i] = 0x55555555;
#else
memcpy(
- swap_block_mem + (new_base + i) * BLOCK_SIZE,
- swap_block_mem + (base + i) * BLOCK_SIZE,
+ swap_block_mem + (new_limit + i) * BLOCK_SIZE,
+ swap_block_mem + (limit + i) * BLOCK_SIZE,
BLOCK_SIZE
);
memset(
- swap_block_mem + (base + i) * BLOCK_SIZE,
+ swap_block_mem + (limit + i) * BLOCK_SIZE,
0xaa,
BLOCK_SIZE
);
{
pool_init(
&swap_table,
+ 0,
#ifdef INDIRECT_SWAP
- ~table_size,
+ table_size,
#else
- ~n_blocks,
+ n_blocks,
#endif
- -1,
swap_move,
#ifdef MOVEABLE_SWAP
swap_move_up
for (k = 0; (c & 1) == 0; ++k)
c >>= 1;
swap_block_bitmap[j] &= ~bits[k];
- assert(table[i] == 0x55555555);
swap_block_next = (j << 3) | k;
assert(table[i] == 0x55555555);
}
#endif
-// swap address complemented wrt. pool and native wrt. backing store
+#if 0 // not needed as always done in core before copying to swap
+void swap_hash_init(int process, int swap_base, int base, int limit) {
+ for (int i = base; i < limit; ++i) {
+#ifdef INDIRECT_SWAP
+ int swap_block = swap_table_mem[swap_base + i];
+#else
+ int swap_block = swap_base + i;
+#endif
+ for (int j = 0; j < BLOCK_SIZE; ++j) {
+ long long hash = process * 17 + i * 29 + j * 37;
+ hash = (hash & 0xffffffffLL) + (hash >> 32);
+ hash = (hash & 0xffffLL) + (hash >> 16);
+ hash = (hash & 0xffLL) + (hash >> 8);
+ hash = (hash & 0xffLL) + (hash >> 8);
+ rassert(swap_block_mem[swap_block * BLOCK_SIZE + j] == 0xaa);
+ swap_block_mem[swap_block * BLOCK_SIZE + j] = (uint8_t)hash;
+ }
+ }
+}
+#endif
+
+void swap_hash_verify(int process, int swap_base, int base, int limit) {
+ for (int i = base; i < limit; ++i) {
+#ifdef INDIRECT_SWAP
+ int swap_block = swap_table_mem[swap_base + i];
+#else
+ int swap_block = swap_base + i;
+#endif
+ for (int j = 0; j < BLOCK_SIZE; ++j) {
+ long long hash = process * 17 + i * 29 + j * 37;
+ hash = (hash & 0xffffffffLL) + (hash >> 32);
+ hash = (hash & 0xffffLL) + (hash >> 16);
+ hash = (hash & 0xffLL) + (hash >> 8);
+ hash = (hash & 0xffLL) + (hash >> 8);
+ rassert(swap_block_mem[swap_block * BLOCK_SIZE + j] == (uint8_t)hash);
+ }
+ memset(
+ swap_block_mem + swap_block * BLOCK_SIZE,
+ 0xaa,
+ BLOCK_SIZE
+ );
+ }
+}
+
void swap_read(int swap_base, int core_base, int size) {
printf("swap_read swap [%d,%d) to core [%d,%d)\n", swap_base, swap_base + size, core_base, core_base + size);
-#ifdef INDIRECT_SWAP
+#if defined(INDIRECT_CORE) || defined(INDIRECT_SWAP)
printf("blocks");
#endif
for (int i = 0; i < size; ++i) {
#endif
#ifdef INDIRECT_CORE
int core_block = core_table_mem[core_base + i];
+#ifdef INDIRECT_SWAP
printf(",%d", core_block);
+#else
+ printf(" %d", core_block);
+#endif
#else
int core_block = core_base + i;
#endif
BLOCK_SIZE
);
}
-#ifdef INDIRECT_SWAP
+#if defined(INDIRECT_CORE) || defined(INDIRECT_SWAP)
printf("\n");
#endif
}
-// swap address complemented wrt. pool and native wrt. backing store
void swap_write(int swap_base, int core_base, int size) {
printf("swap_write core [%d,%d) to swap [%d,%d)\n", core_base, core_base + size, swap_base, swap_base + size);
-#ifdef INDIRECT_SWAP
+#if defined(INDIRECT_CORE) || defined(INDIRECT_SWAP)
printf("blocks");
#endif
for (int i = 0; i < size; ++i) {
#endif
#ifdef INDIRECT_SWAP
int swap_block = swap_table_mem[swap_base + i];
+#ifdef INDIRECT_CORE
printf(",%d", swap_block);
+#else
+ printf(" %d", swap_block);
+#endif
#else
int swap_block = swap_base + i;
#endif
BLOCK_SIZE
);
}
-#ifdef INDIRECT_SWAP
+#if defined(INDIRECT_CORE) || defined(INDIRECT_SWAP)
printf("\n");
#endif
}
-
-void swap_hash_verify(
- int process,
- int swap_base,
- int base,
- int limit,
- int offset
-) {
- for (int i = base; i < limit; ++i) {
-#ifdef INDIRECT_SWAP
- int swap_block = swap_table_mem[swap_base + i];
-#else
- int swap_block = swap_base + i;
-#endif
- for (int j = 0; j < BLOCK_SIZE; ++j) {
- 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);
- hash = (hash & 0xffLL) + (hash >> 8);
- rassert(swap_block_mem[swap_block * BLOCK_SIZE + j] == (uint8_t)hash);
- }
- memset(
- swap_block_mem + swap_block * BLOCK_SIZE,
- 0xaa,
- BLOCK_SIZE
- );
- }
-}
pool_alloc_moveable(&swap_table, item, size)
#define swap_table_realloc(item, size) \
pool_realloc_moveable(&swap_table, item, size)
+#define swap_table_realloc_base(item, size) \
+ pool_realloc_base_moveable(&swap_table, item, size)
#else
#define swap_table_alloc(item, size) \
pool_alloc(&swap_table, item, size)
#define swap_table_realloc(item, size) \
pool_realloc(&swap_table, item, size)
+#define swap_table_realloc_base(item, size) \
+ pool_realloc_base(&swap_table, item, size)
#endif
#define swap_table_free(item) pool_free(&swap_table, item)
#else
void swap_init(int n_table);
#endif
+//void swap_hash_init(int process, int swap_base, int base, int limit);
+void swap_hash_verify(int process, int swap_base, int base, int limit);
void swap_read(int swap_base, int core_base, int size);
void swap_write(int swap_base, int core_base, int size);
-void swap_hash_verify(
- int process,
- int swap_base,
- int base,
- int limit,
- int offset
-);
#endif