altos/lisp: Make sure memmove only happens once per object. Other GC fixes
[fw/altos] / src / lisp / ao_lisp_atom.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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao_lisp.h"
19
20 static int name_size(char *name)
21 {
22         return sizeof(struct ao_lisp_atom) + strlen(name) + 1;
23 }
24
25 static int atom_size(void *addr)
26 {
27         struct ao_lisp_atom     *atom = addr;
28         if (!atom)
29                 return 0;
30         return name_size(atom->name);
31 }
32
33 static void atom_mark(void *addr)
34 {
35         struct ao_lisp_atom     *atom = addr;
36
37         for (;;) {
38                 atom = ao_lisp_poly_atom(atom->next);
39                 if (!atom)
40                         break;
41                 if (ao_lisp_mark_memory(atom, atom_size(atom)))
42                         break;
43         }
44 }
45
46 static void atom_move(void *addr)
47 {
48         struct ao_lisp_atom     *atom = addr;
49         int                     ret;
50
51         for (;;) {
52                 struct ao_lisp_atom *next = ao_lisp_poly_atom(atom->next);
53
54                 if (!next)
55                         break;
56                 ret = ao_lisp_move_memory((void **) &next, atom_size(next));
57                 if (next != ao_lisp_poly_atom(atom->next))
58                         atom->next = ao_lisp_atom_poly(next);
59                 if (ret)
60                         break;
61                 atom = next;
62         }
63 }
64
65 const struct ao_lisp_type ao_lisp_atom_type = {
66         .mark = atom_mark,
67         .size = atom_size,
68         .move = atom_move,
69 };
70
71 struct ao_lisp_atom     *ao_lisp_atoms;
72
73 struct ao_lisp_atom *
74 ao_lisp_atom_intern(char *name)
75 {
76         struct ao_lisp_atom     *atom;
77
78         for (atom = ao_lisp_atoms; atom; atom = ao_lisp_poly_atom(atom->next)) {
79                 if (!strcmp(atom->name, name))
80                         return atom;
81         }
82 #ifdef ao_builtin_atoms
83         for (atom = ao_lisp_poly_atom(ao_builtin_atoms); atom; atom = ao_lisp_poly_atom(atom->next)) {
84                 if (!strcmp(atom->name, name))
85                         return atom;
86         }
87 #endif
88         atom = ao_lisp_alloc(name_size(name));
89         if (atom) {
90                 atom->type = AO_LISP_ATOM;
91                 atom->next = ao_lisp_atom_poly(ao_lisp_atoms);
92                 if (!ao_lisp_atoms)
93                         ao_lisp_root_add(&ao_lisp_atom_type, &ao_lisp_atoms);
94                 ao_lisp_atoms = atom;
95                 strcpy(atom->name, name);
96         }
97         return atom;
98 }
99
100 struct ao_lisp_frame    *ao_lisp_frame_global;
101 struct ao_lisp_frame    *ao_lisp_frame_current;
102
103 static void
104 ao_lisp_atom_init(void)
105 {
106         if (!ao_lisp_frame_global) {
107                 ao_lisp_frame_global = ao_lisp_frame_new(0);
108                 ao_lisp_root_add(&ao_lisp_frame_type, &ao_lisp_frame_global);
109                 ao_lisp_root_add(&ao_lisp_frame_type, &ao_lisp_frame_current);
110         }
111 }
112
113 static ao_poly *
114 ao_lisp_atom_ref(struct ao_lisp_frame *frame, ao_poly atom)
115 {
116         ao_poly *ref;
117         ao_lisp_atom_init();
118         while (frame) {
119                 ref = ao_lisp_frame_ref(frame, atom);
120                 if (ref)
121                         return ref;
122                 frame = ao_lisp_poly_frame(frame->next);
123         }
124         if (ao_lisp_frame_global) {
125                 ref = ao_lisp_frame_ref(ao_lisp_frame_global, atom);
126                 if (ref)
127                         return ref;
128         }
129         return NULL;
130 }
131
132 ao_poly
133 ao_lisp_atom_get(ao_poly atom)
134 {
135         ao_poly *ref = ao_lisp_atom_ref(ao_lisp_frame_current, atom);
136
137         if (!ref && ao_lisp_frame_global)
138                 ref = ao_lisp_frame_ref(ao_lisp_frame_global, atom);
139 #ifdef ao_builtin_frame
140         if (!ref)
141                 ref = ao_lisp_frame_ref(ao_lisp_poly_frame(ao_builtin_frame), atom);
142 #endif
143         if (ref)
144                 return *ref;
145         return ao_lisp_error(AO_LISP_UNDEFINED, "undefined atom %s", ao_lisp_poly_atom(atom)->name);
146 }
147
148 ao_poly
149 ao_lisp_atom_set(ao_poly atom, ao_poly val)
150 {
151         ao_poly *ref = ao_lisp_atom_ref(ao_lisp_frame_current, atom);
152
153         if (!ref && ao_lisp_frame_global)
154                 ref = ao_lisp_frame_ref(ao_lisp_frame_global, atom);
155         if (ref)
156                 *ref = val;
157         else
158                 ao_lisp_frame_add(&ao_lisp_frame_global, atom, val);
159         return val;
160 }
161
162 void
163 ao_lisp_atom_print(ao_poly a)
164 {
165         struct ao_lisp_atom *atom = ao_lisp_poly_atom(a);
166         printf("%s", atom->name);
167 }