0dfad1d7c87bcc6286b924698730a63a849c3dcb
[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 struct ao_lisp_stack     *save_stack[3];
148 static ao_poly                  save_poly[2];
149
150 static const struct ao_lisp_root        ao_lisp_root[] = {
151         {
152                 .type = &ao_lisp_cons_type,
153                 .addr = (void **) &save_cons[0],
154         },
155         {
156                 .type = &ao_lisp_cons_type,
157                 .addr = (void **) &save_cons[1],
158         },
159         {
160                 .type = &ao_lisp_stack_type,
161                 .addr = (void **) &save_stack[0]
162         },
163         {
164                 .type = &ao_lisp_stack_type,
165                 .addr = (void **) &save_stack[1]
166         },
167         {
168                 .type = &ao_lisp_stack_type,
169                 .addr = (void **) &save_stack[2]
170         },
171         {
172                 .type = &ao_lisp_string_type,
173                 .addr = (void **) &save_string[0]
174         },
175         {
176                 .type = &ao_lisp_string_type,
177                 .addr = (void **) &save_string[1]
178         },
179         {
180                 .type = NULL,
181                 .addr = (void **) &save_poly[0]
182         },
183         {
184                 .type = NULL,
185                 .addr = (void **) &save_poly[1]
186         },
187         {
188                 .type = &ao_lisp_atom_type,
189                 .addr = (void **) &ao_lisp_atoms
190         },
191         {
192                 .type = &ao_lisp_frame_type,
193                 .addr = (void **) &ao_lisp_frame_global,
194         },
195         {
196                 .type = &ao_lisp_frame_type,
197                 .addr = (void **) &ao_lisp_frame_current,
198         },
199         {
200                 .type = &ao_lisp_stack_type,
201                 .addr = (void **) &ao_lisp_stack,
202         },
203         {
204                 .type = NULL,
205                 .addr = (void **) &ao_lisp_v,
206         },
207         {
208                 .type = &ao_lisp_cons_type,
209                 .addr = (void **) &ao_lisp_read_cons,
210         },
211         {
212                 .type = &ao_lisp_cons_type,
213                 .addr = (void **) &ao_lisp_read_cons_tail,
214         },
215         {
216                 .type = &ao_lisp_cons_type,
217                 .addr = (void **) &ao_lisp_read_stack,
218         },
219 };
220
221 #define AO_LISP_ROOT    (sizeof (ao_lisp_root) / sizeof (ao_lisp_root[0]))
222
223 static const void ** const ao_lisp_cache[] = {
224         (const void **) &ao_lisp_cons_free_list,
225         (const void **) &ao_lisp_stack_free_list,
226         (const void **) &ao_lisp_frame_free_list[0],
227         (const void **) &ao_lisp_frame_free_list[1],
228         (const void **) &ao_lisp_frame_free_list[2],
229         (const void **) &ao_lisp_frame_free_list[3],
230 };
231
232 #if AO_LISP_FRAME_FREE != 4
233 #error Unexpected AO_LISP_FRAME_FREE value
234 #endif
235
236 #define AO_LISP_CACHE   (sizeof (ao_lisp_cache) / sizeof (ao_lisp_cache[0]))
237
238 #define AO_LISP_BUSY_SIZE       ((AO_LISP_POOL + 31) / 32)
239
240 static uint8_t  ao_lisp_busy[AO_LISP_BUSY_SIZE];
241 static uint8_t  ao_lisp_cons_note[AO_LISP_BUSY_SIZE];
242 static uint8_t  ao_lisp_cons_last[AO_LISP_BUSY_SIZE];
243 static uint8_t  ao_lisp_cons_noted;
244
245 uint16_t        ao_lisp_top;
246
247 struct ao_lisp_chunk {
248         uint16_t                old_addr;
249         union {
250                 uint16_t        size;
251                 uint16_t        new_addr;
252         };
253 };
254
255 #define AO_LISP_NCHUNK  64
256
257 static struct ao_lisp_chunk ao_lisp_chunk[AO_LISP_NCHUNK];
258
259 /* Offset of an address within the pool. */
260 static inline uint16_t pool_offset(void *addr) {
261 #if DBG_MEM
262         if (!AO_LISP_IS_POOL(addr))
263                 ao_lisp_abort();
264 #endif
265         return ((uint8_t *) addr) - ao_lisp_pool;
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, chunk_high;
307 static uint16_t chunk_first, chunk_last;
308 static int chunk_busy;
309
310 static void
311 note_chunk(uint16_t addr, uint16_t size)
312 {
313         int l, r;
314
315         if (addr < chunk_low || chunk_high <= addr)
316                 return;
317
318         /* Binary search for the location */
319         l = 0;
320         r = chunk_busy - 1;
321         while (l <= r) {
322                 int m = (l + r) >> 1;
323                 if (ao_lisp_chunk[m].old_addr < addr)
324                         l = m + 1;
325                 else
326                         r = m - 1;
327         }
328         /*
329          * The correct location is always in 'l', with r = l-1 being
330          * the entry before the right one
331          */
332
333 #if DBG_MEM
334         /* Off the right side */
335         if (l >= AO_LISP_NCHUNK)
336                 ao_lisp_abort();
337
338         /* Off the left side */
339         if (l == 0 && chunk_busy && addr > ao_lisp_chunk[0].old_addr)
340                 ao_lisp_abort();
341 #endif
342
343         /* Shuffle existing entries right */
344         int end = min(AO_LISP_NCHUNK, chunk_busy + 1);
345
346         memmove(&ao_lisp_chunk[l+1],
347                 &ao_lisp_chunk[l],
348                 (end - (l+1)) * sizeof (struct ao_lisp_chunk));
349
350         /* Add new entry */
351         ao_lisp_chunk[l].old_addr = addr;
352         ao_lisp_chunk[l].size = size;
353
354         /* Increment the number of elements up to the size of the array */
355         if (chunk_busy < AO_LISP_NCHUNK)
356                 chunk_busy++;
357
358         /* Set the top address if the array is full */
359         if (chunk_busy == AO_LISP_NCHUNK)
360                 chunk_high = ao_lisp_chunk[AO_LISP_NCHUNK-1].old_addr +
361                         ao_lisp_chunk[AO_LISP_NCHUNK-1].size;
362 }
363
364 static void
365 reset_chunks(void)
366 {
367         memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
368         chunk_high = ao_lisp_top;
369         chunk_busy = 0;
370 }
371
372 /*
373  * Walk all referenced objects calling functions on each one
374  */
375
376 static void
377 walk(int (*visit_addr)(const struct ao_lisp_type *type, void **addr),
378      int (*visit_poly)(ao_poly *p, uint8_t do_note_cons))
379 {
380         int i;
381
382         total_marked = 0;
383         ao_lisp_record_reset();
384         memset(ao_lisp_busy, '\0', sizeof (ao_lisp_busy));
385         memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
386         ao_lisp_cons_noted = 0;
387         for (i = 0; i < (int) AO_LISP_ROOT; i++) {
388                 if (ao_lisp_root[i].type) {
389                         void **a = ao_lisp_root[i].addr, *v;
390                         if (a && (v = *a)) {
391                                 MDBG_MOVE("root ptr %d\n", MDBG_OFFSET(v));
392                                 visit_addr(ao_lisp_root[i].type, a);
393                         }
394                 } else {
395                         ao_poly *a = (ao_poly *) ao_lisp_root[i].addr, p;
396                         if (a && (p = *a)) {
397                                 MDBG_MOVE("root poly %d\n", MDBG_OFFSET(ao_lisp_ref(p)));
398                                 visit_poly(a, 0);
399                         }
400                 }
401         }
402         while (ao_lisp_cons_noted) {
403                 memcpy(ao_lisp_cons_last, ao_lisp_cons_note, sizeof (ao_lisp_cons_note));
404                 memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
405                 ao_lisp_cons_noted = 0;
406                 for (i = 0; i < AO_LISP_POOL; i += 4) {
407                         if (busy(ao_lisp_cons_last, i)) {
408                                 void *v = ao_lisp_pool + i;
409                                 MDBG_MOVE("root cons %d\n", MDBG_OFFSET(v));
410                                 visit_addr(&ao_lisp_cons_type, &v);
411                         }
412                 }
413         }
414 }
415
416 #if MDBG_DUMP
417 static void
418 dump_busy(void)
419 {
420         int     i;
421         MDBG_MOVE("busy:");
422         for (i = 0; i < ao_lisp_top; i += 4) {
423                 if ((i & 0xff) == 0) {
424                         MDBG_MORE("\n");
425                         MDBG_MOVE("%s", "");
426                 }
427                 else if ((i & 0x1f) == 0)
428                         MDBG_MORE(" ");
429                 if (busy(ao_lisp_busy, i))
430                         MDBG_MORE("*");
431                 else
432                         MDBG_MORE("-");
433         }
434         MDBG_MORE ("\n");
435 }
436 #define DUMP_BUSY()     dump_busy()
437 #else
438 #define DUMP_BUSY()
439 #endif
440
441 static const struct ao_lisp_type const *ao_lisp_types[AO_LISP_NUM_TYPE] = {
442         [AO_LISP_CONS] = &ao_lisp_cons_type,
443         [AO_LISP_INT] = NULL,
444         [AO_LISP_STRING] = &ao_lisp_string_type,
445         [AO_LISP_OTHER] = (void *) 0x1,
446         [AO_LISP_ATOM] = &ao_lisp_atom_type,
447         [AO_LISP_BUILTIN] = &ao_lisp_builtin_type,
448         [AO_LISP_FRAME] = &ao_lisp_frame_type,
449         [AO_LISP_LAMBDA] = &ao_lisp_lambda_type,
450         [AO_LISP_STACK] = &ao_lisp_stack_type,
451 };
452
453 static int
454 ao_lisp_mark_ref(const struct ao_lisp_type *type, void **ref)
455 {
456         return ao_lisp_mark(type, *ref);
457 }
458
459 static int
460 ao_lisp_poly_mark_ref(ao_poly *p, uint8_t do_note_cons)
461 {
462         return ao_lisp_poly_mark(*p, do_note_cons);
463 }
464
465 int ao_lisp_collects[2];
466 int ao_lisp_freed[2];
467 int ao_lisp_loops[2];
468
469 int ao_lisp_last_top;
470
471 int
472 ao_lisp_collect(uint8_t style)
473 {
474         int     ret;
475         int     i;
476         int     top;
477         int     loops = 0;
478 #if DBG_MEM
479         int     marked;
480         int     moved;
481         struct ao_lisp_record   *mark_record = NULL, *move_record = NULL;
482
483         MDBG_MOVE("collect %d\n", ao_lisp_collects);
484         marked = moved = 0;
485 #endif
486
487         /* The first time through, we're doing a full collect */
488         if (ao_lisp_last_top == 0)
489                 style = AO_LISP_COLLECT_FULL;
490
491         /* Clear references to all caches */
492         for (i = 0; i < (int) AO_LISP_CACHE; i++)
493                 *ao_lisp_cache[i] = NULL;
494         if (style == AO_LISP_COLLECT_FULL) {
495                 chunk_low = top = 0;
496         } else {
497                 chunk_low = top = ao_lisp_last_top;
498         }
499         for (;;) {
500                 loops++;
501                 MDBG_MOVE("move chunks from %d to %d\n", chunk_low, top);
502                 /* Find the sizes of the first chunk of objects to move */
503                 reset_chunks();
504                 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
505 #if DBG_MEM
506                 marked = total_marked;
507
508                 ao_lisp_record_free(mark_record);
509                 mark_record = ao_lisp_record_save();
510                 if (mark_record && move_record)
511                         ao_lisp_record_compare("mark", move_record, mark_record);
512
513                 if (moved && moved != marked)
514                         ao_lisp_abort();
515 #endif
516
517                 DUMP_BUSY();
518
519                 /* Find the first moving object */
520                 for (i = 0; i < AO_LISP_NCHUNK; i++) {
521                         uint16_t        size = ao_lisp_chunk[i].size;
522
523                         if (!size)
524                                 break;
525
526                         if (ao_lisp_chunk[i].old_addr > top)
527                                 break;
528 #if DBG_MEM
529                         if (ao_lisp_chunk[i].old_addr != top)
530                                 ao_lisp_abort();
531 #endif
532
533                         top += size;
534                         MDBG_MOVE("chunk %d %d not moving\n",
535                                   ao_lisp_chunk[i].old_addr,
536                                   ao_lisp_chunk[i].size);
537                 }
538
539                 chunk_first = i;
540                 /* Copy all of the objects */
541                 for (; i < AO_LISP_NCHUNK; i++) {
542                         uint16_t        size = ao_lisp_chunk[i].size;
543
544                         if (!size)
545                                 break;
546
547                         MDBG_MOVE("chunk %d %d -> %d\n",
548                                   ao_lisp_chunk[i].old_addr,
549                                   size,
550                                   top);
551                         ao_lisp_chunk[i].new_addr = top;
552                         memmove(&ao_lisp_pool[top],
553                                 &ao_lisp_pool[ao_lisp_chunk[i].old_addr],
554                                 size);
555                         top += size;
556                 }
557
558                 chunk_last = i;
559
560                 if (chunk_first < chunk_last) {
561                         /* Relocate all references to the objects */
562                         walk(ao_lisp_move, ao_lisp_poly_move);
563
564 #if DBG_MEM
565                         ao_lisp_record_free(move_record);
566                         move_record = ao_lisp_record_save();
567                         if (mark_record && move_record)
568                                 ao_lisp_record_compare("move", mark_record, move_record);
569
570                         moved = total_marked;
571                         if (moved != marked)
572                                 ao_lisp_abort();
573 #endif
574                 }
575
576                 if (chunk_last != AO_LISP_NCHUNK)
577                         break;
578
579                 chunk_low = chunk_high;
580         }
581
582         /* Compute amount of memory freed */
583         ret = ao_lisp_top - top;
584
585         /* Collect stats */
586         ++ao_lisp_collects[style];
587         ao_lisp_freed[style] += ret;
588         ao_lisp_loops[style] += loops;
589
590         ao_lisp_top = top;
591         if (style == AO_LISP_COLLECT_FULL)
592                 ao_lisp_last_top = top;
593
594         MDBG_DO(memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
595                 walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref));
596
597         return ret;
598 }
599
600 /*
601  * Mark interfaces for objects
602  *
603  * Note a reference to memory and
604  * collect information about a few object sizes
605  * at a time
606  */
607
608 int
609 ao_lisp_mark_memory(const struct ao_lisp_type *type, void *addr)
610 {
611         int offset;
612         if (!AO_LISP_IS_POOL(addr))
613                 return 1;
614
615         offset = pool_offset(addr);
616         MDBG_MOVE("mark memory %d\n", MDBG_OFFSET(addr));
617         if (busy(ao_lisp_busy, offset)) {
618                 MDBG_MOVE("already marked\n");
619                 return 1;
620         }
621         mark(ao_lisp_busy, offset);
622         note_chunk(offset, ao_lisp_size(type, addr));
623         return 0;
624 }
625
626 int
627 ao_lisp_mark(const struct ao_lisp_type *type, void *addr)
628 {
629         int ret;
630         MDBG_MOVE("mark %d\n", MDBG_OFFSET(addr));
631         MDBG_MOVE_IN();
632         ret = ao_lisp_mark_memory(type, addr);
633         if (!ret) {
634                 MDBG_MOVE("mark recurse\n");
635                 type->mark(addr);
636         }
637         MDBG_MOVE_OUT();
638         return ret;
639 }
640
641 int
642 ao_lisp_poly_mark(ao_poly p, uint8_t do_note_cons)
643 {
644         uint8_t type;
645         void    *addr;
646
647         if (!p)
648                 return 1;
649
650         type = ao_lisp_poly_base_type(p);
651         addr = ao_lisp_ref(p);
652
653         if (!AO_LISP_IS_POOL(addr))
654                 return 1;
655
656         if (type == AO_LISP_CONS && do_note_cons) {
657                 note_cons(ao_lisp_ref(p));
658                 return 1;
659         } else {
660                 const struct ao_lisp_type       *lisp_type;
661
662                 if (type == AO_LISP_OTHER) {
663                         type = ao_lisp_other_type(ao_lisp_poly_other(p));
664 #if DBG_MEM
665                         if (type <= AO_LISP_OTHER || AO_LISP_NUM_TYPE <= type)
666                                 ao_lisp_abort();
667 #endif
668                 }
669
670                 lisp_type = ao_lisp_types[ao_lisp_poly_type(p)];
671                 if (!lisp_type)
672                         return 1;
673                 return ao_lisp_mark(lisp_type, ao_lisp_ref(p));
674         }
675 }
676
677 static void *
678 move_map(void *addr)
679 {
680         uint16_t        offset = pool_offset(addr);
681         int             i;
682
683         for (i = chunk_first; i < chunk_last; i++) {
684                 if (ao_lisp_chunk[i].old_addr == offset) {
685                         MDBG_MOVE("move %d -> %d\n",
686                                   ao_lisp_chunk[i].old_addr,
687                                   ao_lisp_chunk[i].new_addr);
688                         return ao_lisp_pool + ao_lisp_chunk[i].new_addr;
689                 }
690         }
691         return addr;
692 }
693
694 int
695 ao_lisp_move_memory(const struct ao_lisp_type *type, void **ref)
696 {
697         void            *addr = *ref;
698         int             offset;
699
700         if (!AO_LISP_IS_POOL(addr))
701                 return 1;
702
703         (void) type;
704
705         MDBG_MOVE("move memory %d\n", MDBG_OFFSET(addr));
706         addr = move_map(addr);
707         if (addr != *ref) {
708                 MDBG_MOVE("update ref %d %d -> %d\n",
709                           AO_LISP_IS_POOL(ref) ? MDBG_OFFSET(ref) : -1,
710                           MDBG_OFFSET(*ref), MDBG_OFFSET(addr));
711                 *ref = addr;
712         }
713         offset = pool_offset(addr);
714         if (busy(ao_lisp_busy, offset)) {
715                 MDBG_MOVE("already moved\n");
716                 return 1;
717         }
718         mark(ao_lisp_busy, offset);
719         MDBG_DO(ao_lisp_record(type, addr, ao_lisp_size(type, addr)));
720         return 0;
721 }
722
723 int
724 ao_lisp_move(const struct ao_lisp_type *type, void **ref)
725 {
726         int ret;
727         MDBG_MOVE("move object %d\n", MDBG_OFFSET(*ref));
728         MDBG_MOVE_IN();
729         ret = ao_lisp_move_memory(type, ref);
730         if (!ret) {
731                 MDBG_MOVE("move recurse\n");
732                 type->move(*ref);
733         }
734         MDBG_MOVE_OUT();
735         return ret;
736 }
737
738 int
739 ao_lisp_poly_move(ao_poly *ref, uint8_t do_note_cons)
740 {
741         uint8_t                         type;
742         ao_poly                         p = *ref;
743         int                             ret;
744         void                            *addr;
745
746         if (!p)
747                 return 1;
748
749         addr = ao_lisp_ref(p);
750
751         if (!AO_LISP_IS_POOL(addr))
752                 return 1;
753
754         type = ao_lisp_poly_base_type(p);
755
756         if (type == AO_LISP_CONS && do_note_cons) {
757                 note_cons(addr);
758                 addr = move_map(addr);
759                 ret = 1;
760         } else {
761                 const struct ao_lisp_type       *lisp_type;
762
763                 if (type == AO_LISP_OTHER) {
764                         type = ao_lisp_other_type(move_map(ao_lisp_poly_other(p)));
765 #if DBG_MEM
766                         if (type <= AO_LISP_OTHER || AO_LISP_NUM_TYPE <= type)
767                                 ao_lisp_abort();
768 #endif
769                 }
770
771                 lisp_type = ao_lisp_types[type];
772                 if (!lisp_type)
773                         return 1;
774                 ret = ao_lisp_move(lisp_type, &addr);
775         }
776
777         /* Re-write the poly value */
778         if (addr != ao_lisp_ref(p)) {
779                 ao_poly np = ao_lisp_poly(addr, p & AO_LISP_TYPE_MASK);
780                 MDBG_MOVE("poly %d moved %d -> %d\n",
781                           type, MDBG_OFFSET(ao_lisp_ref(p)), MDBG_OFFSET(ao_lisp_ref(np)));
782                 *ref = np;
783         }
784         return ret;
785 }
786
787 #if DBG_MEM
788 void
789 ao_lisp_validate(void)
790 {
791         chunk_low = 0;
792         memset(ao_lisp_chunk, '\0', sizeof (ao_lisp_chunk));
793         walk(ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
794 }
795
796 int dbg_allocs;
797
798 #endif
799
800 void *
801 ao_lisp_alloc(int size)
802 {
803         void    *addr;
804
805         MDBG_DO(++dbg_allocs);
806         MDBG_DO(if (dbg_validate) ao_lisp_validate());
807         size = ao_lisp_size_round(size);
808         if (ao_lisp_top + size > AO_LISP_POOL) {
809                 if (!ao_lisp_collect(AO_LISP_COLLECT_INCREMENTAL) &&
810                     !ao_lisp_collect(AO_LISP_COLLECT_FULL))
811                 {
812                         ao_lisp_error(AO_LISP_OOM, "out of memory");
813                         return NULL;
814                 }
815         }
816         addr = ao_lisp_pool + ao_lisp_top;
817         ao_lisp_top += size;
818         return addr;
819 }
820
821 void
822 ao_lisp_cons_stash(int id, struct ao_lisp_cons *cons)
823 {
824         save_cons[id] = cons;
825 }
826
827 struct ao_lisp_cons *
828 ao_lisp_cons_fetch(int id)
829 {
830         struct ao_lisp_cons *cons = save_cons[id];
831         save_cons[id] = NULL;
832         return cons;
833 }
834
835 void
836 ao_lisp_stack_stash(int id, struct ao_lisp_stack *stack)
837 {
838         save_stack[id] = stack;
839 }
840
841 struct ao_lisp_stack *
842 ao_lisp_stack_fetch(int id)
843 {
844         struct ao_lisp_stack *stack = save_stack[id];
845         save_stack[id] = NULL;
846         return stack;
847 }
848
849 void
850 ao_lisp_string_stash(int id, char *string)
851 {
852         save_string[id] = string;
853 }
854
855 char *
856 ao_lisp_string_fetch(int id)
857 {
858         char *string = save_string[id];
859         save_string[id] = NULL;
860         return string;
861 }
862 void
863 ao_lisp_poly_stash(int id, ao_poly poly)
864 {
865         save_poly[id] = poly;
866 }
867
868 ao_poly
869 ao_lisp_poly_fetch(int id)
870 {
871         ao_poly poly = save_poly[id];
872         save_poly[id] = AO_LISP_NIL;
873         return poly;
874 }