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
23 * When building the constant table, it is the
24 * pool for allocations.
28 uint8_t ao_lisp_const[AO_LISP_POOL_CONST] __attribute__((aligned(4)));
29 #define ao_lisp_pool ao_lisp_const
31 #define AO_LISP_POOL AO_LISP_POOL_CONST
35 uint8_t ao_lisp_pool[AO_LISP_POOL + AO_LISP_POOL_EXTRA] __attribute__((aligned(4)));
45 int dbg_mem = DBG_MEM_START;
49 struct ao_lisp_record {
50 struct ao_lisp_record *next;
51 const struct ao_lisp_type *type;
56 static struct ao_lisp_record *record_head, **record_tail;
59 ao_lisp_record_free(struct ao_lisp_record *record)
62 struct ao_lisp_record *next = record->next;
69 ao_lisp_record_reset(void)
71 ao_lisp_record_free(record_head);
73 record_tail = &record_head;
77 ao_lisp_record(const struct ao_lisp_type *type,
81 struct ao_lisp_record *r = malloc(sizeof (struct ao_lisp_record));
88 record_tail = &r->next;
91 static struct ao_lisp_record *
92 ao_lisp_record_save(void)
94 struct ao_lisp_record *r = record_head;
97 record_tail = &record_head;
102 ao_lisp_record_compare(char *where,
103 struct ao_lisp_record *a,
104 struct ao_lisp_record *b)
107 if (a->type != b->type || a->size != b->size) {
108 printf("%s record difers %d %s %d -> %d %s %d\n",
110 MDBG_OFFSET(a->addr),
113 MDBG_OFFSET(b->addr),
122 printf("%s record differs %d %s %d -> NULL\n",
124 MDBG_OFFSET(a->addr),
130 printf("%s record differs NULL -> %d %s %d\n",
132 MDBG_OFFSET(b->addr),
140 #define ao_lisp_record_reset()
143 uint8_t ao_lisp_exception;
145 struct ao_lisp_root {
146 const struct ao_lisp_type *type;
150 static struct ao_lisp_cons *save_cons[2];
151 static char *save_string[2];
152 static ao_poly save_poly[2];
154 static const struct ao_lisp_root ao_lisp_root[] = {
156 .type = &ao_lisp_cons_type,
157 .addr = (void **) &save_cons[0],
160 .type = &ao_lisp_cons_type,
161 .addr = (void **) &save_cons[1],
164 .type = &ao_lisp_string_type,
165 .addr = (void **) &save_string[0]
168 .type = &ao_lisp_string_type,
169 .addr = (void **) &save_string[1]
173 .addr = (void **) &save_poly[0]
177 .addr = (void **) &save_poly[1]
180 .type = &ao_lisp_atom_type,
181 .addr = (void **) &ao_lisp_atoms
184 .type = &ao_lisp_frame_type,
185 .addr = (void **) &ao_lisp_frame_global,
188 .type = &ao_lisp_frame_type,
189 .addr = (void **) &ao_lisp_frame_current,
192 .type = &ao_lisp_stack_type,
193 .addr = (void **) &ao_lisp_stack,
197 .addr = (void **) &ao_lisp_v,
200 .type = &ao_lisp_cons_type,
201 .addr = (void **) &ao_lisp_read_cons,
204 .type = &ao_lisp_cons_type,
205 .addr = (void **) &ao_lisp_read_cons_tail,
208 .type = &ao_lisp_cons_type,
209 .addr = (void **) &ao_lisp_read_stack,
213 #define AO_LISP_ROOT (sizeof (ao_lisp_root) / sizeof (ao_lisp_root[0]))
215 #define AO_LISP_BUSY_SIZE ((AO_LISP_POOL + 31) / 32)
217 static uint8_t ao_lisp_busy[AO_LISP_BUSY_SIZE];
218 static uint8_t ao_lisp_cons_note[AO_LISP_BUSY_SIZE];
219 static uint8_t ao_lisp_cons_last[AO_LISP_BUSY_SIZE];
220 static uint8_t ao_lisp_cons_noted;
222 uint16_t ao_lisp_top;
224 struct ao_lisp_chunk {
232 #define AO_LISP_NCHUNK 32
234 static struct ao_lisp_chunk ao_lisp_chunk[AO_LISP_NCHUNK];
236 /* Offset of an address within the pool. */
237 static inline uint16_t pool_offset(void *addr) {
238 if (!AO_LISP_IS_POOL(addr))
240 return ((uint8_t *) addr) - ao_lisp_pool;
244 * Convert back and forth between 'poly's used
245 * as short addresses in the pool and addresses.
246 * These are used in the chunk code.
248 static inline ao_poly pool_poly(void *addr) {
249 if (!AO_LISP_IS_POOL(addr))
251 return ((uint8_t *) addr) - AO_LISP_POOL_BASE;
254 static inline void *pool_ref(ao_poly p) {
255 return AO_LISP_POOL_BASE + p;
258 static inline void mark(uint8_t *tag, int offset) {
259 int byte = offset >> 5;
260 int bit = (offset >> 2) & 7;
261 tag[byte] |= (1 << bit);
264 static inline void clear(uint8_t *tag, int offset) {
265 int byte = offset >> 5;
266 int bit = (offset >> 2) & 7;
267 tag[byte] &= ~(1 << bit);
270 static inline int busy(uint8_t *tag, int offset) {
271 int byte = offset >> 5;
272 int bit = (offset >> 2) & 7;
273 return (tag[byte] >> bit) & 1;
276 static inline int min(int a, int b) { return a < b ? a : b; }
277 static inline int max(int a, int b) { return a > b ? a : b; }
279 static inline int limit(int offset) {
280 return min(AO_LISP_POOL, max(offset, 0));
283 static int total_marked;
286 * Mark a range of addresses
289 mark_object(uint8_t *tag, void *addr, int size) {
293 MDBG_DO(if (!AO_LISP_IS_POOL((uint8_t *) addr + size - 1))
296 base = pool_offset(addr);
299 MDBG_DO(if (bound > ao_lisp_top) ao_lisp_abort());
303 if (tag == ao_lisp_busy)
304 total_marked += size;
305 while (base < bound) {
314 clear_object(uint8_t *tag, void *addr, int size) {
318 MDBG_DO(if (!AO_LISP_IS_POOL((uint8_t *) addr + size - 1))
321 base = (uint8_t *) addr - ao_lisp_pool;
325 bound = limit(bound);
326 if (!busy(tag, base))
328 total_marked -= size;
329 while (base < bound) {
337 note_cons(void *addr)
339 if (AO_LISP_IS_POOL(addr)) {
340 int offset = (uint8_t *) addr - ao_lisp_pool;
341 MDBG_MOVE("note cons %d\n", MDBG_OFFSET(addr));
342 ao_lisp_cons_noted = 1;
343 mark(ao_lisp_cons_note, offset);
347 static uint16_t chunk_low;
348 static uint16_t chunk_first, chunk_last;
351 note_chunk(uint16_t addr, uint16_t size)
355 if (addr < chunk_low)
358 for (i = 0; i < AO_LISP_NCHUNK; i++) {
359 if (ao_lisp_chunk[i].size && ao_lisp_chunk[i].old_addr == addr) {
360 if (ao_lisp_chunk[i].size != size)
364 if (ao_lisp_chunk[i].old_addr > addr) {
365 memmove(&ao_lisp_chunk[i+1],
367 (AO_LISP_NCHUNK - (i+1)) * sizeof (struct ao_lisp_chunk));
368 ao_lisp_chunk[i].size = 0;
370 if (ao_lisp_chunk[i].size == 0) {
371 ao_lisp_chunk[i].old_addr = addr;
372 ao_lisp_chunk[i].size = size;
379 * Walk all referenced objects calling functions on each one
383 walk(int (*visit_addr)(const struct ao_lisp_type *type, void **addr),
384 int (*visit_poly)(ao_poly *p, uint8_t do_note_cons))
389 ao_lisp_record_reset();
390 memset(ao_lisp_busy, '\0', sizeof (ao_lisp_busy));
391 memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
392 ao_lisp_cons_noted = 0;
393 for (i = 0; i < AO_LISP_ROOT; i++) {
394 if (ao_lisp_root[i].type) {
395 void **a = ao_lisp_root[i].addr, *v;
397 MDBG_MOVE("root ptr %d\n", MDBG_OFFSET(v));
398 visit_addr(ao_lisp_root[i].type, a);
401 ao_poly *a = (ao_poly *) ao_lisp_root[i].addr, p;
403 MDBG_MOVE("root poly %d\n", MDBG_OFFSET(ao_lisp_ref(p)));
408 while (ao_lisp_cons_noted) {
409 memcpy(ao_lisp_cons_last, ao_lisp_cons_note, sizeof (ao_lisp_cons_note));
410 memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
411 ao_lisp_cons_noted = 0;
412 for (i = 0; i < AO_LISP_POOL; i += 4) {
413 if (busy(ao_lisp_cons_last, i)) {
414 void *v = ao_lisp_pool + i;
415 MDBG_MOVE("root cons %d\n", MDBG_OFFSET(v));
416 visit_addr(&ao_lisp_cons_type, &v);
428 for (i = 0; i < ao_lisp_top; i += 4) {
429 if ((i & 0xff) == 0) {
433 else if ((i & 0x1f) == 0)
435 if (busy(ao_lisp_busy, i))
442 #define DUMP_BUSY() dump_busy()
447 static const struct ao_lisp_type const *ao_lisp_types[AO_LISP_NUM_TYPE] = {
448 [AO_LISP_CONS] = &ao_lisp_cons_type,
449 [AO_LISP_INT] = NULL,
450 [AO_LISP_STRING] = &ao_lisp_string_type,
451 [AO_LISP_OTHER] = (void *) 0x1,
452 [AO_LISP_ATOM] = &ao_lisp_atom_type,
453 [AO_LISP_BUILTIN] = &ao_lisp_builtin_type,
454 [AO_LISP_FRAME] = &ao_lisp_frame_type,
455 [AO_LISP_LAMBDA] = &ao_lisp_lambda_type,
459 ao_lisp_mark_ref(const struct ao_lisp_type *type, void **ref)
461 return ao_lisp_mark(type, *ref);
465 ao_lisp_poly_mark_ref(ao_poly *p, uint8_t do_note_cons)
467 return ao_lisp_poly_mark(*p, do_note_cons);
471 ao_lisp_collect(void)
479 struct ao_lisp_record *mark_record = NULL, *move_record = NULL;
482 MDBG_MOVE("collect %d\n", dbg_collects);
489 MDBG_MOVE("move chunks from %d to %d\n", chunk_low, top);
490 /* Find the sizes of the first chunk of objects to move */
491 memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
492 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
494 marked = total_marked;
496 ao_lisp_record_free(mark_record);
497 mark_record = ao_lisp_record_save();
498 if (mark_record && move_record)
499 ao_lisp_record_compare("mark", move_record, mark_record);
501 if (moved && moved != marked)
507 /* Find the first moving object */
508 for (i = 0; i < AO_LISP_NCHUNK; i++) {
509 uint16_t size = ao_lisp_chunk[i].size;
514 if (ao_lisp_chunk[i].old_addr > top)
516 if (ao_lisp_chunk[i].old_addr != top)
520 MDBG_MOVE("chunk %d %d not moving\n",
521 ao_lisp_chunk[i].old_addr,
522 ao_lisp_chunk[i].size);
523 chunk_low = ao_lisp_chunk[i].old_addr + size;
527 /* Copy all of the objects */
528 for (; i < AO_LISP_NCHUNK; i++) {
529 uint16_t size = ao_lisp_chunk[i].size;
534 MDBG_MOVE("chunk %d %d -> %d\n",
535 ao_lisp_chunk[i].old_addr,
538 ao_lisp_chunk[i].new_addr = top;
539 memmove(&ao_lisp_pool[top],
540 &ao_lisp_pool[ao_lisp_chunk[i].old_addr],
542 MDBG_DO(clear_object(ao_lisp_busy, &ao_lisp_pool[ao_lisp_chunk[i].old_addr], size));
543 MDBG_DO(mark_object(ao_lisp_busy, &ao_lisp_pool[top], size));
545 chunk_low = ao_lisp_chunk[i].old_addr + size;
548 MDBG_MOVE("after moving objects, busy is now:\n");
552 if (chunk_first < chunk_last) {
553 /* Relocate all references to the objects */
554 walk(ao_lisp_move, ao_lisp_poly_move);
557 ao_lisp_record_free(move_record);
558 move_record = ao_lisp_record_save();
559 if (mark_record && move_record)
560 ao_lisp_record_compare("move", mark_record, move_record);
562 moved = total_marked;
568 if (chunk_last != AO_LISP_NCHUNK)
573 MDBG_DO(memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
574 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref));
576 // printf ("collect. top %d loops %d\n", top, loops);
580 * Mark interfaces for objects
582 * Note a reference to memory and
583 * collect information about a few object sizes
588 ao_lisp_mark_memory(const struct ao_lisp_type *type, void *addr)
591 if (!AO_LISP_IS_POOL(addr))
594 size = ao_lisp_size(type, addr);
595 MDBG_MOVE("mark memory %d\n", MDBG_OFFSET(addr));
596 if (!mark_object(ao_lisp_busy, addr, size)) {
597 note_chunk(pool_offset(addr), size);
598 MDBG_DO(ao_lisp_record(type, addr, size));
601 MDBG_MOVE("already marked\n");
606 ao_lisp_mark(const struct ao_lisp_type *type, void *addr)
609 MDBG_MOVE("mark %d\n", MDBG_OFFSET(addr));
611 ret = ao_lisp_mark_memory(type, addr);
613 MDBG_MOVE("mark recurse\n");
621 ao_lisp_poly_mark(ao_poly p, uint8_t do_note_cons)
629 type = ao_lisp_poly_base_type(p);
630 addr = ao_lisp_ref(p);
632 if (!AO_LISP_IS_POOL(addr))
635 if (type == AO_LISP_CONS && do_note_cons) {
636 note_cons(ao_lisp_ref(p));
639 const struct ao_lisp_type *lisp_type;
641 if (type == AO_LISP_OTHER) {
642 type = ao_lisp_other_type(ao_lisp_poly_other(p));
643 if (type <= AO_LISP_OTHER || AO_LISP_NUM_TYPE <= type)
647 lisp_type = ao_lisp_types[ao_lisp_poly_type(p)];
650 return ao_lisp_mark(lisp_type, ao_lisp_ref(p));
657 uint16_t offset = pool_offset(addr);
660 for (i = chunk_first; i < chunk_last; i++) {
661 if (ao_lisp_chunk[i].old_addr == offset) {
662 MDBG_MOVE("move %d -> %d\n",
663 ao_lisp_chunk[i].old_addr,
664 ao_lisp_chunk[i].new_addr);
665 return ao_lisp_pool + ao_lisp_chunk[i].new_addr;
672 ao_lisp_move_memory(const struct ao_lisp_type *type, void **ref)
677 if (!AO_LISP_IS_POOL(addr))
680 MDBG_MOVE("move memory %d\n", MDBG_OFFSET(addr));
681 addr = move_map(addr);
682 size = ao_lisp_size(type, addr);
684 MDBG_MOVE("update ref %d %d -> %d\n",
685 AO_LISP_IS_POOL(ref) ? MDBG_OFFSET(ref) : -1,
686 MDBG_OFFSET(*ref), MDBG_OFFSET(addr));
689 if (!mark_object(ao_lisp_busy, addr, size)) {
690 MDBG_DO(ao_lisp_record(type, addr, size));
693 MDBG_MOVE("already moved\n");
698 ao_lisp_move(const struct ao_lisp_type *type, void **ref)
701 MDBG_MOVE("move object %d\n", MDBG_OFFSET(*ref));
703 ret = ao_lisp_move_memory(type, ref);
705 MDBG_MOVE("move recurse\n");
713 ao_lisp_poly_move(ao_poly *ref, uint8_t do_note_cons)
723 type = ao_lisp_poly_base_type(p);
724 addr = ao_lisp_ref(p);
726 if (!AO_LISP_IS_POOL(addr))
729 if (type == AO_LISP_CONS && do_note_cons) {
730 // addr = move_map(addr);
731 MDBG_DO(if (addr != move_map(addr)) MDBG_MOVE("noting cons at old addr %d instead of new addr %d\n", MDBG_OFFSET(addr), MDBG_OFFSET(move_map(addr))););
734 addr = move_map(addr);
737 const struct ao_lisp_type *lisp_type;
739 if (type == AO_LISP_OTHER) {
740 type = ao_lisp_other_type(move_map(ao_lisp_poly_other(p)));
741 if (type <= AO_LISP_OTHER || AO_LISP_NUM_TYPE <= type)
745 lisp_type = ao_lisp_types[type];
748 ret = ao_lisp_move(lisp_type, &addr);
751 /* Re-write the poly value */
752 if (addr != ao_lisp_ref(p)) {
753 ao_poly np = ao_lisp_poly(addr, p & AO_LISP_TYPE_MASK);
754 MDBG_MOVE("poly %d moved %d -> %d\n",
755 type, MDBG_OFFSET(ao_lisp_ref(p)), MDBG_OFFSET(ao_lisp_ref(np)));
762 static int AO_LISP_POOL_CUR = AO_LISP_POOL / 8;
771 for (i = 0; i < AO_LISP_POOL_CUR; i += 4) {
772 uint32_t *a = (uint32_t *) &ao_lisp_pool[i];
773 if (!busy_object(ao_lisp_busy, a))
776 for (i = 0; i < AO_LISP_POOL_CUR; i += 2) {
777 ao_poly *a = (uint16_t *) &ao_lisp_pool[i];
780 if (!ao_lisp_is_const(p)) {
781 void *r = ao_lisp_ref(p);
783 if (ao_lisp_pool <= (uint8_t *) r &&
784 (uint8_t *) r <= ao_lisp_pool + AO_LISP_POOL_CUR)
786 if (!busy_object(ao_lisp_busy, r)) {
787 printf("missing reference from %d to %d\n",
788 (int) ((uint8_t *) a - ao_lisp_pool),
789 (int) ((uint8_t *) r - ao_lisp_pool));
797 #define AO_LISP_POOL_CUR AO_LISP_POOL
802 ao_lisp_validate(void)
805 memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
806 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
815 ao_lisp_alloc(int size)
819 MDBG_DO(++dbg_allocs);
820 MDBG_DO(if (dbg_validate) ao_lisp_validate());
821 size = ao_lisp_size_round(size);
822 if (ao_lisp_top + size > AO_LISP_POOL_CUR) {
824 if (AO_LISP_POOL_CUR < AO_LISP_POOL) {
825 AO_LISP_POOL_CUR += AO_LISP_POOL / 8;
834 for (i = ao_lisp_top; i < AO_LISP_POOL; i += 4) {
835 uint32_t *p = (uint32_t *) &ao_lisp_pool[i];
841 if (ao_lisp_top + size > AO_LISP_POOL) {
842 ao_lisp_error(AO_LISP_OOM, "out of memory");
846 addr = ao_lisp_pool + ao_lisp_top;
852 ao_lisp_cons_stash(int id, struct ao_lisp_cons *cons)
854 if (save_cons[id] != NULL)
856 save_cons[id] = cons;
859 struct ao_lisp_cons *
860 ao_lisp_cons_fetch(int id)
862 struct ao_lisp_cons *cons = save_cons[id];
863 save_cons[id] = NULL;
868 ao_lisp_string_stash(int id, char *string)
870 if (save_cons[id] != NULL)
872 save_string[id] = string;
876 ao_lisp_string_fetch(int id)
878 char *string = save_string[id];
879 save_string[id] = NULL;
883 ao_lisp_poly_stash(int id, ao_poly poly)
885 save_poly[id] = poly;
889 ao_lisp_poly_fetch(int id)
891 ao_poly poly = save_poly[id];
892 save_poly[id] = AO_LISP_NIL;