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;
48 struct ao_lisp_record {
49 struct ao_lisp_record *next;
50 const struct ao_lisp_type *type;
55 static struct ao_lisp_record *record_head, **record_tail;
58 ao_lisp_record_free(struct ao_lisp_record *record)
61 struct ao_lisp_record *next = record->next;
68 ao_lisp_record_reset(void)
70 ao_lisp_record_free(record_head);
72 record_tail = &record_head;
76 ao_lisp_record(const struct ao_lisp_type *type,
80 struct ao_lisp_record *r = malloc(sizeof (struct ao_lisp_record));
87 record_tail = &r->next;
90 static struct ao_lisp_record *
91 ao_lisp_record_save(void)
93 struct ao_lisp_record *r = record_head;
96 record_tail = &record_head;
101 ao_lisp_record_compare(char *where,
102 struct ao_lisp_record *a,
103 struct ao_lisp_record *b)
106 if (a->type != b->type || a->size != b->size) {
107 printf("%s record difers %d %s %d -> %d %s %d\n",
109 MDBG_OFFSET(a->addr),
112 MDBG_OFFSET(b->addr),
121 printf("%s record differs %d %s %d -> NULL\n",
123 MDBG_OFFSET(a->addr),
129 printf("%s record differs NULL -> %d %s %d\n",
131 MDBG_OFFSET(b->addr),
139 #define ao_lisp_record_reset()
142 uint8_t ao_lisp_exception;
144 struct ao_lisp_root {
145 const struct ao_lisp_type *type;
149 static struct ao_lisp_cons *save_cons[2];
150 static char *save_string[2];
151 static ao_poly save_poly[2];
153 static const struct ao_lisp_root ao_lisp_root[] = {
155 .type = &ao_lisp_cons_type,
156 .addr = (void **) &save_cons[0],
159 .type = &ao_lisp_cons_type,
160 .addr = (void **) &save_cons[1],
163 .type = &ao_lisp_string_type,
164 .addr = (void **) &save_string[0]
167 .type = &ao_lisp_string_type,
168 .addr = (void **) &save_string[1]
172 .addr = (void **) &save_poly[0]
176 .addr = (void **) &save_poly[1]
179 .type = &ao_lisp_atom_type,
180 .addr = (void **) &ao_lisp_atoms
183 .type = &ao_lisp_frame_type,
184 .addr = (void **) &ao_lisp_frame_global,
187 .type = &ao_lisp_frame_type,
188 .addr = (void **) &ao_lisp_frame_current,
191 .type = &ao_lisp_stack_type,
192 .addr = (void **) &ao_lisp_stack,
196 .addr = (void **) &ao_lisp_v,
199 .type = &ao_lisp_cons_type,
200 .addr = (void **) &ao_lisp_read_cons,
203 .type = &ao_lisp_cons_type,
204 .addr = (void **) &ao_lisp_read_cons_tail,
207 .type = &ao_lisp_cons_type,
208 .addr = (void **) &ao_lisp_read_stack,
212 #define AO_LISP_ROOT (sizeof (ao_lisp_root) / sizeof (ao_lisp_root[0]))
214 static const void ** const ao_lisp_cache[] = {
215 (const void **) &ao_lisp_cons_free_list,
216 (const void **) &ao_lisp_stack_free_list,
217 (const void **) &ao_lisp_frame_free_list[0],
218 (const void **) &ao_lisp_frame_free_list[1],
219 (const void **) &ao_lisp_frame_free_list[2],
220 (const void **) &ao_lisp_frame_free_list[3],
223 #if AO_LISP_FRAME_FREE != 4
224 #error Unexpected AO_LISP_FRAME_FREE value
227 #define AO_LISP_CACHE (sizeof (ao_lisp_cache) / sizeof (ao_lisp_cache[0]))
229 #define AO_LISP_BUSY_SIZE ((AO_LISP_POOL + 31) / 32)
231 static uint8_t ao_lisp_busy[AO_LISP_BUSY_SIZE];
232 static uint8_t ao_lisp_cons_note[AO_LISP_BUSY_SIZE];
233 static uint8_t ao_lisp_cons_last[AO_LISP_BUSY_SIZE];
234 static uint8_t ao_lisp_cons_noted;
236 uint16_t ao_lisp_top;
238 struct ao_lisp_chunk {
246 #define AO_LISP_NCHUNK 64
248 static struct ao_lisp_chunk ao_lisp_chunk[AO_LISP_NCHUNK];
250 /* Offset of an address within the pool. */
251 static inline uint16_t pool_offset(void *addr) {
253 if (!AO_LISP_IS_POOL(addr))
256 return ((uint8_t *) addr) - ao_lisp_pool;
260 * Convert back and forth between 'poly's used
261 * as short addresses in the pool and addresses.
262 * These are used in the chunk code.
264 static inline ao_poly pool_poly(void *addr) {
266 if (!AO_LISP_IS_POOL(addr))
269 return ((uint8_t *) addr) - AO_LISP_POOL_BASE;
272 static inline void *pool_ref(ao_poly p) {
273 return AO_LISP_POOL_BASE + p;
276 static inline void mark(uint8_t *tag, int offset) {
277 int byte = offset >> 5;
278 int bit = (offset >> 2) & 7;
279 tag[byte] |= (1 << bit);
282 static inline void clear(uint8_t *tag, int offset) {
283 int byte = offset >> 5;
284 int bit = (offset >> 2) & 7;
285 tag[byte] &= ~(1 << bit);
288 static inline int busy(uint8_t *tag, int offset) {
289 int byte = offset >> 5;
290 int bit = (offset >> 2) & 7;
291 return (tag[byte] >> bit) & 1;
294 static inline int min(int a, int b) { return a < b ? a : b; }
295 static inline int max(int a, int b) { return a > b ? a : b; }
297 static inline int limit(int offset) {
298 return min(AO_LISP_POOL, max(offset, 0));
301 static int total_marked;
304 note_cons(void *addr)
306 if (AO_LISP_IS_POOL(addr)) {
307 int offset = pool_offset(addr);
308 MDBG_MOVE("note cons %d\n", MDBG_OFFSET(addr));
309 ao_lisp_cons_noted = 1;
310 mark(ao_lisp_cons_note, offset);
314 static uint16_t chunk_low;
315 static uint16_t chunk_first, chunk_last;
318 note_chunk(uint16_t addr, uint16_t size)
322 if (addr < chunk_low)
325 for (i = 0; i < AO_LISP_NCHUNK; i++) {
326 if (ao_lisp_chunk[i].size && ao_lisp_chunk[i].old_addr == addr) {
328 if (ao_lisp_chunk[i].size != size)
333 if (ao_lisp_chunk[i].old_addr > addr) {
334 memmove(&ao_lisp_chunk[i+1],
336 (AO_LISP_NCHUNK - (i+1)) * sizeof (struct ao_lisp_chunk));
337 ao_lisp_chunk[i].size = 0;
339 if (ao_lisp_chunk[i].size == 0) {
340 ao_lisp_chunk[i].old_addr = addr;
341 ao_lisp_chunk[i].size = size;
348 * Walk all referenced objects calling functions on each one
352 walk(int (*visit_addr)(const struct ao_lisp_type *type, void **addr),
353 int (*visit_poly)(ao_poly *p, uint8_t do_note_cons))
358 ao_lisp_record_reset();
359 memset(ao_lisp_busy, '\0', sizeof (ao_lisp_busy));
360 memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
361 ao_lisp_cons_noted = 0;
362 for (i = 0; i < (int) AO_LISP_ROOT; i++) {
363 if (ao_lisp_root[i].type) {
364 void **a = ao_lisp_root[i].addr, *v;
366 MDBG_MOVE("root ptr %d\n", MDBG_OFFSET(v));
367 visit_addr(ao_lisp_root[i].type, a);
370 ao_poly *a = (ao_poly *) ao_lisp_root[i].addr, p;
372 MDBG_MOVE("root poly %d\n", MDBG_OFFSET(ao_lisp_ref(p)));
377 while (ao_lisp_cons_noted) {
378 memcpy(ao_lisp_cons_last, ao_lisp_cons_note, sizeof (ao_lisp_cons_note));
379 memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
380 ao_lisp_cons_noted = 0;
381 for (i = 0; i < AO_LISP_POOL; i += 4) {
382 if (busy(ao_lisp_cons_last, i)) {
383 void *v = ao_lisp_pool + i;
384 MDBG_MOVE("root cons %d\n", MDBG_OFFSET(v));
385 visit_addr(&ao_lisp_cons_type, &v);
397 for (i = 0; i < ao_lisp_top; i += 4) {
398 if ((i & 0xff) == 0) {
402 else if ((i & 0x1f) == 0)
404 if (busy(ao_lisp_busy, i))
411 #define DUMP_BUSY() dump_busy()
416 static const struct ao_lisp_type const *ao_lisp_types[AO_LISP_NUM_TYPE] = {
417 [AO_LISP_CONS] = &ao_lisp_cons_type,
418 [AO_LISP_INT] = NULL,
419 [AO_LISP_STRING] = &ao_lisp_string_type,
420 [AO_LISP_OTHER] = (void *) 0x1,
421 [AO_LISP_ATOM] = &ao_lisp_atom_type,
422 [AO_LISP_BUILTIN] = &ao_lisp_builtin_type,
423 [AO_LISP_FRAME] = &ao_lisp_frame_type,
424 [AO_LISP_LAMBDA] = &ao_lisp_lambda_type,
428 ao_lisp_mark_ref(const struct ao_lisp_type *type, void **ref)
430 return ao_lisp_mark(type, *ref);
434 ao_lisp_poly_mark_ref(ao_poly *p, uint8_t do_note_cons)
436 return ao_lisp_poly_mark(*p, do_note_cons);
439 int ao_lisp_collects;
442 ao_lisp_collect(void)
450 struct ao_lisp_record *mark_record = NULL, *move_record = NULL;
452 MDBG_MOVE("collect %d\n", ao_lisp_collects);
458 /* Clear references to all caches */
459 for (i = 0; i < (int) AO_LISP_CACHE; i++)
460 *ao_lisp_cache[i] = NULL;
465 MDBG_MOVE("move chunks from %d to %d\n", chunk_low, top);
466 /* Find the sizes of the first chunk of objects to move */
467 memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
468 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
470 marked = total_marked;
472 ao_lisp_record_free(mark_record);
473 mark_record = ao_lisp_record_save();
474 if (mark_record && move_record)
475 ao_lisp_record_compare("mark", move_record, mark_record);
477 if (moved && moved != marked)
483 /* Find the first moving object */
484 for (i = 0; i < AO_LISP_NCHUNK; i++) {
485 uint16_t size = ao_lisp_chunk[i].size;
490 if (ao_lisp_chunk[i].old_addr > top)
493 if (ao_lisp_chunk[i].old_addr != top)
498 MDBG_MOVE("chunk %d %d not moving\n",
499 ao_lisp_chunk[i].old_addr,
500 ao_lisp_chunk[i].size);
501 chunk_low = ao_lisp_chunk[i].old_addr + size;
505 /* Copy all of the objects */
506 for (; i < AO_LISP_NCHUNK; i++) {
507 uint16_t size = ao_lisp_chunk[i].size;
512 MDBG_MOVE("chunk %d %d -> %d\n",
513 ao_lisp_chunk[i].old_addr,
516 ao_lisp_chunk[i].new_addr = top;
517 memmove(&ao_lisp_pool[top],
518 &ao_lisp_pool[ao_lisp_chunk[i].old_addr],
521 chunk_low = ao_lisp_chunk[i].old_addr + size;
526 if (chunk_first < chunk_last) {
527 /* Relocate all references to the objects */
528 walk(ao_lisp_move, ao_lisp_poly_move);
531 ao_lisp_record_free(move_record);
532 move_record = ao_lisp_record_save();
533 if (mark_record && move_record)
534 ao_lisp_record_compare("move", mark_record, move_record);
536 moved = total_marked;
542 if (chunk_last != AO_LISP_NCHUNK)
547 MDBG_DO(memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
548 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref));
550 // printf ("collect. top %d loops %d\n", top, loops);
554 * Mark interfaces for objects
556 * Note a reference to memory and
557 * collect information about a few object sizes
562 ao_lisp_mark_memory(const struct ao_lisp_type *type, void *addr)
565 if (!AO_LISP_IS_POOL(addr))
568 offset = pool_offset(addr);
569 MDBG_MOVE("mark memory %d\n", MDBG_OFFSET(addr));
570 if (busy(ao_lisp_busy, offset)) {
571 MDBG_MOVE("already marked\n");
574 mark(ao_lisp_busy, offset);
575 note_chunk(offset, ao_lisp_size(type, addr));
580 ao_lisp_mark(const struct ao_lisp_type *type, void *addr)
583 MDBG_MOVE("mark %d\n", MDBG_OFFSET(addr));
585 ret = ao_lisp_mark_memory(type, addr);
587 MDBG_MOVE("mark recurse\n");
595 ao_lisp_poly_mark(ao_poly p, uint8_t do_note_cons)
603 type = ao_lisp_poly_base_type(p);
604 addr = ao_lisp_ref(p);
606 if (!AO_LISP_IS_POOL(addr))
609 if (type == AO_LISP_CONS && do_note_cons) {
610 note_cons(ao_lisp_ref(p));
613 const struct ao_lisp_type *lisp_type;
615 if (type == AO_LISP_OTHER) {
616 type = ao_lisp_other_type(ao_lisp_poly_other(p));
618 if (type <= AO_LISP_OTHER || AO_LISP_NUM_TYPE <= type)
623 lisp_type = ao_lisp_types[ao_lisp_poly_type(p)];
626 return ao_lisp_mark(lisp_type, ao_lisp_ref(p));
633 uint16_t offset = pool_offset(addr);
636 for (i = chunk_first; i < chunk_last; i++) {
637 if (ao_lisp_chunk[i].old_addr == offset) {
638 MDBG_MOVE("move %d -> %d\n",
639 ao_lisp_chunk[i].old_addr,
640 ao_lisp_chunk[i].new_addr);
641 return ao_lisp_pool + ao_lisp_chunk[i].new_addr;
648 ao_lisp_move_memory(const struct ao_lisp_type *type, void **ref)
653 if (!AO_LISP_IS_POOL(addr))
658 MDBG_MOVE("move memory %d\n", MDBG_OFFSET(addr));
659 addr = move_map(addr);
661 MDBG_MOVE("update ref %d %d -> %d\n",
662 AO_LISP_IS_POOL(ref) ? MDBG_OFFSET(ref) : -1,
663 MDBG_OFFSET(*ref), MDBG_OFFSET(addr));
666 offset = pool_offset(addr);
667 if (busy(ao_lisp_busy, offset)) {
668 MDBG_MOVE("already moved\n");
671 mark(ao_lisp_busy, offset);
672 MDBG_DO(ao_lisp_record(type, addr, ao_lisp_size(type, addr)));
677 ao_lisp_move(const struct ao_lisp_type *type, void **ref)
680 MDBG_MOVE("move object %d\n", MDBG_OFFSET(*ref));
682 ret = ao_lisp_move_memory(type, ref);
684 MDBG_MOVE("move recurse\n");
692 ao_lisp_poly_move(ao_poly *ref, uint8_t do_note_cons)
702 addr = ao_lisp_ref(p);
704 if (!AO_LISP_IS_POOL(addr))
707 type = ao_lisp_poly_base_type(p);
709 if (type == AO_LISP_CONS && do_note_cons) {
711 addr = move_map(addr);
714 const struct ao_lisp_type *lisp_type;
716 if (type == AO_LISP_OTHER) {
717 type = ao_lisp_other_type(move_map(ao_lisp_poly_other(p)));
719 if (type <= AO_LISP_OTHER || AO_LISP_NUM_TYPE <= type)
724 lisp_type = ao_lisp_types[type];
727 ret = ao_lisp_move(lisp_type, &addr);
730 /* Re-write the poly value */
731 if (addr != ao_lisp_ref(p)) {
732 ao_poly np = ao_lisp_poly(addr, p & AO_LISP_TYPE_MASK);
733 MDBG_MOVE("poly %d moved %d -> %d\n",
734 type, MDBG_OFFSET(ao_lisp_ref(p)), MDBG_OFFSET(ao_lisp_ref(np)));
741 static int AO_LISP_POOL_CUR = AO_LISP_POOL / 8;
750 for (i = 0; i < AO_LISP_POOL_CUR; i += 4) {
751 uint32_t *a = (uint32_t *) &ao_lisp_pool[i];
752 if (!busy_object(ao_lisp_busy, a))
755 for (i = 0; i < AO_LISP_POOL_CUR; i += 2) {
756 ao_poly *a = (uint16_t *) &ao_lisp_pool[i];
759 if (!ao_lisp_is_const(p)) {
760 void *r = ao_lisp_ref(p);
762 if (ao_lisp_pool <= (uint8_t *) r &&
763 (uint8_t *) r <= ao_lisp_pool + AO_LISP_POOL_CUR)
765 if (!busy_object(ao_lisp_busy, r)) {
766 printf("missing reference from %d to %d\n",
767 (int) ((uint8_t *) a - ao_lisp_pool),
768 (int) ((uint8_t *) r - ao_lisp_pool));
776 #define AO_LISP_POOL_CUR AO_LISP_POOL
781 ao_lisp_validate(void)
784 memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
785 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
794 ao_lisp_alloc(int size)
798 MDBG_DO(++dbg_allocs);
799 MDBG_DO(if (dbg_validate) ao_lisp_validate());
800 size = ao_lisp_size_round(size);
801 if (ao_lisp_top + size > AO_LISP_POOL_CUR) {
803 if (AO_LISP_POOL_CUR < AO_LISP_POOL) {
804 AO_LISP_POOL_CUR += AO_LISP_POOL / 8;
813 for (i = ao_lisp_top; i < AO_LISP_POOL; i += 4) {
814 uint32_t *p = (uint32_t *) &ao_lisp_pool[i];
820 if (ao_lisp_top + size > AO_LISP_POOL) {
821 ao_lisp_error(AO_LISP_OOM, "out of memory");
825 addr = ao_lisp_pool + ao_lisp_top;
831 ao_lisp_cons_stash(int id, struct ao_lisp_cons *cons)
833 save_cons[id] = cons;
836 struct ao_lisp_cons *
837 ao_lisp_cons_fetch(int id)
839 struct ao_lisp_cons *cons = save_cons[id];
840 save_cons[id] = NULL;
845 ao_lisp_string_stash(int id, char *string)
847 save_string[id] = string;
851 ao_lisp_string_fetch(int id)
853 char *string = save_string[id];
854 save_string[id] = NULL;
858 ao_lisp_poly_stash(int id, ao_poly poly)
860 save_poly[id] = poly;
864 ao_lisp_poly_fetch(int id)
866 ao_poly poly = save_poly[id];
867 save_poly[id] = AO_LISP_NIL;