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