altos/telegps-v2.0: git ignore make results
[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 };
215
216 #define AO_LISP_ROOT    (sizeof (ao_lisp_root) / sizeof (ao_lisp_root[0]))
217
218 static const void ** const ao_lisp_cache[] = {
219         (const void **) &ao_lisp_cons_free_list,
220         (const void **) &ao_lisp_stack_free_list,
221         (const void **) &ao_lisp_frame_free_list[0],
222         (const void **) &ao_lisp_frame_free_list[1],
223         (const void **) &ao_lisp_frame_free_list[2],
224         (const void **) &ao_lisp_frame_free_list[3],
225         (const void **) &ao_lisp_frame_free_list[4],
226         (const void **) &ao_lisp_frame_free_list[5],
227 };
228
229 #if AO_LISP_FRAME_FREE != 6
230 #error Unexpected AO_LISP_FRAME_FREE value
231 #endif
232
233 #define AO_LISP_CACHE   (sizeof (ao_lisp_cache) / sizeof (ao_lisp_cache[0]))
234
235 #define AO_LISP_BUSY_SIZE       ((AO_LISP_POOL + 31) / 32)
236
237 static uint8_t  ao_lisp_busy[AO_LISP_BUSY_SIZE];
238 static uint8_t  ao_lisp_cons_note[AO_LISP_BUSY_SIZE];
239 static uint8_t  ao_lisp_cons_last[AO_LISP_BUSY_SIZE];
240 static uint8_t  ao_lisp_cons_noted;
241
242 uint16_t        ao_lisp_top;
243
244 struct ao_lisp_chunk {
245         uint16_t                old_offset;
246         union {
247                 uint16_t        size;
248                 uint16_t        new_offset;
249         };
250 };
251
252 #define AO_LISP_NCHUNK  64
253
254 static struct ao_lisp_chunk ao_lisp_chunk[AO_LISP_NCHUNK];
255
256 /* Offset of an address within the pool. */
257 static inline uint16_t pool_offset(void *addr) {
258 #if DBG_MEM
259         if (!AO_LISP_IS_POOL(addr))
260                 ao_lisp_abort();
261 #endif
262         return ((uint8_t *) addr) - ao_lisp_pool;
263 }
264
265 static inline void mark(uint8_t *tag, int offset) {
266         int     byte = offset >> 5;
267         int     bit = (offset >> 2) & 7;
268         tag[byte] |= (1 << bit);
269 }
270
271 static inline void clear(uint8_t *tag, int offset) {
272         int     byte = offset >> 5;
273         int     bit = (offset >> 2) & 7;
274         tag[byte] &= ~(1 << bit);
275 }
276
277 static inline int busy(uint8_t *tag, int offset) {
278         int     byte = offset >> 5;
279         int     bit = (offset >> 2) & 7;
280         return (tag[byte] >> bit) & 1;
281 }
282
283 static inline int min(int a, int b) { return a < b ? a : b; }
284 static inline int max(int a, int b) { return a > b ? a : b; }
285
286 static inline int limit(int offset) {
287         return min(AO_LISP_POOL, max(offset, 0));
288 }
289
290 static void
291 note_cons(uint16_t offset)
292 {
293         MDBG_MOVE("note cons %d\n", offset);
294         ao_lisp_cons_noted = 1;
295         mark(ao_lisp_cons_note, offset);
296 }
297
298 static uint16_t chunk_low, chunk_high;
299 static uint16_t chunk_first, chunk_last;
300
301 static int
302 find_chunk(uint16_t offset)
303 {
304         int l, r;
305         /* Binary search for the location */
306         l = chunk_first;
307         r = chunk_last - 1;
308         while (l <= r) {
309                 int m = (l + r) >> 1;
310                 if (ao_lisp_chunk[m].old_offset < offset)
311                         l = m + 1;
312                 else
313                         r = m - 1;
314         }
315         return l;
316 }
317
318 static void
319 note_chunk(uint16_t offset, uint16_t size)
320 {
321         int l;
322
323         if (offset < chunk_low || chunk_high <= offset)
324                 return;
325
326         l = find_chunk(offset);
327
328         /*
329          * The correct location is always in 'l', with r = l-1 being
330          * the entry before the right one
331          */
332
333 #if DBG_MEM
334         /* Off the right side */
335         if (l >= AO_LISP_NCHUNK)
336                 ao_lisp_abort();
337
338         /* Off the left side */
339         if (l == 0 && chunk_last && offset > ao_lisp_chunk[0].old_offset)
340                 ao_lisp_abort();
341 #endif
342
343         /* Shuffle existing entries right */
344         int end = min(AO_LISP_NCHUNK, chunk_last + 1);
345
346         memmove(&ao_lisp_chunk[l+1],
347                 &ao_lisp_chunk[l],
348                 (end - (l+1)) * sizeof (struct ao_lisp_chunk));
349
350         /* Add new entry */
351         ao_lisp_chunk[l].old_offset = offset;
352         ao_lisp_chunk[l].size = size;
353
354         /* Increment the number of elements up to the size of the array */
355         if (chunk_last < AO_LISP_NCHUNK)
356                 chunk_last++;
357
358         /* Set the top address if the array is full */
359         if (chunk_last == AO_LISP_NCHUNK)
360                 chunk_high = ao_lisp_chunk[AO_LISP_NCHUNK-1].old_offset +
361                         ao_lisp_chunk[AO_LISP_NCHUNK-1].size;
362 }
363
364 static void
365 reset_chunks(void)
366 {
367         chunk_high = ao_lisp_top;
368         chunk_last = 0;
369         chunk_first = 0;
370 }
371
372 /*
373  * Walk all referenced objects calling functions on each one
374  */
375
376 static void
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))
379 {
380         int i;
381
382         ao_lisp_record_reset();
383         memset(ao_lisp_busy, '\0', sizeof (ao_lisp_busy));
384         memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
385         ao_lisp_cons_noted = 0;
386         for (i = 0; i < (int) AO_LISP_ROOT; i++) {
387                 if (ao_lisp_root[i].type) {
388                         void **a = ao_lisp_root[i].addr, *v;
389                         if (a && (v = *a)) {
390                                 MDBG_MOVE("root ptr %d\n", MDBG_OFFSET(v));
391                                 visit_addr(ao_lisp_root[i].type, a);
392                         }
393                 } else {
394                         ao_poly *a = (ao_poly *) ao_lisp_root[i].addr, p;
395                         if (a && (p = *a)) {
396                                 MDBG_MOVE("root poly %d\n", MDBG_OFFSET(ao_lisp_ref(p)));
397                                 visit_poly(a, 0);
398                         }
399                 }
400         }
401         while (ao_lisp_cons_noted) {
402                 memcpy(ao_lisp_cons_last, ao_lisp_cons_note, sizeof (ao_lisp_cons_note));
403                 memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
404                 ao_lisp_cons_noted = 0;
405                 for (i = 0; i < AO_LISP_POOL; i += 4) {
406                         if (busy(ao_lisp_cons_last, i)) {
407                                 void *v = ao_lisp_pool + i;
408                                 MDBG_MOVE("root cons %d\n", MDBG_OFFSET(v));
409                                 visit_addr(&ao_lisp_cons_type, &v);
410                         }
411                 }
412         }
413 }
414
415 #if MDBG_DUMP
416 static void
417 dump_busy(void)
418 {
419         int     i;
420         MDBG_MOVE("busy:");
421         for (i = 0; i < ao_lisp_top; i += 4) {
422                 if ((i & 0xff) == 0) {
423                         MDBG_MORE("\n");
424                         MDBG_MOVE("%s", "");
425                 }
426                 else if ((i & 0x1f) == 0)
427                         MDBG_MORE(" ");
428                 if (busy(ao_lisp_busy, i))
429                         MDBG_MORE("*");
430                 else
431                         MDBG_MORE("-");
432         }
433         MDBG_MORE ("\n");
434 }
435 #define DUMP_BUSY()     dump_busy()
436 #else
437 #define DUMP_BUSY()
438 #endif
439
440 static const struct ao_lisp_type const *ao_lisp_types[AO_LISP_NUM_TYPE] = {
441         [AO_LISP_CONS] = &ao_lisp_cons_type,
442         [AO_LISP_INT] = NULL,
443         [AO_LISP_STRING] = &ao_lisp_string_type,
444         [AO_LISP_OTHER] = (void *) 0x1,
445         [AO_LISP_ATOM] = &ao_lisp_atom_type,
446         [AO_LISP_BUILTIN] = &ao_lisp_builtin_type,
447         [AO_LISP_FRAME] = &ao_lisp_frame_type,
448         [AO_LISP_LAMBDA] = &ao_lisp_lambda_type,
449         [AO_LISP_STACK] = &ao_lisp_stack_type,
450 };
451
452 static int
453 ao_lisp_mark_ref(const struct ao_lisp_type *type, void **ref)
454 {
455         return ao_lisp_mark(type, *ref);
456 }
457
458 static int
459 ao_lisp_poly_mark_ref(ao_poly *p, uint8_t do_note_cons)
460 {
461         return ao_lisp_poly_mark(*p, do_note_cons);
462 }
463
464 #if DBG_MEM_STATS
465 int ao_lisp_collects[2];
466 int ao_lisp_freed[2];
467 int ao_lisp_loops[2];
468 #endif
469
470 int ao_lisp_last_top;
471
472 int
473 ao_lisp_collect(uint8_t style)
474 {
475         int     i;
476         int     top;
477 #if DBG_MEM_STATS
478         int     loops = 0;
479 #endif
480 #if DBG_MEM
481         struct ao_lisp_record   *mark_record = NULL, *move_record = NULL;
482
483         MDBG_MOVE("collect %d\n", ao_lisp_collects[style]);
484 #endif
485
486         /* The first time through, we're doing a full collect */
487         if (ao_lisp_last_top == 0)
488                 style = AO_LISP_COLLECT_FULL;
489
490         /* Clear references to all caches */
491         for (i = 0; i < (int) AO_LISP_CACHE; i++)
492                 *ao_lisp_cache[i] = NULL;
493         if (style == AO_LISP_COLLECT_FULL) {
494                 chunk_low = top = 0;
495         } else {
496                 chunk_low = top = ao_lisp_last_top;
497         }
498         for (;;) {
499 #if DBG_MEM_STATS
500                 loops++;
501 #endif
502                 MDBG_MOVE("move chunks from %d to %d\n", chunk_low, top);
503                 /* Find the sizes of the first chunk of objects to move */
504                 reset_chunks();
505                 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
506 #if DBG_MEM
507
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);
512 #endif
513
514                 DUMP_BUSY();
515
516                 /* Find the first moving object */
517                 for (i = 0; i < chunk_last; i++) {
518                         uint16_t        size = ao_lisp_chunk[i].size;
519
520 #if DBG_MEM
521                         if (!size)
522                                 ao_lisp_abort();
523 #endif
524
525                         if (ao_lisp_chunk[i].old_offset > top)
526                                 break;
527
528                         MDBG_MOVE("chunk %d %d not moving\n",
529                                   ao_lisp_chunk[i].old_offset,
530                                   ao_lisp_chunk[i].size);
531 #if DBG_MEM
532                         if (ao_lisp_chunk[i].old_offset != top)
533                                 ao_lisp_abort();
534 #endif
535                         top += size;
536                 }
537
538                 /*
539                  * Limit amount of chunk array used in mapping moves
540                  * to the active region
541                  */
542                 chunk_first = i;
543                 chunk_low = ao_lisp_chunk[i].old_offset;
544
545                 /* Copy all of the objects */
546                 for (; i < chunk_last; i++) {
547                         uint16_t        size = ao_lisp_chunk[i].size;
548
549 #if DBG_MEM
550                         if (!size)
551                                 ao_lisp_abort();
552 #endif
553
554                         MDBG_MOVE("chunk %d %d -> %d\n",
555                                   ao_lisp_chunk[i].old_offset,
556                                   size,
557                                   top);
558                         ao_lisp_chunk[i].new_offset = top;
559
560                         memmove(&ao_lisp_pool[top],
561                                 &ao_lisp_pool[ao_lisp_chunk[i].old_offset],
562                                 size);
563
564                         top += size;
565                 }
566
567                 if (chunk_first < chunk_last) {
568                         /* Relocate all references to the objects */
569                         walk(ao_lisp_move, ao_lisp_poly_move);
570
571 #if DBG_MEM
572                         ao_lisp_record_free(move_record);
573                         move_record = ao_lisp_record_save();
574                         if (mark_record && move_record)
575                                 ao_lisp_record_compare("move", mark_record, move_record);
576 #endif
577                 }
578
579                 /* If we ran into the end of the heap, then
580                  * there's no need to keep walking
581                  */
582                 if (chunk_last != AO_LISP_NCHUNK)
583                         break;
584
585                 /* Next loop starts right above this loop */
586                 chunk_low = chunk_high;
587         }
588
589 #if DBG_MEM_STATS
590         /* Collect stats */
591         ++ao_lisp_collects[style];
592         ao_lisp_freed[style] += ao_lisp_top - top;
593         ao_lisp_loops[style] += loops;
594 #endif
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 AO_LISP_POOL - ao_lisp_top;
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;
701
702         if (offset < chunk_low || chunk_high <= offset)
703                 return offset;
704
705         l = find_chunk(offset);
706
707 #if DBG_MEM
708         if (ao_lisp_chunk[l].old_offset != offset)
709                 ao_lisp_abort();
710 #endif
711         return ao_lisp_chunk[l].new_offset;
712 }
713
714 int
715 ao_lisp_move_memory(const struct ao_lisp_type *type, void **ref)
716 {
717         void            *addr = *ref;
718         uint16_t        offset, orig_offset;
719
720         if (!AO_LISP_IS_POOL(addr))
721                 return 1;
722
723         (void) type;
724
725         MDBG_MOVE("move memory %d\n", MDBG_OFFSET(addr));
726         orig_offset = pool_offset(addr);
727         offset = move_map(orig_offset);
728         if (offset != orig_offset) {
729                 MDBG_MOVE("update ref %d %d -> %d\n",
730                           AO_LISP_IS_POOL(ref) ? MDBG_OFFSET(ref) : -1,
731                           orig_offset, offset);
732                 *ref = ao_lisp_pool + offset;
733         }
734         if (busy(ao_lisp_busy, offset)) {
735                 MDBG_MOVE("already moved\n");
736                 return 1;
737         }
738         mark(ao_lisp_busy, offset);
739         MDBG_DO(ao_lisp_record(type, addr, ao_lisp_size(type, addr)));
740         return 0;
741 }
742
743 int
744 ao_lisp_move(const struct ao_lisp_type *type, void **ref)
745 {
746         int ret;
747         MDBG_MOVE("move object %d\n", MDBG_OFFSET(*ref));
748         MDBG_MOVE_IN();
749         ret = ao_lisp_move_memory(type, ref);
750         if (!ret) {
751                 MDBG_MOVE("move recurse\n");
752                 type->move(*ref);
753         }
754         MDBG_MOVE_OUT();
755         return ret;
756 }
757
758 int
759 ao_lisp_poly_move(ao_poly *ref, uint8_t do_note_cons)
760 {
761         uint8_t         type;
762         ao_poly         p = *ref;
763         int             ret;
764         void            *addr;
765         uint16_t        offset, orig_offset;
766         uint8_t         base_type;
767
768         base_type = type = ao_lisp_poly_base_type(p);
769
770         if (type == AO_LISP_INT)
771                 return 1;
772
773         addr = ao_lisp_ref(p);
774         if (!AO_LISP_IS_POOL(addr))
775                 return 1;
776
777         orig_offset = pool_offset(addr);
778         offset = move_map(orig_offset);
779
780         if (type == AO_LISP_CONS && do_note_cons) {
781                 note_cons(orig_offset);
782                 ret = 1;
783         } else {
784                 if (type == AO_LISP_OTHER)
785                         type = ao_lisp_other_type(ao_lisp_pool + offset);
786
787                 const struct ao_lisp_type *lisp_type = ao_lisp_types[type];
788 #if DBG_MEM
789                 if (!lisp_type)
790                         ao_lisp_abort();
791 #endif
792
793                 ret = ao_lisp_move(lisp_type, &addr);
794         }
795
796         /* Re-write the poly value */
797         if (offset != orig_offset) {
798                 ao_poly np = ao_lisp_poly(ao_lisp_pool + offset, base_type);
799                 MDBG_MOVE("poly %d moved %d -> %d\n",
800                           type, orig_offset, offset);
801                 *ref = np;
802         }
803         return ret;
804 }
805
806 #if DBG_MEM
807 void
808 ao_lisp_validate(void)
809 {
810         chunk_low = 0;
811         memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
812         walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
813 }
814
815 int dbg_allocs;
816
817 #endif
818
819 void *
820 ao_lisp_alloc(int size)
821 {
822         void    *addr;
823
824         MDBG_DO(++dbg_allocs);
825         MDBG_DO(if (dbg_validate) ao_lisp_validate());
826         size = ao_lisp_size_round(size);
827         if (AO_LISP_POOL - ao_lisp_top < size &&
828             ao_lisp_collect(AO_LISP_COLLECT_INCREMENTAL) < size &&
829             ao_lisp_collect(AO_LISP_COLLECT_FULL) < size)
830         {
831                 ao_lisp_error(AO_LISP_OOM, "out of memory");
832                 return NULL;
833         }
834         addr = ao_lisp_pool + ao_lisp_top;
835         ao_lisp_top += size;
836         return addr;
837 }
838
839 void
840 ao_lisp_cons_stash(int id, struct ao_lisp_cons *cons)
841 {
842         save_cons[id] = cons;
843 }
844
845 struct ao_lisp_cons *
846 ao_lisp_cons_fetch(int id)
847 {
848         struct ao_lisp_cons *cons = save_cons[id];
849         save_cons[id] = NULL;
850         return cons;
851 }
852
853 void
854 ao_lisp_poly_stash(int id, ao_poly poly)
855 {
856         save_poly[id] = poly;
857 }
858
859 ao_poly
860 ao_lisp_poly_fetch(int id)
861 {
862         ao_poly poly = save_poly[id];
863         save_poly[id] = AO_LISP_NIL;
864         return poly;
865 }
866
867 void
868 ao_lisp_string_stash(int id, char *string)
869 {
870         save_string[id] = string;
871 }
872
873 char *
874 ao_lisp_string_fetch(int id)
875 {
876         char *string = save_string[id];
877         save_string[id] = NULL;
878         return string;
879 }
880