2 * Copyright © 2016 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
15 #define AO_LISP_CONST_BITS
20 #ifdef AO_LISP_MAKE_CONST
22 uint8_t ao_lisp_const[AO_LISP_POOL_CONST] __attribute__((aligned(4)));
23 #define ao_lisp_pool ao_lisp_const
25 #define AO_LISP_POOL AO_LISP_POOL_CONST
27 uint8_t ao_lisp_pool[AO_LISP_POOL] __attribute__((aligned(4)));
32 #define DBG_OFFSET(a) ((int) ((uint8_t *) (a) - ao_lisp_pool))
33 #define DBG(...) printf(__VA_ARGS__)
35 static int move_depth;
36 #define DBG_RESET() (move_depth = 0)
37 #define DBG_MOVE(...) do { if(move_dump) { int d; for (d = 0; d < move_depth; d++) printf (" "); printf(__VA_ARGS__); } } while (0)
38 #define DBG_MOVE_IN() (move_depth++)
39 #define DBG_MOVE_OUT() (move_depth--)
45 #define DBG_MOVE_OUT()
48 uint8_t ao_lisp_exception;
52 const struct ao_lisp_type *type;
55 #define AO_LISP_ROOT 16
57 static struct ao_lisp_root ao_lisp_root[AO_LISP_ROOT];
59 static uint8_t ao_lisp_busy[AO_LISP_POOL / 32];
61 static uint8_t ao_lisp_moving[AO_LISP_POOL / 32];
65 static inline void mark(uint8_t *tag, int offset) {
66 int byte = offset >> 5;
67 int bit = (offset >> 2) & 7;
68 tag[byte] |= (1 << bit);
71 static inline void clear(uint8_t *tag, int offset) {
72 int byte = offset >> 5;
73 int bit = (offset >> 2) & 7;
74 tag[byte] &= ~(1 << bit);
77 static inline int busy(uint8_t *tag, int offset) {
78 int byte = offset >> 5;
79 int bit = (offset >> 2) & 7;
80 return (tag[byte] >> bit) & 1;
83 static inline int min(int a, int b) { return a < b ? a : b; }
84 static inline int max(int a, int b) { return a > b ? a : b; }
86 static inline int limit(int offset) {
87 return min(AO_LISP_POOL, max(offset, 0));
91 mark_object(uint8_t *tag, void *addr, int size) {
98 if ((uint8_t *) addr < ao_lisp_pool || ao_lisp_pool + AO_LISP_POOL <= (uint8_t*) addr)
101 base = (uint8_t *) addr - ao_lisp_pool;
105 bound = limit(bound);
108 while (base < bound) {
116 clear_object(uint8_t *tag, void *addr, int size) {
122 base = (uint8_t *) addr - ao_lisp_pool;
126 bound = limit(bound);
127 if (!busy(tag, base))
129 while (base < bound) {
137 busy_object(uint8_t *tag, void *addr) {
143 if ((uint8_t *) addr < ao_lisp_pool || ao_lisp_pool + AO_LISP_POOL <= (uint8_t*) addr)
146 base = (uint8_t *) addr - ao_lisp_pool;
153 static void *move_old, *move_new;
154 static int move_size;
162 DBG_MOVE("move %d -> %d\n", DBG_OFFSET(move_old), DBG_OFFSET(move_new));
164 memset(ao_lisp_moving, '\0', sizeof (ao_lisp_moving));
165 for (i = 0; i < AO_LISP_ROOT; i++)
166 if (ao_lisp_root[i].addr && *ao_lisp_root[i].addr) {
168 DBG_MOVE("root %d\n", DBG_OFFSET(*ao_lisp_root[i].addr));
169 new = ao_lisp_move(ao_lisp_root[i].type, *ao_lisp_root[i].addr);
171 *ao_lisp_root[i].addr = new;
174 DBG_MOVE("move done\n");
183 for (i = 0; i < ao_lisp_top; i += 4) {
186 else if ((i & 0x1f) == 0)
188 if (busy(ao_lisp_busy, i))
195 #define DUMP_BUSY() dump_busy()
201 ao_lisp_collect(void)
207 memset(ao_lisp_busy, '\0', sizeof (ao_lisp_busy));
209 for (i = 0; i < AO_LISP_ROOT; i++)
210 if (ao_lisp_root[i].addr && *ao_lisp_root[i].addr) {
211 DBG("root %p\n", *ao_lisp_root[i].addr);
212 ao_lisp_mark(ao_lisp_root[i].type, *ao_lisp_root[i].addr);
217 DBG("find first busy\n");
218 for (i = 0; i < ao_lisp_top; i += 4) {
219 if (!busy(ao_lisp_busy, i))
223 while(i < ao_lisp_top) {
224 if (busy(ao_lisp_busy, i)) {
225 DBG("busy %d -> %d\n", i, top);
226 move_old = &ao_lisp_pool[i];
227 move_new = &ao_lisp_pool[top];
230 DBG("\tbusy size %d\n", move_size);
233 clear_object(ao_lisp_busy, move_old, move_size);
234 mark_object(ao_lisp_busy, move_new, move_size);
247 ao_lisp_mark(const struct ao_lisp_type *type, void *addr)
251 if (mark_object(ao_lisp_busy, addr, type->size(addr)))
257 ao_lisp_mark_memory(void *addr, int size)
259 return mark_object(ao_lisp_busy, addr, size);
263 * After the object has been moved, we have to reference it
264 * in the new location. This is only relevant for ao_lisp_poly_move
265 * as it needs to fetch the type byte from the object, which
266 * may have been overwritten by the copy
269 ao_lisp_move_map(void *addr)
271 if (addr == move_old) {
272 if (busy_object(ao_lisp_moving, addr))
279 check_move(void *addr, int size)
281 if (addr == move_old) {
282 DBG_MOVE("mapping %d -> %d\n", DBG_OFFSET(addr), DBG_OFFSET(move_new));
283 if (!busy_object(ao_lisp_moving, addr)) {
284 DBG_MOVE(" copy %d\n", size);
285 memmove(move_new, move_old, size);
286 move_size = (size + 3) & ~3;
294 ao_lisp_move(const struct ao_lisp_type *type, void *addr)
297 int size = type->size(addr);
302 #ifndef AO_LISP_MAKE_CONST
303 if (AO_LISP_IS_CONST(addr))
306 DBG_MOVE("object %d\n", DBG_OFFSET(addr));
307 if (a < ao_lisp_pool || ao_lisp_pool + AO_LISP_POOL <= a)
310 addr = check_move(addr, size);
311 if (mark_object(ao_lisp_moving, addr, size)) {
312 DBG_MOVE("already moved\n");
317 DBG_MOVE("recursing...\n");
321 DBG_MOVE("done %d\n", DBG_OFFSET(addr));
326 ao_lisp_move_memory(void *addr, int size)
331 DBG_MOVE("memory %d\n", DBG_OFFSET(addr));
333 addr = check_move(addr, size);
334 if (mark_object(ao_lisp_moving, addr, size)) {
335 DBG_MOVE("already moved\n");
344 ao_lisp_alloc(int size)
348 size = ao_lisp_mem_round(size);
349 if (ao_lisp_top + size > AO_LISP_POOL) {
351 if (ao_lisp_top + size > AO_LISP_POOL) {
352 ao_lisp_exception |= AO_LISP_OOM;
356 addr = ao_lisp_pool + ao_lisp_top;
362 ao_lisp_root_add(const struct ao_lisp_type *type, void *addr)
365 DBG("add root type %p addr %p\n", type, addr);
366 for (i = 0; i < AO_LISP_ROOT; i++) {
367 if (!ao_lisp_root[i].addr) {
368 ao_lisp_root[i].addr = addr;
369 ao_lisp_root[i].type = type;
378 ao_lisp_root_clear(void *addr)
381 for (i = 0; i < AO_LISP_ROOT; i++) {
382 if (ao_lisp_root[i].addr == addr) {
383 ao_lisp_root[i].addr = 0;
384 ao_lisp_root[i].type = 0;