altos/lisp: Make sure memmove only happens once per object. Other GC fixes
[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 #include <stdlib.h>
22 uint8_t ao_lisp_const[AO_LISP_POOL_CONST] __attribute__((aligned(4)));
23 #define ao_lisp_pool ao_lisp_const
24 #undef AO_LISP_POOL
25 #define AO_LISP_POOL AO_LISP_POOL_CONST
26 #else
27 uint8_t ao_lisp_pool[AO_LISP_POOL] __attribute__((aligned(4)));
28 #endif
29
30 #if 0
31 #define MDBG_COLLECT_ALWAYS
32 #endif
33
34 #if 0
35 #define MDBG_POOL
36 #endif
37
38 #if 0
39 #include <assert.h>
40 #define MDBG_INCLUDE
41 #if 1
42 #define MDBG_MOVE(...) do { int d; for (d = 0; d < move_depth; d++) printf ("  "); printf(__VA_ARGS__); } while (0)
43 #endif
44 #define MDBG_OFFSET(a)  ((int) ((uint8_t *) (a) - ao_lisp_pool))
45 #define MDBG(...) printf(__VA_ARGS__)
46 #define MDBG_DO(a)      a
47 static int move_depth;
48 #define MDBG_MOVE_IN()  (move_depth++)
49 #define MDBG_MOVE_OUT() (assert(--move_depth >= 0))
50 #else
51 #define MDBG(...)
52 #define MDBG_DO(a)
53 #define MDBG_MOVE(...)
54 #define MDBG_MOVE_IN()
55 #define MDBG_MOVE_OUT()
56 #endif
57
58 uint8_t ao_lisp_exception;
59
60 struct ao_lisp_root {
61         void                            **addr;
62         const struct ao_lisp_type       *type;
63 };
64
65 #define AO_LISP_ROOT    16
66
67 static struct ao_lisp_root      ao_lisp_root[AO_LISP_ROOT];
68
69 #define AO_LISP_BUSY_SIZE       ((AO_LISP_POOL + 31) / 32)
70
71 static uint8_t  ao_lisp_busy[AO_LISP_BUSY_SIZE];
72 static uint8_t  ao_lisp_moving[AO_LISP_BUSY_SIZE];
73 static uint8_t  ao_lisp_cons_note[AO_LISP_BUSY_SIZE];
74 static uint8_t  ao_lisp_cons_last[AO_LISP_BUSY_SIZE];
75 static uint8_t  ao_lisp_cons_noted;
76
77 uint16_t        ao_lisp_top;
78
79 static inline void mark(uint8_t *tag, int offset) {
80         int     byte = offset >> 5;
81         int     bit = (offset >> 2) & 7;
82         tag[byte] |= (1 << bit);
83 }
84
85 static inline void clear(uint8_t *tag, int offset) {
86         int     byte = offset >> 5;
87         int     bit = (offset >> 2) & 7;
88         tag[byte] &= ~(1 << bit);
89 }
90
91 static inline int busy(uint8_t *tag, int offset) {
92         int     byte = offset >> 5;
93         int     bit = (offset >> 2) & 7;
94         return (tag[byte] >> bit) & 1;
95 }
96
97 static inline int min(int a, int b) { return a < b ? a : b; }
98 static inline int max(int a, int b) { return a > b ? a : b; }
99
100 static inline int limit(int offset) {
101         return min(AO_LISP_POOL, max(offset, 0));
102 }
103
104 static int
105 mark_object(uint8_t *tag, void *addr, int size) {
106         int     base;
107         int     bound;
108
109         if (!addr)
110                 return 1;
111
112         if ((uint8_t *) addr < ao_lisp_pool || ao_lisp_pool + AO_LISP_POOL <= (uint8_t*) addr)
113                 return 1;
114
115         base = (uint8_t *) addr - ao_lisp_pool;
116         bound = base + size;
117
118         base = limit(base);
119         bound = limit(bound);
120         if (busy(tag, base))
121                 return 1;
122         while (base < bound) {
123                 mark(tag, base);
124                 base += 4;
125         }
126         return 0;
127 }
128
129 static int
130 clear_object(uint8_t *tag, void *addr, int size) {
131         int     base;
132         int     bound;
133         if (!addr)
134                 return 1;
135
136         base = (uint8_t *) addr - ao_lisp_pool;
137         bound = base + size;
138
139         base = limit(base);
140         bound = limit(bound);
141         if (!busy(tag, base))
142                 return 1;
143         while (base < bound) {
144                 clear(tag, base);
145                 base += 4;
146         }
147         return 0;
148 }
149
150 static int
151 busy_object(uint8_t *tag, void *addr) {
152         int     base;
153
154         if (!addr)
155                 return 1;
156
157         if ((uint8_t *) addr < ao_lisp_pool || ao_lisp_pool + AO_LISP_POOL <= (uint8_t*) addr)
158                 return 1;
159
160         base = (uint8_t *) addr - ao_lisp_pool;
161         base = limit(base);
162         if (busy(tag, base))
163                 return 1;
164         return 0;
165 }
166
167 static void
168 note_cons(void *addr)
169 {
170         if (AO_LISP_IS_POOL(addr)) {
171                 int     offset = (uint8_t *) addr - ao_lisp_pool;
172                 MDBG_MOVE("note cons %d\n", MDBG_OFFSET(addr));
173                 ao_lisp_cons_noted = 1;
174                 mark(ao_lisp_cons_note, offset);
175         }
176 }
177
178 /*
179  * Walk all referenced objects calling functions on each one
180  */
181
182 static void
183 walk(uint8_t *tag,
184      int (*visit_addr)(const struct ao_lisp_type *type, void **addr),
185      int (*visit_poly)(ao_poly *p, uint8_t do_note_cons))
186 {
187         int i;
188
189         memset(tag, '\0', sizeof (ao_lisp_busy));
190         memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
191         ao_lisp_cons_noted = 0;
192         for (i = 0; i < AO_LISP_ROOT; i++) {
193                 if (ao_lisp_root[i].type) {
194                         void **a = ao_lisp_root[i].addr, *v;
195                         if (a && (v = *a)) {
196                                 MDBG("root ptr %d\n", MDBG_OFFSET(v));
197                                 visit_addr(ao_lisp_root[i].type, a);
198                         }
199                 } else {
200                         ao_poly *a = (ao_poly *) ao_lisp_root[i].addr, p;
201                         if (a && (p = *a)) {
202                                 MDBG("root poly %d\n", MDBG_OFFSET(ao_lisp_ref(p)));
203                                 visit_poly(a, 0);
204                         }
205                 }
206         }
207         while (ao_lisp_cons_noted) {
208                 memcpy(ao_lisp_cons_last, ao_lisp_cons_note, sizeof (ao_lisp_cons_note));
209                 memset(ao_lisp_cons_note, '\0', sizeof (ao_lisp_cons_note));
210                 ao_lisp_cons_noted = 0;
211                 for (i = 0; i < AO_LISP_POOL; i += 4) {
212                         if (busy(ao_lisp_cons_last, i)) {
213                                 void *v = ao_lisp_pool + i;
214                                 MDBG("root cons %d\n", MDBG_OFFSET(v));
215                                 visit_addr(&ao_lisp_cons_type, &v);
216                         }
217                 }
218         }
219 }
220
221 static void     *move_old, *move_new;
222 static int      move_size;
223
224 #if MDBG_DUMP
225 static void
226 dump_busy(void)
227 {
228         int     i;
229         printf("busy:");
230         for (i = 0; i < ao_lisp_top; i += 4) {
231                 if ((i & 0xff) == 0)
232                         printf("\n");
233                 else if ((i & 0x1f) == 0)
234                         printf(" ");
235                 if (busy(ao_lisp_busy, i))
236                         putchar('*');
237                 else
238                         putchar('-');
239         }
240         printf ("\n");
241 }
242 #define DUMP_BUSY()     dump_busy()
243 #else
244 #define DUMP_BUSY()
245 #endif
246
247 static const struct ao_lisp_type const *ao_lisp_types[AO_LISP_NUM_TYPE] = {
248         [AO_LISP_CONS] = &ao_lisp_cons_type,
249         [AO_LISP_INT] = NULL,
250         [AO_LISP_STRING] = &ao_lisp_string_type,
251         [AO_LISP_OTHER] = (void *) 0x1,
252         [AO_LISP_ATOM] = &ao_lisp_atom_type,
253         [AO_LISP_BUILTIN] = &ao_lisp_builtin_type,
254         [AO_LISP_FRAME] = &ao_lisp_frame_type,
255         [AO_LISP_LAMBDA] = &ao_lisp_lambda_type,
256 };
257
258 static int
259 ao_lisp_mark_ref(const struct ao_lisp_type *type, void **ref)
260 {
261         return ao_lisp_mark(type, *ref);
262 }
263
264 static int
265 ao_lisp_poly_mark_ref(ao_poly *p, uint8_t do_note_cons)
266 {
267         return ao_lisp_poly_mark(*p, do_note_cons);
268 }
269
270 void
271 ao_lisp_collect(void)
272 {
273         int     i;
274         int     top;
275
276         MDBG("collect\n");
277         /* Mark */
278         walk(ao_lisp_busy, ao_lisp_mark_ref, ao_lisp_poly_mark_ref);
279
280         DUMP_BUSY();
281         /* Compact */
282         MDBG("find first busy\n");
283         for (i = 0; i < ao_lisp_top; i += 4) {
284                 if (!busy(ao_lisp_busy, i))
285                         break;
286         }
287         top = i;
288         while(i < ao_lisp_top) {
289                 if (busy(ao_lisp_busy, i)) {
290                         MDBG("busy %d -> %d\n", i, top);
291                         MDBG_MOVE_IN();
292                         move_old = &ao_lisp_pool[i];
293                         move_new = &ao_lisp_pool[top];
294                         move_size = 0;
295                         walk(ao_lisp_moving, ao_lisp_move, ao_lisp_poly_move);
296                         MDBG("\tbusy size %d\n", move_size);
297                         if (move_size == 0)
298                                 ao_lisp_abort();
299                         clear_object(ao_lisp_busy, move_old, move_size);
300                         mark_object(ao_lisp_busy, move_new, move_size);
301                         if (busy_object(ao_lisp_cons_note, move_old)) {
302                                 clear_object(ao_lisp_cons_note, move_old, move_size);
303                                 mark_object(ao_lisp_cons_note, move_new, move_size);
304                         }
305                         i += move_size;
306                         top += move_size;
307 #if MDBG_MOVE
308                         DUMP_BUSY();
309 #endif
310                         MDBG_MOVE_OUT();
311                 } else {
312                         i += 4;
313                 }
314         }
315         ao_lisp_top = top;
316 }
317
318
319 int
320 ao_lisp_mark(const struct ao_lisp_type *type, void *addr)
321 {
322         if (!addr)
323                 return 1;
324         MDBG_MOVE_IN();
325         MDBG_MOVE("mark %d\n", MDBG_OFFSET(addr));
326         if (mark_object(ao_lisp_busy, addr, type->size(addr))) {
327                 MDBG_MOVE("already marked\n");
328                 MDBG_MOVE_OUT();
329                 return 1;
330         }
331         type->mark(addr);
332         MDBG_MOVE_OUT();
333         return 0;
334 }
335
336 int
337 ao_lisp_poly_mark(ao_poly p, uint8_t do_note_cons)
338 {
339         uint8_t type = ao_lisp_poly_type(p);
340
341         if (!p)
342                 return 1;
343         if (type == AO_LISP_CONS && do_note_cons) {
344                 MDBG_MOVE("note cons %d\n", MDBG_OFFSET(ao_lisp_ref(p)));
345                 note_cons(ao_lisp_ref(p));
346                 return 0;
347         } else {
348                 const struct ao_lisp_type *lisp_type = ao_lisp_types[ao_lisp_poly_type(p)];
349                 if (lisp_type)
350                         return ao_lisp_mark(lisp_type, ao_lisp_ref(p));
351                 return 1;
352         }
353 }
354
355 int
356 ao_lisp_mark_memory(void *addr, int size)
357 {
358         return mark_object(ao_lisp_busy, addr, size);
359 }
360
361 /*
362  * After the object has been moved, we have to reference it
363  * in the new location. This is only relevant for ao_lisp_poly_move
364  * as it needs to fetch the type byte from the object, which
365  * may have been overwritten by the copy
366  */
367 void *
368 ao_lisp_move_map(void *addr)
369 {
370         if (addr == move_old) {
371                 if (move_size != 0)
372                         return move_new;
373         }
374         return addr;
375 }
376
377 static void *
378 check_move(void *addr, int size)
379 {
380         if (addr == move_old) {
381                 MDBG_MOVE("mapping %d -> %d\n", MDBG_OFFSET(addr), MDBG_OFFSET(move_new));
382                 if (move_size && move_size != ((size + 3) & ~3))
383                         ao_lisp_abort();
384
385                 /* Only copy the object once, otherwise we may
386                  * smash stuff
387                  */
388                 if (move_size == 0) {
389                         MDBG_MOVE("  copy %d\n", size);
390                         memmove(move_new, move_old, size);
391                         move_size = (size + 3) & ~3;
392                 }
393                 addr = move_new;
394         }
395         return addr;
396 }
397
398 int
399 ao_lisp_move(const struct ao_lisp_type *type, void **ref)
400 {
401         void            *addr = *ref;
402         uint8_t         *a = addr;
403         int             size = type->size(ao_lisp_move_map(addr));
404
405         if (!addr)
406                 return 1;
407
408 #ifndef AO_LISP_MAKE_CONST
409         if (AO_LISP_IS_CONST(addr))
410                 return 1;
411 #endif
412         MDBG_MOVE("object %d\n", MDBG_OFFSET(addr));
413         if (!AO_LISP_IS_POOL(a))
414                 ao_lisp_abort();
415         MDBG_MOVE_IN();
416         addr = check_move(addr, size);
417         if (addr != *ref)
418                 *ref = addr;
419         if (mark_object(ao_lisp_moving, addr, size)) {
420                 MDBG_MOVE("already moved\n");
421                 MDBG_MOVE_OUT();
422                 return 1;
423         }
424         MDBG_MOVE_OUT();
425         MDBG_MOVE("recursing...\n");
426         MDBG_MOVE_IN();
427         type->move(addr);
428         MDBG_MOVE_OUT();
429         MDBG_MOVE("done %d\n", MDBG_OFFSET(addr));
430         return 0;
431 }
432
433 int
434 ao_lisp_move_memory(void **ref, int size)
435 {
436         void *addr = *ref;
437         if (!addr)
438                 return 1;
439
440         MDBG_MOVE("memory %d\n", MDBG_OFFSET(addr));
441         MDBG_MOVE_IN();
442         addr = check_move(addr, size);
443         if (addr != *ref)
444                 *ref = addr;
445         if (mark_object(ao_lisp_moving, addr, size)) {
446                 MDBG_MOVE("already moved\n");
447                 MDBG_MOVE_OUT();
448                 return 1;
449         }
450         MDBG_MOVE_OUT();
451         return 0;
452 }
453
454 int
455 ao_lisp_poly_move(ao_poly *ref, uint8_t do_note_cons)
456 {
457         uint8_t                         type;
458         ao_poly                         p = *ref;
459         const struct ao_lisp_type       *lisp_type;
460         int                             ret;
461         void                            *addr;
462
463         if (!p)
464                 return 1;
465
466         type = ao_lisp_poly_base_type(p);
467         addr = ao_lisp_ref(p);
468
469         if ((uint8_t *) addr < ao_lisp_pool || ao_lisp_pool + AO_LISP_POOL <= (uint8_t*) addr)
470                 return 1;
471
472         if (type == AO_LISP_CONS && do_note_cons) {
473                 addr = check_move(addr, sizeof (struct ao_lisp_cons));
474                 note_cons(addr);
475                 ret = 1;
476         } else {
477
478                 if (type == AO_LISP_OTHER)
479                         type = ao_lisp_other_type(ao_lisp_move_map(ao_lisp_poly_other(p)));
480
481                 if (type >= AO_LISP_NUM_TYPE)
482                         ao_lisp_abort();
483
484                 lisp_type = ao_lisp_types[type];
485                 if (!lisp_type)
486                         return 1;
487                 ret = ao_lisp_move(lisp_type, &addr);
488         }
489
490         if (addr != ao_lisp_ref(p)) {
491                 ao_poly np = ao_lisp_poly(addr, p & AO_LISP_TYPE_MASK);
492                 MDBG("poly %d moved %d -> %d\n",
493                      type, MDBG_OFFSET(ao_lisp_ref(p)), MDBG_OFFSET(ao_lisp_ref(np)));
494                 *ref = np;
495         }
496         return ret;
497 }
498
499 #ifdef MDBG_POOL
500 static int AO_LISP_POOL_CUR = AO_LISP_POOL / 8;
501
502 static void
503 ao_lisp_poison(void)
504 {
505         int     i;
506
507         printf("poison\n");
508         ao_lisp_mark_busy();
509         for (i = 0; i < AO_LISP_POOL_CUR; i += 4) {
510                 uint32_t        *a = (uint32_t *) &ao_lisp_pool[i];
511                 if (!busy_object(ao_lisp_busy, a))
512                         *a = 0xBEEFBEEF;
513         }
514         for (i = 0; i < AO_LISP_POOL_CUR; i += 2) {
515                 ao_poly         *a = (uint16_t *) &ao_lisp_pool[i];
516                 ao_poly         p = *a;
517
518                 if (!ao_lisp_is_const(p)) {
519                         void    *r = ao_lisp_ref(p);
520
521                         if (ao_lisp_pool <= (uint8_t *) r &&
522                             (uint8_t *) r <= ao_lisp_pool + AO_LISP_POOL_CUR)
523                         {
524                                 if (!busy_object(ao_lisp_busy, r)) {
525                                         printf("missing reference from %d to %d\n",
526                                                (int) ((uint8_t *) a - ao_lisp_pool),
527                                                (int) ((uint8_t *) r - ao_lisp_pool));
528                                 }
529                         }
530                 }
531         }
532 }
533
534 #else
535 #define AO_LISP_POOL_CUR AO_LISP_POOL
536 #endif
537
538 void *
539 ao_lisp_alloc(int size)
540 {
541         void    *addr;
542
543         size = ao_lisp_mem_round(size);
544 #ifdef MDBG_COLLECT_ALWAYS
545         ao_lisp_collect();
546 #endif
547         if (ao_lisp_top + size > AO_LISP_POOL_CUR) {
548 #ifdef MDBG_POOL
549                 if (AO_LISP_POOL_CUR < AO_LISP_POOL) {
550                         AO_LISP_POOL_CUR += AO_LISP_POOL / 8;
551                         ao_lisp_poison();
552                 } else
553 #endif
554                 ao_lisp_collect();
555 #ifdef MDBG_POOL
556                 {
557                         int     i;
558
559                         for (i = ao_lisp_top; i < AO_LISP_POOL; i += 4) {
560                                 uint32_t        *p = (uint32_t *) &ao_lisp_pool[i];
561                                 *p = 0xbeefbeef;
562                         }
563                 }
564 #endif
565
566                 if (ao_lisp_top + size > AO_LISP_POOL) {
567                         ao_lisp_error(AO_LISP_OOM, "out of memory");
568                         return NULL;
569                 }
570         }
571         addr = ao_lisp_pool + ao_lisp_top;
572         ao_lisp_top += size;
573         return addr;
574 }
575
576 int
577 ao_lisp_root_add(const struct ao_lisp_type *type, void *addr)
578 {
579         int     i;
580         MDBG("add root type %p addr %p\n", type, addr);
581         for (i = 0; i < AO_LISP_ROOT; i++) {
582                 if (!ao_lisp_root[i].addr) {
583                         ao_lisp_root[i].addr = addr;
584                         ao_lisp_root[i].type = type;
585                         return 1;
586                 }
587         }
588         ao_lisp_abort();
589         return 0;
590 }
591
592 int
593 ao_lisp_root_poly_add(ao_poly *p)
594 {
595         return ao_lisp_root_add(NULL, p);
596 }
597
598 void
599 ao_lisp_root_clear(void *addr)
600 {
601         int     i;
602         for (i = 0; i < AO_LISP_ROOT; i++) {
603                 if (ao_lisp_root[i].addr == addr) {
604                         ao_lisp_root[i].addr = 0;
605                         ao_lisp_root[i].type = 0;
606                         break;
607                 }
608         }
609 }