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,
219 #define AO_LISP_CACHE (sizeof (ao_lisp_cache) / sizeof (ao_lisp_cache[0]))
221 #define AO_LISP_BUSY_SIZE ((AO_LISP_POOL + 31) / 32)
223 static uint8_t ao_lisp_busy[AO_LISP_BUSY_SIZE];
224 static uint8_t ao_lisp_cons_note[AO_LISP_BUSY_SIZE];
225 static uint8_t ao_lisp_cons_last[AO_LISP_BUSY_SIZE];
226 static uint8_t ao_lisp_cons_noted;
228 uint16_t ao_lisp_top;
230 struct ao_lisp_chunk {
238 #define AO_LISP_NCHUNK 64
240 static struct ao_lisp_chunk ao_lisp_chunk[AO_LISP_NCHUNK];
242 /* Offset of an address within the pool. */
243 static inline uint16_t pool_offset(void *addr) {
245 if (!AO_LISP_IS_POOL(addr))
248 return ((uint8_t *) addr) - ao_lisp_pool;
252 * Convert back and forth between 'poly's used
253 * as short addresses in the pool and addresses.
254 * These are used in the chunk code.
256 static inline ao_poly pool_poly(void *addr) {
258 if (!AO_LISP_IS_POOL(addr))
261 return ((uint8_t *) addr) - AO_LISP_POOL_BASE;
264 static inline void *pool_ref(ao_poly p) {
265 return AO_LISP_POOL_BASE + p;
268 static inline void mark(uint8_t *tag, int offset) {
269 int byte = offset >> 5;
270 int bit = (offset >> 2) & 7;
271 tag[byte] |= (1 << bit);
274 static inline void clear(uint8_t *tag, int offset) {
275 int byte = offset >> 5;
276 int bit = (offset >> 2) & 7;
277 tag[byte] &= ~(1 << bit);
280 static inline int busy(uint8_t *tag, int offset) {
281 int byte = offset >> 5;
282 int bit = (offset >> 2) & 7;
283 return (tag[byte] >> bit) & 1;
286 static inline int min(int a, int b) { return a < b ? a : b; }
287 static inline int max(int a, int b) { return a > b ? a : b; }
289 static inline int limit(int offset) {
290 return min(AO_LISP_POOL, max(offset, 0));
293 static int total_marked;
296 note_cons(void *addr)
298 if (AO_LISP_IS_POOL(addr)) {
299 int offset = pool_offset(addr);
300 MDBG_MOVE("note cons %d\n", MDBG_OFFSET(addr));
301 ao_lisp_cons_noted = 1;
302 mark(ao_lisp_cons_note, offset);
306 static uint16_t chunk_low;
307 static uint16_t chunk_first, chunk_last;
310 note_chunk(uint16_t addr, uint16_t size)
314 if (addr < chunk_low)
317 for (i = 0; i < AO_LISP_NCHUNK; i++) {
318 if (ao_lisp_chunk[i].size && ao_lisp_chunk[i].old_addr == addr) {
320 if (ao_lisp_chunk[i].size != size)
325 if (ao_lisp_chunk[i].old_addr > addr) {
326 memmove(&ao_lisp_chunk[i+1],
328 (AO_LISP_NCHUNK - (i+1)) * sizeof (struct ao_lisp_chunk));
329 ao_lisp_chunk[i].size = 0;
331 if (ao_lisp_chunk[i].size == 0) {
332 ao_lisp_chunk[i].old_addr = addr;
333 ao_lisp_chunk[i].size = size;
340 * Walk all referenced objects calling functions on each one
344 walk(int (*visit_addr)(const struct ao_lisp_type *type, void **addr),
345 int (*visit_poly)(ao_poly *p, uint8_t do_note_cons))
350 ao_lisp_record_reset();
351 memset(ao_lisp_busy, '\0', sizeof (ao_lisp_busy));
352 memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
353 ao_lisp_cons_noted = 0;
354 for (i = 0; i < (int) AO_LISP_ROOT; i++) {
355 if (ao_lisp_root[i].type) {
356 void **a = ao_lisp_root[i].addr, *v;
358 MDBG_MOVE("root ptr %d\n", MDBG_OFFSET(v));
359 visit_addr(ao_lisp_root[i].type, a);
362 ao_poly *a = (ao_poly *) ao_lisp_root[i].addr, p;
364 MDBG_MOVE("root poly %d\n", MDBG_OFFSET(ao_lisp_ref(p)));
369 while (ao_lisp_cons_noted) {
370 memcpy(ao_lisp_cons_last, ao_lisp_cons_note, sizeof (ao_lisp_cons_note));
371 memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
372 ao_lisp_cons_noted = 0;
373 for (i = 0; i < AO_LISP_POOL; i += 4) {
374 if (busy(ao_lisp_cons_last, i)) {
375 void *v = ao_lisp_pool + i;
376 MDBG_MOVE("root cons %d\n", MDBG_OFFSET(v));
377 visit_addr(&ao_lisp_cons_type, &v);
389 for (i = 0; i < ao_lisp_top; i += 4) {
390 if ((i & 0xff) == 0) {
394 else if ((i & 0x1f) == 0)
396 if (busy(ao_lisp_busy, i))
403 #define DUMP_BUSY() dump_busy()
408 static const struct ao_lisp_type const *ao_lisp_types[AO_LISP_NUM_TYPE] = {
409 [AO_LISP_CONS] = &ao_lisp_cons_type,
410 [AO_LISP_INT] = NULL,
411 [AO_LISP_STRING] = &ao_lisp_string_type,
412 [AO_LISP_OTHER] = (void *) 0x1,
413 [AO_LISP_ATOM] = &ao_lisp_atom_type,
414 [AO_LISP_BUILTIN] = &ao_lisp_builtin_type,
415 [AO_LISP_FRAME] = &ao_lisp_frame_type,
416 [AO_LISP_LAMBDA] = &ao_lisp_lambda_type,
420 ao_lisp_mark_ref(const struct ao_lisp_type *type, void **ref)
422 return ao_lisp_mark(type, *ref);
426 ao_lisp_poly_mark_ref(ao_poly *p, uint8_t do_note_cons)
428 return ao_lisp_poly_mark(*p, do_note_cons);
431 int ao_lisp_collects;
434 ao_lisp_collect(void)
442 struct ao_lisp_record *mark_record = NULL, *move_record = NULL;
444 MDBG_MOVE("collect %d\n", ao_lisp_collects);
450 /* Clear references to all caches */
451 for (i = 0; i < (int) AO_LISP_CACHE; i++)
452 *ao_lisp_cache[i] = NULL;
457 MDBG_MOVE("move chunks from %d to %d\n", chunk_low, top);
458 /* Find the sizes of the first chunk of objects to move */
459 memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
460 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
462 marked = total_marked;
464 ao_lisp_record_free(mark_record);
465 mark_record = ao_lisp_record_save();
466 if (mark_record && move_record)
467 ao_lisp_record_compare("mark", move_record, mark_record);
469 if (moved && moved != marked)
475 /* Find the first moving object */
476 for (i = 0; i < AO_LISP_NCHUNK; i++) {
477 uint16_t size = ao_lisp_chunk[i].size;
482 if (ao_lisp_chunk[i].old_addr > top)
485 if (ao_lisp_chunk[i].old_addr != top)
490 MDBG_MOVE("chunk %d %d not moving\n",
491 ao_lisp_chunk[i].old_addr,
492 ao_lisp_chunk[i].size);
493 chunk_low = ao_lisp_chunk[i].old_addr + size;
497 /* Copy all of the objects */
498 for (; i < AO_LISP_NCHUNK; i++) {
499 uint16_t size = ao_lisp_chunk[i].size;
504 MDBG_MOVE("chunk %d %d -> %d\n",
505 ao_lisp_chunk[i].old_addr,
508 ao_lisp_chunk[i].new_addr = top;
509 memmove(&ao_lisp_pool[top],
510 &ao_lisp_pool[ao_lisp_chunk[i].old_addr],
513 chunk_low = ao_lisp_chunk[i].old_addr + size;
518 if (chunk_first < chunk_last) {
519 /* Relocate all references to the objects */
520 walk(ao_lisp_move, ao_lisp_poly_move);
523 ao_lisp_record_free(move_record);
524 move_record = ao_lisp_record_save();
525 if (mark_record && move_record)
526 ao_lisp_record_compare("move", mark_record, move_record);
528 moved = total_marked;
534 if (chunk_last != AO_LISP_NCHUNK)
539 MDBG_DO(memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
540 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref));
542 // printf ("collect. top %d loops %d\n", top, loops);
546 * Mark interfaces for objects
548 * Note a reference to memory and
549 * collect information about a few object sizes
554 ao_lisp_mark_memory(const struct ao_lisp_type *type, void *addr)
557 if (!AO_LISP_IS_POOL(addr))
560 offset = pool_offset(addr);
561 MDBG_MOVE("mark memory %d\n", MDBG_OFFSET(addr));
562 if (busy(ao_lisp_busy, offset)) {
563 MDBG_MOVE("already marked\n");
566 mark(ao_lisp_busy, offset);
567 note_chunk(offset, ao_lisp_size(type, addr));
572 ao_lisp_mark(const struct ao_lisp_type *type, void *addr)
575 MDBG_MOVE("mark %d\n", MDBG_OFFSET(addr));
577 ret = ao_lisp_mark_memory(type, addr);
579 MDBG_MOVE("mark recurse\n");
587 ao_lisp_poly_mark(ao_poly p, uint8_t do_note_cons)
595 type = ao_lisp_poly_base_type(p);
596 addr = ao_lisp_ref(p);
598 if (!AO_LISP_IS_POOL(addr))
601 if (type == AO_LISP_CONS && do_note_cons) {
602 note_cons(ao_lisp_ref(p));
605 const struct ao_lisp_type *lisp_type;
607 if (type == AO_LISP_OTHER) {
608 type = ao_lisp_other_type(ao_lisp_poly_other(p));
610 if (type <= AO_LISP_OTHER || AO_LISP_NUM_TYPE <= type)
615 lisp_type = ao_lisp_types[ao_lisp_poly_type(p)];
618 return ao_lisp_mark(lisp_type, ao_lisp_ref(p));
625 uint16_t offset = pool_offset(addr);
628 for (i = chunk_first; i < chunk_last; i++) {
629 if (ao_lisp_chunk[i].old_addr == offset) {
630 MDBG_MOVE("move %d -> %d\n",
631 ao_lisp_chunk[i].old_addr,
632 ao_lisp_chunk[i].new_addr);
633 return ao_lisp_pool + ao_lisp_chunk[i].new_addr;
640 ao_lisp_move_memory(const struct ao_lisp_type *type, void **ref)
645 if (!AO_LISP_IS_POOL(addr))
650 MDBG_MOVE("move memory %d\n", MDBG_OFFSET(addr));
651 addr = move_map(addr);
653 MDBG_MOVE("update ref %d %d -> %d\n",
654 AO_LISP_IS_POOL(ref) ? MDBG_OFFSET(ref) : -1,
655 MDBG_OFFSET(*ref), MDBG_OFFSET(addr));
658 offset = pool_offset(addr);
659 if (busy(ao_lisp_busy, offset)) {
660 MDBG_MOVE("already moved\n");
663 mark(ao_lisp_busy, offset);
664 MDBG_DO(ao_lisp_record(type, addr, ao_lisp_size(type, addr)));
669 ao_lisp_move(const struct ao_lisp_type *type, void **ref)
672 MDBG_MOVE("move object %d\n", MDBG_OFFSET(*ref));
674 ret = ao_lisp_move_memory(type, ref);
676 MDBG_MOVE("move recurse\n");
684 ao_lisp_poly_move(ao_poly *ref, uint8_t do_note_cons)
694 addr = ao_lisp_ref(p);
696 if (!AO_LISP_IS_POOL(addr))
699 type = ao_lisp_poly_base_type(p);
701 if (type == AO_LISP_CONS && do_note_cons) {
703 addr = move_map(addr);
706 const struct ao_lisp_type *lisp_type;
708 if (type == AO_LISP_OTHER) {
709 type = ao_lisp_other_type(move_map(ao_lisp_poly_other(p)));
711 if (type <= AO_LISP_OTHER || AO_LISP_NUM_TYPE <= type)
716 lisp_type = ao_lisp_types[type];
719 ret = ao_lisp_move(lisp_type, &addr);
722 /* Re-write the poly value */
723 if (addr != ao_lisp_ref(p)) {
724 ao_poly np = ao_lisp_poly(addr, p & AO_LISP_TYPE_MASK);
725 MDBG_MOVE("poly %d moved %d -> %d\n",
726 type, MDBG_OFFSET(ao_lisp_ref(p)), MDBG_OFFSET(ao_lisp_ref(np)));
733 static int AO_LISP_POOL_CUR = AO_LISP_POOL / 8;
742 for (i = 0; i < AO_LISP_POOL_CUR; i += 4) {
743 uint32_t *a = (uint32_t *) &ao_lisp_pool[i];
744 if (!busy_object(ao_lisp_busy, a))
747 for (i = 0; i < AO_LISP_POOL_CUR; i += 2) {
748 ao_poly *a = (uint16_t *) &ao_lisp_pool[i];
751 if (!ao_lisp_is_const(p)) {
752 void *r = ao_lisp_ref(p);
754 if (ao_lisp_pool <= (uint8_t *) r &&
755 (uint8_t *) r <= ao_lisp_pool + AO_LISP_POOL_CUR)
757 if (!busy_object(ao_lisp_busy, r)) {
758 printf("missing reference from %d to %d\n",
759 (int) ((uint8_t *) a - ao_lisp_pool),
760 (int) ((uint8_t *) r - ao_lisp_pool));
768 #define AO_LISP_POOL_CUR AO_LISP_POOL
773 ao_lisp_validate(void)
776 memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
777 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
786 ao_lisp_alloc(int size)
790 MDBG_DO(++dbg_allocs);
791 MDBG_DO(if (dbg_validate) ao_lisp_validate());
792 size = ao_lisp_size_round(size);
793 if (ao_lisp_top + size > AO_LISP_POOL_CUR) {
795 if (AO_LISP_POOL_CUR < AO_LISP_POOL) {
796 AO_LISP_POOL_CUR += AO_LISP_POOL / 8;
805 for (i = ao_lisp_top; i < AO_LISP_POOL; i += 4) {
806 uint32_t *p = (uint32_t *) &ao_lisp_pool[i];
812 if (ao_lisp_top + size > AO_LISP_POOL) {
813 ao_lisp_error(AO_LISP_OOM, "out of memory");
817 addr = ao_lisp_pool + ao_lisp_top;
823 ao_lisp_cons_stash(int id, struct ao_lisp_cons *cons)
825 save_cons[id] = cons;
828 struct ao_lisp_cons *
829 ao_lisp_cons_fetch(int id)
831 struct ao_lisp_cons *cons = save_cons[id];
832 save_cons[id] = NULL;
837 ao_lisp_string_stash(int id, char *string)
839 save_string[id] = string;
843 ao_lisp_string_fetch(int id)
845 char *string = save_string[id];
846 save_string[id] = NULL;
850 ao_lisp_poly_stash(int id, ao_poly poly)
852 save_poly[id] = poly;
856 ao_lisp_poly_fetch(int id)
858 ao_poly poly = save_poly[id];
859 save_poly[id] = AO_LISP_NIL;