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 + AO_LISP_POOL_EXTRA] __attribute__((aligned(4)));
31 #define MDBG_COLLECT_ALWAYS
42 #define MDBG_MOVE(...) do { int d; for (d = 0; d < move_depth; d++) printf (" "); printf(__VA_ARGS__); } while (0)
44 #define MDBG_OFFSET(a) ((int) ((uint8_t *) (a) - ao_lisp_pool))
45 #define MDBG(...) printf(__VA_ARGS__)
47 static int move_depth;
48 #define MDBG_MOVE_IN() (move_depth++)
49 #define MDBG_MOVE_OUT() (assert(--move_depth >= 0))
53 #define MDBG_MOVE(...)
54 #define MDBG_MOVE_IN()
55 #define MDBG_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 #define AO_LISP_BUSY_SIZE ((AO_LISP_POOL + 31) / 32)
71 static uint8_t ao_lisp_busy[AO_LISP_BUSY_SIZE];
72 static uint8_t ao_lisp_moving[AO_LISP_BUSY_SIZE];
73 static uint8_t ao_lisp_cons_note[AO_LISP_BUSY_SIZE];
74 static uint8_t ao_lisp_cons_last[AO_LISP_BUSY_SIZE];
75 static uint8_t ao_lisp_cons_noted;
79 static inline void mark(uint8_t *tag, int offset) {
80 int byte = offset >> 5;
81 int bit = (offset >> 2) & 7;
82 tag[byte] |= (1 << bit);
85 static inline void clear(uint8_t *tag, int offset) {
86 int byte = offset >> 5;
87 int bit = (offset >> 2) & 7;
88 tag[byte] &= ~(1 << bit);
91 static inline int busy(uint8_t *tag, int offset) {
92 int byte = offset >> 5;
93 int bit = (offset >> 2) & 7;
94 return (tag[byte] >> bit) & 1;
97 static inline int min(int a, int b) { return a < b ? a : b; }
98 static inline int max(int a, int b) { return a > b ? a : b; }
100 static inline int limit(int offset) {
101 return min(AO_LISP_POOL, max(offset, 0));
105 mark_object(uint8_t *tag, void *addr, int size) {
112 if ((uint8_t *) addr < ao_lisp_pool || ao_lisp_pool + AO_LISP_POOL <= (uint8_t*) addr)
115 base = (uint8_t *) addr - ao_lisp_pool;
119 bound = limit(bound);
122 while (base < bound) {
130 clear_object(uint8_t *tag, void *addr, int size) {
136 base = (uint8_t *) addr - ao_lisp_pool;
140 bound = limit(bound);
141 if (!busy(tag, base))
143 while (base < bound) {
151 busy_object(uint8_t *tag, void *addr) {
157 if ((uint8_t *) addr < ao_lisp_pool || ao_lisp_pool + AO_LISP_POOL <= (uint8_t*) addr)
160 base = (uint8_t *) addr - ao_lisp_pool;
168 note_cons(void *addr)
170 if (AO_LISP_IS_POOL(addr)) {
171 int offset = (uint8_t *) addr - ao_lisp_pool;
172 MDBG_MOVE("note cons %d\n", MDBG_OFFSET(addr));
173 ao_lisp_cons_noted = 1;
174 mark(ao_lisp_cons_note, offset);
179 * Walk all referenced objects calling functions on each one
184 int (*visit_addr)(const struct ao_lisp_type *type, void **addr),
185 int (*visit_poly)(ao_poly *p, uint8_t do_note_cons))
189 memset(tag, '\0', sizeof (ao_lisp_busy));
190 memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
191 ao_lisp_cons_noted = 0;
192 for (i = 0; i < AO_LISP_ROOT; i++) {
193 if (ao_lisp_root[i].type) {
194 void **a = ao_lisp_root[i].addr, *v;
196 MDBG("root ptr %d\n", MDBG_OFFSET(v));
197 visit_addr(ao_lisp_root[i].type, a);
200 ao_poly *a = (ao_poly *) ao_lisp_root[i].addr, p;
202 MDBG("root poly %d\n", MDBG_OFFSET(ao_lisp_ref(p)));
207 while (ao_lisp_cons_noted) {
208 memcpy(ao_lisp_cons_last, ao_lisp_cons_note, sizeof (ao_lisp_cons_note));
209 memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
210 ao_lisp_cons_noted = 0;
211 for (i = 0; i < AO_LISP_POOL; i += 4) {
212 if (busy(ao_lisp_cons_last, i)) {
213 void *v = ao_lisp_pool + i;
214 MDBG("root cons %d\n", MDBG_OFFSET(v));
215 visit_addr(&ao_lisp_cons_type, &v);
221 static void *move_old, *move_new;
222 static int move_size;
230 for (i = 0; i < ao_lisp_top; i += 4) {
233 else if ((i & 0x1f) == 0)
235 if (busy(ao_lisp_busy, i))
242 #define DUMP_BUSY() dump_busy()
247 static const struct ao_lisp_type const *ao_lisp_types[AO_LISP_NUM_TYPE] = {
248 [AO_LISP_CONS] = &ao_lisp_cons_type,
249 [AO_LISP_INT] = NULL,
250 [AO_LISP_STRING] = &ao_lisp_string_type,
251 [AO_LISP_OTHER] = (void *) 0x1,
252 [AO_LISP_ATOM] = &ao_lisp_atom_type,
253 [AO_LISP_BUILTIN] = &ao_lisp_builtin_type,
254 [AO_LISP_FRAME] = &ao_lisp_frame_type,
255 [AO_LISP_LAMBDA] = &ao_lisp_lambda_type,
259 ao_lisp_mark_ref(const struct ao_lisp_type *type, void **ref)
261 return ao_lisp_mark(type, *ref);
265 ao_lisp_poly_mark_ref(ao_poly *p, uint8_t do_note_cons)
267 return ao_lisp_poly_mark(*p, do_note_cons);
271 ao_lisp_collect(void)
278 walk(ao_lisp_busy, ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
282 MDBG("find first busy\n");
283 for (i = 0; i < ao_lisp_top; i += 4) {
284 if (!busy(ao_lisp_busy, i))
288 while(i < ao_lisp_top) {
289 if (busy(ao_lisp_busy, i)) {
290 MDBG("busy %d -> %d\n", i, top);
292 move_old = &ao_lisp_pool[i];
293 move_new = &ao_lisp_pool[top];
295 walk(ao_lisp_moving, ao_lisp_move, ao_lisp_poly_move);
296 MDBG("\tbusy size %d\n", move_size);
299 clear_object(ao_lisp_busy, move_old, move_size);
300 mark_object(ao_lisp_busy, move_new, move_size);
301 if (busy_object(ao_lisp_cons_note, move_old)) {
302 clear_object(ao_lisp_cons_note, move_old, move_size);
303 mark_object(ao_lisp_cons_note, move_new, move_size);
320 ao_lisp_mark(const struct ao_lisp_type *type, void *addr)
325 MDBG_MOVE("mark %d\n", MDBG_OFFSET(addr));
326 if (mark_object(ao_lisp_busy, addr, type->size(addr))) {
327 MDBG_MOVE("already marked\n");
337 ao_lisp_poly_mark(ao_poly p, uint8_t do_note_cons)
339 uint8_t type = ao_lisp_poly_type(p);
343 if (type == AO_LISP_CONS && do_note_cons) {
344 MDBG_MOVE("note cons %d\n", MDBG_OFFSET(ao_lisp_ref(p)));
345 note_cons(ao_lisp_ref(p));
348 const struct ao_lisp_type *lisp_type = ao_lisp_types[ao_lisp_poly_type(p)];
350 return ao_lisp_mark(lisp_type, ao_lisp_ref(p));
356 ao_lisp_mark_memory(void *addr, int size)
358 return mark_object(ao_lisp_busy, addr, size);
362 * After the object has been moved, we have to reference it
363 * in the new location. This is only relevant for ao_lisp_poly_move
364 * as it needs to fetch the type byte from the object, which
365 * may have been overwritten by the copy
368 ao_lisp_move_map(void *addr)
370 if (addr == move_old) {
378 check_move(void *addr, int size)
380 if (addr == move_old) {
381 MDBG_MOVE("mapping %d -> %d\n", MDBG_OFFSET(addr), MDBG_OFFSET(move_new));
382 if (move_size && move_size != ((size + 3) & ~3))
385 /* Only copy the object once, otherwise we may
388 if (move_size == 0) {
389 MDBG_MOVE(" copy %d\n", size);
390 memmove(move_new, move_old, size);
391 move_size = (size + 3) & ~3;
399 ao_lisp_move(const struct ao_lisp_type *type, void **ref)
403 int size = type->size(ao_lisp_move_map(addr));
408 #ifndef AO_LISP_MAKE_CONST
409 if (AO_LISP_IS_CONST(addr))
412 MDBG_MOVE("object %d\n", MDBG_OFFSET(addr));
413 if (!AO_LISP_IS_POOL(a))
416 addr = check_move(addr, size);
419 if (mark_object(ao_lisp_moving, addr, size)) {
420 MDBG_MOVE("already moved\n");
425 MDBG_MOVE("recursing...\n");
429 MDBG_MOVE("done %d\n", MDBG_OFFSET(addr));
434 ao_lisp_move_memory(void **ref, int size)
440 MDBG_MOVE("memory %d\n", MDBG_OFFSET(addr));
442 addr = check_move(addr, size);
445 if (mark_object(ao_lisp_moving, addr, size)) {
446 MDBG_MOVE("already moved\n");
455 ao_lisp_poly_move(ao_poly *ref, uint8_t do_note_cons)
459 const struct ao_lisp_type *lisp_type;
466 type = ao_lisp_poly_base_type(p);
467 addr = ao_lisp_ref(p);
469 if ((uint8_t *) addr < ao_lisp_pool || ao_lisp_pool + AO_LISP_POOL <= (uint8_t*) addr)
472 if (type == AO_LISP_CONS && do_note_cons) {
473 addr = check_move(addr, sizeof (struct ao_lisp_cons));
478 if (type == AO_LISP_OTHER)
479 type = ao_lisp_other_type(ao_lisp_move_map(ao_lisp_poly_other(p)));
481 if (type >= AO_LISP_NUM_TYPE)
484 lisp_type = ao_lisp_types[type];
487 ret = ao_lisp_move(lisp_type, &addr);
490 if (addr != ao_lisp_ref(p)) {
491 ao_poly np = ao_lisp_poly(addr, p & AO_LISP_TYPE_MASK);
492 MDBG("poly %d moved %d -> %d\n",
493 type, MDBG_OFFSET(ao_lisp_ref(p)), MDBG_OFFSET(ao_lisp_ref(np)));
500 static int AO_LISP_POOL_CUR = AO_LISP_POOL / 8;
509 for (i = 0; i < AO_LISP_POOL_CUR; i += 4) {
510 uint32_t *a = (uint32_t *) &ao_lisp_pool[i];
511 if (!busy_object(ao_lisp_busy, a))
514 for (i = 0; i < AO_LISP_POOL_CUR; i += 2) {
515 ao_poly *a = (uint16_t *) &ao_lisp_pool[i];
518 if (!ao_lisp_is_const(p)) {
519 void *r = ao_lisp_ref(p);
521 if (ao_lisp_pool <= (uint8_t *) r &&
522 (uint8_t *) r <= ao_lisp_pool + AO_LISP_POOL_CUR)
524 if (!busy_object(ao_lisp_busy, r)) {
525 printf("missing reference from %d to %d\n",
526 (int) ((uint8_t *) a - ao_lisp_pool),
527 (int) ((uint8_t *) r - ao_lisp_pool));
535 #define AO_LISP_POOL_CUR AO_LISP_POOL
539 ao_lisp_alloc(int size)
543 size = ao_lisp_mem_round(size);
544 #ifdef MDBG_COLLECT_ALWAYS
547 if (ao_lisp_top + size > AO_LISP_POOL_CUR) {
549 if (AO_LISP_POOL_CUR < AO_LISP_POOL) {
550 AO_LISP_POOL_CUR += AO_LISP_POOL / 8;
559 for (i = ao_lisp_top; i < AO_LISP_POOL; i += 4) {
560 uint32_t *p = (uint32_t *) &ao_lisp_pool[i];
566 if (ao_lisp_top + size > AO_LISP_POOL) {
567 ao_lisp_error(AO_LISP_OOM, "out of memory");
571 addr = ao_lisp_pool + ao_lisp_top;
577 ao_lisp_root_add(const struct ao_lisp_type *type, void *addr)
580 MDBG("add root type %p addr %p\n", type, addr);
581 for (i = 0; i < AO_LISP_ROOT; i++) {
582 if (!ao_lisp_root[i].addr) {
583 ao_lisp_root[i].addr = addr;
584 ao_lisp_root[i].type = type;
593 ao_lisp_root_poly_add(ao_poly *p)
595 return ao_lisp_root_add(NULL, p);
599 ao_lisp_root_clear(void *addr)
602 for (i = 0; i < AO_LISP_ROOT; i++) {
603 if (ao_lisp_root[i].addr == addr) {
604 ao_lisp_root[i].addr = 0;
605 ao_lisp_root[i].type = 0;