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 MDBG_COLLECT_ALWAYS
42 #define MDBG_OFFSET(a) ((int) ((uint8_t *) (a) - ao_lisp_pool))
43 #define MDBG(...) printf(__VA_ARGS__)
45 static int move_dump = MDBG_MOVE;
46 static int move_depth;
47 #define MDBG_RESET() (move_depth = 0)
48 #define MDBG_MOVE(...) do { if(move_dump) { int d; for (d = 0; d < move_depth; d++) printf (" "); printf(__VA_ARGS__); } } while (0)
49 #define MDBG_MOVE_IN() (move_depth++)
50 #define MDBG_MOVE_OUT() (move_depth--)
55 #define MDBG_MOVE(...)
56 #define MDBG_MOVE_IN()
57 #define MDBG_MOVE_OUT()
60 uint8_t ao_lisp_exception;
64 const struct ao_lisp_type *type;
67 #define AO_LISP_ROOT 16
69 static struct ao_lisp_root ao_lisp_root[AO_LISP_ROOT];
71 static uint8_t ao_lisp_busy[AO_LISP_POOL / 32];
72 static uint8_t ao_lisp_moving[AO_LISP_POOL / 32];
73 static uint8_t ao_lisp_cons_note[AO_LISP_POOL / 32];
74 static uint8_t ao_lisp_cons_last[AO_LISP_POOL / 32];
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 MDBG_MOVE("note cons %d\n", MDBG_OFFSET(addr));
171 if (AO_LISP_IS_POOL(addr)) {
172 int offset = (uint8_t *) addr - ao_lisp_pool;
173 ao_lisp_cons_noted = 1;
174 mark(ao_lisp_cons_note, offset);
179 * Walk all referenced objects calling functions on each one
183 walk_all(uint8_t *tag,
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 %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 0x%04x\n", 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("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;
227 walk_all(ao_lisp_moving, ao_lisp_move, ao_lisp_poly_move);
236 for (i = 0; i < ao_lisp_top; i += 4) {
239 else if ((i & 0x1f) == 0)
241 if (busy(ao_lisp_busy, i))
248 #define DUMP_BUSY() dump_busy()
253 static const struct ao_lisp_type const *ao_lisp_types[AO_LISP_NUM_TYPE] = {
254 [AO_LISP_CONS] = &ao_lisp_cons_type,
255 [AO_LISP_INT] = NULL,
256 [AO_LISP_STRING] = &ao_lisp_string_type,
257 [AO_LISP_OTHER] = (void *) 0x1,
258 [AO_LISP_ATOM] = &ao_lisp_atom_type,
259 [AO_LISP_BUILTIN] = &ao_lisp_builtin_type,
260 [AO_LISP_FRAME] = &ao_lisp_frame_type,
261 [AO_LISP_LAMBDA] = &ao_lisp_lambda_type,
265 ao_lisp_mark_ref(const struct ao_lisp_type *type, void **ref)
267 return ao_lisp_mark(type, *ref);
271 ao_lisp_poly_mark_ref(ao_poly *p, uint8_t do_note_cons)
273 return ao_lisp_poly_mark(*p, do_note_cons);
277 ao_lisp_mark_busy(void)
279 walk_all(ao_lisp_busy, ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
283 ao_lisp_collect(void)
294 MDBG("find first busy\n");
295 for (i = 0; i < ao_lisp_top; i += 4) {
296 if (!busy(ao_lisp_busy, i))
300 while(i < ao_lisp_top) {
301 if (busy(ao_lisp_busy, i)) {
302 MDBG("busy %d -> %d\n", i, top);
303 move_old = &ao_lisp_pool[i];
304 move_new = &ao_lisp_pool[top];
307 MDBG("\tbusy size %d\n", move_size);
310 clear_object(ao_lisp_busy, move_old, move_size);
311 mark_object(ao_lisp_busy, move_new, move_size);
312 if (busy_object(ao_lisp_cons_note, move_old)) {
313 clear_object(ao_lisp_cons_note, move_old, move_size);
314 mark_object(ao_lisp_cons_note, move_new, move_size);
330 ao_lisp_mark(const struct ao_lisp_type *type, void *addr)
334 if (mark_object(ao_lisp_busy, addr, type->size(addr)))
341 ao_lisp_poly_mark(ao_poly p, uint8_t do_note_cons)
343 uint8_t type = ao_lisp_poly_type(p);
347 if (type == AO_LISP_CONS && do_note_cons) {
348 note_cons(ao_lisp_ref(p));
351 const struct ao_lisp_type *lisp_type = ao_lisp_types[ao_lisp_poly_type(p)];
353 return ao_lisp_mark(lisp_type, ao_lisp_ref(p));
359 ao_lisp_mark_memory(void *addr, int size)
361 return mark_object(ao_lisp_busy, addr, size);
365 * After the object has been moved, we have to reference it
366 * in the new location. This is only relevant for ao_lisp_poly_move
367 * as it needs to fetch the type byte from the object, which
368 * may have been overwritten by the copy
371 ao_lisp_move_map(void *addr)
373 if (addr == move_old) {
374 if (busy_object(ao_lisp_moving, addr))
381 check_move(void *addr, int size)
383 if (addr == move_old) {
384 MDBG_MOVE("mapping %d -> %d\n", MDBG_OFFSET(addr), MDBG_OFFSET(move_new));
385 if (!busy_object(ao_lisp_moving, addr)) {
386 MDBG_MOVE(" copy %d\n", size);
387 memmove(move_new, move_old, size);
388 move_size = (size + 3) & ~3;
396 ao_lisp_move(const struct ao_lisp_type *type, void **ref)
400 int size = type->size(addr);
405 #ifndef AO_LISP_MAKE_CONST
406 if (AO_LISP_IS_CONST(addr))
409 MDBG_MOVE("object %d\n", MDBG_OFFSET(addr));
410 if (!AO_LISP_IS_POOL(a))
413 addr = check_move(addr, size);
416 if (mark_object(ao_lisp_moving, addr, size)) {
417 MDBG_MOVE("already moved\n");
422 MDBG_MOVE("recursing...\n");
426 MDBG_MOVE("done %d\n", MDBG_OFFSET(addr));
431 ao_lisp_move_memory(void **ref, int size)
437 MDBG_MOVE("memory %d\n", MDBG_OFFSET(addr));
439 addr = check_move(addr, size);
442 if (mark_object(ao_lisp_moving, addr, size)) {
443 MDBG_MOVE("already moved\n");
452 ao_lisp_poly_move(ao_poly *ref, uint8_t do_note_cons)
456 const struct ao_lisp_type *lisp_type;
463 type = ao_lisp_poly_base_type(p);
464 addr = ao_lisp_ref(p);
465 if (type == AO_LISP_CONS && do_note_cons) {
467 addr = check_move(addr, sizeof (struct ao_lisp_cons));
471 if (type == AO_LISP_OTHER)
472 type = ao_lisp_other_type(ao_lisp_move_map(ao_lisp_poly_other(p)));
474 if (type >= AO_LISP_NUM_TYPE)
477 lisp_type = ao_lisp_types[type];
480 ret = ao_lisp_move(lisp_type, &addr);
483 if (addr != ao_lisp_ref(p)) {
484 ao_poly np = ao_lisp_poly(addr, p & AO_LISP_TYPE_MASK);
485 MDBG("poly %d moved %04x -> %04x\n",
493 static int AO_LISP_POOL_CUR = AO_LISP_POOL / 8;
502 for (i = 0; i < AO_LISP_POOL_CUR; i += 4) {
503 uint32_t *a = (uint32_t *) &ao_lisp_pool[i];
504 if (!busy_object(ao_lisp_busy, a))
507 for (i = 0; i < AO_LISP_POOL_CUR; i += 2) {
508 ao_poly *a = (uint16_t *) &ao_lisp_pool[i];
511 if (!ao_lisp_is_const(p)) {
512 void *r = ao_lisp_ref(p);
514 if (ao_lisp_pool <= (uint8_t *) r &&
515 (uint8_t *) r <= ao_lisp_pool + AO_LISP_POOL_CUR)
517 if (!busy_object(ao_lisp_busy, r)) {
518 printf("missing reference from %d to %d\n",
519 (int) ((uint8_t *) a - ao_lisp_pool),
520 (int) ((uint8_t *) r - ao_lisp_pool));
528 #define AO_LISP_POOL_CUR AO_LISP_POOL
532 ao_lisp_alloc(int size)
536 size = ao_lisp_mem_round(size);
537 #ifdef MDBG_COLLECT_ALWAYS
540 if (ao_lisp_top + size > AO_LISP_POOL_CUR) {
542 if (AO_LISP_POOL_CUR < AO_LISP_POOL) {
543 AO_LISP_POOL_CUR += AO_LISP_POOL / 8;
552 for (i = ao_lisp_top; i < AO_LISP_POOL; i += 4) {
553 uint32_t *p = (uint32_t *) &ao_lisp_pool[i];
559 if (ao_lisp_top + size > AO_LISP_POOL) {
560 ao_lisp_error(AO_LISP_OOM, "out of memory");
564 addr = ao_lisp_pool + ao_lisp_top;
570 ao_lisp_root_add(const struct ao_lisp_type *type, void *addr)
573 MDBG("add root type %p addr %p\n", type, addr);
574 for (i = 0; i < AO_LISP_ROOT; i++) {
575 if (!ao_lisp_root[i].addr) {
576 ao_lisp_root[i].addr = addr;
577 ao_lisp_root[i].type = type;
586 ao_lisp_root_poly_add(ao_poly *p)
588 return ao_lisp_root_add(NULL, p);
592 ao_lisp_root_clear(void *addr)
595 for (i = 0; i < AO_LISP_ROOT; i++) {
596 if (ao_lisp_root[i].addr == addr) {
597 ao_lisp_root[i].addr = 0;
598 ao_lisp_root[i].type = 0;