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_SCHEME_CONST_BITS
17 #include "ao_scheme.h"
21 #ifdef AO_SCHEME_MAKE_CONST
24 * When building the constant table, it is the
25 * pool for allocations.
29 uint8_t ao_scheme_const[AO_SCHEME_POOL_CONST] __attribute__((aligned(4)));
30 #define ao_scheme_pool ao_scheme_const
32 #define AO_SCHEME_POOL AO_SCHEME_POOL_CONST
36 uint8_t ao_scheme_pool[AO_SCHEME_POOL + AO_SCHEME_POOL_EXTRA] __attribute__((aligned(4)));
41 #define DBG_MEM_STATS DBG_MEM
46 int dbg_mem = DBG_MEM_START;
49 struct ao_scheme_record {
50 struct ao_scheme_record *next;
51 const struct ao_scheme_type *type;
56 static struct ao_scheme_record *record_head, **record_tail;
59 ao_scheme_record_free(struct ao_scheme_record *record)
62 struct ao_scheme_record *next = record->next;
69 ao_scheme_record_reset(void)
71 ao_scheme_record_free(record_head);
73 record_tail = &record_head;
77 ao_scheme_record(const struct ao_scheme_type *type,
81 struct ao_scheme_record *r = malloc(sizeof (struct ao_scheme_record));
88 record_tail = &r->next;
91 static struct ao_scheme_record *
92 ao_scheme_record_save(void)
94 struct ao_scheme_record *r = record_head;
97 record_tail = &record_head;
102 ao_scheme_record_compare(char *where,
103 struct ao_scheme_record *a,
104 struct ao_scheme_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_scheme_record_reset()
143 uint8_t ao_scheme_exception;
145 struct ao_scheme_root {
146 const struct ao_scheme_type *type;
150 static struct ao_scheme_cons *save_cons[2];
151 static char *save_string[2];
152 static struct ao_scheme_frame *save_frame[1];
153 static ao_poly save_poly[3];
155 static const struct ao_scheme_root ao_scheme_root[] = {
157 .type = &ao_scheme_cons_type,
158 .addr = (void **) &save_cons[0],
161 .type = &ao_scheme_cons_type,
162 .addr = (void **) &save_cons[1],
165 .type = &ao_scheme_string_type,
166 .addr = (void **) &save_string[0],
169 .type = &ao_scheme_string_type,
170 .addr = (void **) &save_string[1],
173 .type = &ao_scheme_frame_type,
174 .addr = (void **) &save_frame[0],
178 .addr = (void **) (void *) &save_poly[0]
182 .addr = (void **) (void *) &save_poly[1]
186 .addr = (void **) (void *) &save_poly[2]
189 .type = &ao_scheme_atom_type,
190 .addr = (void **) &ao_scheme_atoms
193 .type = &ao_scheme_frame_type,
194 .addr = (void **) &ao_scheme_frame_global,
197 .type = &ao_scheme_frame_type,
198 .addr = (void **) &ao_scheme_frame_current,
201 .type = &ao_scheme_stack_type,
202 .addr = (void **) &ao_scheme_stack,
206 .addr = (void **) (void *) &ao_scheme_v,
209 .type = &ao_scheme_cons_type,
210 .addr = (void **) &ao_scheme_read_cons,
213 .type = &ao_scheme_cons_type,
214 .addr = (void **) &ao_scheme_read_cons_tail,
217 .type = &ao_scheme_cons_type,
218 .addr = (void **) &ao_scheme_read_stack,
220 #ifdef AO_SCHEME_MAKE_CONST
222 .type = &ao_scheme_bool_type,
223 .addr = (void **) &ao_scheme_false,
226 .type = &ao_scheme_bool_type,
227 .addr = (void **) &ao_scheme_true,
232 #define AO_SCHEME_ROOT (sizeof (ao_scheme_root) / sizeof (ao_scheme_root[0]))
234 static const void ** const ao_scheme_cache[] = {
235 (const void **) &ao_scheme_cons_free_list,
236 (const void **) &ao_scheme_stack_free_list,
237 (const void **) &ao_scheme_frame_free_list[0],
238 (const void **) &ao_scheme_frame_free_list[1],
239 (const void **) &ao_scheme_frame_free_list[2],
240 (const void **) &ao_scheme_frame_free_list[3],
241 (const void **) &ao_scheme_frame_free_list[4],
242 (const void **) &ao_scheme_frame_free_list[5],
245 #if AO_SCHEME_FRAME_FREE != 6
246 #error Unexpected AO_SCHEME_FRAME_FREE value
249 #define AO_SCHEME_CACHE (sizeof (ao_scheme_cache) / sizeof (ao_scheme_cache[0]))
251 #define AO_SCHEME_BUSY_SIZE ((AO_SCHEME_POOL + 31) / 32)
253 static uint8_t ao_scheme_busy[AO_SCHEME_BUSY_SIZE];
254 static uint8_t ao_scheme_cons_note[AO_SCHEME_BUSY_SIZE];
255 static uint8_t ao_scheme_cons_last[AO_SCHEME_BUSY_SIZE];
256 static uint8_t ao_scheme_cons_noted;
258 uint16_t ao_scheme_top;
260 struct ao_scheme_chunk {
268 #define AO_SCHEME_NCHUNK 64
270 static struct ao_scheme_chunk ao_scheme_chunk[AO_SCHEME_NCHUNK];
272 /* Offset of an address within the pool. */
273 static inline uint16_t pool_offset(void *addr) {
275 if (!AO_SCHEME_IS_POOL(addr))
278 return ((uint8_t *) addr) - ao_scheme_pool;
281 static inline void mark(uint8_t *tag, int offset) {
282 int byte = offset >> 5;
283 int bit = (offset >> 2) & 7;
284 tag[byte] |= (1 << bit);
287 static inline void clear(uint8_t *tag, int offset) {
288 int byte = offset >> 5;
289 int bit = (offset >> 2) & 7;
290 tag[byte] &= ~(1 << bit);
293 static inline int busy(uint8_t *tag, int offset) {
294 int byte = offset >> 5;
295 int bit = (offset >> 2) & 7;
296 return (tag[byte] >> bit) & 1;
299 static inline int min(int a, int b) { return a < b ? a : b; }
300 static inline int max(int a, int b) { return a > b ? a : b; }
302 static inline int limit(int offset) {
303 return min(AO_SCHEME_POOL, max(offset, 0));
307 note_cons(uint16_t offset)
309 MDBG_MOVE("note cons %d\n", offset);
310 ao_scheme_cons_noted = 1;
311 mark(ao_scheme_cons_note, offset);
314 static uint16_t chunk_low, chunk_high;
315 static uint16_t chunk_first, chunk_last;
318 find_chunk(uint16_t offset)
321 /* Binary search for the location */
325 int m = (l + r) >> 1;
326 if (ao_scheme_chunk[m].old_offset < offset)
335 note_chunk(uint16_t offset, uint16_t size)
339 if (offset < chunk_low || chunk_high <= offset)
342 l = find_chunk(offset);
345 * The correct location is always in 'l', with r = l-1 being
346 * the entry before the right one
350 /* Off the right side */
351 if (l >= AO_SCHEME_NCHUNK)
354 /* Off the left side */
355 if (l == 0 && chunk_last && offset > ao_scheme_chunk[0].old_offset)
359 /* Shuffle existing entries right */
360 int end = min(AO_SCHEME_NCHUNK, chunk_last + 1);
362 memmove(&ao_scheme_chunk[l+1],
364 (end - (l+1)) * sizeof (struct ao_scheme_chunk));
367 ao_scheme_chunk[l].old_offset = offset;
368 ao_scheme_chunk[l].size = size;
370 /* Increment the number of elements up to the size of the array */
371 if (chunk_last < AO_SCHEME_NCHUNK)
374 /* Set the top address if the array is full */
375 if (chunk_last == AO_SCHEME_NCHUNK)
376 chunk_high = ao_scheme_chunk[AO_SCHEME_NCHUNK-1].old_offset +
377 ao_scheme_chunk[AO_SCHEME_NCHUNK-1].size;
383 chunk_high = ao_scheme_top;
389 * Walk all referenced objects calling functions on each one
393 walk(int (*visit_addr)(const struct ao_scheme_type *type, void **addr),
394 int (*visit_poly)(ao_poly *p, uint8_t do_note_cons))
398 ao_scheme_record_reset();
399 memset(ao_scheme_busy, '\0', sizeof (ao_scheme_busy));
400 memset(ao_scheme_cons_note, '\0', sizeof (ao_scheme_cons_note));
401 ao_scheme_cons_noted = 0;
402 for (i = 0; i < (int) AO_SCHEME_ROOT; i++) {
403 if (ao_scheme_root[i].type) {
404 void **a = ao_scheme_root[i].addr, *v;
406 MDBG_MOVE("root ptr %d\n", MDBG_OFFSET(v));
407 visit_addr(ao_scheme_root[i].type, a);
410 ao_poly *a = (ao_poly *) ao_scheme_root[i].addr, p;
412 MDBG_MOVE("root poly %d\n", MDBG_OFFSET(ao_scheme_ref(p)));
417 while (ao_scheme_cons_noted) {
418 memcpy(ao_scheme_cons_last, ao_scheme_cons_note, sizeof (ao_scheme_cons_note));
419 memset(ao_scheme_cons_note, '\0', sizeof (ao_scheme_cons_note));
420 ao_scheme_cons_noted = 0;
421 for (i = 0; i < AO_SCHEME_POOL; i += 4) {
422 if (busy(ao_scheme_cons_last, i)) {
423 void *v = ao_scheme_pool + i;
424 MDBG_MOVE("root cons %d\n", MDBG_OFFSET(v));
425 visit_addr(&ao_scheme_cons_type, &v);
437 for (i = 0; i < ao_scheme_top; i += 4) {
438 if ((i & 0xff) == 0) {
442 else if ((i & 0x1f) == 0)
444 if (busy(ao_scheme_busy, i))
451 #define DUMP_BUSY() dump_busy()
456 static const struct ao_scheme_type * const ao_scheme_types[AO_SCHEME_NUM_TYPE] = {
457 [AO_SCHEME_CONS] = &ao_scheme_cons_type,
458 [AO_SCHEME_INT] = NULL,
459 [AO_SCHEME_STRING] = &ao_scheme_string_type,
460 [AO_SCHEME_OTHER] = (void *) 0x1,
461 [AO_SCHEME_ATOM] = &ao_scheme_atom_type,
462 [AO_SCHEME_BUILTIN] = &ao_scheme_builtin_type,
463 [AO_SCHEME_FRAME] = &ao_scheme_frame_type,
464 [AO_SCHEME_FRAME_VALS] = &ao_scheme_frame_vals_type,
465 [AO_SCHEME_LAMBDA] = &ao_scheme_lambda_type,
466 [AO_SCHEME_STACK] = &ao_scheme_stack_type,
467 [AO_SCHEME_BOOL] = &ao_scheme_bool_type,
468 [AO_SCHEME_BIGINT] = &ao_scheme_bigint_type,
469 [AO_SCHEME_FLOAT] = &ao_scheme_float_type,
470 [AO_SCHEME_VECTOR] = &ao_scheme_vector_type,
474 ao_scheme_mark_ref(const struct ao_scheme_type *type, void **ref)
476 return ao_scheme_mark(type, *ref);
480 ao_scheme_poly_mark_ref(ao_poly *p, uint8_t do_note_cons)
482 return ao_scheme_poly_mark(*p, do_note_cons);
486 uint64_t ao_scheme_collects[2];
487 uint64_t ao_scheme_freed[2];
488 uint64_t ao_scheme_loops[2];
491 int ao_scheme_last_top;
494 ao_scheme_collect(uint8_t style)
502 struct ao_scheme_record *mark_record = NULL, *move_record = NULL;
504 MDBG_MOVE("collect %d\n", ao_scheme_collects[style]);
506 MDBG_DO(ao_scheme_frame_write(ao_scheme_frame_poly(ao_scheme_frame_global)));
508 /* The first time through, we're doing a full collect */
509 if (ao_scheme_last_top == 0)
510 style = AO_SCHEME_COLLECT_FULL;
512 /* Clear references to all caches */
513 for (i = 0; i < (int) AO_SCHEME_CACHE; i++)
514 *ao_scheme_cache[i] = NULL;
515 if (style == AO_SCHEME_COLLECT_FULL) {
518 chunk_low = top = ao_scheme_last_top;
524 MDBG_MOVE("move chunks from %d to %d\n", chunk_low, top);
525 /* Find the sizes of the first chunk of objects to move */
527 walk(ao_scheme_mark_ref, ao_scheme_poly_mark_ref);
530 ao_scheme_record_free(mark_record);
531 mark_record = ao_scheme_record_save();
532 if (mark_record && move_record)
533 ao_scheme_record_compare("mark", move_record, mark_record);
538 /* Find the first moving object */
539 for (i = 0; i < chunk_last; i++) {
540 uint16_t size = ao_scheme_chunk[i].size;
547 if (ao_scheme_chunk[i].old_offset > top)
550 MDBG_MOVE("chunk %d %d not moving\n",
551 ao_scheme_chunk[i].old_offset,
552 ao_scheme_chunk[i].size);
554 if (ao_scheme_chunk[i].old_offset != top)
561 * Limit amount of chunk array used in mapping moves
562 * to the active region
565 chunk_low = ao_scheme_chunk[i].old_offset;
567 /* Copy all of the objects */
568 for (; i < chunk_last; i++) {
569 uint16_t size = ao_scheme_chunk[i].size;
576 MDBG_MOVE("chunk %d %d -> %d\n",
577 ao_scheme_chunk[i].old_offset,
580 ao_scheme_chunk[i].new_offset = top;
582 memmove(&ao_scheme_pool[top],
583 &ao_scheme_pool[ao_scheme_chunk[i].old_offset],
589 if (chunk_first < chunk_last) {
590 /* Relocate all references to the objects */
591 walk(ao_scheme_move, ao_scheme_poly_move);
594 ao_scheme_record_free(move_record);
595 move_record = ao_scheme_record_save();
596 if (mark_record && move_record)
597 ao_scheme_record_compare("move", mark_record, move_record);
601 /* If we ran into the end of the heap, then
602 * there's no need to keep walking
604 if (chunk_last != AO_SCHEME_NCHUNK)
607 /* Next loop starts right above this loop */
608 chunk_low = chunk_high;
613 ++ao_scheme_collects[style];
614 ao_scheme_freed[style] += ao_scheme_top - top;
615 ao_scheme_loops[style] += loops;
619 if (style == AO_SCHEME_COLLECT_FULL)
620 ao_scheme_last_top = top;
622 MDBG_DO(memset(ao_scheme_chunk, '\0', sizeof (ao_scheme_chunk));
623 walk(ao_scheme_mark_ref, ao_scheme_poly_mark_ref));
625 return AO_SCHEME_POOL - ao_scheme_top;
630 ao_scheme_cons_check(struct ao_scheme_cons *cons)
637 walk(ao_scheme_mark_ref, ao_scheme_poly_mark_ref);
639 if (!AO_SCHEME_IS_POOL(cons))
641 offset = pool_offset(cons);
642 if (busy(ao_scheme_busy, offset)) {
643 ao_scheme_printf("cons at %p offset %d poly %d is busy\n\t%v\n", cons, offset, ao_scheme_cons_poly(cons), ao_scheme_cons_poly(cons));
647 if (!ao_scheme_is_pair(cdr))
649 cons = ao_scheme_poly_cons(cdr);
655 * Mark interfaces for objects
660 * Mark a block of memory with an explicit size
664 ao_scheme_mark_block(void *addr, int size)
667 if (!AO_SCHEME_IS_POOL(addr))
670 offset = pool_offset(addr);
671 MDBG_MOVE("mark memory %d\n", MDBG_OFFSET(addr));
672 if (busy(ao_scheme_busy, offset)) {
673 MDBG_MOVE("already marked\n");
676 mark(ao_scheme_busy, offset);
677 note_chunk(offset, size);
682 * Note a reference to memory and collect information about a few
683 * object sizes at a time
687 ao_scheme_mark_memory(const struct ao_scheme_type *type, void *addr)
690 if (!AO_SCHEME_IS_POOL(addr))
693 offset = pool_offset(addr);
694 MDBG_MOVE("mark memory %d\n", MDBG_OFFSET(addr));
695 if (busy(ao_scheme_busy, offset)) {
696 MDBG_MOVE("already marked\n");
699 mark(ao_scheme_busy, offset);
700 note_chunk(offset, ao_scheme_size(type, addr));
705 * Mark an object and all that it refereces
708 ao_scheme_mark(const struct ao_scheme_type *type, void *addr)
711 MDBG_MOVE("mark %d\n", MDBG_OFFSET(addr));
713 ret = ao_scheme_mark_memory(type, addr);
715 MDBG_MOVE("mark recurse\n");
723 * Mark an object, unless it is a cons cell and
724 * do_note_cons is set. In that case, just
725 * set a bit in the cons note array; those
726 * will be marked in a separate pass to avoid
727 * deep recursion in the collector
730 ao_scheme_poly_mark(ao_poly p, uint8_t do_note_cons)
735 type = ao_scheme_poly_base_type(p);
737 if (type == AO_SCHEME_INT)
740 addr = ao_scheme_ref(p);
741 if (!AO_SCHEME_IS_POOL(addr))
744 if (type == AO_SCHEME_CONS && do_note_cons) {
745 note_cons(pool_offset(addr));
748 if (type == AO_SCHEME_OTHER)
749 type = ao_scheme_other_type(addr);
751 const struct ao_scheme_type *lisp_type = ao_scheme_types[type];
757 return ao_scheme_mark(lisp_type, addr);
762 * Find the current location of an object
763 * based on the original location. For unmoved
764 * objects, this is simple. For moved objects,
769 move_map(uint16_t offset)
773 if (offset < chunk_low || chunk_high <= offset)
776 l = find_chunk(offset);
779 if (ao_scheme_chunk[l].old_offset != offset)
782 return ao_scheme_chunk[l].new_offset;
786 ao_scheme_move_memory(const struct ao_scheme_type *type, void **ref)
789 uint16_t offset, orig_offset;
791 if (!AO_SCHEME_IS_POOL(addr))
796 MDBG_MOVE("move memory %d\n", MDBG_OFFSET(addr));
797 orig_offset = pool_offset(addr);
798 offset = move_map(orig_offset);
799 if (offset != orig_offset) {
800 MDBG_MOVE("update ref %d %d -> %d\n",
801 AO_SCHEME_IS_POOL(ref) ? MDBG_OFFSET(ref) : -1,
802 orig_offset, offset);
803 *ref = ao_scheme_pool + offset;
805 if (busy(ao_scheme_busy, offset)) {
806 MDBG_MOVE("already moved\n");
809 mark(ao_scheme_busy, offset);
810 MDBG_DO(ao_scheme_record(type, addr, ao_scheme_size(type, addr)));
815 ao_scheme_move(const struct ao_scheme_type *type, void **ref)
818 MDBG_MOVE("move object %d\n", MDBG_OFFSET(*ref));
820 ret = ao_scheme_move_memory(type, ref);
822 MDBG_MOVE("move recurse\n");
830 ao_scheme_poly_move(ao_poly *ref, uint8_t do_note_cons)
836 uint16_t offset, orig_offset;
839 base_type = type = ao_scheme_poly_base_type(p);
841 if (type == AO_SCHEME_INT)
844 addr = ao_scheme_ref(p);
845 if (!AO_SCHEME_IS_POOL(addr))
848 orig_offset = pool_offset(addr);
849 offset = move_map(orig_offset);
851 if (type == AO_SCHEME_CONS && do_note_cons) {
852 note_cons(orig_offset);
855 if (type == AO_SCHEME_OTHER)
856 type = ao_scheme_other_type(ao_scheme_pool + offset);
858 const struct ao_scheme_type *lisp_type = ao_scheme_types[type];
864 ret = ao_scheme_move(lisp_type, &addr);
867 /* Re-write the poly value */
868 if (offset != orig_offset) {
869 ao_poly np = ao_scheme_poly(ao_scheme_pool + offset, base_type);
870 MDBG_MOVE("poly %d moved %d -> %d\n",
871 type, orig_offset, offset);
879 ao_scheme_validate(void)
882 memset(ao_scheme_chunk, '\0', sizeof (ao_scheme_chunk));
883 walk(ao_scheme_mark_ref, ao_scheme_poly_mark_ref);
891 ao_scheme_alloc(int size)
895 MDBG_DO(++dbg_allocs);
896 MDBG_DO(if (dbg_validate) ao_scheme_validate());
897 size = ao_scheme_size_round(size);
898 if (AO_SCHEME_POOL - ao_scheme_top < size &&
899 ao_scheme_collect(AO_SCHEME_COLLECT_INCREMENTAL) < size &&
900 ao_scheme_collect(AO_SCHEME_COLLECT_FULL) < size)
902 ao_scheme_error(AO_SCHEME_OOM, "out of memory");
905 addr = ao_scheme_pool + ao_scheme_top;
906 ao_scheme_top += size;
907 MDBG_MOVE("alloc %d size %d\n", MDBG_OFFSET(addr), size);
912 ao_scheme_cons_stash(int id, struct ao_scheme_cons *cons)
914 assert(save_cons[id] == 0);
915 save_cons[id] = cons;
918 struct ao_scheme_cons *
919 ao_scheme_cons_fetch(int id)
921 struct ao_scheme_cons *cons = save_cons[id];
922 save_cons[id] = NULL;
927 ao_scheme_poly_stash(int id, ao_poly poly)
929 assert(save_poly[id] == AO_SCHEME_NIL);
930 save_poly[id] = poly;
934 ao_scheme_poly_fetch(int id)
936 ao_poly poly = save_poly[id];
937 save_poly[id] = AO_SCHEME_NIL;
942 ao_scheme_string_stash(int id, char *string)
944 assert(save_string[id] == NULL);
945 save_string[id] = string;
949 ao_scheme_string_fetch(int id)
951 char *string = save_string[id];
952 save_string[id] = NULL;
957 ao_scheme_frame_stash(int id, struct ao_scheme_frame *frame)
959 assert(save_frame[id] == NULL);
960 save_frame[id] = frame;
963 struct ao_scheme_frame *
964 ao_scheme_frame_fetch(int id)
966 struct ao_scheme_frame *frame = save_frame[id];
967 save_frame[id] = NULL;