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