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