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;
83 tag[byte] |= (1 << bit);
86 static inline void clear(uint8_t *tag, int offset) {
87 int byte = offset >> 5;
88 int bit = (offset >> 2) & 7;
89 tag[byte] &= ~(1 << bit);
92 static inline int busy(uint8_t *tag, int offset) {
93 int byte = offset >> 5;
94 int bit = (offset >> 2) & 7;
95 return (tag[byte] >> bit) & 1;
98 static inline int min(int a, int b) { return a < b ? a : b; }
99 static inline int max(int a, int b) { return a > b ? a : b; }
101 static inline int limit(int offset) {
102 return min(AO_LISP_POOL, max(offset, 0));
106 mark_object(uint8_t *tag, void *addr, int size) {
113 if ((uint8_t *) addr < ao_lisp_pool || ao_lisp_pool + AO_LISP_POOL <= (uint8_t*) addr)
116 base = (uint8_t *) addr - ao_lisp_pool;
120 bound = limit(bound);
123 while (base < bound) {
131 clear_object(uint8_t *tag, void *addr, int size) {
137 base = (uint8_t *) addr - ao_lisp_pool;
141 bound = limit(bound);
142 if (!busy(tag, base))
144 while (base < bound) {
152 busy_object(uint8_t *tag, void *addr) {
158 if ((uint8_t *) addr < ao_lisp_pool || ao_lisp_pool + AO_LISP_POOL <= (uint8_t*) addr)
161 base = (uint8_t *) addr - ao_lisp_pool;
169 note_cons(void *addr)
171 MDBG_MOVE("note cons %d\n", MDBG_OFFSET(addr));
172 if (AO_LISP_IS_POOL(addr)) {
173 int offset = (uint8_t *) addr - ao_lisp_pool;
174 ao_lisp_cons_noted = 1;
175 mark(ao_lisp_cons_note, offset);
180 static void *move_old, *move_new;
181 static int move_size;
189 MDBG_MOVE("move %d -> %d\n", MDBG_OFFSET(move_old), MDBG_OFFSET(move_new));
191 memset(ao_lisp_moving, '\0', sizeof (ao_lisp_moving));
192 memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
193 ao_lisp_cons_noted = 0;
194 for (i = 0; i < AO_LISP_ROOT; i++) {
195 if (!ao_lisp_root[i].addr)
197 if (ao_lisp_root[i].type) {
198 void *addr = *ao_lisp_root[i].addr;
201 MDBG_MOVE("root %d\n", MDBG_OFFSET(addr));
202 if (!ao_lisp_move(ao_lisp_root[i].type,
203 ao_lisp_root[i].addr)) {
204 MDBG_MOVE("root moves from %p to %p\n",
206 *ao_lisp_root[i].addr);
209 ao_poly p = *(ao_poly *) ao_lisp_root[i].addr;
212 if (!ao_lisp_poly_move((ao_poly *) ao_lisp_root[i].addr, 0)) {
213 MDBG_MOVE("root poly move from %04x to %04x\n",
214 p, *(ao_poly *) ao_lisp_root[i].addr);
218 while (ao_lisp_cons_noted) {
219 memcpy(ao_lisp_cons_last, ao_lisp_cons_note, sizeof (ao_lisp_cons_note));
220 memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
221 ao_lisp_cons_noted = 0;
222 for (i = 0; i < AO_LISP_POOL; i += 4) {
223 if (busy(ao_lisp_cons_last, i)) {
224 void *addr = ao_lisp_pool + i;
225 MDBG_MOVE("cons %d\n", MDBG_OFFSET(addr));
226 if (!ao_lisp_move(&ao_lisp_cons_type, &addr)) {
227 MDBG_MOVE("cons moves from %p to %p\n",
228 ao_lisp_pool + i, addr);
234 MDBG_MOVE("move done\n");
243 for (i = 0; i < ao_lisp_top; i += 4) {
246 else if ((i & 0x1f) == 0)
248 if (busy(ao_lisp_busy, i))
255 #define DUMP_BUSY() dump_busy()
260 static const struct ao_lisp_type const *ao_lisp_types[AO_LISP_NUM_TYPE] = {
261 [AO_LISP_CONS] = &ao_lisp_cons_type,
262 [AO_LISP_INT] = NULL,
263 [AO_LISP_STRING] = &ao_lisp_string_type,
264 [AO_LISP_OTHER] = (void *) 0x1,
265 [AO_LISP_ATOM] = &ao_lisp_atom_type,
266 [AO_LISP_BUILTIN] = &ao_lisp_builtin_type,
267 [AO_LISP_FRAME] = &ao_lisp_frame_type,
268 [AO_LISP_LAMBDA] = &ao_lisp_lambda_type,
273 ao_lisp_mark_busy(void)
277 memset(ao_lisp_busy, '\0', sizeof (ao_lisp_busy));
278 memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
279 ao_lisp_cons_noted = 0;
281 for (i = 0; i < AO_LISP_ROOT; i++) {
282 if (ao_lisp_root[i].type) {
283 void **a = ao_lisp_root[i].addr, *v;
285 MDBG("root %d\n", MDBG_OFFSET(v));
286 ao_lisp_mark(ao_lisp_root[i].type, v);
289 ao_poly *a = (ao_poly *) ao_lisp_root[i].addr, p;
291 MDBG("root 0x%04x\n", p);
292 ao_lisp_poly_mark(p, 0);
296 while (ao_lisp_cons_noted) {
297 memcpy(ao_lisp_cons_last, ao_lisp_cons_note, sizeof (ao_lisp_cons_note));
298 memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
299 ao_lisp_cons_noted = 0;
300 for (i = 0; i < AO_LISP_POOL; i += 4) {
301 if (busy(ao_lisp_cons_last, i)) {
302 void *v = ao_lisp_pool + i;
303 MDBG("cons %d\n", MDBG_OFFSET(v));
304 ao_lisp_mark(&ao_lisp_cons_type, v);
311 ao_lisp_collect(void)
322 MDBG("find first busy\n");
323 for (i = 0; i < ao_lisp_top; i += 4) {
324 if (!busy(ao_lisp_busy, i))
328 while(i < ao_lisp_top) {
329 if (busy(ao_lisp_busy, i)) {
330 MDBG("busy %d -> %d\n", i, top);
331 move_old = &ao_lisp_pool[i];
332 move_new = &ao_lisp_pool[top];
335 MDBG("\tbusy size %d\n", move_size);
338 clear_object(ao_lisp_busy, move_old, move_size);
339 mark_object(ao_lisp_busy, move_new, move_size);
340 if (busy_object(ao_lisp_cons_note, move_old)) {
341 clear_object(ao_lisp_cons_note, move_old, move_size);
342 mark_object(ao_lisp_cons_note, move_new, move_size);
358 ao_lisp_mark(const struct ao_lisp_type *type, void *addr)
362 if (mark_object(ao_lisp_busy, addr, type->size(addr)))
369 ao_lisp_poly_mark(ao_poly p, uint8_t do_note_cons)
371 uint8_t type = ao_lisp_poly_type(p);
375 if (type == AO_LISP_CONS && do_note_cons) {
376 note_cons(ao_lisp_ref(p));
379 const struct ao_lisp_type *lisp_type = ao_lisp_types[ao_lisp_poly_type(p)];
381 return ao_lisp_mark(lisp_type, ao_lisp_ref(p));
387 ao_lisp_mark_memory(void *addr, int size)
389 return mark_object(ao_lisp_busy, addr, size);
393 * After the object has been moved, we have to reference it
394 * in the new location. This is only relevant for ao_lisp_poly_move
395 * as it needs to fetch the type byte from the object, which
396 * may have been overwritten by the copy
399 ao_lisp_move_map(void *addr)
401 if (addr == move_old) {
402 if (busy_object(ao_lisp_moving, addr))
409 check_move(void *addr, int size)
411 if (addr == move_old) {
412 MDBG_MOVE("mapping %d -> %d\n", MDBG_OFFSET(addr), MDBG_OFFSET(move_new));
413 if (!busy_object(ao_lisp_moving, addr)) {
414 MDBG_MOVE(" copy %d\n", size);
415 memmove(move_new, move_old, size);
416 move_size = (size + 3) & ~3;
424 ao_lisp_move(const struct ao_lisp_type *type, void **ref)
428 int size = type->size(addr);
433 #ifndef AO_LISP_MAKE_CONST
434 if (AO_LISP_IS_CONST(addr))
437 MDBG_MOVE("object %d\n", MDBG_OFFSET(addr));
438 if (!AO_LISP_IS_POOL(a))
441 addr = check_move(addr, size);
444 if (mark_object(ao_lisp_moving, addr, size)) {
445 MDBG_MOVE("already moved\n");
450 MDBG_MOVE("recursing...\n");
454 MDBG_MOVE("done %d\n", MDBG_OFFSET(addr));
459 ao_lisp_move_memory(void **ref, int size)
465 MDBG_MOVE("memory %d\n", MDBG_OFFSET(addr));
467 addr = check_move(addr, size);
470 if (mark_object(ao_lisp_moving, addr, size)) {
471 MDBG_MOVE("already moved\n");
480 ao_lisp_poly_move(ao_poly *ref, uint8_t do_note_cons)
484 const struct ao_lisp_type *lisp_type;
491 type = ao_lisp_poly_base_type(p);
492 addr = ao_lisp_ref(p);
493 if (type == AO_LISP_CONS && do_note_cons) {
495 addr = check_move(addr, sizeof (struct ao_lisp_cons));
499 if (type == AO_LISP_OTHER)
500 type = ao_lisp_other_type(ao_lisp_move_map(ao_lisp_poly_other(p)));
502 if (type >= AO_LISP_NUM_TYPE)
505 lisp_type = ao_lisp_types[type];
508 ret = ao_lisp_move(lisp_type, &addr);
511 if (addr != ao_lisp_ref(p)) {
512 ao_poly np = ao_lisp_poly(addr, p & AO_LISP_TYPE_MASK);
513 MDBG("poly %d moved %04x -> %04x\n",
521 static int AO_LISP_POOL_CUR = AO_LISP_POOL / 8;
530 for (i = 0; i < AO_LISP_POOL_CUR; i += 4) {
531 uint32_t *a = (uint32_t *) &ao_lisp_pool[i];
532 if (!busy_object(ao_lisp_busy, a))
535 for (i = 0; i < AO_LISP_POOL_CUR; i += 2) {
536 ao_poly *a = (uint16_t *) &ao_lisp_pool[i];
539 if (!ao_lisp_is_const(p)) {
540 void *r = ao_lisp_ref(p);
542 if (ao_lisp_pool <= (uint8_t *) r &&
543 (uint8_t *) r <= ao_lisp_pool + AO_LISP_POOL_CUR)
545 if (!busy_object(ao_lisp_busy, r)) {
546 printf("missing reference from %d to %d\n",
547 (int) ((uint8_t *) a - ao_lisp_pool),
548 (int) ((uint8_t *) r - ao_lisp_pool));
556 #define AO_LISP_POOL_CUR AO_LISP_POOL
560 ao_lisp_alloc(int size)
564 size = ao_lisp_mem_round(size);
565 #ifdef MDBG_COLLECT_ALWAYS
568 if (ao_lisp_top + size > AO_LISP_POOL_CUR) {
570 if (AO_LISP_POOL_CUR < AO_LISP_POOL) {
571 AO_LISP_POOL_CUR += AO_LISP_POOL / 8;
580 for (i = ao_lisp_top; i < AO_LISP_POOL; i += 4) {
581 uint32_t *p = (uint32_t *) &ao_lisp_pool[i];
587 if (ao_lisp_top + size > AO_LISP_POOL) {
588 ao_lisp_error(AO_LISP_OOM, "out of memory");
592 addr = ao_lisp_pool + ao_lisp_top;
598 ao_lisp_root_add(const struct ao_lisp_type *type, void *addr)
601 MDBG("add root type %p addr %p\n", type, addr);
602 for (i = 0; i < AO_LISP_ROOT; i++) {
603 if (!ao_lisp_root[i].addr) {
604 ao_lisp_root[i].addr = addr;
605 ao_lisp_root[i].type = type;
614 ao_lisp_root_poly_add(ao_poly *p)
616 return ao_lisp_root_add(NULL, p);
620 ao_lisp_root_clear(void *addr)
623 for (i = 0; i < AO_LISP_ROOT; i++) {
624 if (ao_lisp_root[i].addr == addr) {
625 ao_lisp_root[i].addr = 0;
626 ao_lisp_root[i].type = 0;