altos/lisp: Sort frames by atom
[fw/altos] / src / lisp / ao_lisp_mem.c
1 /*
2  * Copyright © 2016 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  */
14
15 #define AO_LISP_CONST_BITS
16
17 #include "ao_lisp.h"
18 #include <stdio.h>
19
20 #ifdef AO_LISP_MAKE_CONST
21
22 /*
23  * When building the constant table, it is the
24  * pool for allocations.
25  */
26
27 #include <stdlib.h>
28 uint8_t ao_lisp_const[AO_LISP_POOL_CONST] __attribute__((aligned(4)));
29 #define ao_lisp_pool ao_lisp_const
30 #undef AO_LISP_POOL
31 #define AO_LISP_POOL AO_LISP_POOL_CONST
32
33 #else
34
35 uint8_t ao_lisp_pool[AO_LISP_POOL + AO_LISP_POOL_EXTRA] __attribute__((aligned(4)));
36
37 #endif
38
39 #if DBG_MEM
40 int dbg_move_depth;
41 int dbg_mem = DBG_MEM_START;
42 int dbg_validate = 0;
43
44 struct ao_lisp_record {
45         struct ao_lisp_record           *next;
46         const struct ao_lisp_type       *type;
47         void                            *addr;
48         int                             size;
49 };
50
51 static struct ao_lisp_record    *record_head, **record_tail;
52
53 static void
54 ao_lisp_record_free(struct ao_lisp_record *record)
55 {
56         while (record) {
57                 struct ao_lisp_record *next = record->next;
58                 free(record);
59                 record = next;
60         }
61 }
62
63 static void
64 ao_lisp_record_reset(void)
65 {
66         ao_lisp_record_free(record_head);
67         record_head = NULL;
68         record_tail = &record_head;
69 }
70
71 static void
72 ao_lisp_record(const struct ao_lisp_type        *type,
73                void                             *addr,
74                int                              size)
75 {
76         struct ao_lisp_record   *r = malloc(sizeof (struct ao_lisp_record));
77
78         r->next = NULL;
79         r->type = type;
80         r->addr = addr;
81         r->size = size;
82         *record_tail = r;
83         record_tail = &r->next;
84 }
85
86 static struct ao_lisp_record *
87 ao_lisp_record_save(void)
88 {
89         struct ao_lisp_record *r = record_head;
90
91         record_head = NULL;
92         record_tail = &record_head;
93         return r;
94 }
95
96 static void
97 ao_lisp_record_compare(char *where,
98                        struct ao_lisp_record *a,
99                        struct ao_lisp_record *b)
100 {
101         while (a && b) {
102                 if (a->type != b->type || a->size != b->size) {
103                         printf("%s record difers %d %s %d -> %d %s %d\n",
104                                where,
105                                MDBG_OFFSET(a->addr),
106                                a->type->name,
107                                a->size,
108                                MDBG_OFFSET(b->addr),
109                                b->type->name,
110                                b->size);
111                         ao_lisp_abort();
112                 }
113                 a = a->next;
114                 b = b->next;
115         }
116         if (a) {
117                 printf("%s record differs %d %s %d -> NULL\n",
118                        where,
119                        MDBG_OFFSET(a->addr),
120                        a->type->name,
121                        a->size);
122                 ao_lisp_abort();
123         }
124         if (b) {
125                 printf("%s record differs NULL -> %d %s %d\n",
126                        where,
127                        MDBG_OFFSET(b->addr),
128                        b->type->name,
129                        b->size);
130                 ao_lisp_abort();
131         }
132 }
133
134 #else
135 #define ao_lisp_record_reset()
136 #endif
137
138 uint8_t ao_lisp_exception;
139
140 struct ao_lisp_root {
141         const struct ao_lisp_type       *type;
142         void                            **addr;
143 };
144
145 static struct ao_lisp_cons      *save_cons[2];
146 static char                     *save_string[2];
147 static ao_poly                  save_poly[3];
148
149 static const struct ao_lisp_root        ao_lisp_root[] = {
150         {
151                 .type = &ao_lisp_cons_type,
152                 .addr = (void **) &save_cons[0],
153         },
154         {
155                 .type = &ao_lisp_cons_type,
156                 .addr = (void **) &save_cons[1],
157         },
158         {
159                 .type = &ao_lisp_string_type,
160                 .addr = (void **) &save_string[0],
161         },
162         {
163                 .type = &ao_lisp_string_type,
164                 .addr = (void **) &save_string[1],
165         },
166         {
167                 .type = NULL,
168                 .addr = (void **) &save_poly[0]
169         },
170         {
171                 .type = NULL,
172                 .addr = (void **) &save_poly[1]
173         },
174         {
175                 .type = NULL,
176                 .addr = (void **) &save_poly[2]
177         },
178         {
179                 .type = &ao_lisp_atom_type,
180                 .addr = (void **) &ao_lisp_atoms
181         },
182         {
183                 .type = &ao_lisp_frame_type,
184                 .addr = (void **) &ao_lisp_frame_global,
185         },
186         {
187                 .type = &ao_lisp_frame_type,
188                 .addr = (void **) &ao_lisp_frame_current,
189         },
190         {
191                 .type = &ao_lisp_stack_type,
192                 .addr = (void **) &ao_lisp_stack,
193         },
194         {
195                 .type = NULL,
196                 .addr = (void **) &ao_lisp_v,
197         },
198         {
199                 .type = &ao_lisp_cons_type,
200                 .addr = (void **) &ao_lisp_read_cons,
201         },
202         {
203                 .type = &ao_lisp_cons_type,
204                 .addr = (void **) &ao_lisp_read_cons_tail,
205         },
206         {
207                 .type = &ao_lisp_cons_type,
208                 .addr = (void **) &ao_lisp_read_stack,
209         },
210 };
211
212 #define AO_LISP_ROOT    (sizeof (ao_lisp_root) / sizeof (ao_lisp_root[0]))
213
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],
221         (const void **) &ao_lisp_frame_free_list[4],
222         (const void **) &ao_lisp_frame_free_list[5],
223 };
224
225 #if AO_LISP_FRAME_FREE != 6
226 #error Unexpected AO_LISP_FRAME_FREE value
227 #endif
228
229 #define AO_LISP_CACHE   (sizeof (ao_lisp_cache) / sizeof (ao_lisp_cache[0]))
230
231 #define AO_LISP_BUSY_SIZE       ((AO_LISP_POOL + 31) / 32)
232
233 static uint8_t  ao_lisp_busy[AO_LISP_BUSY_SIZE];
234 static uint8_t  ao_lisp_cons_note[AO_LISP_BUSY_SIZE];
235 static uint8_t  ao_lisp_cons_last[AO_LISP_BUSY_SIZE];
236 static uint8_t  ao_lisp_cons_noted;
237
238 uint16_t        ao_lisp_top;
239
240 struct ao_lisp_chunk {
241         uint16_t                old_offset;
242         union {
243                 uint16_t        size;
244                 uint16_t        new_offset;
245         };
246 };
247
248 #define AO_LISP_NCHUNK  64
249
250 static struct ao_lisp_chunk ao_lisp_chunk[AO_LISP_NCHUNK];
251
252 /* Offset of an address within the pool. */
253 static inline uint16_t pool_offset(void *addr) {
254 #if DBG_MEM
255         if (!AO_LISP_IS_POOL(addr))
256                 ao_lisp_abort();
257 #endif
258         return ((uint8_t *) addr) - ao_lisp_pool;
259 }
260
261 static inline void mark(uint8_t *tag, int offset) {
262         int     byte = offset >> 5;
263         int     bit = (offset >> 2) & 7;
264         tag[byte] |= (1 << bit);
265 }
266
267 static inline void clear(uint8_t *tag, int offset) {
268         int     byte = offset >> 5;
269         int     bit = (offset >> 2) & 7;
270         tag[byte] &= ~(1 << bit);
271 }
272
273 static inline int busy(uint8_t *tag, int offset) {
274         int     byte = offset >> 5;
275         int     bit = (offset >> 2) & 7;
276         return (tag[byte] >> bit) & 1;
277 }
278
279 static inline int min(int a, int b) { return a < b ? a : b; }
280 static inline int max(int a, int b) { return a > b ? a : b; }
281
282 static inline int limit(int offset) {
283         return min(AO_LISP_POOL, max(offset, 0));
284 }
285
286 static int total_marked;
287
288 static void
289 note_cons(uint16_t offset)
290 {
291         MDBG_MOVE("note cons %d\n", offset);
292         ao_lisp_cons_noted = 1;
293         mark(ao_lisp_cons_note, offset);
294 }
295
296 static uint16_t chunk_low, chunk_high;
297 static uint16_t chunk_first, chunk_last;
298 static int chunk_busy;
299
300 static void
301 note_chunk(uint16_t offset, uint16_t size)
302 {
303         int l, r;
304
305         if (offset < chunk_low || chunk_high <= offset)
306                 return;
307
308         /* Binary search for the location */
309         l = 0;
310         r = chunk_busy - 1;
311         while (l <= r) {
312                 int m = (l + r) >> 1;
313                 if (ao_lisp_chunk[m].old_offset < offset)
314                         l = m + 1;
315                 else
316                         r = m - 1;
317         }
318         /*
319          * The correct location is always in 'l', with r = l-1 being
320          * the entry before the right one
321          */
322
323 #if DBG_MEM
324         /* Off the right side */
325         if (l >= AO_LISP_NCHUNK)
326                 ao_lisp_abort();
327
328         /* Off the left side */
329         if (l == 0 && chunk_busy && offset > ao_lisp_chunk[0].old_offset)
330                 ao_lisp_abort();
331 #endif
332
333         /* Shuffle existing entries right */
334         int end = min(AO_LISP_NCHUNK, chunk_busy + 1);
335
336         memmove(&ao_lisp_chunk[l+1],
337                 &ao_lisp_chunk[l],
338                 (end - (l+1)) * sizeof (struct ao_lisp_chunk));
339
340         /* Add new entry */
341         ao_lisp_chunk[l].old_offset = offset;
342         ao_lisp_chunk[l].size = size;
343
344         /* Increment the number of elements up to the size of the array */
345         if (chunk_busy < AO_LISP_NCHUNK)
346                 chunk_busy++;
347
348         /* Set the top address if the array is full */
349         if (chunk_busy == AO_LISP_NCHUNK)
350                 chunk_high = ao_lisp_chunk[AO_LISP_NCHUNK-1].old_offset +
351                         ao_lisp_chunk[AO_LISP_NCHUNK-1].size;
352 }
353
354 static void
355 reset_chunks(void)
356 {
357         memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
358         chunk_high = ao_lisp_top;
359         chunk_busy = 0;
360 }
361
362 /*
363  * Walk all referenced objects calling functions on each one
364  */
365
366 static void
367 walk(int (*visit_addr)(const struct ao_lisp_type *type, void **addr),
368      int (*visit_poly)(ao_poly *p, uint8_t do_note_cons))
369 {
370         int i;
371
372         total_marked = 0;
373         ao_lisp_record_reset();
374         memset(ao_lisp_busy, '\0', sizeof (ao_lisp_busy));
375         memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
376         ao_lisp_cons_noted = 0;
377         for (i = 0; i < (int) AO_LISP_ROOT; i++) {
378                 if (ao_lisp_root[i].type) {
379                         void **a = ao_lisp_root[i].addr, *v;
380                         if (a && (v = *a)) {
381                                 MDBG_MOVE("root ptr %d\n", MDBG_OFFSET(v));
382                                 visit_addr(ao_lisp_root[i].type, a);
383                         }
384                 } else {
385                         ao_poly *a = (ao_poly *) ao_lisp_root[i].addr, p;
386                         if (a && (p = *a)) {
387                                 MDBG_MOVE("root poly %d\n", MDBG_OFFSET(ao_lisp_ref(p)));
388                                 visit_poly(a, 0);
389                         }
390                 }
391         }
392         while (ao_lisp_cons_noted) {
393                 memcpy(ao_lisp_cons_last, ao_lisp_cons_note, sizeof (ao_lisp_cons_note));
394                 memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
395                 ao_lisp_cons_noted = 0;
396                 for (i = 0; i < AO_LISP_POOL; i += 4) {
397                         if (busy(ao_lisp_cons_last, i)) {
398                                 void *v = ao_lisp_pool + i;
399                                 MDBG_MOVE("root cons %d\n", MDBG_OFFSET(v));
400                                 visit_addr(&ao_lisp_cons_type, &v);
401                         }
402                 }
403         }
404 }
405
406 #if MDBG_DUMP
407 static void
408 dump_busy(void)
409 {
410         int     i;
411         MDBG_MOVE("busy:");
412         for (i = 0; i < ao_lisp_top; i += 4) {
413                 if ((i & 0xff) == 0) {
414                         MDBG_MORE("\n");
415                         MDBG_MOVE("%s", "");
416                 }
417                 else if ((i & 0x1f) == 0)
418                         MDBG_MORE(" ");
419                 if (busy(ao_lisp_busy, i))
420                         MDBG_MORE("*");
421                 else
422                         MDBG_MORE("-");
423         }
424         MDBG_MORE ("\n");
425 }
426 #define DUMP_BUSY()     dump_busy()
427 #else
428 #define DUMP_BUSY()
429 #endif
430
431 static const struct ao_lisp_type const *ao_lisp_types[AO_LISP_NUM_TYPE] = {
432         [AO_LISP_CONS] = &ao_lisp_cons_type,
433         [AO_LISP_INT] = NULL,
434         [AO_LISP_STRING] = &ao_lisp_string_type,
435         [AO_LISP_OTHER] = (void *) 0x1,
436         [AO_LISP_ATOM] = &ao_lisp_atom_type,
437         [AO_LISP_BUILTIN] = &ao_lisp_builtin_type,
438         [AO_LISP_FRAME] = &ao_lisp_frame_type,
439         [AO_LISP_LAMBDA] = &ao_lisp_lambda_type,
440         [AO_LISP_STACK] = &ao_lisp_stack_type,
441 };
442
443 static int
444 ao_lisp_mark_ref(const struct ao_lisp_type *type, void **ref)
445 {
446         return ao_lisp_mark(type, *ref);
447 }
448
449 static int
450 ao_lisp_poly_mark_ref(ao_poly *p, uint8_t do_note_cons)
451 {
452         return ao_lisp_poly_mark(*p, do_note_cons);
453 }
454
455 int ao_lisp_collects[2];
456 int ao_lisp_freed[2];
457 int ao_lisp_loops[2];
458
459 int ao_lisp_last_top;
460
461 int
462 ao_lisp_collect(uint8_t style)
463 {
464         int     ret;
465         int     i;
466         int     top;
467         int     loops = 0;
468 #if DBG_MEM
469         int     marked;
470         int     moved;
471         struct ao_lisp_record   *mark_record = NULL, *move_record = NULL;
472
473         MDBG_MOVE("collect %d\n", ao_lisp_collects);
474         marked = moved = 0;
475 #endif
476
477         /* The first time through, we're doing a full collect */
478         if (ao_lisp_last_top == 0)
479                 style = AO_LISP_COLLECT_FULL;
480
481         /* Clear references to all caches */
482         for (i = 0; i < (int) AO_LISP_CACHE; i++)
483                 *ao_lisp_cache[i] = NULL;
484         if (style == AO_LISP_COLLECT_FULL) {
485                 chunk_low = top = 0;
486         } else {
487                 chunk_low = top = ao_lisp_last_top;
488         }
489         for (;;) {
490                 loops++;
491                 MDBG_MOVE("move chunks from %d to %d\n", chunk_low, top);
492                 /* Find the sizes of the first chunk of objects to move */
493                 reset_chunks();
494                 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
495 #if DBG_MEM
496                 marked = total_marked;
497
498                 ao_lisp_record_free(mark_record);
499                 mark_record = ao_lisp_record_save();
500                 if (mark_record && move_record)
501                         ao_lisp_record_compare("mark", move_record, mark_record);
502
503                 if (moved && moved != marked)
504                         ao_lisp_abort();
505 #endif
506
507                 DUMP_BUSY();
508
509                 /* Find the first moving object */
510                 for (i = 0; i < chunk_busy; i++) {
511                         uint16_t        size = ao_lisp_chunk[i].size;
512
513 #if DBG_MEM
514                         if (!size)
515                                 ao_lisp_abort();
516 #endif
517
518                         if (ao_lisp_chunk[i].old_offset > top)
519                                 break;
520
521                         MDBG_MOVE("chunk %d %d not moving\n",
522                                   ao_lisp_chunk[i].old_offset,
523                                   ao_lisp_chunk[i].size);
524 #if DBG_MEM
525                         if (ao_lisp_chunk[i].old_offset != top)
526                                 ao_lisp_abort();
527 #endif
528                         top += size;
529                 }
530
531                 /*
532                  * Limit amount of chunk array used in mapping moves
533                  * to the active region
534                  */
535                 chunk_first = i;
536                 chunk_low = ao_lisp_chunk[i].old_offset;
537
538                 /* Copy all of the objects */
539                 for (; i < chunk_busy; i++) {
540                         uint16_t        size = ao_lisp_chunk[i].size;
541
542 #if DBG_MEM
543                         if (!size)
544                                 ao_lisp_abort();
545 #endif
546
547                         MDBG_MOVE("chunk %d %d -> %d\n",
548                                   ao_lisp_chunk[i].old_offset,
549                                   size,
550                                   top);
551                         ao_lisp_chunk[i].new_offset = top;
552
553                         memmove(&ao_lisp_pool[top],
554                                 &ao_lisp_pool[ao_lisp_chunk[i].old_offset],
555                                 size);
556
557                         top += size;
558                 }
559
560                 chunk_last = i;
561
562                 if (chunk_first < chunk_last) {
563                         /* Relocate all references to the objects */
564                         walk(ao_lisp_move, ao_lisp_poly_move);
565
566 #if DBG_MEM
567                         ao_lisp_record_free(move_record);
568                         move_record = ao_lisp_record_save();
569                         if (mark_record && move_record)
570                                 ao_lisp_record_compare("move", mark_record, move_record);
571
572                         moved = total_marked;
573                         if (moved != marked)
574                                 ao_lisp_abort();
575 #endif
576                 }
577
578                 /* If we ran into the end of the heap, then
579                  * there's no need to keep walking
580                  */
581                 if (chunk_last != AO_LISP_NCHUNK)
582                         break;
583
584                 /* Next loop starts right above this loop */
585                 chunk_low = chunk_high;
586         }
587
588         /* Compute amount of memory freed */
589         ret = ao_lisp_top - top;
590
591         /* Collect stats */
592         ++ao_lisp_collects[style];
593         ao_lisp_freed[style] += ret;
594         ao_lisp_loops[style] += loops;
595
596         ao_lisp_top = top;
597         if (style == AO_LISP_COLLECT_FULL)
598                 ao_lisp_last_top = top;
599
600         MDBG_DO(memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
601                 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref));
602
603         return ret;
604 }
605
606 /*
607  * Mark interfaces for objects
608  */
609
610 /*
611  * Note a reference to memory and collect information about a few
612  * object sizes at a time
613  */
614
615 int
616 ao_lisp_mark_memory(const struct ao_lisp_type *type, void *addr)
617 {
618         int offset;
619         if (!AO_LISP_IS_POOL(addr))
620                 return 1;
621
622         offset = pool_offset(addr);
623         MDBG_MOVE("mark memory %d\n", MDBG_OFFSET(addr));
624         if (busy(ao_lisp_busy, offset)) {
625                 MDBG_MOVE("already marked\n");
626                 return 1;
627         }
628         mark(ao_lisp_busy, offset);
629         note_chunk(offset, ao_lisp_size(type, addr));
630         return 0;
631 }
632
633 /*
634  * Mark an object and all that it refereces
635  */
636 int
637 ao_lisp_mark(const struct ao_lisp_type *type, void *addr)
638 {
639         int ret;
640         MDBG_MOVE("mark %d\n", MDBG_OFFSET(addr));
641         MDBG_MOVE_IN();
642         ret = ao_lisp_mark_memory(type, addr);
643         if (!ret) {
644                 MDBG_MOVE("mark recurse\n");
645                 type->mark(addr);
646         }
647         MDBG_MOVE_OUT();
648         return ret;
649 }
650
651 /*
652  * Mark an object, unless it is a cons cell and
653  * do_note_cons is set. In that case, just
654  * set a bit in the cons note array; those
655  * will be marked in a separate pass to avoid
656  * deep recursion in the collector
657  */
658 int
659 ao_lisp_poly_mark(ao_poly p, uint8_t do_note_cons)
660 {
661         uint8_t type;
662         void    *addr;
663
664         type = ao_lisp_poly_base_type(p);
665
666         if (type == AO_LISP_INT)
667                 return 1;
668
669         addr = ao_lisp_ref(p);
670         if (!AO_LISP_IS_POOL(addr))
671                 return 1;
672
673         if (type == AO_LISP_CONS && do_note_cons) {
674                 note_cons(pool_offset(addr));
675                 return 1;
676         } else {
677                 if (type == AO_LISP_OTHER)
678                         type = ao_lisp_other_type(addr);
679
680                 const struct ao_lisp_type *lisp_type = ao_lisp_types[type];
681 #if DBG_MEM
682                 if (!lisp_type)
683                         ao_lisp_abort();
684 #endif
685
686                 return ao_lisp_mark(lisp_type, addr);
687         }
688 }
689
690 /*
691  * Find the current location of an object
692  * based on the original location. For unmoved
693  * objects, this is simple. For moved objects,
694  * go search for it
695  */
696
697 static uint16_t
698 move_map(uint16_t offset)
699 {
700         int             l, r;
701
702         if (offset < chunk_low || chunk_high <= offset)
703                 return offset;
704
705         /* Binary search for the location */
706         l = chunk_first;
707         r = chunk_busy - 1;
708         while (l <= r) {
709                 int m = (l + r) >> 1;
710                 if (ao_lisp_chunk[m].old_offset < offset)
711                         l = m + 1;
712                 else
713                         r = m - 1;
714         }
715 #if DBG_MEM
716         if (ao_lisp_chunk[l].old_offset != offset)
717                 ao_lisp_abort();
718 #endif
719         return ao_lisp_chunk[l].new_offset;
720 }
721
722 int
723 ao_lisp_move_memory(const struct ao_lisp_type *type, void **ref)
724 {
725         void            *addr = *ref;
726         uint16_t        offset, orig_offset;
727
728         if (!AO_LISP_IS_POOL(addr))
729                 return 1;
730
731         (void) type;
732
733         MDBG_MOVE("move memory %d\n", MDBG_OFFSET(addr));
734         orig_offset = pool_offset(addr);
735         offset = move_map(orig_offset);
736         if (offset != orig_offset) {
737                 MDBG_MOVE("update ref %d %d -> %d\n",
738                           AO_LISP_IS_POOL(ref) ? MDBG_OFFSET(ref) : -1,
739                           orig_offset, offset);
740                 *ref = ao_lisp_pool + offset;
741         }
742         if (busy(ao_lisp_busy, offset)) {
743                 MDBG_MOVE("already moved\n");
744                 return 1;
745         }
746         mark(ao_lisp_busy, offset);
747         MDBG_DO(ao_lisp_record(type, addr, ao_lisp_size(type, addr)));
748         return 0;
749 }
750
751 int
752 ao_lisp_move(const struct ao_lisp_type *type, void **ref)
753 {
754         int ret;
755         MDBG_MOVE("move object %d\n", MDBG_OFFSET(*ref));
756         MDBG_MOVE_IN();
757         ret = ao_lisp_move_memory(type, ref);
758         if (!ret) {
759                 MDBG_MOVE("move recurse\n");
760                 type->move(*ref);
761         }
762         MDBG_MOVE_OUT();
763         return ret;
764 }
765
766 int
767 ao_lisp_poly_move(ao_poly *ref, uint8_t do_note_cons)
768 {
769         uint8_t         type;
770         ao_poly         p = *ref;
771         int             ret;
772         void            *addr;
773         uint16_t        offset, orig_offset;
774         uint8_t         base_type;
775
776         base_type = type = ao_lisp_poly_base_type(p);
777
778         if (type == AO_LISP_INT)
779                 return 1;
780
781         addr = ao_lisp_ref(p);
782         if (!AO_LISP_IS_POOL(addr))
783                 return 1;
784
785         orig_offset = pool_offset(addr);
786         offset = move_map(orig_offset);
787
788         if (type == AO_LISP_CONS && do_note_cons) {
789                 note_cons(orig_offset);
790                 ret = 1;
791         } else {
792                 if (type == AO_LISP_OTHER)
793                         type = ao_lisp_other_type(ao_lisp_pool + offset);
794
795                 const struct ao_lisp_type *lisp_type = ao_lisp_types[type];
796 #if DBG_MEM
797                 if (!lisp_type)
798                         ao_lisp_abort();
799 #endif
800
801                 ret = ao_lisp_move(lisp_type, &addr);
802         }
803
804         /* Re-write the poly value */
805         if (offset != orig_offset) {
806                 ao_poly np = ao_lisp_poly(ao_lisp_pool + offset, base_type);
807                 MDBG_MOVE("poly %d moved %d -> %d\n",
808                           type, orig_offset, offset);
809                 *ref = np;
810         }
811         return ret;
812 }
813
814 #if DBG_MEM
815 void
816 ao_lisp_validate(void)
817 {
818         chunk_low = 0;
819         memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
820         walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
821 }
822
823 int dbg_allocs;
824
825 #endif
826
827 void *
828 ao_lisp_alloc(int size)
829 {
830         void    *addr;
831
832         MDBG_DO(++dbg_allocs);
833         MDBG_DO(if (dbg_validate) ao_lisp_validate());
834         size = ao_lisp_size_round(size);
835         if (ao_lisp_top + size > AO_LISP_POOL) {
836                 if (ao_lisp_collect(AO_LISP_COLLECT_INCREMENTAL) < size &&
837                     ao_lisp_collect(AO_LISP_COLLECT_FULL) < size)
838                 {
839                         ao_lisp_error(AO_LISP_OOM, "out of memory");
840                         return NULL;
841                 }
842         }
843         addr = ao_lisp_pool + ao_lisp_top;
844         ao_lisp_top += size;
845         return addr;
846 }
847
848 void
849 ao_lisp_cons_stash(int id, struct ao_lisp_cons *cons)
850 {
851         save_cons[id] = cons;
852 }
853
854 struct ao_lisp_cons *
855 ao_lisp_cons_fetch(int id)
856 {
857         struct ao_lisp_cons *cons = save_cons[id];
858         save_cons[id] = NULL;
859         return cons;
860 }
861
862 void
863 ao_lisp_poly_stash(int id, ao_poly poly)
864 {
865         save_poly[id] = poly;
866 }
867
868 ao_poly
869 ao_lisp_poly_fetch(int id)
870 {
871         ao_poly poly = save_poly[id];
872         save_poly[id] = AO_LISP_NIL;
873         return poly;
874 }
875
876 void
877 ao_lisp_string_stash(int id, char *string)
878 {
879         save_string[id] = string;
880 }
881
882 char *
883 ao_lisp_string_fetch(int id)
884 {
885         char *string = save_string[id];
886         save_string[id] = NULL;
887         return string;
888 }
889