Unify process_alloc() and process_realloc() with PROCESS_ALLOC_MODE_REALLOC bit
[moveable_pool.git] / swap.c
1 #include <assert.h>
2 #include <stddef.h>
3 //#include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include "swap.h"
7 #include "process.h"
8 #include "rassert.h"
9
10 struct pool_head swap_table;
11 #ifdef INDIRECT_SWAP
12 int *swap_table_mem;
13 struct block_pool swap_block_pool;
14 #endif /* INDIRECT_SWAP */
15
16 static void swap_move(struct pool_item *item, int new_base) {
17 #if 0 // only called for block under realloc, whose contents are ignored
18   int base, blocks;
19   int i;
20
21   base = item->base;
22   blocks = item->limit - base;
23  //printf("swap_move [%d,%d) to [%d,%d)\n", base, base + blocks, new_base, new_base + blocks);
24   assert(new_base <= base || new_base >= base + blocks);
25
26   for (i = 0; i < blocks; ++i) {
27     swap_table_mem[new_base + i] = swap_table_mem[base + i];
28     swap_table_mem[base + i] = 0x55555555;
29   }
30 #endif
31 }
32
33 static void swap_move_up(struct pool_item *item, int new_base) {
34 #if 0 // only called for block under realloc, whose contents are ignored
35   int base, blocks;
36   int i;
37
38   limit = item->limit;
39   blocks = limit - item->base;
40  printf("swap_move_up [%d,%d) to [%d,%d)\n", limit - blocks, limit, new_limit - blocks, new_limit);
41   assert(new_limit >= limit || new_limit <= limit - blocks);
42
43   blocks = -blocks;
44   for (i = -1; i >= blocks; --i) {
45     swap_table_mem[new_limit + i] = swap_table_mem[limit + i];
46     swap_table_mem[limit + i] = 0x55555555;
47   }
48 #endif
49 }
50
51 #ifndef INDIRECT_SWAP
52 void swap_init(int n_blocks)
53 #else /* INDIRECT_SWAP */
54 void swap_init(int n_blocks, int table_size)
55 #endif /* INDIRECT_SWAP */
56 {
57   pool_init(
58     &swap_table,
59     0,
60 #ifndef INDIRECT_SWAP
61     n_blocks,
62 #else /* INDIRECT_SWAP */
63     table_size,
64 #endif /* INDIRECT_SWAP */
65     swap_move,
66     swap_move_up
67   );
68
69 #ifdef INDIRECT_SWAP
70   swap_table_mem = malloc(table_size * sizeof(int));
71   rassert(swap_table_mem);
72   memset(swap_table_mem, 0x55, table_size * sizeof(int));
73
74   block_pool_init(&swap_block_pool, n_blocks);
75 #endif /* INDIRECT_SWAP */
76 }