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
41 #define DBG_OFFSET(a) ((int) ((uint8_t *) (a) - ao_lisp_pool))
42 #define DBG(...) printf(__VA_ARGS__)
45 static int move_depth;
46 #define DBG_RESET() (move_depth = 0)
47 #define DBG_MOVE(...) do { if(move_dump) { int d; for (d = 0; d < move_depth; d++) printf (" "); printf(__VA_ARGS__); } } while (0)
48 #define DBG_MOVE_IN() (move_depth++)
49 #define DBG_MOVE_OUT() (move_depth--)
56 #define DBG_MOVE_OUT()
59 uint8_t ao_lisp_exception;
63 const struct ao_lisp_type *type;
66 #define AO_LISP_ROOT 16
68 static struct ao_lisp_root ao_lisp_root[AO_LISP_ROOT];
70 static uint8_t ao_lisp_busy[AO_LISP_POOL / 32];
72 static uint8_t ao_lisp_moving[AO_LISP_POOL / 32];
76 static inline void mark(uint8_t *tag, int offset) {
77 int byte = offset >> 5;
78 int bit = (offset >> 2) & 7;
79 tag[byte] |= (1 << bit);
82 static inline void clear(uint8_t *tag, int offset) {
83 int byte = offset >> 5;
84 int bit = (offset >> 2) & 7;
85 tag[byte] &= ~(1 << bit);
88 static inline int busy(uint8_t *tag, int offset) {
89 int byte = offset >> 5;
90 int bit = (offset >> 2) & 7;
91 return (tag[byte] >> bit) & 1;
94 static inline int min(int a, int b) { return a < b ? a : b; }
95 static inline int max(int a, int b) { return a > b ? a : b; }
97 static inline int limit(int offset) {
98 return min(AO_LISP_POOL, max(offset, 0));
102 mark_object(uint8_t *tag, void *addr, int size) {
109 if ((uint8_t *) addr < ao_lisp_pool || ao_lisp_pool + AO_LISP_POOL <= (uint8_t*) addr)
112 base = (uint8_t *) addr - ao_lisp_pool;
116 bound = limit(bound);
119 while (base < bound) {
127 clear_object(uint8_t *tag, void *addr, int size) {
133 base = (uint8_t *) addr - ao_lisp_pool;
137 bound = limit(bound);
138 if (!busy(tag, base))
140 while (base < bound) {
148 busy_object(uint8_t *tag, void *addr) {
154 if ((uint8_t *) addr < ao_lisp_pool || ao_lisp_pool + AO_LISP_POOL <= (uint8_t*) addr)
157 base = (uint8_t *) addr - ao_lisp_pool;
164 static void *move_old, *move_new;
165 static int move_size;
173 DBG_MOVE("move %d -> %d\n", DBG_OFFSET(move_old), DBG_OFFSET(move_new));
175 memset(ao_lisp_moving, '\0', sizeof (ao_lisp_moving));
176 for (i = 0; i < AO_LISP_ROOT; i++) {
177 if (!ao_lisp_root[i].addr)
179 if (ao_lisp_root[i].type) {
180 DBG_DO(void *addr = *ao_lisp_root[i].addr);
181 DBG_MOVE("root %d\n", DBG_OFFSET(addr));
182 if (!ao_lisp_move(ao_lisp_root[i].type,
183 ao_lisp_root[i].addr)) {
184 DBG_MOVE("root moves from %p to %p\n",
186 *ao_lisp_root[i].addr);
189 DBG_DO(ao_poly p = *(ao_poly *) ao_lisp_root[i].addr);
190 if (!ao_lisp_poly_move((ao_poly *) ao_lisp_root[i].addr)) {
191 DBG_MOVE("root poly move from %04x to %04x\n",
192 p, *(ao_poly *) ao_lisp_root[i].addr);
197 DBG_MOVE("move done\n");
206 for (i = 0; i < ao_lisp_top; i += 4) {
209 else if ((i & 0x1f) == 0)
211 if (busy(ao_lisp_busy, i))
218 #define DUMP_BUSY() dump_busy()
224 ao_lisp_mark_busy(void)
228 memset(ao_lisp_busy, '\0', sizeof (ao_lisp_busy));
230 for (i = 0; i < AO_LISP_ROOT; i++) {
231 if (ao_lisp_root[i].type) {
232 void **a = ao_lisp_root[i].addr, *v;
235 ao_lisp_mark(ao_lisp_root[i].type, v);
238 ao_poly *a = (ao_poly *) ao_lisp_root[i].addr, p;
240 DBG("root %04x\n", p);
241 ao_lisp_poly_mark(p);
248 ao_lisp_collect(void)
259 DBG("find first busy\n");
260 for (i = 0; i < ao_lisp_top; i += 4) {
261 if (!busy(ao_lisp_busy, i))
265 while(i < ao_lisp_top) {
266 if (busy(ao_lisp_busy, i)) {
267 DBG("busy %d -> %d\n", i, top);
268 move_old = &ao_lisp_pool[i];
269 move_new = &ao_lisp_pool[top];
272 DBG("\tbusy size %d\n", move_size);
275 clear_object(ao_lisp_busy, move_old, move_size);
276 mark_object(ao_lisp_busy, move_new, move_size);
289 ao_lisp_mark(const struct ao_lisp_type *type, void *addr)
293 if (mark_object(ao_lisp_busy, addr, type->size(addr)))
300 ao_lisp_mark_memory(void *addr, int size)
302 return mark_object(ao_lisp_busy, addr, size);
306 * After the object has been moved, we have to reference it
307 * in the new location. This is only relevant for ao_lisp_poly_move
308 * as it needs to fetch the type byte from the object, which
309 * may have been overwritten by the copy
312 ao_lisp_move_map(void *addr)
314 if (addr == move_old) {
315 if (busy_object(ao_lisp_moving, addr))
322 check_move(void *addr, int size)
324 if (addr == move_old) {
325 DBG_MOVE("mapping %d -> %d\n", DBG_OFFSET(addr), DBG_OFFSET(move_new));
326 if (!busy_object(ao_lisp_moving, addr)) {
327 DBG_MOVE(" copy %d\n", size);
328 memmove(move_new, move_old, size);
329 move_size = (size + 3) & ~3;
337 ao_lisp_move(const struct ao_lisp_type *type, void **ref)
341 int size = type->size(addr);
346 #ifndef AO_LISP_MAKE_CONST
347 if (AO_LISP_IS_CONST(addr))
350 DBG_MOVE("object %d\n", DBG_OFFSET(addr));
351 if (a < ao_lisp_pool || ao_lisp_pool + AO_LISP_POOL <= a)
354 addr = check_move(addr, size);
357 if (mark_object(ao_lisp_moving, addr, size)) {
358 DBG_MOVE("already moved\n");
363 DBG_MOVE("recursing...\n");
367 DBG_MOVE("done %d\n", DBG_OFFSET(addr));
372 ao_lisp_move_memory(void **ref, int size)
378 DBG_MOVE("memory %d\n", DBG_OFFSET(addr));
380 addr = check_move(addr, size);
383 if (mark_object(ao_lisp_moving, addr, size)) {
384 DBG_MOVE("already moved\n");
393 static int AO_LISP_POOL_CUR = AO_LISP_POOL / 8;
402 for (i = 0; i < AO_LISP_POOL_CUR; i += 4) {
403 uint32_t *a = (uint32_t *) &ao_lisp_pool[i];
404 if (!busy_object(ao_lisp_busy, a))
407 for (i = 0; i < AO_LISP_POOL_CUR; i += 2) {
408 ao_poly *a = (uint16_t *) &ao_lisp_pool[i];
411 if (!ao_lisp_is_const(p)) {
412 void *r = ao_lisp_ref(p);
414 if (ao_lisp_pool <= (uint8_t *) r &&
415 (uint8_t *) r <= ao_lisp_pool + AO_LISP_POOL_CUR)
417 if (!busy_object(ao_lisp_busy, r)) {
418 printf("missing reference from %d to %d\n",
419 (int) ((uint8_t *) a - ao_lisp_pool),
420 (int) ((uint8_t *) r - ao_lisp_pool));
428 #define AO_LISP_POOL_CUR AO_LISP_POOL
432 ao_lisp_alloc(int size)
436 size = ao_lisp_mem_round(size);
437 #ifdef DBG_COLLECT_ALWAYS
440 if (ao_lisp_top + size > AO_LISP_POOL_CUR) {
442 if (AO_LISP_POOL_CUR < AO_LISP_POOL) {
443 AO_LISP_POOL_CUR += AO_LISP_POOL / 8;
452 for (i = ao_lisp_top; i < AO_LISP_POOL; i += 4) {
453 uint32_t *p = (uint32_t *) &ao_lisp_pool[i];
459 if (ao_lisp_top + size > AO_LISP_POOL) {
460 ao_lisp_exception |= AO_LISP_OOM;
464 addr = ao_lisp_pool + ao_lisp_top;
470 ao_lisp_root_add(const struct ao_lisp_type *type, void *addr)
473 DBG("add root type %p addr %p\n", type, addr);
474 for (i = 0; i < AO_LISP_ROOT; i++) {
475 if (!ao_lisp_root[i].addr) {
476 ao_lisp_root[i].addr = addr;
477 ao_lisp_root[i].type = type;
486 ao_lisp_root_poly_add(ao_poly *p)
488 return ao_lisp_root_add(NULL, p);
492 ao_lisp_root_clear(void *addr)
495 for (i = 0; i < AO_LISP_ROOT; i++) {
496 if (ao_lisp_root[i].addr == addr) {
497 ao_lisp_root[i].addr = 0;
498 ao_lisp_root[i].type = 0;