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 ao_poly save_poly[3];
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 **) (void *) &save_poly[0]
176 .addr = (void **) (void *) &save_poly[1]
180 .addr = (void **) (void *) &save_poly[2]
183 .type = &ao_lisp_atom_type,
184 .addr = (void **) &ao_lisp_atoms
187 .type = &ao_lisp_frame_type,
188 .addr = (void **) &ao_lisp_frame_global,
191 .type = &ao_lisp_frame_type,
192 .addr = (void **) &ao_lisp_frame_current,
195 .type = &ao_lisp_stack_type,
196 .addr = (void **) &ao_lisp_stack,
200 .addr = (void **) (void *) &ao_lisp_v,
203 .type = &ao_lisp_cons_type,
204 .addr = (void **) &ao_lisp_read_cons,
207 .type = &ao_lisp_cons_type,
208 .addr = (void **) &ao_lisp_read_cons_tail,
211 .type = &ao_lisp_cons_type,
212 .addr = (void **) &ao_lisp_read_stack,
214 #ifdef AO_LISP_MAKE_CONST
216 .type = &ao_lisp_bool_type,
217 .addr = (void **) &ao_lisp_false,
220 .type = &ao_lisp_bool_type,
221 .addr = (void **) &ao_lisp_true,
226 #define AO_LISP_ROOT (sizeof (ao_lisp_root) / sizeof (ao_lisp_root[0]))
228 static const void ** const ao_lisp_cache[] = {
229 (const void **) &ao_lisp_cons_free_list,
230 (const void **) &ao_lisp_stack_free_list,
231 (const void **) &ao_lisp_frame_free_list[0],
232 (const void **) &ao_lisp_frame_free_list[1],
233 (const void **) &ao_lisp_frame_free_list[2],
234 (const void **) &ao_lisp_frame_free_list[3],
235 (const void **) &ao_lisp_frame_free_list[4],
236 (const void **) &ao_lisp_frame_free_list[5],
239 #if AO_LISP_FRAME_FREE != 6
240 #error Unexpected AO_LISP_FRAME_FREE value
243 #define AO_LISP_CACHE (sizeof (ao_lisp_cache) / sizeof (ao_lisp_cache[0]))
245 #define AO_LISP_BUSY_SIZE ((AO_LISP_POOL + 31) / 32)
247 static uint8_t ao_lisp_busy[AO_LISP_BUSY_SIZE];
248 static uint8_t ao_lisp_cons_note[AO_LISP_BUSY_SIZE];
249 static uint8_t ao_lisp_cons_last[AO_LISP_BUSY_SIZE];
250 static uint8_t ao_lisp_cons_noted;
252 uint16_t ao_lisp_top;
254 struct ao_lisp_chunk {
262 #define AO_LISP_NCHUNK 64
264 static struct ao_lisp_chunk ao_lisp_chunk[AO_LISP_NCHUNK];
266 /* Offset of an address within the pool. */
267 static inline uint16_t pool_offset(void *addr) {
269 if (!AO_LISP_IS_POOL(addr))
272 return ((uint8_t *) addr) - ao_lisp_pool;
275 static inline void mark(uint8_t *tag, int offset) {
276 int byte = offset >> 5;
277 int bit = (offset >> 2) & 7;
278 tag[byte] |= (1 << bit);
281 static inline void clear(uint8_t *tag, int offset) {
282 int byte = offset >> 5;
283 int bit = (offset >> 2) & 7;
284 tag[byte] &= ~(1 << bit);
287 static inline int busy(uint8_t *tag, int offset) {
288 int byte = offset >> 5;
289 int bit = (offset >> 2) & 7;
290 return (tag[byte] >> bit) & 1;
293 static inline int min(int a, int b) { return a < b ? a : b; }
294 static inline int max(int a, int b) { return a > b ? a : b; }
296 static inline int limit(int offset) {
297 return min(AO_LISP_POOL, max(offset, 0));
301 note_cons(uint16_t offset)
303 MDBG_MOVE("note cons %d\n", offset);
304 ao_lisp_cons_noted = 1;
305 mark(ao_lisp_cons_note, offset);
308 static uint16_t chunk_low, chunk_high;
309 static uint16_t chunk_first, chunk_last;
312 find_chunk(uint16_t offset)
315 /* Binary search for the location */
319 int m = (l + r) >> 1;
320 if (ao_lisp_chunk[m].old_offset < offset)
329 note_chunk(uint16_t offset, uint16_t size)
333 if (offset < chunk_low || chunk_high <= offset)
336 l = find_chunk(offset);
339 * The correct location is always in 'l', with r = l-1 being
340 * the entry before the right one
344 /* Off the right side */
345 if (l >= AO_LISP_NCHUNK)
348 /* Off the left side */
349 if (l == 0 && chunk_last && offset > ao_lisp_chunk[0].old_offset)
353 /* Shuffle existing entries right */
354 int end = min(AO_LISP_NCHUNK, chunk_last + 1);
356 memmove(&ao_lisp_chunk[l+1],
358 (end - (l+1)) * sizeof (struct ao_lisp_chunk));
361 ao_lisp_chunk[l].old_offset = offset;
362 ao_lisp_chunk[l].size = size;
364 /* Increment the number of elements up to the size of the array */
365 if (chunk_last < AO_LISP_NCHUNK)
368 /* Set the top address if the array is full */
369 if (chunk_last == AO_LISP_NCHUNK)
370 chunk_high = ao_lisp_chunk[AO_LISP_NCHUNK-1].old_offset +
371 ao_lisp_chunk[AO_LISP_NCHUNK-1].size;
377 chunk_high = ao_lisp_top;
383 * Walk all referenced objects calling functions on each one
387 walk(int (*visit_addr)(const struct ao_lisp_type *type, void **addr),
388 int (*visit_poly)(ao_poly *p, uint8_t do_note_cons))
392 ao_lisp_record_reset();
393 memset(ao_lisp_busy, '\0', sizeof (ao_lisp_busy));
394 memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
395 ao_lisp_cons_noted = 0;
396 for (i = 0; i < (int) AO_LISP_ROOT; i++) {
397 if (ao_lisp_root[i].type) {
398 void **a = ao_lisp_root[i].addr, *v;
400 MDBG_MOVE("root ptr %d\n", MDBG_OFFSET(v));
401 visit_addr(ao_lisp_root[i].type, a);
404 ao_poly *a = (ao_poly *) ao_lisp_root[i].addr, p;
406 MDBG_MOVE("root poly %d\n", MDBG_OFFSET(ao_lisp_ref(p)));
411 while (ao_lisp_cons_noted) {
412 memcpy(ao_lisp_cons_last, ao_lisp_cons_note, sizeof (ao_lisp_cons_note));
413 memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
414 ao_lisp_cons_noted = 0;
415 for (i = 0; i < AO_LISP_POOL; i += 4) {
416 if (busy(ao_lisp_cons_last, i)) {
417 void *v = ao_lisp_pool + i;
418 MDBG_MOVE("root cons %d\n", MDBG_OFFSET(v));
419 visit_addr(&ao_lisp_cons_type, &v);
431 for (i = 0; i < ao_lisp_top; i += 4) {
432 if ((i & 0xff) == 0) {
436 else if ((i & 0x1f) == 0)
438 if (busy(ao_lisp_busy, i))
445 #define DUMP_BUSY() dump_busy()
450 static const struct ao_lisp_type *ao_lisp_types[AO_LISP_NUM_TYPE] = {
451 [AO_LISP_CONS] = &ao_lisp_cons_type,
452 [AO_LISP_INT] = NULL,
453 [AO_LISP_STRING] = &ao_lisp_string_type,
454 [AO_LISP_OTHER] = (void *) 0x1,
455 [AO_LISP_ATOM] = &ao_lisp_atom_type,
456 [AO_LISP_BUILTIN] = &ao_lisp_builtin_type,
457 [AO_LISP_FRAME] = &ao_lisp_frame_type,
458 [AO_LISP_LAMBDA] = &ao_lisp_lambda_type,
459 [AO_LISP_STACK] = &ao_lisp_stack_type,
460 [AO_LISP_BOOL] = &ao_lisp_bool_type,
464 ao_lisp_mark_ref(const struct ao_lisp_type *type, void **ref)
466 return ao_lisp_mark(type, *ref);
470 ao_lisp_poly_mark_ref(ao_poly *p, uint8_t do_note_cons)
472 return ao_lisp_poly_mark(*p, do_note_cons);
476 int ao_lisp_collects[2];
477 int ao_lisp_freed[2];
478 int ao_lisp_loops[2];
481 int ao_lisp_last_top;
484 ao_lisp_collect(uint8_t style)
492 struct ao_lisp_record *mark_record = NULL, *move_record = NULL;
494 MDBG_MOVE("collect %d\n", ao_lisp_collects[style]);
497 /* The first time through, we're doing a full collect */
498 if (ao_lisp_last_top == 0)
499 style = AO_LISP_COLLECT_FULL;
501 /* Clear references to all caches */
502 for (i = 0; i < (int) AO_LISP_CACHE; i++)
503 *ao_lisp_cache[i] = NULL;
504 if (style == AO_LISP_COLLECT_FULL) {
507 chunk_low = top = ao_lisp_last_top;
513 MDBG_MOVE("move chunks from %d to %d\n", chunk_low, top);
514 /* Find the sizes of the first chunk of objects to move */
516 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
519 ao_lisp_record_free(mark_record);
520 mark_record = ao_lisp_record_save();
521 if (mark_record && move_record)
522 ao_lisp_record_compare("mark", move_record, mark_record);
527 /* Find the first moving object */
528 for (i = 0; i < chunk_last; i++) {
529 uint16_t size = ao_lisp_chunk[i].size;
536 if (ao_lisp_chunk[i].old_offset > top)
539 MDBG_MOVE("chunk %d %d not moving\n",
540 ao_lisp_chunk[i].old_offset,
541 ao_lisp_chunk[i].size);
543 if (ao_lisp_chunk[i].old_offset != top)
550 * Limit amount of chunk array used in mapping moves
551 * to the active region
554 chunk_low = ao_lisp_chunk[i].old_offset;
556 /* Copy all of the objects */
557 for (; i < chunk_last; i++) {
558 uint16_t size = ao_lisp_chunk[i].size;
565 MDBG_MOVE("chunk %d %d -> %d\n",
566 ao_lisp_chunk[i].old_offset,
569 ao_lisp_chunk[i].new_offset = top;
571 memmove(&ao_lisp_pool[top],
572 &ao_lisp_pool[ao_lisp_chunk[i].old_offset],
578 if (chunk_first < chunk_last) {
579 /* Relocate all references to the objects */
580 walk(ao_lisp_move, ao_lisp_poly_move);
583 ao_lisp_record_free(move_record);
584 move_record = ao_lisp_record_save();
585 if (mark_record && move_record)
586 ao_lisp_record_compare("move", mark_record, move_record);
590 /* If we ran into the end of the heap, then
591 * there's no need to keep walking
593 if (chunk_last != AO_LISP_NCHUNK)
596 /* Next loop starts right above this loop */
597 chunk_low = chunk_high;
602 ++ao_lisp_collects[style];
603 ao_lisp_freed[style] += ao_lisp_top - top;
604 ao_lisp_loops[style] += loops;
608 if (style == AO_LISP_COLLECT_FULL)
609 ao_lisp_last_top = top;
611 MDBG_DO(memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
612 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref));
614 return AO_LISP_POOL - ao_lisp_top;
618 * Mark interfaces for objects
622 * Note a reference to memory and collect information about a few
623 * object sizes at a time
627 ao_lisp_mark_memory(const struct ao_lisp_type *type, void *addr)
630 if (!AO_LISP_IS_POOL(addr))
633 offset = pool_offset(addr);
634 MDBG_MOVE("mark memory %d\n", MDBG_OFFSET(addr));
635 if (busy(ao_lisp_busy, offset)) {
636 MDBG_MOVE("already marked\n");
639 mark(ao_lisp_busy, offset);
640 note_chunk(offset, ao_lisp_size(type, addr));
645 * Mark an object and all that it refereces
648 ao_lisp_mark(const struct ao_lisp_type *type, void *addr)
651 MDBG_MOVE("mark %d\n", MDBG_OFFSET(addr));
653 ret = ao_lisp_mark_memory(type, addr);
655 MDBG_MOVE("mark recurse\n");
663 * Mark an object, unless it is a cons cell and
664 * do_note_cons is set. In that case, just
665 * set a bit in the cons note array; those
666 * will be marked in a separate pass to avoid
667 * deep recursion in the collector
670 ao_lisp_poly_mark(ao_poly p, uint8_t do_note_cons)
675 type = ao_lisp_poly_base_type(p);
677 if (type == AO_LISP_INT)
680 addr = ao_lisp_ref(p);
681 if (!AO_LISP_IS_POOL(addr))
684 if (type == AO_LISP_CONS && do_note_cons) {
685 note_cons(pool_offset(addr));
688 if (type == AO_LISP_OTHER)
689 type = ao_lisp_other_type(addr);
691 const struct ao_lisp_type *lisp_type = ao_lisp_types[type];
697 return ao_lisp_mark(lisp_type, addr);
702 * Find the current location of an object
703 * based on the original location. For unmoved
704 * objects, this is simple. For moved objects,
709 move_map(uint16_t offset)
713 if (offset < chunk_low || chunk_high <= offset)
716 l = find_chunk(offset);
719 if (ao_lisp_chunk[l].old_offset != offset)
722 return ao_lisp_chunk[l].new_offset;
726 ao_lisp_move_memory(const struct ao_lisp_type *type, void **ref)
729 uint16_t offset, orig_offset;
731 if (!AO_LISP_IS_POOL(addr))
736 MDBG_MOVE("move memory %d\n", MDBG_OFFSET(addr));
737 orig_offset = pool_offset(addr);
738 offset = move_map(orig_offset);
739 if (offset != orig_offset) {
740 MDBG_MOVE("update ref %d %d -> %d\n",
741 AO_LISP_IS_POOL(ref) ? MDBG_OFFSET(ref) : -1,
742 orig_offset, offset);
743 *ref = ao_lisp_pool + offset;
745 if (busy(ao_lisp_busy, offset)) {
746 MDBG_MOVE("already moved\n");
749 mark(ao_lisp_busy, offset);
750 MDBG_DO(ao_lisp_record(type, addr, ao_lisp_size(type, addr)));
755 ao_lisp_move(const struct ao_lisp_type *type, void **ref)
758 MDBG_MOVE("move object %d\n", MDBG_OFFSET(*ref));
760 ret = ao_lisp_move_memory(type, ref);
762 MDBG_MOVE("move recurse\n");
770 ao_lisp_poly_move(ao_poly *ref, uint8_t do_note_cons)
776 uint16_t offset, orig_offset;
779 base_type = type = ao_lisp_poly_base_type(p);
781 if (type == AO_LISP_INT)
784 addr = ao_lisp_ref(p);
785 if (!AO_LISP_IS_POOL(addr))
788 orig_offset = pool_offset(addr);
789 offset = move_map(orig_offset);
791 if (type == AO_LISP_CONS && do_note_cons) {
792 note_cons(orig_offset);
795 if (type == AO_LISP_OTHER)
796 type = ao_lisp_other_type(ao_lisp_pool + offset);
798 const struct ao_lisp_type *lisp_type = ao_lisp_types[type];
804 ret = ao_lisp_move(lisp_type, &addr);
807 /* Re-write the poly value */
808 if (offset != orig_offset) {
809 ao_poly np = ao_lisp_poly(ao_lisp_pool + offset, base_type);
810 MDBG_MOVE("poly %d moved %d -> %d\n",
811 type, orig_offset, offset);
819 ao_lisp_validate(void)
822 memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
823 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
831 ao_lisp_alloc(int size)
835 MDBG_DO(++dbg_allocs);
836 MDBG_DO(if (dbg_validate) ao_lisp_validate());
837 size = ao_lisp_size_round(size);
838 if (AO_LISP_POOL - ao_lisp_top < size &&
839 ao_lisp_collect(AO_LISP_COLLECT_INCREMENTAL) < size &&
840 ao_lisp_collect(AO_LISP_COLLECT_FULL) < size)
842 ao_lisp_error(AO_LISP_OOM, "out of memory");
845 addr = ao_lisp_pool + ao_lisp_top;
851 ao_lisp_cons_stash(int id, struct ao_lisp_cons *cons)
853 save_cons[id] = cons;
856 struct ao_lisp_cons *
857 ao_lisp_cons_fetch(int id)
859 struct ao_lisp_cons *cons = save_cons[id];
860 save_cons[id] = NULL;
865 ao_lisp_poly_stash(int id, ao_poly poly)
867 save_poly[id] = poly;
871 ao_lisp_poly_fetch(int id)
873 ao_poly poly = save_poly[id];
874 save_poly[id] = AO_LISP_NIL;
879 ao_lisp_string_stash(int id, char *string)
881 save_string[id] = string;
885 ao_lisp_string_fetch(int id)
887 char *string = save_string[id];
888 save_string[id] = NULL;