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