Unify process_alloc() and process_realloc() with PROCESS_ALLOC_MODE_REALLOC bit
[moveable_pool.git] / core.c
1 #include <assert.h>
2 #include <stddef.h>
3 #include <stdio.h> // temporary
4 #include <stdlib.h>
5 #include <string.h>
6 #include "core.h"
7 #include "process.h"
8 #include "rassert.h"
9
10 struct pool_head core_table;
11 #ifdef INDIRECT_CORE
12 int *core_table_mem;
13 struct block_pool core_block_pool;
14 #endif /* INDIRECT_CORE */
15
16 static void core_move(struct pool_item *item, int new_base) {
17   int base, blocks;
18 #ifndef INDIRECT_CORE
19   struct process *process;
20   struct process_calc calc;
21   int in_core_para, para_base;
22 #else /* INDIRECT_CORE */
23   int i;
24 #endif /* INDIRECT_CORE */
25
26   base = item->base;
27   blocks = item->limit - base;
28  printf("core_move [%d,%d) to [%d,%d)\n", base, base + blocks, new_base, new_base + blocks);
29   assert(new_base <= base || new_base >= base + blocks);
30
31 #ifndef INDIRECT_CORE
32 #if 0 // debugging
33  core_copy(
34   base << BLOCK_PARAS_SHIFT,
35   new_base << BLOCK_PARAS_SHIFT,
36   blocks << BLOCK_PARAS_SHIFT
37  );
38 #else
39   // figure out how much of item is valid
40   process = (struct process *)(
41     (char *)item - offsetof(struct process, core_item)
42   );
43   process_calc(process, &calc);
44   in_core_para = calc.in_core_block << BLOCK_PARAS_SHIFT;
45   if (in_core_para < process->para_base)
46     in_core_para = process->para_base;
47   else if (in_core_para > process->para_limit)
48     in_core_para = process->para_limit;
49
50   // copy valid part by abstract routine
51   para_base = (calc.core_origin << BLOCK_PARAS_SHIFT) + process->para_base;
52   core_copy(
53     para_base,
54     para_base + ((new_base - base) << BLOCK_PARAS_SHIFT),
55     in_core_para - process->para_base
56   );
57 #endif
58 #else /* INDIRECT_CORE */
59   for (i = 0; i < blocks; ++i) {
60     core_table_mem[new_base + i] = core_table_mem[base + i];
61     core_table_mem[base + i] = 0x55555555;
62   }
63 #endif /* INDIRECT_CORE */
64 }
65
66 static void core_move_up(struct pool_item *item, int new_limit) {
67   int limit, blocks;
68 #ifndef INDIRECT_CORE
69   struct process *process;
70   struct process_calc calc;
71   int in_core_para, para_limit;
72 #else /* INDIRECT_CORE */
73   int i;
74 #endif /* INDIRECT_CORE */
75
76   limit = item->limit;
77   blocks = limit - item->base;
78  printf("core_move_up [%d,%d) to [%d,%d)\n", limit - blocks, limit, new_limit - blocks, new_limit);
79   assert(new_limit >= limit || new_limit <= limit - blocks);
80
81 #ifndef INDIRECT_CORE
82 #if 0 // debugging
83  core_copy_up(
84   limit << BLOCK_PARAS_SHIFT,
85   new_limit << BLOCK_PARAS_SHIFT,
86   blocks << BLOCK_PARAS_SHIFT
87  );
88 #else
89   // figure out how much of item is valid
90   process = (struct process *)(
91     (char *)item - offsetof(struct process, core_item)
92   );
93   process_calc(process, &calc);
94   in_core_para = calc.in_core_block << BLOCK_PARAS_SHIFT;
95   if (in_core_para < process->para_base)
96     in_core_para = process->para_base;
97   else if (in_core_para > process->para_limit)
98     in_core_para = process->para_limit;
99
100   // copy valid part by abstract routine
101   para_limit = (calc.core_origin << BLOCK_PARAS_SHIFT) + in_core_para;
102   core_copy_up(
103     para_limit,
104     para_limit + ((new_limit - limit) << BLOCK_PARAS_SHIFT),
105     in_core_para - process->para_base
106   );
107 #endif
108 #else /* INDIRECT_CORE */
109   blocks = -blocks;
110   for (i = -1; i >= blocks; --i) {
111     core_table_mem[new_limit + i] = core_table_mem[limit + i];
112     core_table_mem[limit + i] = 0x55555555;
113   }
114 #endif /* INDIRECT_CORE */
115 }
116
117 #ifndef INDIRECT_CORE
118 void core_init(int n_blocks)
119 #else /* INDIRECT_CORE */
120 void core_init(int n_blocks, int table_size)
121 #endif /* INDIRECT_CORE */
122 {
123   pool_init(
124     &core_table,
125     0,
126 #ifndef INDIRECT_CORE
127     n_blocks,
128 #else /* INDIRECT_CORE */
129     table_size,
130 #endif /* INDIRECT_CORE */
131     core_move,
132     core_move_up
133   );
134
135 #ifdef INDIRECT_CORE
136   core_table_mem = malloc(table_size * sizeof(int));
137   rassert(core_table_mem);
138   memset(core_table_mem, 0x55, table_size * sizeof(int));
139
140   block_pool_init(&core_block_pool, n_blocks);
141 #endif /* INDIRECT_CORE */
142 }