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 ao_poly save_poly[3];
149 static const struct ao_lisp_root ao_lisp_root[] = {
151 .type = &ao_lisp_cons_type,
152 .addr = (void **) &save_cons[0],
155 .type = &ao_lisp_cons_type,
156 .addr = (void **) &save_cons[1],
159 .type = &ao_lisp_string_type,
160 .addr = (void **) &save_string[0],
163 .type = &ao_lisp_string_type,
164 .addr = (void **) &save_string[1],
168 .addr = (void **) &save_poly[0]
172 .addr = (void **) &save_poly[1]
176 .addr = (void **) &save_poly[2]
179 .type = &ao_lisp_atom_type,
180 .addr = (void **) &ao_lisp_atoms
183 .type = &ao_lisp_frame_type,
184 .addr = (void **) &ao_lisp_frame_global,
187 .type = &ao_lisp_frame_type,
188 .addr = (void **) &ao_lisp_frame_current,
191 .type = &ao_lisp_stack_type,
192 .addr = (void **) &ao_lisp_stack,
196 .addr = (void **) &ao_lisp_v,
199 .type = &ao_lisp_cons_type,
200 .addr = (void **) &ao_lisp_read_cons,
203 .type = &ao_lisp_cons_type,
204 .addr = (void **) &ao_lisp_read_cons_tail,
207 .type = &ao_lisp_cons_type,
208 .addr = (void **) &ao_lisp_read_stack,
212 #define AO_LISP_ROOT (sizeof (ao_lisp_root) / sizeof (ao_lisp_root[0]))
214 static const void ** const ao_lisp_cache[] = {
215 (const void **) &ao_lisp_cons_free_list,
216 (const void **) &ao_lisp_stack_free_list,
217 (const void **) &ao_lisp_frame_free_list[0],
218 (const void **) &ao_lisp_frame_free_list[1],
219 (const void **) &ao_lisp_frame_free_list[2],
220 (const void **) &ao_lisp_frame_free_list[3],
223 #if AO_LISP_FRAME_FREE != 4
224 #error Unexpected AO_LISP_FRAME_FREE value
227 #define AO_LISP_CACHE (sizeof (ao_lisp_cache) / sizeof (ao_lisp_cache[0]))
229 #define AO_LISP_BUSY_SIZE ((AO_LISP_POOL + 31) / 32)
231 static uint8_t ao_lisp_busy[AO_LISP_BUSY_SIZE];
232 static uint8_t ao_lisp_cons_note[AO_LISP_BUSY_SIZE];
233 static uint8_t ao_lisp_cons_last[AO_LISP_BUSY_SIZE];
234 static uint8_t ao_lisp_cons_noted;
236 uint16_t ao_lisp_top;
238 struct ao_lisp_chunk {
246 #define AO_LISP_NCHUNK 64
248 static struct ao_lisp_chunk ao_lisp_chunk[AO_LISP_NCHUNK];
250 /* Offset of an address within the pool. */
251 static inline uint16_t pool_offset(void *addr) {
253 if (!AO_LISP_IS_POOL(addr))
256 return ((uint8_t *) addr) - ao_lisp_pool;
259 static inline void mark(uint8_t *tag, int offset) {
260 int byte = offset >> 5;
261 int bit = (offset >> 2) & 7;
262 tag[byte] |= (1 << bit);
265 static inline void clear(uint8_t *tag, int offset) {
266 int byte = offset >> 5;
267 int bit = (offset >> 2) & 7;
268 tag[byte] &= ~(1 << bit);
271 static inline int busy(uint8_t *tag, int offset) {
272 int byte = offset >> 5;
273 int bit = (offset >> 2) & 7;
274 return (tag[byte] >> bit) & 1;
277 static inline int min(int a, int b) { return a < b ? a : b; }
278 static inline int max(int a, int b) { return a > b ? a : b; }
280 static inline int limit(int offset) {
281 return min(AO_LISP_POOL, max(offset, 0));
284 static int total_marked;
287 note_cons(uint16_t offset)
289 MDBG_MOVE("note cons %d\n", offset);
290 ao_lisp_cons_noted = 1;
291 mark(ao_lisp_cons_note, offset);
294 static uint16_t chunk_low, chunk_high;
295 static uint16_t chunk_first, chunk_last;
296 static int chunk_busy;
299 note_chunk(uint16_t offset, uint16_t size)
303 if (offset < chunk_low || chunk_high <= offset)
306 /* Binary search for the location */
310 int m = (l + r) >> 1;
311 if (ao_lisp_chunk[m].old_offset < offset)
317 * The correct location is always in 'l', with r = l-1 being
318 * the entry before the right one
322 /* Off the right side */
323 if (l >= AO_LISP_NCHUNK)
326 /* Off the left side */
327 if (l == 0 && chunk_busy && offset > ao_lisp_chunk[0].old_offset)
331 /* Shuffle existing entries right */
332 int end = min(AO_LISP_NCHUNK, chunk_busy + 1);
334 memmove(&ao_lisp_chunk[l+1],
336 (end - (l+1)) * sizeof (struct ao_lisp_chunk));
339 ao_lisp_chunk[l].old_offset = offset;
340 ao_lisp_chunk[l].size = size;
342 /* Increment the number of elements up to the size of the array */
343 if (chunk_busy < AO_LISP_NCHUNK)
346 /* Set the top address if the array is full */
347 if (chunk_busy == AO_LISP_NCHUNK)
348 chunk_high = ao_lisp_chunk[AO_LISP_NCHUNK-1].old_offset +
349 ao_lisp_chunk[AO_LISP_NCHUNK-1].size;
355 memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
356 chunk_high = ao_lisp_top;
361 * Walk all referenced objects calling functions on each one
365 walk(int (*visit_addr)(const struct ao_lisp_type *type, void **addr),
366 int (*visit_poly)(ao_poly *p, uint8_t do_note_cons))
371 ao_lisp_record_reset();
372 memset(ao_lisp_busy, '\0', sizeof (ao_lisp_busy));
373 memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
374 ao_lisp_cons_noted = 0;
375 for (i = 0; i < (int) AO_LISP_ROOT; i++) {
376 if (ao_lisp_root[i].type) {
377 void **a = ao_lisp_root[i].addr, *v;
379 MDBG_MOVE("root ptr %d\n", MDBG_OFFSET(v));
380 visit_addr(ao_lisp_root[i].type, a);
383 ao_poly *a = (ao_poly *) ao_lisp_root[i].addr, p;
385 MDBG_MOVE("root poly %d\n", MDBG_OFFSET(ao_lisp_ref(p)));
390 while (ao_lisp_cons_noted) {
391 memcpy(ao_lisp_cons_last, ao_lisp_cons_note, sizeof (ao_lisp_cons_note));
392 memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
393 ao_lisp_cons_noted = 0;
394 for (i = 0; i < AO_LISP_POOL; i += 4) {
395 if (busy(ao_lisp_cons_last, i)) {
396 void *v = ao_lisp_pool + i;
397 MDBG_MOVE("root cons %d\n", MDBG_OFFSET(v));
398 visit_addr(&ao_lisp_cons_type, &v);
410 for (i = 0; i < ao_lisp_top; i += 4) {
411 if ((i & 0xff) == 0) {
415 else if ((i & 0x1f) == 0)
417 if (busy(ao_lisp_busy, i))
424 #define DUMP_BUSY() dump_busy()
429 static const struct ao_lisp_type const *ao_lisp_types[AO_LISP_NUM_TYPE] = {
430 [AO_LISP_CONS] = &ao_lisp_cons_type,
431 [AO_LISP_INT] = NULL,
432 [AO_LISP_STRING] = &ao_lisp_string_type,
433 [AO_LISP_OTHER] = (void *) 0x1,
434 [AO_LISP_ATOM] = &ao_lisp_atom_type,
435 [AO_LISP_BUILTIN] = &ao_lisp_builtin_type,
436 [AO_LISP_FRAME] = &ao_lisp_frame_type,
437 [AO_LISP_LAMBDA] = &ao_lisp_lambda_type,
438 [AO_LISP_STACK] = &ao_lisp_stack_type,
442 ao_lisp_mark_ref(const struct ao_lisp_type *type, void **ref)
444 return ao_lisp_mark(type, *ref);
448 ao_lisp_poly_mark_ref(ao_poly *p, uint8_t do_note_cons)
450 return ao_lisp_poly_mark(*p, do_note_cons);
453 int ao_lisp_collects[2];
454 int ao_lisp_freed[2];
455 int ao_lisp_loops[2];
457 int ao_lisp_last_top;
460 ao_lisp_collect(uint8_t style)
469 struct ao_lisp_record *mark_record = NULL, *move_record = NULL;
471 MDBG_MOVE("collect %d\n", ao_lisp_collects);
475 /* The first time through, we're doing a full collect */
476 if (ao_lisp_last_top == 0)
477 style = AO_LISP_COLLECT_FULL;
479 /* Clear references to all caches */
480 for (i = 0; i < (int) AO_LISP_CACHE; i++)
481 *ao_lisp_cache[i] = NULL;
482 if (style == AO_LISP_COLLECT_FULL) {
485 chunk_low = top = ao_lisp_last_top;
489 MDBG_MOVE("move chunks from %d to %d\n", chunk_low, top);
490 /* Find the sizes of the first chunk of objects to move */
492 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
494 marked = total_marked;
496 ao_lisp_record_free(mark_record);
497 mark_record = ao_lisp_record_save();
498 if (mark_record && move_record)
499 ao_lisp_record_compare("mark", move_record, mark_record);
501 if (moved && moved != marked)
507 /* Find the first moving object */
508 for (i = 0; i < chunk_busy; i++) {
509 uint16_t size = ao_lisp_chunk[i].size;
516 if (ao_lisp_chunk[i].old_offset > top)
519 MDBG_MOVE("chunk %d %d not moving\n",
520 ao_lisp_chunk[i].old_offset,
521 ao_lisp_chunk[i].size);
523 if (ao_lisp_chunk[i].old_offset != top)
530 * Limit amount of chunk array used in mapping moves
531 * to the active region
534 chunk_low = ao_lisp_chunk[i].old_offset;
536 /* Copy all of the objects */
537 for (; i < chunk_busy; i++) {
538 uint16_t size = ao_lisp_chunk[i].size;
545 MDBG_MOVE("chunk %d %d -> %d\n",
546 ao_lisp_chunk[i].old_offset,
549 ao_lisp_chunk[i].new_offset = top;
551 memmove(&ao_lisp_pool[top],
552 &ao_lisp_pool[ao_lisp_chunk[i].old_offset],
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 we ran into the end of the heap, then
577 * there's no need to keep walking
579 if (chunk_last != AO_LISP_NCHUNK)
582 /* Next loop starts right above this loop */
583 chunk_low = chunk_high;
586 /* Compute amount of memory freed */
587 ret = ao_lisp_top - top;
590 ++ao_lisp_collects[style];
591 ao_lisp_freed[style] += ret;
592 ao_lisp_loops[style] += loops;
595 if (style == AO_LISP_COLLECT_FULL)
596 ao_lisp_last_top = top;
598 MDBG_DO(memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
599 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref));
605 * Mark interfaces for objects
609 * Note a reference to memory and collect information about a few
610 * object sizes at a time
614 ao_lisp_mark_memory(const struct ao_lisp_type *type, void *addr)
617 if (!AO_LISP_IS_POOL(addr))
620 offset = pool_offset(addr);
621 MDBG_MOVE("mark memory %d\n", MDBG_OFFSET(addr));
622 if (busy(ao_lisp_busy, offset)) {
623 MDBG_MOVE("already marked\n");
626 mark(ao_lisp_busy, offset);
627 note_chunk(offset, ao_lisp_size(type, addr));
632 * Mark an object and all that it refereces
635 ao_lisp_mark(const struct ao_lisp_type *type, void *addr)
638 MDBG_MOVE("mark %d\n", MDBG_OFFSET(addr));
640 ret = ao_lisp_mark_memory(type, addr);
642 MDBG_MOVE("mark recurse\n");
650 * Mark an object, unless it is a cons cell and
651 * do_note_cons is set. In that case, just
652 * set a bit in the cons note array; those
653 * will be marked in a separate pass to avoid
654 * deep recursion in the collector
657 ao_lisp_poly_mark(ao_poly p, uint8_t do_note_cons)
662 type = ao_lisp_poly_base_type(p);
664 if (type == AO_LISP_INT)
667 addr = ao_lisp_ref(p);
668 if (!AO_LISP_IS_POOL(addr))
671 if (type == AO_LISP_CONS && do_note_cons) {
672 note_cons(pool_offset(addr));
675 if (type == AO_LISP_OTHER)
676 type = ao_lisp_other_type(addr);
678 const struct ao_lisp_type *lisp_type = ao_lisp_types[type];
684 return ao_lisp_mark(lisp_type, addr);
689 * Find the current location of an object
690 * based on the original location. For unmoved
691 * objects, this is simple. For moved objects,
696 move_map(uint16_t offset)
700 if (offset < chunk_low || chunk_high <= offset)
703 /* Binary search for the location */
707 int m = (l + r) >> 1;
708 if (ao_lisp_chunk[m].old_offset < offset)
714 if (ao_lisp_chunk[l].old_offset != offset)
717 return ao_lisp_chunk[l].new_offset;
721 ao_lisp_move_memory(const struct ao_lisp_type *type, void **ref)
724 uint16_t offset, orig_offset;
726 if (!AO_LISP_IS_POOL(addr))
731 MDBG_MOVE("move memory %d\n", MDBG_OFFSET(addr));
732 orig_offset = pool_offset(addr);
733 offset = move_map(orig_offset);
734 if (offset != orig_offset) {
735 MDBG_MOVE("update ref %d %d -> %d\n",
736 AO_LISP_IS_POOL(ref) ? MDBG_OFFSET(ref) : -1,
737 orig_offset, offset);
738 *ref = ao_lisp_pool + offset;
740 if (busy(ao_lisp_busy, offset)) {
741 MDBG_MOVE("already moved\n");
744 mark(ao_lisp_busy, offset);
745 MDBG_DO(ao_lisp_record(type, addr, ao_lisp_size(type, addr)));
750 ao_lisp_move(const struct ao_lisp_type *type, void **ref)
753 MDBG_MOVE("move object %d\n", MDBG_OFFSET(*ref));
755 ret = ao_lisp_move_memory(type, ref);
757 MDBG_MOVE("move recurse\n");
765 ao_lisp_poly_move(ao_poly *ref, uint8_t do_note_cons)
771 uint16_t offset, orig_offset;
774 base_type = type = ao_lisp_poly_base_type(p);
776 if (type == AO_LISP_INT)
779 addr = ao_lisp_ref(p);
780 if (!AO_LISP_IS_POOL(addr))
783 orig_offset = pool_offset(addr);
784 offset = move_map(orig_offset);
786 if (type == AO_LISP_CONS && do_note_cons) {
787 note_cons(orig_offset);
790 if (type == AO_LISP_OTHER)
791 type = ao_lisp_other_type(ao_lisp_pool + offset);
793 const struct ao_lisp_type *lisp_type = ao_lisp_types[type];
799 ret = ao_lisp_move(lisp_type, &addr);
802 /* Re-write the poly value */
803 if (offset != orig_offset) {
804 ao_poly np = ao_lisp_poly(ao_lisp_pool + offset, base_type);
805 MDBG_MOVE("poly %d moved %d -> %d\n",
806 type, orig_offset, offset);
814 ao_lisp_validate(void)
817 memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
818 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
826 ao_lisp_alloc(int size)
830 MDBG_DO(++dbg_allocs);
831 MDBG_DO(if (dbg_validate) ao_lisp_validate());
832 size = ao_lisp_size_round(size);
833 if (ao_lisp_top + size > AO_LISP_POOL) {
834 if (ao_lisp_collect(AO_LISP_COLLECT_INCREMENTAL) < size &&
835 ao_lisp_collect(AO_LISP_COLLECT_FULL) < size)
837 ao_lisp_error(AO_LISP_OOM, "out of memory");
841 addr = ao_lisp_pool + ao_lisp_top;
847 ao_lisp_cons_stash(int id, struct ao_lisp_cons *cons)
849 save_cons[id] = cons;
852 struct ao_lisp_cons *
853 ao_lisp_cons_fetch(int id)
855 struct ao_lisp_cons *cons = save_cons[id];
856 save_cons[id] = NULL;
861 ao_lisp_poly_stash(int id, ao_poly poly)
863 save_poly[id] = poly;
867 ao_lisp_poly_fetch(int id)
869 ao_poly poly = save_poly[id];
870 save_poly[id] = AO_LISP_NIL;
875 ao_lisp_string_stash(int id, char *string)
877 save_string[id] = string;
881 ao_lisp_string_fetch(int id)
883 char *string = save_string[id];
884 save_string[id] = NULL;