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)));
40 #define DBG_MEM_STATS DBG_MEM
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 struct ao_lisp_frame *save_frame[1];
152 static ao_poly save_poly[3];
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],
172 .type = &ao_lisp_frame_type,
173 .addr = (void **) &save_frame[0],
177 .addr = (void **) (void *) &save_poly[0]
181 .addr = (void **) (void *) &save_poly[1]
185 .addr = (void **) (void *) &save_poly[2]
188 .type = &ao_lisp_atom_type,
189 .addr = (void **) &ao_lisp_atoms
192 .type = &ao_lisp_frame_type,
193 .addr = (void **) &ao_lisp_frame_global,
196 .type = &ao_lisp_frame_type,
197 .addr = (void **) &ao_lisp_frame_current,
200 .type = &ao_lisp_stack_type,
201 .addr = (void **) &ao_lisp_stack,
205 .addr = (void **) (void *) &ao_lisp_v,
208 .type = &ao_lisp_cons_type,
209 .addr = (void **) &ao_lisp_read_cons,
212 .type = &ao_lisp_cons_type,
213 .addr = (void **) &ao_lisp_read_cons_tail,
216 .type = &ao_lisp_cons_type,
217 .addr = (void **) &ao_lisp_read_stack,
219 #ifdef AO_LISP_MAKE_CONST
221 .type = &ao_lisp_bool_type,
222 .addr = (void **) &ao_lisp_false,
225 .type = &ao_lisp_bool_type,
226 .addr = (void **) &ao_lisp_true,
231 #define AO_LISP_ROOT (sizeof (ao_lisp_root) / sizeof (ao_lisp_root[0]))
233 static const void ** const ao_lisp_cache[] = {
234 (const void **) &ao_lisp_cons_free_list,
235 (const void **) &ao_lisp_stack_free_list,
236 (const void **) &ao_lisp_frame_free_list[0],
237 (const void **) &ao_lisp_frame_free_list[1],
238 (const void **) &ao_lisp_frame_free_list[2],
239 (const void **) &ao_lisp_frame_free_list[3],
240 (const void **) &ao_lisp_frame_free_list[4],
241 (const void **) &ao_lisp_frame_free_list[5],
244 #if AO_LISP_FRAME_FREE != 6
245 #error Unexpected AO_LISP_FRAME_FREE value
248 #define AO_LISP_CACHE (sizeof (ao_lisp_cache) / sizeof (ao_lisp_cache[0]))
250 #define AO_LISP_BUSY_SIZE ((AO_LISP_POOL + 31) / 32)
252 static uint8_t ao_lisp_busy[AO_LISP_BUSY_SIZE];
253 static uint8_t ao_lisp_cons_note[AO_LISP_BUSY_SIZE];
254 static uint8_t ao_lisp_cons_last[AO_LISP_BUSY_SIZE];
255 static uint8_t ao_lisp_cons_noted;
257 uint16_t ao_lisp_top;
259 struct ao_lisp_chunk {
267 #define AO_LISP_NCHUNK 64
269 static struct ao_lisp_chunk ao_lisp_chunk[AO_LISP_NCHUNK];
271 /* Offset of an address within the pool. */
272 static inline uint16_t pool_offset(void *addr) {
274 if (!AO_LISP_IS_POOL(addr))
277 return ((uint8_t *) addr) - ao_lisp_pool;
280 static inline void mark(uint8_t *tag, int offset) {
281 int byte = offset >> 5;
282 int bit = (offset >> 2) & 7;
283 tag[byte] |= (1 << bit);
286 static inline void clear(uint8_t *tag, int offset) {
287 int byte = offset >> 5;
288 int bit = (offset >> 2) & 7;
289 tag[byte] &= ~(1 << bit);
292 static inline int busy(uint8_t *tag, int offset) {
293 int byte = offset >> 5;
294 int bit = (offset >> 2) & 7;
295 return (tag[byte] >> bit) & 1;
298 static inline int min(int a, int b) { return a < b ? a : b; }
299 static inline int max(int a, int b) { return a > b ? a : b; }
301 static inline int limit(int offset) {
302 return min(AO_LISP_POOL, max(offset, 0));
306 note_cons(uint16_t offset)
308 MDBG_MOVE("note cons %d\n", offset);
309 ao_lisp_cons_noted = 1;
310 mark(ao_lisp_cons_note, offset);
313 static uint16_t chunk_low, chunk_high;
314 static uint16_t chunk_first, chunk_last;
317 find_chunk(uint16_t offset)
320 /* Binary search for the location */
324 int m = (l + r) >> 1;
325 if (ao_lisp_chunk[m].old_offset < offset)
334 note_chunk(uint16_t offset, uint16_t size)
338 if (offset < chunk_low || chunk_high <= offset)
341 l = find_chunk(offset);
344 * The correct location is always in 'l', with r = l-1 being
345 * the entry before the right one
349 /* Off the right side */
350 if (l >= AO_LISP_NCHUNK)
353 /* Off the left side */
354 if (l == 0 && chunk_last && offset > ao_lisp_chunk[0].old_offset)
358 /* Shuffle existing entries right */
359 int end = min(AO_LISP_NCHUNK, chunk_last + 1);
361 memmove(&ao_lisp_chunk[l+1],
363 (end - (l+1)) * sizeof (struct ao_lisp_chunk));
366 ao_lisp_chunk[l].old_offset = offset;
367 ao_lisp_chunk[l].size = size;
369 /* Increment the number of elements up to the size of the array */
370 if (chunk_last < AO_LISP_NCHUNK)
373 /* Set the top address if the array is full */
374 if (chunk_last == AO_LISP_NCHUNK)
375 chunk_high = ao_lisp_chunk[AO_LISP_NCHUNK-1].old_offset +
376 ao_lisp_chunk[AO_LISP_NCHUNK-1].size;
382 chunk_high = ao_lisp_top;
388 * Walk all referenced objects calling functions on each one
392 walk(int (*visit_addr)(const struct ao_lisp_type *type, void **addr),
393 int (*visit_poly)(ao_poly *p, uint8_t do_note_cons))
397 ao_lisp_record_reset();
398 memset(ao_lisp_busy, '\0', sizeof (ao_lisp_busy));
399 memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
400 ao_lisp_cons_noted = 0;
401 for (i = 0; i < (int) AO_LISP_ROOT; i++) {
402 if (ao_lisp_root[i].type) {
403 void **a = ao_lisp_root[i].addr, *v;
405 MDBG_MOVE("root ptr %d\n", MDBG_OFFSET(v));
406 visit_addr(ao_lisp_root[i].type, a);
409 ao_poly *a = (ao_poly *) ao_lisp_root[i].addr, p;
411 MDBG_MOVE("root poly %d\n", MDBG_OFFSET(ao_lisp_ref(p)));
416 while (ao_lisp_cons_noted) {
417 memcpy(ao_lisp_cons_last, ao_lisp_cons_note, sizeof (ao_lisp_cons_note));
418 memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
419 ao_lisp_cons_noted = 0;
420 for (i = 0; i < AO_LISP_POOL; i += 4) {
421 if (busy(ao_lisp_cons_last, i)) {
422 void *v = ao_lisp_pool + i;
423 MDBG_MOVE("root cons %d\n", MDBG_OFFSET(v));
424 visit_addr(&ao_lisp_cons_type, &v);
436 for (i = 0; i < ao_lisp_top; i += 4) {
437 if ((i & 0xff) == 0) {
441 else if ((i & 0x1f) == 0)
443 if (busy(ao_lisp_busy, i))
450 #define DUMP_BUSY() dump_busy()
455 static const struct ao_lisp_type *ao_lisp_types[AO_LISP_NUM_TYPE] = {
456 [AO_LISP_CONS] = &ao_lisp_cons_type,
457 [AO_LISP_INT] = NULL,
458 [AO_LISP_STRING] = &ao_lisp_string_type,
459 [AO_LISP_OTHER] = (void *) 0x1,
460 [AO_LISP_ATOM] = &ao_lisp_atom_type,
461 [AO_LISP_BUILTIN] = &ao_lisp_builtin_type,
462 [AO_LISP_FRAME] = &ao_lisp_frame_type,
463 [AO_LISP_FRAME_VALS] = &ao_lisp_frame_vals_type,
464 [AO_LISP_LAMBDA] = &ao_lisp_lambda_type,
465 [AO_LISP_STACK] = &ao_lisp_stack_type,
466 [AO_LISP_BOOL] = &ao_lisp_bool_type,
467 [AO_LISP_BIGINT] = &ao_lisp_bigint_type,
468 [AO_LISP_FLOAT] = &ao_lisp_float_type,
472 ao_lisp_mark_ref(const struct ao_lisp_type *type, void **ref)
474 return ao_lisp_mark(type, *ref);
478 ao_lisp_poly_mark_ref(ao_poly *p, uint8_t do_note_cons)
480 return ao_lisp_poly_mark(*p, do_note_cons);
484 int ao_lisp_collects[2];
485 int ao_lisp_freed[2];
486 int ao_lisp_loops[2];
489 int ao_lisp_last_top;
492 ao_lisp_collect(uint8_t style)
500 struct ao_lisp_record *mark_record = NULL, *move_record = NULL;
502 MDBG_MOVE("collect %d\n", ao_lisp_collects[style]);
504 MDBG_DO(ao_lisp_frame_write(ao_lisp_frame_poly(ao_lisp_frame_global)));
506 /* The first time through, we're doing a full collect */
507 if (ao_lisp_last_top == 0)
508 style = AO_LISP_COLLECT_FULL;
510 /* Clear references to all caches */
511 for (i = 0; i < (int) AO_LISP_CACHE; i++)
512 *ao_lisp_cache[i] = NULL;
513 if (style == AO_LISP_COLLECT_FULL) {
516 chunk_low = top = ao_lisp_last_top;
522 MDBG_MOVE("move chunks from %d to %d\n", chunk_low, top);
523 /* Find the sizes of the first chunk of objects to move */
525 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
528 ao_lisp_record_free(mark_record);
529 mark_record = ao_lisp_record_save();
530 if (mark_record && move_record)
531 ao_lisp_record_compare("mark", move_record, mark_record);
536 /* Find the first moving object */
537 for (i = 0; i < chunk_last; i++) {
538 uint16_t size = ao_lisp_chunk[i].size;
545 if (ao_lisp_chunk[i].old_offset > top)
548 MDBG_MOVE("chunk %d %d not moving\n",
549 ao_lisp_chunk[i].old_offset,
550 ao_lisp_chunk[i].size);
552 if (ao_lisp_chunk[i].old_offset != top)
559 * Limit amount of chunk array used in mapping moves
560 * to the active region
563 chunk_low = ao_lisp_chunk[i].old_offset;
565 /* Copy all of the objects */
566 for (; i < chunk_last; i++) {
567 uint16_t size = ao_lisp_chunk[i].size;
574 MDBG_MOVE("chunk %d %d -> %d\n",
575 ao_lisp_chunk[i].old_offset,
578 ao_lisp_chunk[i].new_offset = top;
580 memmove(&ao_lisp_pool[top],
581 &ao_lisp_pool[ao_lisp_chunk[i].old_offset],
587 if (chunk_first < chunk_last) {
588 /* Relocate all references to the objects */
589 walk(ao_lisp_move, ao_lisp_poly_move);
592 ao_lisp_record_free(move_record);
593 move_record = ao_lisp_record_save();
594 if (mark_record && move_record)
595 ao_lisp_record_compare("move", mark_record, move_record);
599 /* If we ran into the end of the heap, then
600 * there's no need to keep walking
602 if (chunk_last != AO_LISP_NCHUNK)
605 /* Next loop starts right above this loop */
606 chunk_low = chunk_high;
611 ++ao_lisp_collects[style];
612 ao_lisp_freed[style] += ao_lisp_top - top;
613 ao_lisp_loops[style] += loops;
617 if (style == AO_LISP_COLLECT_FULL)
618 ao_lisp_last_top = top;
620 MDBG_DO(memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
621 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref));
623 return AO_LISP_POOL - ao_lisp_top;
627 * Mark interfaces for objects
632 * Mark a block of memory with an explicit size
636 ao_lisp_mark_block(void *addr, int size)
639 if (!AO_LISP_IS_POOL(addr))
642 offset = pool_offset(addr);
643 MDBG_MOVE("mark memory %d\n", MDBG_OFFSET(addr));
644 if (busy(ao_lisp_busy, offset)) {
645 MDBG_MOVE("already marked\n");
648 mark(ao_lisp_busy, offset);
649 note_chunk(offset, size);
654 * Note a reference to memory and collect information about a few
655 * object sizes at a time
659 ao_lisp_mark_memory(const struct ao_lisp_type *type, void *addr)
662 if (!AO_LISP_IS_POOL(addr))
665 offset = pool_offset(addr);
666 MDBG_MOVE("mark memory %d\n", MDBG_OFFSET(addr));
667 if (busy(ao_lisp_busy, offset)) {
668 MDBG_MOVE("already marked\n");
671 mark(ao_lisp_busy, offset);
672 note_chunk(offset, ao_lisp_size(type, addr));
677 * Mark an object and all that it refereces
680 ao_lisp_mark(const struct ao_lisp_type *type, void *addr)
683 MDBG_MOVE("mark %d\n", MDBG_OFFSET(addr));
685 ret = ao_lisp_mark_memory(type, addr);
687 MDBG_MOVE("mark recurse\n");
695 * Mark an object, unless it is a cons cell and
696 * do_note_cons is set. In that case, just
697 * set a bit in the cons note array; those
698 * will be marked in a separate pass to avoid
699 * deep recursion in the collector
702 ao_lisp_poly_mark(ao_poly p, uint8_t do_note_cons)
707 type = ao_lisp_poly_base_type(p);
709 if (type == AO_LISP_INT)
712 addr = ao_lisp_ref(p);
713 if (!AO_LISP_IS_POOL(addr))
716 if (type == AO_LISP_CONS && do_note_cons) {
717 note_cons(pool_offset(addr));
720 if (type == AO_LISP_OTHER)
721 type = ao_lisp_other_type(addr);
723 const struct ao_lisp_type *lisp_type = ao_lisp_types[type];
729 return ao_lisp_mark(lisp_type, addr);
734 * Find the current location of an object
735 * based on the original location. For unmoved
736 * objects, this is simple. For moved objects,
741 move_map(uint16_t offset)
745 if (offset < chunk_low || chunk_high <= offset)
748 l = find_chunk(offset);
751 if (ao_lisp_chunk[l].old_offset != offset)
754 return ao_lisp_chunk[l].new_offset;
758 ao_lisp_move_memory(const struct ao_lisp_type *type, void **ref)
761 uint16_t offset, orig_offset;
763 if (!AO_LISP_IS_POOL(addr))
768 MDBG_MOVE("move memory %d\n", MDBG_OFFSET(addr));
769 orig_offset = pool_offset(addr);
770 offset = move_map(orig_offset);
771 if (offset != orig_offset) {
772 MDBG_MOVE("update ref %d %d -> %d\n",
773 AO_LISP_IS_POOL(ref) ? MDBG_OFFSET(ref) : -1,
774 orig_offset, offset);
775 *ref = ao_lisp_pool + offset;
777 if (busy(ao_lisp_busy, offset)) {
778 MDBG_MOVE("already moved\n");
781 mark(ao_lisp_busy, offset);
782 MDBG_DO(ao_lisp_record(type, addr, ao_lisp_size(type, addr)));
787 ao_lisp_move(const struct ao_lisp_type *type, void **ref)
790 MDBG_MOVE("move object %d\n", MDBG_OFFSET(*ref));
792 ret = ao_lisp_move_memory(type, ref);
794 MDBG_MOVE("move recurse\n");
802 ao_lisp_poly_move(ao_poly *ref, uint8_t do_note_cons)
808 uint16_t offset, orig_offset;
811 base_type = type = ao_lisp_poly_base_type(p);
813 if (type == AO_LISP_INT)
816 addr = ao_lisp_ref(p);
817 if (!AO_LISP_IS_POOL(addr))
820 orig_offset = pool_offset(addr);
821 offset = move_map(orig_offset);
823 if (type == AO_LISP_CONS && do_note_cons) {
824 note_cons(orig_offset);
827 if (type == AO_LISP_OTHER)
828 type = ao_lisp_other_type(ao_lisp_pool + offset);
830 const struct ao_lisp_type *lisp_type = ao_lisp_types[type];
836 ret = ao_lisp_move(lisp_type, &addr);
839 /* Re-write the poly value */
840 if (offset != orig_offset) {
841 ao_poly np = ao_lisp_poly(ao_lisp_pool + offset, base_type);
842 MDBG_MOVE("poly %d moved %d -> %d\n",
843 type, orig_offset, offset);
851 ao_lisp_validate(void)
854 memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
855 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
863 ao_lisp_alloc(int size)
867 MDBG_DO(++dbg_allocs);
868 MDBG_DO(if (dbg_validate) ao_lisp_validate());
869 size = ao_lisp_size_round(size);
870 if (AO_LISP_POOL - ao_lisp_top < size &&
871 ao_lisp_collect(AO_LISP_COLLECT_INCREMENTAL) < size &&
872 ao_lisp_collect(AO_LISP_COLLECT_FULL) < size)
874 ao_lisp_error(AO_LISP_OOM, "out of memory");
877 addr = ao_lisp_pool + ao_lisp_top;
879 MDBG_MOVE("alloc %d size %d\n", MDBG_OFFSET(addr), size);
884 ao_lisp_cons_stash(int id, struct ao_lisp_cons *cons)
886 save_cons[id] = cons;
889 struct ao_lisp_cons *
890 ao_lisp_cons_fetch(int id)
892 struct ao_lisp_cons *cons = save_cons[id];
893 save_cons[id] = NULL;
898 ao_lisp_poly_stash(int id, ao_poly poly)
900 save_poly[id] = poly;
904 ao_lisp_poly_fetch(int id)
906 ao_poly poly = save_poly[id];
907 save_poly[id] = AO_LISP_NIL;
912 ao_lisp_string_stash(int id, char *string)
914 save_string[id] = string;
918 ao_lisp_string_fetch(int id)
920 char *string = save_string[id];
921 save_string[id] = NULL;
926 ao_lisp_frame_stash(int id, struct ao_lisp_frame *frame)
928 save_frame[id] = frame;
931 struct ao_lisp_frame *
932 ao_lisp_frame_fetch(int id)
934 struct ao_lisp_frame *frame = save_frame[id];
935 save_frame[id] = NULL;