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__)
44 static int move_dump = 1;
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];
71 static uint8_t ao_lisp_moving[AO_LISP_POOL / 32];
72 static uint8_t ao_lisp_cons[AO_LISP_POOL / 32];
73 static uint8_t ao_lisp_cons_last[AO_LISP_POOL / 32];
74 static uint8_t ao_lisp_cons_noted;
78 static inline void mark(uint8_t *tag, int offset) {
79 int byte = offset >> 5;
80 int bit = (offset >> 2) & 7;
81 tag[byte] |= (1 << bit);
84 static inline void clear(uint8_t *tag, int offset) {
85 int byte = offset >> 5;
86 int bit = (offset >> 2) & 7;
87 tag[byte] &= ~(1 << bit);
90 static inline int busy(uint8_t *tag, int offset) {
91 int byte = offset >> 5;
92 int bit = (offset >> 2) & 7;
93 return (tag[byte] >> bit) & 1;
96 static inline int min(int a, int b) { return a < b ? a : b; }
97 static inline int max(int a, int b) { return a > b ? a : b; }
99 static inline int limit(int offset) {
100 return min(AO_LISP_POOL, max(offset, 0));
104 mark_object(uint8_t *tag, void *addr, int size) {
111 if ((uint8_t *) addr < ao_lisp_pool || ao_lisp_pool + AO_LISP_POOL <= (uint8_t*) addr)
114 base = (uint8_t *) addr - ao_lisp_pool;
118 bound = limit(bound);
121 while (base < bound) {
129 clear_object(uint8_t *tag, void *addr, int size) {
135 base = (uint8_t *) addr - ao_lisp_pool;
139 bound = limit(bound);
140 if (!busy(tag, base))
142 while (base < bound) {
150 busy_object(uint8_t *tag, void *addr) {
156 if ((uint8_t *) addr < ao_lisp_pool || ao_lisp_pool + AO_LISP_POOL <= (uint8_t*) addr)
159 base = (uint8_t *) addr - ao_lisp_pool;
167 note_cons(void *addr)
169 DBG_MOVE("note cons %d\n", DBG_OFFSET(addr));
170 if (AO_LISP_IS_POOL(addr)) {
171 ao_lisp_cons_noted = 1;
172 mark(ao_lisp_cons, (uint8_t *) addr - ao_lisp_pool);
177 static void *move_old, *move_new;
178 static int move_size;
186 DBG_MOVE("move %d -> %d\n", DBG_OFFSET(move_old), DBG_OFFSET(move_new));
188 memset(ao_lisp_moving, '\0', sizeof (ao_lisp_moving));
189 memset(ao_lisp_cons, '\0', sizeof (ao_lisp_cons));
190 ao_lisp_cons_noted = 0;
191 for (i = 0; i < AO_LISP_ROOT; i++) {
192 if (!ao_lisp_root[i].addr)
194 if (ao_lisp_root[i].type) {
195 void *addr = *ao_lisp_root[i].addr;
198 DBG_MOVE("root %d\n", DBG_OFFSET(addr));
199 if (!ao_lisp_move(ao_lisp_root[i].type,
200 ao_lisp_root[i].addr)) {
201 DBG_MOVE("root moves from %p to %p\n",
203 *ao_lisp_root[i].addr);
206 ao_poly p = *(ao_poly *) ao_lisp_root[i].addr;
209 if (!ao_lisp_poly_move((ao_poly *) ao_lisp_root[i].addr, 0)) {
210 DBG_MOVE("root poly move from %04x to %04x\n",
211 p, *(ao_poly *) ao_lisp_root[i].addr);
215 while (ao_lisp_cons_noted) {
216 memcpy(ao_lisp_cons_last, ao_lisp_cons, sizeof (ao_lisp_cons));
217 memset(ao_lisp_cons, '\0', sizeof (ao_lisp_cons));
218 ao_lisp_cons_noted = 0;
219 for (i = 0; i < AO_LISP_POOL; i += 4) {
220 if (busy(ao_lisp_cons_last, i)) {
221 void *addr = ao_lisp_pool + i;
222 DBG_MOVE("cons %d\n", DBG_OFFSET(addr));
223 if (!ao_lisp_move(&ao_lisp_cons_type, &addr)) {
224 DBG_MOVE("cons moves from %p to %p\n",
225 ao_lisp_pool + i, addr);
231 DBG_MOVE("move done\n");
240 for (i = 0; i < ao_lisp_top; i += 4) {
243 else if ((i & 0x1f) == 0)
245 if (busy(ao_lisp_busy, i))
252 #define DUMP_BUSY() dump_busy()
257 static const struct ao_lisp_type const *ao_lisp_types[AO_LISP_NUM_TYPE] = {
258 [AO_LISP_CONS] = &ao_lisp_cons_type,
259 [AO_LISP_INT] = NULL,
260 [AO_LISP_STRING] = &ao_lisp_string_type,
261 [AO_LISP_OTHER] = (void *) 0x1,
262 [AO_LISP_ATOM] = &ao_lisp_atom_type,
263 [AO_LISP_BUILTIN] = &ao_lisp_builtin_type,
264 [AO_LISP_FRAME] = &ao_lisp_frame_type,
269 ao_lisp_mark_busy(void)
273 memset(ao_lisp_busy, '\0', sizeof (ao_lisp_busy));
274 memset(ao_lisp_cons, '\0', sizeof (ao_lisp_cons));
275 ao_lisp_cons_noted = 0;
277 for (i = 0; i < AO_LISP_ROOT; i++) {
278 if (ao_lisp_root[i].type) {
279 void **a = ao_lisp_root[i].addr, *v;
281 DBG("root %d\n", DBG_OFFSET(v));
282 ao_lisp_mark(ao_lisp_root[i].type, v);
285 ao_poly *a = (ao_poly *) ao_lisp_root[i].addr, p;
287 DBG("root 0x%04x\n", p);
288 ao_lisp_poly_mark(p, 0);
292 while (ao_lisp_cons_noted) {
293 memcpy(ao_lisp_cons_last, ao_lisp_cons, sizeof (ao_lisp_cons));
294 memset(ao_lisp_cons, '\0', sizeof (ao_lisp_cons));
295 ao_lisp_cons_noted = 0;
296 for (i = 0; i < AO_LISP_POOL; i += 4) {
297 if (busy(ao_lisp_cons_last, i)) {
298 void *v = ao_lisp_pool + i;
299 DBG("cons %d\n", DBG_OFFSET(v));
300 ao_lisp_mark(&ao_lisp_cons_type, v);
307 ao_lisp_collect(void)
318 DBG("find first busy\n");
319 for (i = 0; i < ao_lisp_top; i += 4) {
320 if (!busy(ao_lisp_busy, i))
324 while(i < ao_lisp_top) {
325 if (busy(ao_lisp_busy, i)) {
326 DBG("busy %d -> %d\n", i, top);
327 move_old = &ao_lisp_pool[i];
328 move_new = &ao_lisp_pool[top];
331 DBG("\tbusy size %d\n", move_size);
334 clear_object(ao_lisp_busy, move_old, move_size);
335 mark_object(ao_lisp_busy, move_new, move_size);
336 if (busy_object(ao_lisp_cons, move_old)) {
337 clear_object(ao_lisp_cons, move_old, move_size);
338 mark_object(ao_lisp_cons, move_new, move_size);
352 ao_lisp_mark(const struct ao_lisp_type *type, void *addr)
356 if (mark_object(ao_lisp_busy, addr, type->size(addr)))
363 ao_lisp_poly_mark(ao_poly p, uint8_t do_note_cons)
365 uint8_t type = ao_lisp_poly_type(p);
369 if (type == AO_LISP_CONS && do_note_cons) {
370 note_cons(ao_lisp_ref(p));
373 const struct ao_lisp_type *lisp_type = ao_lisp_types[ao_lisp_poly_type(p)];
375 return ao_lisp_mark(lisp_type, ao_lisp_ref(p));
381 ao_lisp_mark_memory(void *addr, int size)
383 return mark_object(ao_lisp_busy, addr, size);
387 * After the object has been moved, we have to reference it
388 * in the new location. This is only relevant for ao_lisp_poly_move
389 * as it needs to fetch the type byte from the object, which
390 * may have been overwritten by the copy
393 ao_lisp_move_map(void *addr)
395 if (addr == move_old) {
396 if (busy_object(ao_lisp_moving, addr))
403 check_move(void *addr, int size)
405 if (addr == move_old) {
406 DBG_MOVE("mapping %d -> %d\n", DBG_OFFSET(addr), DBG_OFFSET(move_new));
407 if (!busy_object(ao_lisp_moving, addr)) {
408 DBG_MOVE(" copy %d\n", size);
409 memmove(move_new, move_old, size);
410 move_size = (size + 3) & ~3;
418 ao_lisp_move(const struct ao_lisp_type *type, void **ref)
422 int size = type->size(addr);
427 #ifndef AO_LISP_MAKE_CONST
428 if (AO_LISP_IS_CONST(addr))
431 DBG_MOVE("object %d\n", DBG_OFFSET(addr));
432 if (!AO_LISP_IS_POOL(a))
435 addr = check_move(addr, size);
438 if (mark_object(ao_lisp_moving, addr, size)) {
439 DBG_MOVE("already moved\n");
444 DBG_MOVE("recursing...\n");
448 DBG_MOVE("done %d\n", DBG_OFFSET(addr));
453 ao_lisp_move_memory(void **ref, int size)
459 DBG_MOVE("memory %d\n", DBG_OFFSET(addr));
461 addr = check_move(addr, size);
464 if (mark_object(ao_lisp_moving, addr, size)) {
465 DBG_MOVE("already moved\n");
474 ao_lisp_poly_move(ao_poly *ref, uint8_t do_note_cons)
478 const struct ao_lisp_type *lisp_type;
485 type = ao_lisp_poly_base_type(p);
486 addr = ao_lisp_ref(p);
487 if (type == AO_LISP_CONS && do_note_cons) {
489 addr = check_move(addr, sizeof (struct ao_lisp_cons));
493 if (type == AO_LISP_OTHER)
494 type = ao_lisp_other_type(ao_lisp_move_map(ao_lisp_poly_other(p)));
496 if (type >= AO_LISP_NUM_TYPE)
499 lisp_type = ao_lisp_types[type];
502 ret = ao_lisp_move(lisp_type, &addr);
505 if (addr != ao_lisp_ref(p)) {
506 ao_poly np = ao_lisp_poly(addr, p & AO_LISP_TYPE_MASK);
507 DBG("poly %d moved %04x -> %04x\n",
515 static int AO_LISP_POOL_CUR = AO_LISP_POOL / 8;
524 for (i = 0; i < AO_LISP_POOL_CUR; i += 4) {
525 uint32_t *a = (uint32_t *) &ao_lisp_pool[i];
526 if (!busy_object(ao_lisp_busy, a))
529 for (i = 0; i < AO_LISP_POOL_CUR; i += 2) {
530 ao_poly *a = (uint16_t *) &ao_lisp_pool[i];
533 if (!ao_lisp_is_const(p)) {
534 void *r = ao_lisp_ref(p);
536 if (ao_lisp_pool <= (uint8_t *) r &&
537 (uint8_t *) r <= ao_lisp_pool + AO_LISP_POOL_CUR)
539 if (!busy_object(ao_lisp_busy, r)) {
540 printf("missing reference from %d to %d\n",
541 (int) ((uint8_t *) a - ao_lisp_pool),
542 (int) ((uint8_t *) r - ao_lisp_pool));
550 #define AO_LISP_POOL_CUR AO_LISP_POOL
554 ao_lisp_alloc(int size)
558 size = ao_lisp_mem_round(size);
559 #ifdef DBG_COLLECT_ALWAYS
562 if (ao_lisp_top + size > AO_LISP_POOL_CUR) {
564 if (AO_LISP_POOL_CUR < AO_LISP_POOL) {
565 AO_LISP_POOL_CUR += AO_LISP_POOL / 8;
574 for (i = ao_lisp_top; i < AO_LISP_POOL; i += 4) {
575 uint32_t *p = (uint32_t *) &ao_lisp_pool[i];
581 if (ao_lisp_top + size > AO_LISP_POOL) {
582 ao_lisp_exception |= AO_LISP_OOM;
586 addr = ao_lisp_pool + ao_lisp_top;
592 ao_lisp_root_add(const struct ao_lisp_type *type, void *addr)
595 DBG("add root type %p addr %p\n", type, addr);
596 for (i = 0; i < AO_LISP_ROOT; i++) {
597 if (!ao_lisp_root[i].addr) {
598 ao_lisp_root[i].addr = addr;
599 ao_lisp_root[i].type = type;
608 ao_lisp_root_poly_add(ao_poly *p)
610 return ao_lisp_root_add(NULL, p);
614 ao_lisp_root_clear(void *addr)
617 for (i = 0; i < AO_LISP_ROOT; i++) {
618 if (ao_lisp_root[i].addr == addr) {
619 ao_lisp_root[i].addr = 0;
620 ao_lisp_root[i].type = 0;