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
44 #define DBG_MEM_STACK 0
46 char *mem_collect_stack;
47 int64_t mem_collect_max_depth;
50 ao_scheme_check_stack(void)
55 depth = mem_collect_stack - &x;
56 if (depth > mem_collect_max_depth)
57 mem_collect_max_depth = depth;
61 _ao_scheme_reset_stack(char *x)
63 mem_collect_stack = x;
64 // mem_collect_max_depth = 0;
66 #define ao_scheme_declare_stack char x;
67 #define ao_scheme_reset_stack() _ao_scheme_reset_stack(&x)
69 #define ao_scheme_check_stack()
70 #define ao_scheme_declare_stack
71 #define ao_scheme_reset_stack()
76 int dbg_mem = DBG_MEM_START;
79 struct ao_scheme_record {
80 struct ao_scheme_record *next;
81 const struct ao_scheme_type *type;
86 static struct ao_scheme_record *record_head, **record_tail;
89 ao_scheme_record_free(struct ao_scheme_record *record)
92 struct ao_scheme_record *next = record->next;
99 ao_scheme_record_reset(void)
101 ao_scheme_record_free(record_head);
103 record_tail = &record_head;
107 ao_scheme_record(const struct ao_scheme_type *type,
111 struct ao_scheme_record *r = malloc(sizeof (struct ao_scheme_record));
118 record_tail = &r->next;
121 static struct ao_scheme_record *
122 ao_scheme_record_save(void)
124 struct ao_scheme_record *r = record_head;
127 record_tail = &record_head;
132 ao_scheme_record_compare(char *where,
133 struct ao_scheme_record *a,
134 struct ao_scheme_record *b)
137 if (a->type != b->type || a->size != b->size) {
138 printf("%s record difers %d %s %d -> %d %s %d\n",
140 MDBG_OFFSET(a->addr),
143 MDBG_OFFSET(b->addr),
152 printf("%s record differs %d %s %d -> NULL\n",
154 MDBG_OFFSET(a->addr),
160 printf("%s record differs NULL -> %d %s %d\n",
162 MDBG_OFFSET(b->addr),
170 #define ao_scheme_record_reset()
173 uint8_t ao_scheme_exception;
175 struct ao_scheme_root {
176 const struct ao_scheme_type *type;
180 static struct ao_scheme_cons *save_cons[2];
181 static char *save_string[2];
182 static struct ao_scheme_frame *save_frame[1];
183 static ao_poly save_poly[3];
185 static const struct ao_scheme_root ao_scheme_root[] = {
187 .type = &ao_scheme_cons_type,
188 .addr = (void **) &save_cons[0],
191 .type = &ao_scheme_cons_type,
192 .addr = (void **) &save_cons[1],
195 .type = &ao_scheme_string_type,
196 .addr = (void **) &save_string[0],
199 .type = &ao_scheme_string_type,
200 .addr = (void **) &save_string[1],
203 .type = &ao_scheme_frame_type,
204 .addr = (void **) &save_frame[0],
208 .addr = (void **) (void *) &save_poly[0]
212 .addr = (void **) (void *) &save_poly[1]
216 .addr = (void **) (void *) &save_poly[2]
219 .type = &ao_scheme_atom_type,
220 .addr = (void **) &ao_scheme_atoms
223 .type = &ao_scheme_frame_type,
224 .addr = (void **) &ao_scheme_frame_global,
227 .type = &ao_scheme_frame_type,
228 .addr = (void **) &ao_scheme_frame_current,
231 .type = &ao_scheme_stack_type,
232 .addr = (void **) &ao_scheme_stack,
236 .addr = (void **) (void *) &ao_scheme_v,
239 .type = &ao_scheme_cons_type,
240 .addr = (void **) &ao_scheme_read_cons,
243 .type = &ao_scheme_cons_type,
244 .addr = (void **) &ao_scheme_read_cons_tail,
247 .type = &ao_scheme_cons_type,
248 .addr = (void **) &ao_scheme_read_stack,
250 #ifdef AO_SCHEME_MAKE_CONST
252 .type = &ao_scheme_bool_type,
253 .addr = (void **) &ao_scheme_false,
256 .type = &ao_scheme_bool_type,
257 .addr = (void **) &ao_scheme_true,
262 #define AO_SCHEME_ROOT (sizeof (ao_scheme_root) / sizeof (ao_scheme_root[0]))
264 static const void ** const ao_scheme_cache[] = {
265 (const void **) &ao_scheme_cons_free_list,
266 (const void **) &ao_scheme_stack_free_list,
267 (const void **) &ao_scheme_frame_free_list[0],
268 (const void **) &ao_scheme_frame_free_list[1],
269 (const void **) &ao_scheme_frame_free_list[2],
270 (const void **) &ao_scheme_frame_free_list[3],
271 (const void **) &ao_scheme_frame_free_list[4],
272 (const void **) &ao_scheme_frame_free_list[5],
275 #if AO_SCHEME_FRAME_FREE != 6
276 #error Unexpected AO_SCHEME_FRAME_FREE value
279 #define AO_SCHEME_CACHE (sizeof (ao_scheme_cache) / sizeof (ao_scheme_cache[0]))
281 #define AO_SCHEME_BUSY_SIZE ((AO_SCHEME_POOL + 31) / 32)
283 static uint8_t ao_scheme_busy[AO_SCHEME_BUSY_SIZE];
284 static uint8_t ao_scheme_cons_note[AO_SCHEME_BUSY_SIZE];
285 static uint8_t ao_scheme_cons_last[AO_SCHEME_BUSY_SIZE];
286 static uint8_t ao_scheme_cons_noted;
288 uint16_t ao_scheme_top;
290 struct ao_scheme_chunk {
298 #define AO_SCHEME_NCHUNK 64
300 static struct ao_scheme_chunk ao_scheme_chunk[AO_SCHEME_NCHUNK];
302 /* Offset of an address within the pool. */
303 static inline uint16_t pool_offset(void *addr) {
305 if (!AO_SCHEME_IS_POOL(addr))
308 return ((uint8_t *) addr) - ao_scheme_pool;
311 static inline void mark(uint8_t *tag, int offset) {
312 int byte = offset >> 5;
313 int bit = (offset >> 2) & 7;
314 ao_scheme_check_stack();
315 tag[byte] |= (1 << bit);
318 static inline void clear(uint8_t *tag, int offset) {
319 int byte = offset >> 5;
320 int bit = (offset >> 2) & 7;
321 tag[byte] &= ~(1 << bit);
324 static inline int busy(uint8_t *tag, int offset) {
325 int byte = offset >> 5;
326 int bit = (offset >> 2) & 7;
327 return (tag[byte] >> bit) & 1;
330 static inline int min(int a, int b) { return a < b ? a : b; }
331 static inline int max(int a, int b) { return a > b ? a : b; }
333 static inline int limit(int offset) {
334 return min(AO_SCHEME_POOL, max(offset, 0));
338 note_cons(uint16_t offset)
340 MDBG_MOVE("note cons %d\n", offset);
341 ao_scheme_cons_noted = 1;
342 mark(ao_scheme_cons_note, offset);
345 static uint16_t chunk_low, chunk_high;
346 static uint16_t chunk_first, chunk_last;
349 find_chunk(uint16_t offset)
352 /* Binary search for the location */
356 int m = (l + r) >> 1;
357 if (ao_scheme_chunk[m].old_offset < offset)
366 note_chunk(uint16_t offset, uint16_t size)
371 if (offset < chunk_low || chunk_high <= offset)
374 l = find_chunk(offset);
377 * The correct location is always in 'l', with r = l-1 being
378 * the entry before the right one
382 /* Off the right side */
383 if (l >= AO_SCHEME_NCHUNK)
386 /* Off the left side */
387 if (l == 0 && chunk_last && offset > ao_scheme_chunk[0].old_offset)
391 /* Shuffle existing entries right */
392 end = min(AO_SCHEME_NCHUNK, chunk_last + 1);
394 memmove(&ao_scheme_chunk[l+1],
396 (end - (l+1)) * sizeof (struct ao_scheme_chunk));
399 ao_scheme_chunk[l].old_offset = offset;
400 ao_scheme_chunk[l].size = size;
402 /* Increment the number of elements up to the size of the array */
403 if (chunk_last < AO_SCHEME_NCHUNK)
406 /* Set the top address if the array is full */
407 if (chunk_last == AO_SCHEME_NCHUNK)
408 chunk_high = ao_scheme_chunk[AO_SCHEME_NCHUNK-1].old_offset +
409 ao_scheme_chunk[AO_SCHEME_NCHUNK-1].size;
415 chunk_high = ao_scheme_top;
421 * Walk all referenced objects calling functions on each one
425 walk(int (*visit_addr)(const struct ao_scheme_type *type, void **addr),
426 int (*visit_poly)(ao_poly *p, uint8_t do_note_cons))
430 ao_scheme_record_reset();
431 memset(ao_scheme_busy, '\0', sizeof (ao_scheme_busy));
432 memset(ao_scheme_cons_note, '\0', sizeof (ao_scheme_cons_note));
433 ao_scheme_cons_noted = 0;
434 for (i = 0; i < (int) AO_SCHEME_ROOT; i++) {
435 if (ao_scheme_root[i].type) {
436 void **a = ao_scheme_root[i].addr, *v;
438 MDBG_MOVE("root ptr %d\n", MDBG_OFFSET(v));
439 visit_addr(ao_scheme_root[i].type, a);
442 ao_poly *a = (ao_poly *) ao_scheme_root[i].addr, p;
444 MDBG_MOVE("root poly %d\n", MDBG_OFFSET(ao_scheme_ref(p)));
449 while (ao_scheme_cons_noted) {
450 memcpy(ao_scheme_cons_last, ao_scheme_cons_note, sizeof (ao_scheme_cons_note));
451 memset(ao_scheme_cons_note, '\0', sizeof (ao_scheme_cons_note));
452 ao_scheme_cons_noted = 0;
453 for (i = 0; i < AO_SCHEME_POOL; i += 4) {
454 if (busy(ao_scheme_cons_last, i)) {
455 void *v = ao_scheme_pool + i;
456 MDBG_MOVE("root cons %d\n", MDBG_OFFSET(v));
457 visit_addr(&ao_scheme_cons_type, &v);
469 for (i = 0; i < ao_scheme_top; i += 4) {
470 if ((i & 0xff) == 0) {
474 else if ((i & 0x1f) == 0)
476 if (busy(ao_scheme_busy, i))
483 #define DUMP_BUSY() dump_busy()
488 static const struct ao_scheme_type * const ao_scheme_types[AO_SCHEME_NUM_TYPE] = {
489 [AO_SCHEME_CONS] = &ao_scheme_cons_type,
490 [AO_SCHEME_INT] = NULL,
491 [AO_SCHEME_STRING] = &ao_scheme_string_type,
492 [AO_SCHEME_OTHER] = (void *) 0x1,
493 [AO_SCHEME_ATOM] = &ao_scheme_atom_type,
494 [AO_SCHEME_BUILTIN] = &ao_scheme_builtin_type,
495 [AO_SCHEME_FRAME] = &ao_scheme_frame_type,
496 [AO_SCHEME_FRAME_VALS] = &ao_scheme_frame_vals_type,
497 [AO_SCHEME_LAMBDA] = &ao_scheme_lambda_type,
498 [AO_SCHEME_STACK] = &ao_scheme_stack_type,
499 [AO_SCHEME_BOOL] = &ao_scheme_bool_type,
500 #ifdef AO_SCHEME_FEATURE_BIGINT
501 [AO_SCHEME_BIGINT] = &ao_scheme_bigint_type,
503 #ifdef AO_SCHEME_FEATURE_FLOAT
504 [AO_SCHEME_FLOAT] = &ao_scheme_float_type,
506 #ifdef AO_SCHEME_FEATURE_VECTOR
507 [AO_SCHEME_VECTOR] = &ao_scheme_vector_type,
512 ao_scheme_mark(const struct ao_scheme_type *type, void *addr);
515 ao_scheme_move(const struct ao_scheme_type *type, void **ref);
518 ao_scheme_mark_ref(const struct ao_scheme_type *type, void **ref)
520 return ao_scheme_mark(type, *ref);
524 ao_scheme_poly_mark_ref(ao_poly *p, uint8_t do_note_cons)
526 return ao_scheme_poly_mark(*p, do_note_cons);
530 uint64_t ao_scheme_collects[2];
531 uint64_t ao_scheme_freed[2];
532 uint64_t ao_scheme_loops[2];
535 int ao_scheme_last_top;
538 ao_scheme_collect(uint8_t style)
540 ao_scheme_declare_stack
547 struct ao_scheme_record *mark_record = NULL, *move_record = NULL;
549 MDBG_MOVE("collect %d\n", ao_scheme_collects[style]);
551 MDBG_DO(ao_scheme_frame_write(ao_scheme_frame_poly(ao_scheme_frame_global)));
553 ao_scheme_reset_stack();
555 /* The first time through, we're doing a full collect */
556 if (ao_scheme_last_top == 0)
557 style = AO_SCHEME_COLLECT_FULL;
559 /* Clear references to all caches */
560 for (i = 0; i < (int) AO_SCHEME_CACHE; i++)
561 *ao_scheme_cache[i] = NULL;
562 if (style == AO_SCHEME_COLLECT_FULL) {
565 chunk_low = top = ao_scheme_last_top;
571 MDBG_MOVE("move chunks from %d to %d\n", chunk_low, top);
572 /* Find the sizes of the first chunk of objects to move */
574 walk(ao_scheme_mark_ref, ao_scheme_poly_mark_ref);
577 ao_scheme_record_free(mark_record);
578 mark_record = ao_scheme_record_save();
579 if (mark_record && move_record)
580 ao_scheme_record_compare("mark", move_record, mark_record);
585 /* Find the first moving object */
586 for (i = 0; i < chunk_last; i++) {
587 uint16_t size = ao_scheme_chunk[i].size;
594 if (ao_scheme_chunk[i].old_offset > top)
597 MDBG_MOVE("chunk %d %d not moving\n",
598 ao_scheme_chunk[i].old_offset,
599 ao_scheme_chunk[i].size);
601 if (ao_scheme_chunk[i].old_offset != top)
608 * Limit amount of chunk array used in mapping moves
609 * to the active region
612 chunk_low = ao_scheme_chunk[i].old_offset;
614 /* Copy all of the objects */
615 for (; i < chunk_last; i++) {
616 uint16_t size = ao_scheme_chunk[i].size;
623 MDBG_MOVE("chunk %d %d -> %d\n",
624 ao_scheme_chunk[i].old_offset,
627 ao_scheme_chunk[i].new_offset = top;
629 memmove(&ao_scheme_pool[top],
630 &ao_scheme_pool[ao_scheme_chunk[i].old_offset],
636 if (chunk_first < chunk_last) {
637 /* Relocate all references to the objects */
638 walk(ao_scheme_move, ao_scheme_poly_move);
641 ao_scheme_record_free(move_record);
642 move_record = ao_scheme_record_save();
643 if (mark_record && move_record)
644 ao_scheme_record_compare("move", mark_record, move_record);
648 /* If we ran into the end of the heap, then
649 * there's no need to keep walking
651 if (chunk_last != AO_SCHEME_NCHUNK)
654 /* Next loop starts right above this loop */
655 chunk_low = chunk_high;
660 ++ao_scheme_collects[style];
661 ao_scheme_freed[style] += ao_scheme_top - top;
662 ao_scheme_loops[style] += loops;
666 if (style == AO_SCHEME_COLLECT_FULL)
667 ao_scheme_last_top = top;
669 MDBG_DO(memset(ao_scheme_chunk, '\0', sizeof (ao_scheme_chunk));
670 walk(ao_scheme_mark_ref, ao_scheme_poly_mark_ref));
673 fprintf(stderr, "max collect stack depth %lu\n", mem_collect_max_depth);
675 return AO_SCHEME_POOL - ao_scheme_top;
680 ao_scheme_cons_check(struct ao_scheme_cons *cons)
687 walk(ao_scheme_mark_ref, ao_scheme_poly_mark_ref);
689 if (!AO_SCHEME_IS_POOL(cons))
691 offset = pool_offset(cons);
692 if (busy(ao_scheme_busy, offset)) {
693 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));
697 if (!ao_scheme_is_pair(cdr))
699 cons = ao_scheme_poly_cons(cdr);
705 * Mark interfaces for objects
710 * Note a reference to memory and collect information about a few
711 * object sizes at a time
715 ao_scheme_mark_memory(const struct ao_scheme_type *type, void *addr)
718 if (!AO_SCHEME_IS_POOL(addr))
721 offset = pool_offset(addr);
722 MDBG_MOVE("mark memory %d\n", MDBG_OFFSET(addr));
723 if (busy(ao_scheme_busy, offset)) {
724 MDBG_MOVE("already marked\n");
727 mark(ao_scheme_busy, offset);
728 note_chunk(offset, ao_scheme_size(type, addr));
733 * Mark an object and all that it refereces
736 ao_scheme_mark(const struct ao_scheme_type *type, void *addr)
739 MDBG_MOVE("mark %d\n", MDBG_OFFSET(addr));
741 ret = ao_scheme_mark_memory(type, addr);
743 MDBG_MOVE("mark recurse\n");
751 * Mark an object, unless it is a cons cell and
752 * do_note_cons is set. In that case, just
753 * set a bit in the cons note array; those
754 * will be marked in a separate pass to avoid
755 * deep recursion in the collector
758 ao_scheme_poly_mark(ao_poly p, uint8_t do_note_cons)
764 type = ao_scheme_poly_base_type(p);
766 if (type == AO_SCHEME_INT)
769 addr = ao_scheme_ref(p);
770 if (!AO_SCHEME_IS_POOL(addr))
773 if (type == AO_SCHEME_CONS && do_note_cons) {
774 note_cons(pool_offset(addr));
777 const struct ao_scheme_type *lisp_type;
779 if (type == AO_SCHEME_OTHER)
780 type = ao_scheme_other_type(addr);
782 lisp_type = ao_scheme_types[type];
788 MDBG_MOVE("mark %d\n", MDBG_OFFSET(addr));
790 ret = ao_scheme_mark_memory(lisp_type, addr);
792 MDBG_MOVE("mark recurse\n");
793 lisp_type->mark(addr);
801 * Find the current location of an object
802 * based on the original location. For unmoved
803 * objects, this is simple. For moved objects,
808 move_map(uint16_t offset)
812 if (offset < chunk_low || chunk_high <= offset)
815 l = find_chunk(offset);
818 if (ao_scheme_chunk[l].old_offset != offset)
821 return ao_scheme_chunk[l].new_offset;
825 ao_scheme_move_memory(const struct ao_scheme_type *type, void **ref)
828 uint16_t offset, orig_offset;
830 if (!AO_SCHEME_IS_POOL(addr))
835 MDBG_MOVE("move memory %d\n", MDBG_OFFSET(addr));
836 orig_offset = pool_offset(addr);
837 offset = move_map(orig_offset);
838 if (offset != orig_offset) {
839 MDBG_MOVE("update ref %d %d -> %d\n",
840 AO_SCHEME_IS_POOL(ref) ? MDBG_OFFSET(ref) : -1,
841 orig_offset, offset);
842 *ref = ao_scheme_pool + offset;
844 if (busy(ao_scheme_busy, offset)) {
845 MDBG_MOVE("already moved\n");
848 mark(ao_scheme_busy, offset);
849 MDBG_DO(ao_scheme_record(type, addr, ao_scheme_size(type, addr)));
854 ao_scheme_move(const struct ao_scheme_type *type, void **ref)
857 MDBG_MOVE("move object %d\n", MDBG_OFFSET(*ref));
859 ret = ao_scheme_move_memory(type, ref);
861 MDBG_MOVE("move recurse\n");
869 ao_scheme_poly_move(ao_poly *ref, uint8_t do_note_cons)
874 uint16_t offset, orig_offset;
876 if (ao_scheme_poly_base_type(p) == AO_SCHEME_INT)
879 addr = ao_scheme_ref(p);
880 if (!AO_SCHEME_IS_POOL(addr))
883 orig_offset = pool_offset(addr);
884 offset = move_map(orig_offset);
886 if (ao_scheme_poly_base_type(p) == AO_SCHEME_CONS && do_note_cons) {
887 note_cons(orig_offset);
890 uint8_t type = ao_scheme_poly_base_type(p);
891 const struct ao_scheme_type *lisp_type;
893 if (type == AO_SCHEME_OTHER)
894 type = ao_scheme_other_type(ao_scheme_pool + offset);
896 lisp_type = ao_scheme_types[type];
901 /* inline ao_scheme_move to save stack space */
902 MDBG_MOVE("move object %d\n", MDBG_OFFSET(addr));
904 ret = ao_scheme_move_memory(lisp_type, &addr);
906 MDBG_MOVE("move recurse\n");
907 lisp_type->move(addr);
912 /* Re-write the poly value */
913 if (offset != orig_offset) {
914 ao_poly np = ao_scheme_poly(ao_scheme_pool + offset, ao_scheme_poly_base_type(p));
915 MDBG_MOVE("poly %d moved %d -> %d\n",
916 type, orig_offset, offset);
924 ao_scheme_validate(void)
927 memset(ao_scheme_chunk, '\0', sizeof (ao_scheme_chunk));
928 walk(ao_scheme_mark_ref, ao_scheme_poly_mark_ref);
936 ao_scheme_alloc(int size)
940 MDBG_DO(++dbg_allocs);
941 MDBG_DO(if (dbg_validate) ao_scheme_validate());
942 size = ao_scheme_size_round(size);
943 if (AO_SCHEME_POOL - ao_scheme_top < size &&
944 ao_scheme_collect(AO_SCHEME_COLLECT_INCREMENTAL) < size &&
945 ao_scheme_collect(AO_SCHEME_COLLECT_FULL) < size)
947 ao_scheme_error(AO_SCHEME_OOM, "out of memory");
950 addr = ao_scheme_pool + ao_scheme_top;
951 ao_scheme_top += size;
952 MDBG_MOVE("alloc %d size %d\n", MDBG_OFFSET(addr), size);
957 ao_scheme_cons_stash(int id, struct ao_scheme_cons *cons)
959 assert(save_cons[id] == 0);
960 save_cons[id] = cons;
963 struct ao_scheme_cons *
964 ao_scheme_cons_fetch(int id)
966 struct ao_scheme_cons *cons = save_cons[id];
967 save_cons[id] = NULL;
972 ao_scheme_poly_stash(int id, ao_poly poly)
974 assert(save_poly[id] == AO_SCHEME_NIL);
975 save_poly[id] = poly;
979 ao_scheme_poly_fetch(int id)
981 ao_poly poly = save_poly[id];
982 save_poly[id] = AO_SCHEME_NIL;
987 ao_scheme_string_stash(int id, char *string)
989 assert(save_string[id] == NULL);
990 save_string[id] = string;
994 ao_scheme_string_fetch(int id)
996 char *string = save_string[id];
997 save_string[id] = NULL;
1002 ao_scheme_frame_stash(int id, struct ao_scheme_frame *frame)
1004 assert(save_frame[id] == NULL);
1005 save_frame[id] = frame;
1008 struct ao_scheme_frame *
1009 ao_scheme_frame_fetch(int id)
1011 struct ao_scheme_frame *frame = save_frame[id];
1012 save_frame[id] = NULL;