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 note_cons(void *addr)
288 if (AO_LISP_IS_POOL(addr)) {
289 int offset = pool_offset(addr);
290 MDBG_MOVE("note cons %d\n", MDBG_OFFSET(addr));
291 ao_lisp_cons_noted = 1;
292 mark(ao_lisp_cons_note, offset);
296 static uint16_t chunk_low;
297 static uint16_t chunk_first, chunk_last;
300 note_chunk(uint16_t addr, uint16_t size)
304 if (addr < chunk_low)
307 for (i = 0; i < AO_LISP_NCHUNK; i++) {
308 if (ao_lisp_chunk[i].size && ao_lisp_chunk[i].old_addr == addr) {
309 if (ao_lisp_chunk[i].size != size)
313 if (ao_lisp_chunk[i].old_addr > addr) {
314 memmove(&ao_lisp_chunk[i+1],
316 (AO_LISP_NCHUNK - (i+1)) * sizeof (struct ao_lisp_chunk));
317 ao_lisp_chunk[i].size = 0;
319 if (ao_lisp_chunk[i].size == 0) {
320 ao_lisp_chunk[i].old_addr = addr;
321 ao_lisp_chunk[i].size = size;
328 * Walk all referenced objects calling functions on each one
332 walk(int (*visit_addr)(const struct ao_lisp_type *type, void **addr),
333 int (*visit_poly)(ao_poly *p, uint8_t do_note_cons))
338 ao_lisp_record_reset();
339 memset(ao_lisp_busy, '\0', sizeof (ao_lisp_busy));
340 memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
341 ao_lisp_cons_noted = 0;
342 for (i = 0; i < AO_LISP_ROOT; i++) {
343 if (ao_lisp_root[i].type) {
344 void **a = ao_lisp_root[i].addr, *v;
346 MDBG_MOVE("root ptr %d\n", MDBG_OFFSET(v));
347 visit_addr(ao_lisp_root[i].type, a);
350 ao_poly *a = (ao_poly *) ao_lisp_root[i].addr, p;
352 MDBG_MOVE("root poly %d\n", MDBG_OFFSET(ao_lisp_ref(p)));
357 while (ao_lisp_cons_noted) {
358 memcpy(ao_lisp_cons_last, ao_lisp_cons_note, sizeof (ao_lisp_cons_note));
359 memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
360 ao_lisp_cons_noted = 0;
361 for (i = 0; i < AO_LISP_POOL; i += 4) {
362 if (busy(ao_lisp_cons_last, i)) {
363 void *v = ao_lisp_pool + i;
364 MDBG_MOVE("root cons %d\n", MDBG_OFFSET(v));
365 visit_addr(&ao_lisp_cons_type, &v);
377 for (i = 0; i < ao_lisp_top; i += 4) {
378 if ((i & 0xff) == 0) {
382 else if ((i & 0x1f) == 0)
384 if (busy(ao_lisp_busy, i))
391 #define DUMP_BUSY() dump_busy()
396 static const struct ao_lisp_type const *ao_lisp_types[AO_LISP_NUM_TYPE] = {
397 [AO_LISP_CONS] = &ao_lisp_cons_type,
398 [AO_LISP_INT] = NULL,
399 [AO_LISP_STRING] = &ao_lisp_string_type,
400 [AO_LISP_OTHER] = (void *) 0x1,
401 [AO_LISP_ATOM] = &ao_lisp_atom_type,
402 [AO_LISP_BUILTIN] = &ao_lisp_builtin_type,
403 [AO_LISP_FRAME] = &ao_lisp_frame_type,
404 [AO_LISP_LAMBDA] = &ao_lisp_lambda_type,
408 ao_lisp_mark_ref(const struct ao_lisp_type *type, void **ref)
410 return ao_lisp_mark(type, *ref);
414 ao_lisp_poly_mark_ref(ao_poly *p, uint8_t do_note_cons)
416 return ao_lisp_poly_mark(*p, do_note_cons);
420 ao_lisp_collect(void)
428 struct ao_lisp_record *mark_record = NULL, *move_record = NULL;
431 MDBG_MOVE("collect %d\n", dbg_collects);
438 MDBG_MOVE("move chunks from %d to %d\n", chunk_low, top);
439 /* Find the sizes of the first chunk of objects to move */
440 memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
441 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
443 marked = total_marked;
445 ao_lisp_record_free(mark_record);
446 mark_record = ao_lisp_record_save();
447 if (mark_record && move_record)
448 ao_lisp_record_compare("mark", move_record, mark_record);
450 if (moved && moved != marked)
456 /* Find the first moving object */
457 for (i = 0; i < AO_LISP_NCHUNK; i++) {
458 uint16_t size = ao_lisp_chunk[i].size;
463 if (ao_lisp_chunk[i].old_addr > top)
465 if (ao_lisp_chunk[i].old_addr != top)
469 MDBG_MOVE("chunk %d %d not moving\n",
470 ao_lisp_chunk[i].old_addr,
471 ao_lisp_chunk[i].size);
472 chunk_low = ao_lisp_chunk[i].old_addr + size;
476 /* Copy all of the objects */
477 for (; i < AO_LISP_NCHUNK; i++) {
478 uint16_t size = ao_lisp_chunk[i].size;
483 MDBG_MOVE("chunk %d %d -> %d\n",
484 ao_lisp_chunk[i].old_addr,
487 ao_lisp_chunk[i].new_addr = top;
488 memmove(&ao_lisp_pool[top],
489 &ao_lisp_pool[ao_lisp_chunk[i].old_addr],
492 chunk_low = ao_lisp_chunk[i].old_addr + size;
497 if (chunk_first < chunk_last) {
498 /* Relocate all references to the objects */
499 walk(ao_lisp_move, ao_lisp_poly_move);
502 ao_lisp_record_free(move_record);
503 move_record = ao_lisp_record_save();
504 if (mark_record && move_record)
505 ao_lisp_record_compare("move", mark_record, move_record);
507 moved = total_marked;
513 if (chunk_last != AO_LISP_NCHUNK)
518 MDBG_DO(memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
519 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref));
521 // printf ("collect. top %d loops %d\n", top, loops);
525 * Mark interfaces for objects
527 * Note a reference to memory and
528 * collect information about a few object sizes
533 ao_lisp_mark_memory(const struct ao_lisp_type *type, void *addr)
536 if (!AO_LISP_IS_POOL(addr))
539 offset = pool_offset(addr);
540 MDBG_MOVE("mark memory %d\n", MDBG_OFFSET(addr));
541 if (busy(ao_lisp_busy, offset)) {
542 MDBG_MOVE("already marked\n");
545 mark(ao_lisp_busy, offset);
546 note_chunk(offset, ao_lisp_size(type, addr));
551 ao_lisp_mark(const struct ao_lisp_type *type, void *addr)
554 MDBG_MOVE("mark %d\n", MDBG_OFFSET(addr));
556 ret = ao_lisp_mark_memory(type, addr);
558 MDBG_MOVE("mark recurse\n");
566 ao_lisp_poly_mark(ao_poly p, uint8_t do_note_cons)
574 type = ao_lisp_poly_base_type(p);
575 addr = ao_lisp_ref(p);
577 if (!AO_LISP_IS_POOL(addr))
580 if (type == AO_LISP_CONS && do_note_cons) {
581 note_cons(ao_lisp_ref(p));
584 const struct ao_lisp_type *lisp_type;
586 if (type == AO_LISP_OTHER) {
587 type = ao_lisp_other_type(ao_lisp_poly_other(p));
588 if (type <= AO_LISP_OTHER || AO_LISP_NUM_TYPE <= type)
592 lisp_type = ao_lisp_types[ao_lisp_poly_type(p)];
595 return ao_lisp_mark(lisp_type, ao_lisp_ref(p));
602 uint16_t offset = pool_offset(addr);
605 for (i = chunk_first; i < chunk_last; i++) {
606 if (ao_lisp_chunk[i].old_addr == offset) {
607 MDBG_MOVE("move %d -> %d\n",
608 ao_lisp_chunk[i].old_addr,
609 ao_lisp_chunk[i].new_addr);
610 return ao_lisp_pool + ao_lisp_chunk[i].new_addr;
617 ao_lisp_move_memory(const struct ao_lisp_type *type, void **ref)
622 if (!AO_LISP_IS_POOL(addr))
625 MDBG_MOVE("move memory %d\n", MDBG_OFFSET(addr));
626 addr = move_map(addr);
628 MDBG_MOVE("update ref %d %d -> %d\n",
629 AO_LISP_IS_POOL(ref) ? MDBG_OFFSET(ref) : -1,
630 MDBG_OFFSET(*ref), MDBG_OFFSET(addr));
633 offset = pool_offset(addr);
634 if (busy(ao_lisp_busy, offset)) {
635 MDBG_MOVE("already moved\n");
638 mark(ao_lisp_busy, offset);
639 MDBG_DO(ao_lisp_record(type, addr, ao_lisp_size(type, addr)));
644 ao_lisp_move(const struct ao_lisp_type *type, void **ref)
647 MDBG_MOVE("move object %d\n", MDBG_OFFSET(*ref));
649 ret = ao_lisp_move_memory(type, ref);
651 MDBG_MOVE("move recurse\n");
659 ao_lisp_poly_move(ao_poly *ref, uint8_t do_note_cons)
669 addr = ao_lisp_ref(p);
671 if (!AO_LISP_IS_POOL(addr))
674 type = ao_lisp_poly_base_type(p);
676 if (type == AO_LISP_CONS && do_note_cons) {
678 addr = move_map(addr);
681 const struct ao_lisp_type *lisp_type;
683 if (type == AO_LISP_OTHER) {
684 type = ao_lisp_other_type(move_map(ao_lisp_poly_other(p)));
685 if (type <= AO_LISP_OTHER || AO_LISP_NUM_TYPE <= type)
689 lisp_type = ao_lisp_types[type];
692 ret = ao_lisp_move(lisp_type, &addr);
695 /* Re-write the poly value */
696 if (addr != ao_lisp_ref(p)) {
697 ao_poly np = ao_lisp_poly(addr, p & AO_LISP_TYPE_MASK);
698 MDBG_MOVE("poly %d moved %d -> %d\n",
699 type, MDBG_OFFSET(ao_lisp_ref(p)), MDBG_OFFSET(ao_lisp_ref(np)));
706 static int AO_LISP_POOL_CUR = AO_LISP_POOL / 8;
715 for (i = 0; i < AO_LISP_POOL_CUR; i += 4) {
716 uint32_t *a = (uint32_t *) &ao_lisp_pool[i];
717 if (!busy_object(ao_lisp_busy, a))
720 for (i = 0; i < AO_LISP_POOL_CUR; i += 2) {
721 ao_poly *a = (uint16_t *) &ao_lisp_pool[i];
724 if (!ao_lisp_is_const(p)) {
725 void *r = ao_lisp_ref(p);
727 if (ao_lisp_pool <= (uint8_t *) r &&
728 (uint8_t *) r <= ao_lisp_pool + AO_LISP_POOL_CUR)
730 if (!busy_object(ao_lisp_busy, r)) {
731 printf("missing reference from %d to %d\n",
732 (int) ((uint8_t *) a - ao_lisp_pool),
733 (int) ((uint8_t *) r - ao_lisp_pool));
741 #define AO_LISP_POOL_CUR AO_LISP_POOL
746 ao_lisp_validate(void)
749 memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
750 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
759 ao_lisp_alloc(int size)
763 MDBG_DO(++dbg_allocs);
764 MDBG_DO(if (dbg_validate) ao_lisp_validate());
765 size = ao_lisp_size_round(size);
766 if (ao_lisp_top + size > AO_LISP_POOL_CUR) {
768 if (AO_LISP_POOL_CUR < AO_LISP_POOL) {
769 AO_LISP_POOL_CUR += AO_LISP_POOL / 8;
778 for (i = ao_lisp_top; i < AO_LISP_POOL; i += 4) {
779 uint32_t *p = (uint32_t *) &ao_lisp_pool[i];
785 if (ao_lisp_top + size > AO_LISP_POOL) {
786 ao_lisp_error(AO_LISP_OOM, "out of memory");
790 addr = ao_lisp_pool + ao_lisp_top;
796 ao_lisp_cons_stash(int id, struct ao_lisp_cons *cons)
798 if (save_cons[id] != NULL)
800 save_cons[id] = cons;
803 struct ao_lisp_cons *
804 ao_lisp_cons_fetch(int id)
806 struct ao_lisp_cons *cons = save_cons[id];
807 save_cons[id] = NULL;
812 ao_lisp_string_stash(int id, char *string)
814 if (save_cons[id] != NULL)
816 save_string[id] = string;
820 ao_lisp_string_fetch(int id)
822 char *string = save_string[id];
823 save_string[id] = NULL;
827 ao_lisp_poly_stash(int id, ao_poly poly)
829 save_poly[id] = poly;
833 ao_lisp_poly_fetch(int id)
835 ao_poly poly = save_poly[id];
836 save_poly[id] = AO_LISP_NIL;