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,
265 [AO_LISP_LAMBDA] = &ao_lisp_lambda_type,
270 ao_lisp_mark_busy(void)
274 memset(ao_lisp_busy, '\0', sizeof (ao_lisp_busy));
275 memset(ao_lisp_cons, '\0', sizeof (ao_lisp_cons));
276 ao_lisp_cons_noted = 0;
278 for (i = 0; i < AO_LISP_ROOT; i++) {
279 if (ao_lisp_root[i].type) {
280 void **a = ao_lisp_root[i].addr, *v;
282 DBG("root %d\n", DBG_OFFSET(v));
283 ao_lisp_mark(ao_lisp_root[i].type, v);
286 ao_poly *a = (ao_poly *) ao_lisp_root[i].addr, p;
288 DBG("root 0x%04x\n", p);
289 ao_lisp_poly_mark(p, 0);
293 while (ao_lisp_cons_noted) {
294 memcpy(ao_lisp_cons_last, ao_lisp_cons, sizeof (ao_lisp_cons));
295 memset(ao_lisp_cons, '\0', sizeof (ao_lisp_cons));
296 ao_lisp_cons_noted = 0;
297 for (i = 0; i < AO_LISP_POOL; i += 4) {
298 if (busy(ao_lisp_cons_last, i)) {
299 void *v = ao_lisp_pool + i;
300 DBG("cons %d\n", DBG_OFFSET(v));
301 ao_lisp_mark(&ao_lisp_cons_type, v);
308 ao_lisp_collect(void)
319 DBG("find first busy\n");
320 for (i = 0; i < ao_lisp_top; i += 4) {
321 if (!busy(ao_lisp_busy, i))
325 while(i < ao_lisp_top) {
326 if (busy(ao_lisp_busy, i)) {
327 DBG("busy %d -> %d\n", i, top);
328 move_old = &ao_lisp_pool[i];
329 move_new = &ao_lisp_pool[top];
332 DBG("\tbusy size %d\n", move_size);
335 clear_object(ao_lisp_busy, move_old, move_size);
336 mark_object(ao_lisp_busy, move_new, move_size);
337 if (busy_object(ao_lisp_cons, move_old)) {
338 clear_object(ao_lisp_cons, move_old, move_size);
339 mark_object(ao_lisp_cons, move_new, move_size);
353 ao_lisp_mark(const struct ao_lisp_type *type, void *addr)
357 if (mark_object(ao_lisp_busy, addr, type->size(addr)))
364 ao_lisp_poly_mark(ao_poly p, uint8_t do_note_cons)
366 uint8_t type = ao_lisp_poly_type(p);
370 if (type == AO_LISP_CONS && do_note_cons) {
371 note_cons(ao_lisp_ref(p));
374 const struct ao_lisp_type *lisp_type = ao_lisp_types[ao_lisp_poly_type(p)];
376 return ao_lisp_mark(lisp_type, ao_lisp_ref(p));
382 ao_lisp_mark_memory(void *addr, int size)
384 return mark_object(ao_lisp_busy, addr, size);
388 * After the object has been moved, we have to reference it
389 * in the new location. This is only relevant for ao_lisp_poly_move
390 * as it needs to fetch the type byte from the object, which
391 * may have been overwritten by the copy
394 ao_lisp_move_map(void *addr)
396 if (addr == move_old) {
397 if (busy_object(ao_lisp_moving, addr))
404 check_move(void *addr, int size)
406 if (addr == move_old) {
407 DBG_MOVE("mapping %d -> %d\n", DBG_OFFSET(addr), DBG_OFFSET(move_new));
408 if (!busy_object(ao_lisp_moving, addr)) {
409 DBG_MOVE(" copy %d\n", size);
410 memmove(move_new, move_old, size);
411 move_size = (size + 3) & ~3;
419 ao_lisp_move(const struct ao_lisp_type *type, void **ref)
423 int size = type->size(addr);
428 #ifndef AO_LISP_MAKE_CONST
429 if (AO_LISP_IS_CONST(addr))
432 DBG_MOVE("object %d\n", DBG_OFFSET(addr));
433 if (!AO_LISP_IS_POOL(a))
436 addr = check_move(addr, size);
439 if (mark_object(ao_lisp_moving, addr, size)) {
440 DBG_MOVE("already moved\n");
445 DBG_MOVE("recursing...\n");
449 DBG_MOVE("done %d\n", DBG_OFFSET(addr));
454 ao_lisp_move_memory(void **ref, int size)
460 DBG_MOVE("memory %d\n", DBG_OFFSET(addr));
462 addr = check_move(addr, size);
465 if (mark_object(ao_lisp_moving, addr, size)) {
466 DBG_MOVE("already moved\n");
475 ao_lisp_poly_move(ao_poly *ref, uint8_t do_note_cons)
479 const struct ao_lisp_type *lisp_type;
486 type = ao_lisp_poly_base_type(p);
487 addr = ao_lisp_ref(p);
488 if (type == AO_LISP_CONS && do_note_cons) {
490 addr = check_move(addr, sizeof (struct ao_lisp_cons));
494 if (type == AO_LISP_OTHER)
495 type = ao_lisp_other_type(ao_lisp_move_map(ao_lisp_poly_other(p)));
497 if (type >= AO_LISP_NUM_TYPE)
500 lisp_type = ao_lisp_types[type];
503 ret = ao_lisp_move(lisp_type, &addr);
506 if (addr != ao_lisp_ref(p)) {
507 ao_poly np = ao_lisp_poly(addr, p & AO_LISP_TYPE_MASK);
508 DBG("poly %d moved %04x -> %04x\n",
516 static int AO_LISP_POOL_CUR = AO_LISP_POOL / 8;
525 for (i = 0; i < AO_LISP_POOL_CUR; i += 4) {
526 uint32_t *a = (uint32_t *) &ao_lisp_pool[i];
527 if (!busy_object(ao_lisp_busy, a))
530 for (i = 0; i < AO_LISP_POOL_CUR; i += 2) {
531 ao_poly *a = (uint16_t *) &ao_lisp_pool[i];
534 if (!ao_lisp_is_const(p)) {
535 void *r = ao_lisp_ref(p);
537 if (ao_lisp_pool <= (uint8_t *) r &&
538 (uint8_t *) r <= ao_lisp_pool + AO_LISP_POOL_CUR)
540 if (!busy_object(ao_lisp_busy, r)) {
541 printf("missing reference from %d to %d\n",
542 (int) ((uint8_t *) a - ao_lisp_pool),
543 (int) ((uint8_t *) r - ao_lisp_pool));
551 #define AO_LISP_POOL_CUR AO_LISP_POOL
555 ao_lisp_alloc(int size)
559 size = ao_lisp_mem_round(size);
560 #ifdef DBG_COLLECT_ALWAYS
563 if (ao_lisp_top + size > AO_LISP_POOL_CUR) {
565 if (AO_LISP_POOL_CUR < AO_LISP_POOL) {
566 AO_LISP_POOL_CUR += AO_LISP_POOL / 8;
575 for (i = ao_lisp_top; i < AO_LISP_POOL; i += 4) {
576 uint32_t *p = (uint32_t *) &ao_lisp_pool[i];
582 if (ao_lisp_top + size > AO_LISP_POOL) {
583 ao_lisp_exception |= AO_LISP_OOM;
587 addr = ao_lisp_pool + ao_lisp_top;
593 ao_lisp_root_add(const struct ao_lisp_type *type, void *addr)
596 DBG("add root type %p addr %p\n", type, addr);
597 for (i = 0; i < AO_LISP_ROOT; i++) {
598 if (!ao_lisp_root[i].addr) {
599 ao_lisp_root[i].addr = addr;
600 ao_lisp_root[i].type = type;
609 ao_lisp_root_poly_add(ao_poly *p)
611 return ao_lisp_root_add(NULL, p);
615 ao_lisp_root_clear(void *addr)
618 for (i = 0; i < AO_LISP_ROOT; i++) {
619 if (ao_lisp_root[i].addr == addr) {
620 ao_lisp_root[i].addr = 0;
621 ao_lisp_root[i].type = 0;