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