altos/lisp: Use poly stashes for stacks
[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 };
222
223 #if AO_LISP_FRAME_FREE != 4
224 #error Unexpected AO_LISP_FRAME_FREE value
225 #endif
226
227 #define AO_LISP_CACHE   (sizeof (ao_lisp_cache) / sizeof (ao_lisp_cache[0]))
228
229 #define AO_LISP_BUSY_SIZE       ((AO_LISP_POOL + 31) / 32)
230
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;
235
236 uint16_t        ao_lisp_top;
237
238 struct ao_lisp_chunk {
239         uint16_t                old_addr;
240         union {
241                 uint16_t        size;
242                 uint16_t        new_addr;
243         };
244 };
245
246 #define AO_LISP_NCHUNK  64
247
248 static struct ao_lisp_chunk ao_lisp_chunk[AO_LISP_NCHUNK];
249
250 /* Offset of an address within the pool. */
251 static inline uint16_t pool_offset(void *addr) {
252 #if DBG_MEM
253         if (!AO_LISP_IS_POOL(addr))
254                 ao_lisp_abort();
255 #endif
256         return ((uint8_t *) addr) - ao_lisp_pool;
257 }
258
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);
263 }
264
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);
269 }
270
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;
275 }
276
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; }
279
280 static inline int limit(int offset) {
281         return min(AO_LISP_POOL, max(offset, 0));
282 }
283
284 static int total_marked;
285
286 static void
287 note_cons(void *addr)
288 {
289         if (AO_LISP_IS_POOL(addr)) {
290                 int     offset = pool_offset(addr);
291                 MDBG_MOVE("note cons %d\n", MDBG_OFFSET(addr));
292                 ao_lisp_cons_noted = 1;
293                 mark(ao_lisp_cons_note, offset);
294         }
295 }
296
297 static uint16_t chunk_low, chunk_high;
298 static uint16_t chunk_first, chunk_last;
299 static int chunk_busy;
300
301 static void
302 note_chunk(uint16_t addr, uint16_t size)
303 {
304         int l, r;
305
306         if (addr < chunk_low || chunk_high <= addr)
307                 return;
308
309         /* Binary search for the location */
310         l = 0;
311         r = chunk_busy - 1;
312         while (l <= r) {
313                 int m = (l + r) >> 1;
314                 if (ao_lisp_chunk[m].old_addr < addr)
315                         l = m + 1;
316                 else
317                         r = m - 1;
318         }
319         /*
320          * The correct location is always in 'l', with r = l-1 being
321          * the entry before the right one
322          */
323
324 #if DBG_MEM
325         /* Off the right side */
326         if (l >= AO_LISP_NCHUNK)
327                 ao_lisp_abort();
328
329         /* Off the left side */
330         if (l == 0 && chunk_busy && addr > ao_lisp_chunk[0].old_addr)
331                 ao_lisp_abort();
332 #endif
333
334         /* Shuffle existing entries right */
335         int end = min(AO_LISP_NCHUNK, chunk_busy + 1);
336
337         memmove(&ao_lisp_chunk[l+1],
338                 &ao_lisp_chunk[l],
339                 (end - (l+1)) * sizeof (struct ao_lisp_chunk));
340
341         /* Add new entry */
342         ao_lisp_chunk[l].old_addr = addr;
343         ao_lisp_chunk[l].size = size;
344
345         /* Increment the number of elements up to the size of the array */
346         if (chunk_busy < AO_LISP_NCHUNK)
347                 chunk_busy++;
348
349         /* Set the top address if the array is full */
350         if (chunk_busy == AO_LISP_NCHUNK)
351                 chunk_high = ao_lisp_chunk[AO_LISP_NCHUNK-1].old_addr +
352                         ao_lisp_chunk[AO_LISP_NCHUNK-1].size;
353 }
354
355 static void
356 reset_chunks(void)
357 {
358         memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
359         chunk_high = ao_lisp_top;
360         chunk_busy = 0;
361 }
362
363 /*
364  * Walk all referenced objects calling functions on each one
365  */
366
367 static void
368 walk(int (*visit_addr)(const struct ao_lisp_type *type, void **addr),
369      int (*visit_poly)(ao_poly *p, uint8_t do_note_cons))
370 {
371         int i;
372
373         total_marked = 0;
374         ao_lisp_record_reset();
375         memset(ao_lisp_busy, '\0', sizeof (ao_lisp_busy));
376         memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
377         ao_lisp_cons_noted = 0;
378         for (i = 0; i < (int) AO_LISP_ROOT; i++) {
379                 if (ao_lisp_root[i].type) {
380                         void **a = ao_lisp_root[i].addr, *v;
381                         if (a && (v = *a)) {
382                                 MDBG_MOVE("root ptr %d\n", MDBG_OFFSET(v));
383                                 visit_addr(ao_lisp_root[i].type, a);
384                         }
385                 } else {
386                         ao_poly *a = (ao_poly *) ao_lisp_root[i].addr, p;
387                         if (a && (p = *a)) {
388                                 MDBG_MOVE("root poly %d\n", MDBG_OFFSET(ao_lisp_ref(p)));
389                                 visit_poly(a, 0);
390                         }
391                 }
392         }
393         while (ao_lisp_cons_noted) {
394                 memcpy(ao_lisp_cons_last, ao_lisp_cons_note, sizeof (ao_lisp_cons_note));
395                 memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
396                 ao_lisp_cons_noted = 0;
397                 for (i = 0; i < AO_LISP_POOL; i += 4) {
398                         if (busy(ao_lisp_cons_last, i)) {
399                                 void *v = ao_lisp_pool + i;
400                                 MDBG_MOVE("root cons %d\n", MDBG_OFFSET(v));
401                                 visit_addr(&ao_lisp_cons_type, &v);
402                         }
403                 }
404         }
405 }
406
407 #if MDBG_DUMP
408 static void
409 dump_busy(void)
410 {
411         int     i;
412         MDBG_MOVE("busy:");
413         for (i = 0; i < ao_lisp_top; i += 4) {
414                 if ((i & 0xff) == 0) {
415                         MDBG_MORE("\n");
416                         MDBG_MOVE("%s", "");
417                 }
418                 else if ((i & 0x1f) == 0)
419                         MDBG_MORE(" ");
420                 if (busy(ao_lisp_busy, i))
421                         MDBG_MORE("*");
422                 else
423                         MDBG_MORE("-");
424         }
425         MDBG_MORE ("\n");
426 }
427 #define DUMP_BUSY()     dump_busy()
428 #else
429 #define DUMP_BUSY()
430 #endif
431
432 static const struct ao_lisp_type const *ao_lisp_types[AO_LISP_NUM_TYPE] = {
433         [AO_LISP_CONS] = &ao_lisp_cons_type,
434         [AO_LISP_INT] = NULL,
435         [AO_LISP_STRING] = &ao_lisp_string_type,
436         [AO_LISP_OTHER] = (void *) 0x1,
437         [AO_LISP_ATOM] = &ao_lisp_atom_type,
438         [AO_LISP_BUILTIN] = &ao_lisp_builtin_type,
439         [AO_LISP_FRAME] = &ao_lisp_frame_type,
440         [AO_LISP_LAMBDA] = &ao_lisp_lambda_type,
441         [AO_LISP_STACK] = &ao_lisp_stack_type,
442 };
443
444 static int
445 ao_lisp_mark_ref(const struct ao_lisp_type *type, void **ref)
446 {
447         return ao_lisp_mark(type, *ref);
448 }
449
450 static int
451 ao_lisp_poly_mark_ref(ao_poly *p, uint8_t do_note_cons)
452 {
453         return ao_lisp_poly_mark(*p, do_note_cons);
454 }
455
456 int ao_lisp_collects[2];
457 int ao_lisp_freed[2];
458 int ao_lisp_loops[2];
459
460 int ao_lisp_last_top;
461
462 int
463 ao_lisp_collect(uint8_t style)
464 {
465         int     ret;
466         int     i;
467         int     top;
468         int     loops = 0;
469 #if DBG_MEM
470         int     marked;
471         int     moved;
472         struct ao_lisp_record   *mark_record = NULL, *move_record = NULL;
473
474         MDBG_MOVE("collect %d\n", ao_lisp_collects);
475         marked = moved = 0;
476 #endif
477
478         /* The first time through, we're doing a full collect */
479         if (ao_lisp_last_top == 0)
480                 style = AO_LISP_COLLECT_FULL;
481
482         /* Clear references to all caches */
483         for (i = 0; i < (int) AO_LISP_CACHE; i++)
484                 *ao_lisp_cache[i] = NULL;
485         if (style == AO_LISP_COLLECT_FULL) {
486                 chunk_low = top = 0;
487         } else {
488                 chunk_low = top = ao_lisp_last_top;
489         }
490         for (;;) {
491                 loops++;
492                 MDBG_MOVE("move chunks from %d to %d\n", chunk_low, top);
493                 /* Find the sizes of the first chunk of objects to move */
494                 reset_chunks();
495                 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
496 #if DBG_MEM
497                 marked = total_marked;
498
499                 ao_lisp_record_free(mark_record);
500                 mark_record = ao_lisp_record_save();
501                 if (mark_record && move_record)
502                         ao_lisp_record_compare("mark", move_record, mark_record);
503
504                 if (moved && moved != marked)
505                         ao_lisp_abort();
506 #endif
507
508                 DUMP_BUSY();
509
510                 /* Find the first moving object */
511                 for (i = 0; i < AO_LISP_NCHUNK; i++) {
512                         uint16_t        size = ao_lisp_chunk[i].size;
513
514                         if (!size)
515                                 break;
516
517                         if (ao_lisp_chunk[i].old_addr > top)
518                                 break;
519 #if DBG_MEM
520                         if (ao_lisp_chunk[i].old_addr != top)
521                                 ao_lisp_abort();
522 #endif
523
524                         top += size;
525                         MDBG_MOVE("chunk %d %d not moving\n",
526                                   ao_lisp_chunk[i].old_addr,
527                                   ao_lisp_chunk[i].size);
528                 }
529
530                 chunk_first = i;
531                 /* Copy all of the objects */
532                 for (; i < AO_LISP_NCHUNK; i++) {
533                         uint16_t        size = ao_lisp_chunk[i].size;
534
535                         if (!size)
536                                 break;
537
538                         MDBG_MOVE("chunk %d %d -> %d\n",
539                                   ao_lisp_chunk[i].old_addr,
540                                   size,
541                                   top);
542                         ao_lisp_chunk[i].new_addr = top;
543                         memmove(&ao_lisp_pool[top],
544                                 &ao_lisp_pool[ao_lisp_chunk[i].old_addr],
545                                 size);
546                         top += size;
547                 }
548
549                 chunk_last = i;
550
551                 if (chunk_first < chunk_last) {
552                         /* Relocate all references to the objects */
553                         walk(ao_lisp_move, ao_lisp_poly_move);
554
555 #if DBG_MEM
556                         ao_lisp_record_free(move_record);
557                         move_record = ao_lisp_record_save();
558                         if (mark_record && move_record)
559                                 ao_lisp_record_compare("move", mark_record, move_record);
560
561                         moved = total_marked;
562                         if (moved != marked)
563                                 ao_lisp_abort();
564 #endif
565                 }
566
567                 if (chunk_last != AO_LISP_NCHUNK)
568                         break;
569
570                 chunk_low = chunk_high;
571         }
572
573         /* Compute amount of memory freed */
574         ret = ao_lisp_top - top;
575
576         /* Collect stats */
577         ++ao_lisp_collects[style];
578         ao_lisp_freed[style] += ret;
579         ao_lisp_loops[style] += loops;
580
581         ao_lisp_top = top;
582         if (style == AO_LISP_COLLECT_FULL)
583                 ao_lisp_last_top = top;
584
585         MDBG_DO(memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
586                 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref));
587
588         return ret;
589 }
590
591 /*
592  * Mark interfaces for objects
593  *
594  * Note a reference to memory and
595  * collect information about a few object sizes
596  * at a time
597  */
598
599 int
600 ao_lisp_mark_memory(const struct ao_lisp_type *type, void *addr)
601 {
602         int offset;
603         if (!AO_LISP_IS_POOL(addr))
604                 return 1;
605
606         offset = pool_offset(addr);
607         MDBG_MOVE("mark memory %d\n", MDBG_OFFSET(addr));
608         if (busy(ao_lisp_busy, offset)) {
609                 MDBG_MOVE("already marked\n");
610                 return 1;
611         }
612         mark(ao_lisp_busy, offset);
613         note_chunk(offset, ao_lisp_size(type, addr));
614         return 0;
615 }
616
617 int
618 ao_lisp_mark(const struct ao_lisp_type *type, void *addr)
619 {
620         int ret;
621         MDBG_MOVE("mark %d\n", MDBG_OFFSET(addr));
622         MDBG_MOVE_IN();
623         ret = ao_lisp_mark_memory(type, addr);
624         if (!ret) {
625                 MDBG_MOVE("mark recurse\n");
626                 type->mark(addr);
627         }
628         MDBG_MOVE_OUT();
629         return ret;
630 }
631
632 int
633 ao_lisp_poly_mark(ao_poly p, uint8_t do_note_cons)
634 {
635         uint8_t type;
636         void    *addr;
637
638         if (!p)
639                 return 1;
640
641         type = ao_lisp_poly_base_type(p);
642         addr = ao_lisp_ref(p);
643
644         if (!AO_LISP_IS_POOL(addr))
645                 return 1;
646
647         if (type == AO_LISP_CONS && do_note_cons) {
648                 note_cons(ao_lisp_ref(p));
649                 return 1;
650         } else {
651                 const struct ao_lisp_type       *lisp_type;
652
653                 if (type == AO_LISP_OTHER) {
654                         type = ao_lisp_other_type(ao_lisp_poly_other(p));
655 #if DBG_MEM
656                         if (type <= AO_LISP_OTHER || AO_LISP_NUM_TYPE <= type)
657                                 ao_lisp_abort();
658 #endif
659                 }
660
661                 lisp_type = ao_lisp_types[ao_lisp_poly_type(p)];
662                 if (!lisp_type)
663                         return 1;
664                 return ao_lisp_mark(lisp_type, ao_lisp_ref(p));
665         }
666 }
667
668 static void *
669 move_map(void *addr)
670 {
671         uint16_t        offset = pool_offset(addr);
672         int             i;
673
674         for (i = chunk_first; i < chunk_last; i++) {
675                 if (ao_lisp_chunk[i].old_addr == offset) {
676                         MDBG_MOVE("move %d -> %d\n",
677                                   ao_lisp_chunk[i].old_addr,
678                                   ao_lisp_chunk[i].new_addr);
679                         return ao_lisp_pool + ao_lisp_chunk[i].new_addr;
680                 }
681         }
682         return addr;
683 }
684
685 int
686 ao_lisp_move_memory(const struct ao_lisp_type *type, void **ref)
687 {
688         void            *addr = *ref;
689         int             offset;
690
691         if (!AO_LISP_IS_POOL(addr))
692                 return 1;
693
694         (void) type;
695
696         MDBG_MOVE("move memory %d\n", MDBG_OFFSET(addr));
697         addr = move_map(addr);
698         if (addr != *ref) {
699                 MDBG_MOVE("update ref %d %d -> %d\n",
700                           AO_LISP_IS_POOL(ref) ? MDBG_OFFSET(ref) : -1,
701                           MDBG_OFFSET(*ref), MDBG_OFFSET(addr));
702                 *ref = addr;
703         }
704         offset = pool_offset(addr);
705         if (busy(ao_lisp_busy, offset)) {
706                 MDBG_MOVE("already moved\n");
707                 return 1;
708         }
709         mark(ao_lisp_busy, offset);
710         MDBG_DO(ao_lisp_record(type, addr, ao_lisp_size(type, addr)));
711         return 0;
712 }
713
714 int
715 ao_lisp_move(const struct ao_lisp_type *type, void **ref)
716 {
717         int ret;
718         MDBG_MOVE("move object %d\n", MDBG_OFFSET(*ref));
719         MDBG_MOVE_IN();
720         ret = ao_lisp_move_memory(type, ref);
721         if (!ret) {
722                 MDBG_MOVE("move recurse\n");
723                 type->move(*ref);
724         }
725         MDBG_MOVE_OUT();
726         return ret;
727 }
728
729 int
730 ao_lisp_poly_move(ao_poly *ref, uint8_t do_note_cons)
731 {
732         uint8_t                         type;
733         ao_poly                         p = *ref;
734         int                             ret;
735         void                            *addr;
736
737         if (!p)
738                 return 1;
739
740         addr = ao_lisp_ref(p);
741
742         if (!AO_LISP_IS_POOL(addr))
743                 return 1;
744
745         type = ao_lisp_poly_base_type(p);
746
747         if (type == AO_LISP_CONS && do_note_cons) {
748                 note_cons(addr);
749                 addr = move_map(addr);
750                 ret = 1;
751         } else {
752                 const struct ao_lisp_type       *lisp_type;
753
754                 if (type == AO_LISP_OTHER) {
755                         type = ao_lisp_other_type(move_map(ao_lisp_poly_other(p)));
756 #if DBG_MEM
757                         if (type <= AO_LISP_OTHER || AO_LISP_NUM_TYPE <= type)
758                                 ao_lisp_abort();
759 #endif
760                 }
761
762                 lisp_type = ao_lisp_types[type];
763                 if (!lisp_type)
764                         return 1;
765                 ret = ao_lisp_move(lisp_type, &addr);
766         }
767
768         /* Re-write the poly value */
769         if (addr != ao_lisp_ref(p)) {
770                 ao_poly np = ao_lisp_poly(addr, p & AO_LISP_TYPE_MASK);
771                 MDBG_MOVE("poly %d moved %d -> %d\n",
772                           type, MDBG_OFFSET(ao_lisp_ref(p)), MDBG_OFFSET(ao_lisp_ref(np)));
773                 *ref = np;
774         }
775         return ret;
776 }
777
778 #if DBG_MEM
779 void
780 ao_lisp_validate(void)
781 {
782         chunk_low = 0;
783         memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
784         walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
785 }
786
787 int dbg_allocs;
788
789 #endif
790
791 void *
792 ao_lisp_alloc(int size)
793 {
794         void    *addr;
795
796         MDBG_DO(++dbg_allocs);
797         MDBG_DO(if (dbg_validate) ao_lisp_validate());
798         size = ao_lisp_size_round(size);
799         if (ao_lisp_top + size > AO_LISP_POOL) {
800                 if (!ao_lisp_collect(AO_LISP_COLLECT_INCREMENTAL) &&
801                     !ao_lisp_collect(AO_LISP_COLLECT_FULL))
802                 {
803                         ao_lisp_error(AO_LISP_OOM, "out of memory");
804                         return NULL;
805                 }
806         }
807         addr = ao_lisp_pool + ao_lisp_top;
808         ao_lisp_top += size;
809         return addr;
810 }
811
812 void
813 ao_lisp_cons_stash(int id, struct ao_lisp_cons *cons)
814 {
815         save_cons[id] = cons;
816 }
817
818 struct ao_lisp_cons *
819 ao_lisp_cons_fetch(int id)
820 {
821         struct ao_lisp_cons *cons = save_cons[id];
822         save_cons[id] = NULL;
823         return cons;
824 }
825
826 void
827 ao_lisp_poly_stash(int id, ao_poly poly)
828 {
829         save_poly[id] = poly;
830 }
831
832 ao_poly
833 ao_lisp_poly_fetch(int id)
834 {
835         ao_poly poly = save_poly[id];
836         save_poly[id] = AO_LISP_NIL;
837         return poly;
838 }
839
840 void
841 ao_lisp_string_stash(int id, char *string)
842 {
843         save_string[id] = string;
844 }
845
846 char *
847 ao_lisp_string_fetch(int id)
848 {
849         char *string = save_string[id];
850         save_string[id] = NULL;
851         return string;
852 }
853