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