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)));
31 #define DBG_COLLECT_ALWAYS
40 #define DBG_OFFSET(a) ((int) ((uint8_t *) (a) - ao_lisp_pool))
41 #define DBG(...) printf(__VA_ARGS__)
44 static int move_depth;
45 #define DBG_RESET() (move_depth = 0)
46 #define DBG_MOVE(...) do { if(move_dump) { int d; for (d = 0; d < move_depth; d++) printf (" "); printf(__VA_ARGS__); } } while (0)
47 #define DBG_MOVE_IN() (move_depth++)
48 #define DBG_MOVE_OUT() (move_depth--)
55 #define DBG_MOVE_OUT()
58 uint8_t ao_lisp_exception;
62 const struct ao_lisp_type *type;
65 #define AO_LISP_ROOT 16
67 static struct ao_lisp_root ao_lisp_root[AO_LISP_ROOT];
69 static uint8_t ao_lisp_busy[AO_LISP_POOL / 32];
71 static uint8_t ao_lisp_moving[AO_LISP_POOL / 32];
75 static inline void mark(uint8_t *tag, int offset) {
76 int byte = offset >> 5;
77 int bit = (offset >> 2) & 7;
78 tag[byte] |= (1 << bit);
81 static inline void clear(uint8_t *tag, int offset) {
82 int byte = offset >> 5;
83 int bit = (offset >> 2) & 7;
84 tag[byte] &= ~(1 << bit);
87 static inline int busy(uint8_t *tag, int offset) {
88 int byte = offset >> 5;
89 int bit = (offset >> 2) & 7;
90 return (tag[byte] >> bit) & 1;
93 static inline int min(int a, int b) { return a < b ? a : b; }
94 static inline int max(int a, int b) { return a > b ? a : b; }
96 static inline int limit(int offset) {
97 return min(AO_LISP_POOL, max(offset, 0));
101 mark_object(uint8_t *tag, void *addr, int size) {
108 if ((uint8_t *) addr < ao_lisp_pool || ao_lisp_pool + AO_LISP_POOL <= (uint8_t*) addr)
111 base = (uint8_t *) addr - ao_lisp_pool;
115 bound = limit(bound);
118 while (base < bound) {
126 clear_object(uint8_t *tag, void *addr, int size) {
132 base = (uint8_t *) addr - ao_lisp_pool;
136 bound = limit(bound);
137 if (!busy(tag, base))
139 while (base < bound) {
147 busy_object(uint8_t *tag, void *addr) {
153 if ((uint8_t *) addr < ao_lisp_pool || ao_lisp_pool + AO_LISP_POOL <= (uint8_t*) addr)
156 base = (uint8_t *) addr - ao_lisp_pool;
163 static void *move_old, *move_new;
164 static int move_size;
172 DBG_MOVE("move %d -> %d\n", DBG_OFFSET(move_old), DBG_OFFSET(move_new));
174 memset(ao_lisp_moving, '\0', sizeof (ao_lisp_moving));
175 for (i = 0; i < AO_LISP_ROOT; i++) {
176 if (!ao_lisp_root[i].addr)
178 if (ao_lisp_root[i].type) {
179 DBG_DO(void *addr = *ao_lisp_root[i].addr);
180 DBG_MOVE("root %d\n", DBG_OFFSET(addr));
181 if (!ao_lisp_move(ao_lisp_root[i].type,
182 ao_lisp_root[i].addr))
183 DBG_MOVE("root moves from %p to %p\n",
185 *ao_lisp_root[i].addr);
187 DBG_DO(ao_poly p = *(ao_poly *) ao_lisp_root[i].addr);
188 if (!ao_lisp_poly_move((ao_poly *) ao_lisp_root[i].addr))
189 DBG_MOVE("root poly move from %04x to %04x\n",
190 p, *(ao_poly *) ao_lisp_root[i].addr);
194 DBG_MOVE("move done\n");
203 for (i = 0; i < ao_lisp_top; i += 4) {
206 else if ((i & 0x1f) == 0)
208 if (busy(ao_lisp_busy, i))
215 #define DUMP_BUSY() dump_busy()
221 ao_lisp_mark_busy(void)
225 memset(ao_lisp_busy, '\0', sizeof (ao_lisp_busy));
227 for (i = 0; i < AO_LISP_ROOT; i++) {
228 if (ao_lisp_root[i].type) {
229 void **a = ao_lisp_root[i].addr, *v;
232 ao_lisp_mark(ao_lisp_root[i].type, v);
235 ao_poly *a = (ao_poly *) ao_lisp_root[i].addr, p;
237 DBG("root %04x\n", p);
238 ao_lisp_poly_mark(p);
245 ao_lisp_collect(void)
256 DBG("find first busy\n");
257 for (i = 0; i < ao_lisp_top; i += 4) {
258 if (!busy(ao_lisp_busy, i))
262 while(i < ao_lisp_top) {
263 if (busy(ao_lisp_busy, i)) {
264 DBG("busy %d -> %d\n", i, top);
265 move_old = &ao_lisp_pool[i];
266 move_new = &ao_lisp_pool[top];
269 DBG("\tbusy size %d\n", move_size);
272 clear_object(ao_lisp_busy, move_old, move_size);
273 mark_object(ao_lisp_busy, move_new, move_size);
286 ao_lisp_mark(const struct ao_lisp_type *type, void *addr)
290 if (mark_object(ao_lisp_busy, addr, type->size(addr)))
297 ao_lisp_mark_memory(void *addr, int size)
299 return mark_object(ao_lisp_busy, addr, size);
303 * After the object has been moved, we have to reference it
304 * in the new location. This is only relevant for ao_lisp_poly_move
305 * as it needs to fetch the type byte from the object, which
306 * may have been overwritten by the copy
309 ao_lisp_move_map(void *addr)
311 if (addr == move_old) {
312 if (busy_object(ao_lisp_moving, addr))
319 check_move(void *addr, int size)
321 if (addr == move_old) {
322 DBG_MOVE("mapping %d -> %d\n", DBG_OFFSET(addr), DBG_OFFSET(move_new));
323 if (!busy_object(ao_lisp_moving, addr)) {
324 DBG_MOVE(" copy %d\n", size);
325 memmove(move_new, move_old, size);
326 move_size = (size + 3) & ~3;
334 ao_lisp_move(const struct ao_lisp_type *type, void **ref)
338 int size = type->size(addr);
343 #ifndef AO_LISP_MAKE_CONST
344 if (AO_LISP_IS_CONST(addr))
347 DBG_MOVE("object %d\n", DBG_OFFSET(addr));
348 if (a < ao_lisp_pool || ao_lisp_pool + AO_LISP_POOL <= a)
351 addr = check_move(addr, size);
354 if (mark_object(ao_lisp_moving, addr, size)) {
355 DBG_MOVE("already moved\n");
360 DBG_MOVE("recursing...\n");
364 DBG_MOVE("done %d\n", DBG_OFFSET(addr));
369 ao_lisp_move_memory(void **ref, int size)
375 DBG_MOVE("memory %d\n", DBG_OFFSET(addr));
377 addr = check_move(addr, size);
380 if (mark_object(ao_lisp_moving, addr, size)) {
381 DBG_MOVE("already moved\n");
390 static int AO_LISP_POOL_CUR = AO_LISP_POOL / 8;
399 for (i = 0; i < AO_LISP_POOL_CUR; i += 4) {
400 uint32_t *a = (uint32_t *) &ao_lisp_pool[i];
401 if (!busy_object(ao_lisp_busy, a))
404 for (i = 0; i < AO_LISP_POOL_CUR; i += 2) {
405 ao_poly *a = (uint16_t *) &ao_lisp_pool[i];
408 if (!ao_lisp_is_const(p)) {
409 void *r = ao_lisp_ref(p);
411 if (ao_lisp_pool <= (uint8_t *) r &&
412 (uint8_t *) r <= ao_lisp_pool + AO_LISP_POOL_CUR)
414 if (!busy_object(ao_lisp_busy, r)) {
415 printf("missing reference from %d to %d\n",
416 (int) ((uint8_t *) a - ao_lisp_pool),
417 (int) ((uint8_t *) r - ao_lisp_pool));
425 #define AO_LISP_POOL_CUR AO_LISP_POOL
429 ao_lisp_alloc(int size)
433 size = ao_lisp_mem_round(size);
434 #ifdef DBG_COLLECT_ALWAYS
437 if (ao_lisp_top + size > AO_LISP_POOL_CUR) {
439 if (AO_LISP_POOL_CUR < AO_LISP_POOL) {
440 AO_LISP_POOL_CUR += AO_LISP_POOL / 8;
449 for (i = ao_lisp_top; i < AO_LISP_POOL; i += 4) {
450 uint32_t *p = (uint32_t *) &ao_lisp_pool[i];
456 if (ao_lisp_top + size > AO_LISP_POOL) {
457 ao_lisp_exception |= AO_LISP_OOM;
461 addr = ao_lisp_pool + ao_lisp_top;
467 ao_lisp_root_add(const struct ao_lisp_type *type, void *addr)
470 DBG("add root type %p addr %p\n", type, addr);
471 for (i = 0; i < AO_LISP_ROOT; i++) {
472 if (!ao_lisp_root[i].addr) {
473 ao_lisp_root[i].addr = addr;
474 ao_lisp_root[i].type = type;
483 ao_lisp_root_poly_add(ao_poly *p)
485 return ao_lisp_root_add(NULL, p);
489 ao_lisp_root_clear(void *addr)
492 for (i = 0; i < AO_LISP_ROOT; i++) {
493 if (ao_lisp_root[i].addr == addr) {
494 ao_lisp_root[i].addr = 0;
495 ao_lisp_root[i].type = 0;