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