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)));
41 int dbg_mem = DBG_MEM_START;
44 struct ao_lisp_record {
45 struct ao_lisp_record *next;
46 const struct ao_lisp_type *type;
51 static struct ao_lisp_record *record_head, **record_tail;
54 ao_lisp_record_free(struct ao_lisp_record *record)
57 struct ao_lisp_record *next = record->next;
64 ao_lisp_record_reset(void)
66 ao_lisp_record_free(record_head);
68 record_tail = &record_head;
72 ao_lisp_record(const struct ao_lisp_type *type,
76 struct ao_lisp_record *r = malloc(sizeof (struct ao_lisp_record));
83 record_tail = &r->next;
86 static struct ao_lisp_record *
87 ao_lisp_record_save(void)
89 struct ao_lisp_record *r = record_head;
92 record_tail = &record_head;
97 ao_lisp_record_compare(char *where,
98 struct ao_lisp_record *a,
99 struct ao_lisp_record *b)
102 if (a->type != b->type || a->size != b->size) {
103 printf("%s record difers %d %s %d -> %d %s %d\n",
105 MDBG_OFFSET(a->addr),
108 MDBG_OFFSET(b->addr),
117 printf("%s record differs %d %s %d -> NULL\n",
119 MDBG_OFFSET(a->addr),
125 printf("%s record differs NULL -> %d %s %d\n",
127 MDBG_OFFSET(b->addr),
135 #define ao_lisp_record_reset()
138 uint8_t ao_lisp_exception;
140 struct ao_lisp_root {
141 const struct ao_lisp_type *type;
145 static struct ao_lisp_cons *save_cons[2];
146 static char *save_string[2];
147 static struct ao_lisp_stack *save_stack[3];
148 static ao_poly save_poly[2];
150 static const struct ao_lisp_root ao_lisp_root[] = {
152 .type = &ao_lisp_cons_type,
153 .addr = (void **) &save_cons[0],
156 .type = &ao_lisp_cons_type,
157 .addr = (void **) &save_cons[1],
160 .type = &ao_lisp_stack_type,
161 .addr = (void **) &save_stack[0]
164 .type = &ao_lisp_stack_type,
165 .addr = (void **) &save_stack[1]
168 .type = &ao_lisp_stack_type,
169 .addr = (void **) &save_stack[2]
172 .type = &ao_lisp_string_type,
173 .addr = (void **) &save_string[0]
176 .type = &ao_lisp_string_type,
177 .addr = (void **) &save_string[1]
181 .addr = (void **) &save_poly[0]
185 .addr = (void **) &save_poly[1]
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 **) &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,
221 #define AO_LISP_ROOT (sizeof (ao_lisp_root) / sizeof (ao_lisp_root[0]))
223 static const void ** const ao_lisp_cache[] = {
224 (const void **) &ao_lisp_cons_free_list,
225 (const void **) &ao_lisp_stack_free_list,
226 (const void **) &ao_lisp_frame_free_list[0],
227 (const void **) &ao_lisp_frame_free_list[1],
228 (const void **) &ao_lisp_frame_free_list[2],
229 (const void **) &ao_lisp_frame_free_list[3],
232 #if AO_LISP_FRAME_FREE != 4
233 #error Unexpected AO_LISP_FRAME_FREE value
236 #define AO_LISP_CACHE (sizeof (ao_lisp_cache) / sizeof (ao_lisp_cache[0]))
238 #define AO_LISP_BUSY_SIZE ((AO_LISP_POOL + 31) / 32)
240 static uint8_t ao_lisp_busy[AO_LISP_BUSY_SIZE];
241 static uint8_t ao_lisp_cons_note[AO_LISP_BUSY_SIZE];
242 static uint8_t ao_lisp_cons_last[AO_LISP_BUSY_SIZE];
243 static uint8_t ao_lisp_cons_noted;
245 uint16_t ao_lisp_top;
247 struct ao_lisp_chunk {
255 #define AO_LISP_NCHUNK 64
257 static struct ao_lisp_chunk ao_lisp_chunk[AO_LISP_NCHUNK];
259 /* Offset of an address within the pool. */
260 static inline uint16_t pool_offset(void *addr) {
262 if (!AO_LISP_IS_POOL(addr))
265 return ((uint8_t *) addr) - ao_lisp_pool;
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, chunk_high;
307 static uint16_t chunk_first, chunk_last;
308 static int chunk_busy;
311 note_chunk(uint16_t addr, uint16_t size)
315 if (addr < chunk_low || chunk_high <= addr)
318 /* Binary search for the location */
322 int m = (l + r) >> 1;
323 if (ao_lisp_chunk[m].old_addr < addr)
329 * The correct location is always in 'l', with r = l-1 being
330 * the entry before the right one
334 /* Off the right side */
335 if (l >= AO_LISP_NCHUNK)
338 /* Off the left side */
339 if (l == 0 && chunk_busy && addr > ao_lisp_chunk[0].old_addr)
343 /* Shuffle existing entries right */
344 int end = min(AO_LISP_NCHUNK, chunk_busy + 1);
346 memmove(&ao_lisp_chunk[l+1],
348 (end - (l+1)) * sizeof (struct ao_lisp_chunk));
351 ao_lisp_chunk[l].old_addr = addr;
352 ao_lisp_chunk[l].size = size;
354 /* Increment the number of elements up to the size of the array */
355 if (chunk_busy < AO_LISP_NCHUNK)
358 /* Set the top address if the array is full */
359 if (chunk_busy == AO_LISP_NCHUNK)
360 chunk_high = ao_lisp_chunk[AO_LISP_NCHUNK-1].old_addr +
361 ao_lisp_chunk[AO_LISP_NCHUNK-1].size;
367 memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
368 chunk_high = ao_lisp_top;
373 * Walk all referenced objects calling functions on each one
377 walk(int (*visit_addr)(const struct ao_lisp_type *type, void **addr),
378 int (*visit_poly)(ao_poly *p, uint8_t do_note_cons))
383 ao_lisp_record_reset();
384 memset(ao_lisp_busy, '\0', sizeof (ao_lisp_busy));
385 memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
386 ao_lisp_cons_noted = 0;
387 for (i = 0; i < (int) AO_LISP_ROOT; i++) {
388 if (ao_lisp_root[i].type) {
389 void **a = ao_lisp_root[i].addr, *v;
391 MDBG_MOVE("root ptr %d\n", MDBG_OFFSET(v));
392 visit_addr(ao_lisp_root[i].type, a);
395 ao_poly *a = (ao_poly *) ao_lisp_root[i].addr, p;
397 MDBG_MOVE("root poly %d\n", MDBG_OFFSET(ao_lisp_ref(p)));
402 while (ao_lisp_cons_noted) {
403 memcpy(ao_lisp_cons_last, ao_lisp_cons_note, sizeof (ao_lisp_cons_note));
404 memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
405 ao_lisp_cons_noted = 0;
406 for (i = 0; i < AO_LISP_POOL; i += 4) {
407 if (busy(ao_lisp_cons_last, i)) {
408 void *v = ao_lisp_pool + i;
409 MDBG_MOVE("root cons %d\n", MDBG_OFFSET(v));
410 visit_addr(&ao_lisp_cons_type, &v);
422 for (i = 0; i < ao_lisp_top; i += 4) {
423 if ((i & 0xff) == 0) {
427 else if ((i & 0x1f) == 0)
429 if (busy(ao_lisp_busy, i))
436 #define DUMP_BUSY() dump_busy()
441 static const struct ao_lisp_type const *ao_lisp_types[AO_LISP_NUM_TYPE] = {
442 [AO_LISP_CONS] = &ao_lisp_cons_type,
443 [AO_LISP_INT] = NULL,
444 [AO_LISP_STRING] = &ao_lisp_string_type,
445 [AO_LISP_OTHER] = (void *) 0x1,
446 [AO_LISP_ATOM] = &ao_lisp_atom_type,
447 [AO_LISP_BUILTIN] = &ao_lisp_builtin_type,
448 [AO_LISP_FRAME] = &ao_lisp_frame_type,
449 [AO_LISP_LAMBDA] = &ao_lisp_lambda_type,
450 [AO_LISP_STACK] = &ao_lisp_stack_type,
454 ao_lisp_mark_ref(const struct ao_lisp_type *type, void **ref)
456 return ao_lisp_mark(type, *ref);
460 ao_lisp_poly_mark_ref(ao_poly *p, uint8_t do_note_cons)
462 return ao_lisp_poly_mark(*p, do_note_cons);
465 int ao_lisp_collects[2];
466 int ao_lisp_freed[2];
467 int ao_lisp_loops[2];
469 int ao_lisp_last_top;
472 ao_lisp_collect(uint8_t style)
481 struct ao_lisp_record *mark_record = NULL, *move_record = NULL;
483 MDBG_MOVE("collect %d\n", ao_lisp_collects);
487 /* The first time through, we're doing a full collect */
488 if (ao_lisp_last_top == 0)
489 style = AO_LISP_COLLECT_FULL;
491 /* Clear references to all caches */
492 for (i = 0; i < (int) AO_LISP_CACHE; i++)
493 *ao_lisp_cache[i] = NULL;
494 if (style == AO_LISP_COLLECT_FULL) {
497 chunk_low = top = ao_lisp_last_top;
501 MDBG_MOVE("move chunks from %d to %d\n", chunk_low, top);
502 /* Find the sizes of the first chunk of objects to move */
504 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
506 marked = total_marked;
508 ao_lisp_record_free(mark_record);
509 mark_record = ao_lisp_record_save();
510 if (mark_record && move_record)
511 ao_lisp_record_compare("mark", move_record, mark_record);
513 if (moved && moved != marked)
519 /* Find the first moving object */
520 for (i = 0; i < AO_LISP_NCHUNK; i++) {
521 uint16_t size = ao_lisp_chunk[i].size;
526 if (ao_lisp_chunk[i].old_addr > top)
529 if (ao_lisp_chunk[i].old_addr != top)
534 MDBG_MOVE("chunk %d %d not moving\n",
535 ao_lisp_chunk[i].old_addr,
536 ao_lisp_chunk[i].size);
540 /* Copy all of the objects */
541 for (; i < AO_LISP_NCHUNK; i++) {
542 uint16_t size = ao_lisp_chunk[i].size;
547 MDBG_MOVE("chunk %d %d -> %d\n",
548 ao_lisp_chunk[i].old_addr,
551 ao_lisp_chunk[i].new_addr = top;
552 memmove(&ao_lisp_pool[top],
553 &ao_lisp_pool[ao_lisp_chunk[i].old_addr],
560 if (chunk_first < chunk_last) {
561 /* Relocate all references to the objects */
562 walk(ao_lisp_move, ao_lisp_poly_move);
565 ao_lisp_record_free(move_record);
566 move_record = ao_lisp_record_save();
567 if (mark_record && move_record)
568 ao_lisp_record_compare("move", mark_record, move_record);
570 moved = total_marked;
576 if (chunk_last != AO_LISP_NCHUNK)
579 chunk_low = chunk_high;
582 /* Compute amount of memory freed */
583 ret = ao_lisp_top - top;
586 ++ao_lisp_collects[style];
587 ao_lisp_freed[style] += ret;
588 ao_lisp_loops[style] += loops;
591 if (style == AO_LISP_COLLECT_FULL)
592 ao_lisp_last_top = top;
594 MDBG_DO(memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
595 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref));
601 * Mark interfaces for objects
603 * Note a reference to memory and
604 * collect information about a few object sizes
609 ao_lisp_mark_memory(const struct ao_lisp_type *type, void *addr)
612 if (!AO_LISP_IS_POOL(addr))
615 offset = pool_offset(addr);
616 MDBG_MOVE("mark memory %d\n", MDBG_OFFSET(addr));
617 if (busy(ao_lisp_busy, offset)) {
618 MDBG_MOVE("already marked\n");
621 mark(ao_lisp_busy, offset);
622 note_chunk(offset, ao_lisp_size(type, addr));
627 ao_lisp_mark(const struct ao_lisp_type *type, void *addr)
630 MDBG_MOVE("mark %d\n", MDBG_OFFSET(addr));
632 ret = ao_lisp_mark_memory(type, addr);
634 MDBG_MOVE("mark recurse\n");
642 ao_lisp_poly_mark(ao_poly p, uint8_t do_note_cons)
650 type = ao_lisp_poly_base_type(p);
651 addr = ao_lisp_ref(p);
653 if (!AO_LISP_IS_POOL(addr))
656 if (type == AO_LISP_CONS && do_note_cons) {
657 note_cons(ao_lisp_ref(p));
660 const struct ao_lisp_type *lisp_type;
662 if (type == AO_LISP_OTHER) {
663 type = ao_lisp_other_type(ao_lisp_poly_other(p));
665 if (type <= AO_LISP_OTHER || AO_LISP_NUM_TYPE <= type)
670 lisp_type = ao_lisp_types[ao_lisp_poly_type(p)];
673 return ao_lisp_mark(lisp_type, ao_lisp_ref(p));
680 uint16_t offset = pool_offset(addr);
683 for (i = chunk_first; i < chunk_last; i++) {
684 if (ao_lisp_chunk[i].old_addr == offset) {
685 MDBG_MOVE("move %d -> %d\n",
686 ao_lisp_chunk[i].old_addr,
687 ao_lisp_chunk[i].new_addr);
688 return ao_lisp_pool + ao_lisp_chunk[i].new_addr;
695 ao_lisp_move_memory(const struct ao_lisp_type *type, void **ref)
700 if (!AO_LISP_IS_POOL(addr))
705 MDBG_MOVE("move memory %d\n", MDBG_OFFSET(addr));
706 addr = move_map(addr);
708 MDBG_MOVE("update ref %d %d -> %d\n",
709 AO_LISP_IS_POOL(ref) ? MDBG_OFFSET(ref) : -1,
710 MDBG_OFFSET(*ref), MDBG_OFFSET(addr));
713 offset = pool_offset(addr);
714 if (busy(ao_lisp_busy, offset)) {
715 MDBG_MOVE("already moved\n");
718 mark(ao_lisp_busy, offset);
719 MDBG_DO(ao_lisp_record(type, addr, ao_lisp_size(type, addr)));
724 ao_lisp_move(const struct ao_lisp_type *type, void **ref)
727 MDBG_MOVE("move object %d\n", MDBG_OFFSET(*ref));
729 ret = ao_lisp_move_memory(type, ref);
731 MDBG_MOVE("move recurse\n");
739 ao_lisp_poly_move(ao_poly *ref, uint8_t do_note_cons)
749 addr = ao_lisp_ref(p);
751 if (!AO_LISP_IS_POOL(addr))
754 type = ao_lisp_poly_base_type(p);
756 if (type == AO_LISP_CONS && do_note_cons) {
758 addr = move_map(addr);
761 const struct ao_lisp_type *lisp_type;
763 if (type == AO_LISP_OTHER) {
764 type = ao_lisp_other_type(move_map(ao_lisp_poly_other(p)));
766 if (type <= AO_LISP_OTHER || AO_LISP_NUM_TYPE <= type)
771 lisp_type = ao_lisp_types[type];
774 ret = ao_lisp_move(lisp_type, &addr);
777 /* Re-write the poly value */
778 if (addr != ao_lisp_ref(p)) {
779 ao_poly np = ao_lisp_poly(addr, p & AO_LISP_TYPE_MASK);
780 MDBG_MOVE("poly %d moved %d -> %d\n",
781 type, MDBG_OFFSET(ao_lisp_ref(p)), MDBG_OFFSET(ao_lisp_ref(np)));
789 ao_lisp_validate(void)
792 memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
793 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
801 ao_lisp_alloc(int size)
805 MDBG_DO(++dbg_allocs);
806 MDBG_DO(if (dbg_validate) ao_lisp_validate());
807 size = ao_lisp_size_round(size);
808 if (ao_lisp_top + size > AO_LISP_POOL) {
809 if (!ao_lisp_collect(AO_LISP_COLLECT_INCREMENTAL) &&
810 !ao_lisp_collect(AO_LISP_COLLECT_FULL))
812 ao_lisp_error(AO_LISP_OOM, "out of memory");
816 addr = ao_lisp_pool + ao_lisp_top;
822 ao_lisp_cons_stash(int id, struct ao_lisp_cons *cons)
824 save_cons[id] = cons;
827 struct ao_lisp_cons *
828 ao_lisp_cons_fetch(int id)
830 struct ao_lisp_cons *cons = save_cons[id];
831 save_cons[id] = NULL;
836 ao_lisp_stack_stash(int id, struct ao_lisp_stack *stack)
838 save_stack[id] = stack;
841 struct ao_lisp_stack *
842 ao_lisp_stack_fetch(int id)
844 struct ao_lisp_stack *stack = save_stack[id];
845 save_stack[id] = NULL;
850 ao_lisp_string_stash(int id, char *string)
852 save_string[id] = string;
856 ao_lisp_string_fetch(int id)
858 char *string = save_string[id];
859 save_string[id] = NULL;
863 ao_lisp_poly_stash(int id, ao_poly poly)
865 save_poly[id] = poly;
869 ao_lisp_poly_fetch(int id)
871 ao_poly poly = save_poly[id];
872 save_poly[id] = AO_LISP_NIL;