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