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