2 * Copyright © 2016 Keith Packard <keithp@keithp.com>
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.
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.
15 #include "ao_scheme.h"
18 frame_vals_num_size(int num)
20 return sizeof (struct ao_scheme_frame_vals) + num * sizeof (struct ao_scheme_val);
24 frame_vals_size(void *addr)
26 struct ao_scheme_frame_vals *vals = addr;
27 return frame_vals_num_size(vals->size);
31 frame_vals_mark(void *addr)
33 struct ao_scheme_frame_vals *vals = addr;
36 for (f = 0; f < vals->size; f++) {
37 struct ao_scheme_val *v = &vals->vals[f];
39 ao_scheme_poly_mark(v->val, 0);
40 MDBG_MOVE("frame mark atom %s %d val %d at %d ",
41 ao_scheme_poly_atom(v->atom)->name,
42 MDBG_OFFSET(ao_scheme_ref(v->atom)),
43 MDBG_OFFSET(ao_scheme_ref(v->val)), f);
44 MDBG_DO(ao_scheme_poly_write(v->val));
45 MDBG_DO(printf("\n"));
50 frame_vals_move(void *addr)
52 struct ao_scheme_frame_vals *vals = addr;
55 for (f = 0; f < vals->size; f++) {
56 struct ao_scheme_val *v = &vals->vals[f];
58 ao_scheme_poly_move(&v->atom, 0);
59 ao_scheme_poly_move(&v->val, 0);
60 MDBG_MOVE("frame move atom %s %d val %d at %d\n",
61 ao_scheme_poly_atom(v->atom)->name,
62 MDBG_OFFSET(ao_scheme_ref(v->atom)),
63 MDBG_OFFSET(ao_scheme_ref(v->val)), f);
67 const struct ao_scheme_type ao_scheme_frame_vals_type = {
68 .mark = frame_vals_mark,
69 .size = frame_vals_size,
70 .move = frame_vals_move,
75 frame_size(void *addr)
78 return sizeof (struct ao_scheme_frame);
82 frame_mark(void *addr)
84 struct ao_scheme_frame *frame = addr;
87 MDBG_MOVE("frame mark %d\n", MDBG_OFFSET(frame));
88 if (!AO_SCHEME_IS_POOL(frame))
90 ao_scheme_poly_mark(frame->vals, 0);
91 frame = ao_scheme_poly_frame(frame->prev);
92 MDBG_MOVE("frame next %d\n", MDBG_OFFSET(frame));
95 if (ao_scheme_mark_memory(&ao_scheme_frame_type, frame))
101 frame_move(void *addr)
103 struct ao_scheme_frame *frame = addr;
106 struct ao_scheme_frame *prev;
109 MDBG_MOVE("frame move %d\n", MDBG_OFFSET(frame));
110 if (!AO_SCHEME_IS_POOL(frame))
112 ao_scheme_poly_move(&frame->vals, 0);
113 prev = ao_scheme_poly_frame(frame->prev);
116 ret = ao_scheme_move_memory(&ao_scheme_frame_type, (void **) &prev);
117 if (prev != ao_scheme_poly_frame(frame->prev)) {
118 MDBG_MOVE("frame prev moved from %d to %d\n",
119 MDBG_OFFSET(ao_scheme_poly_frame(frame->prev)),
121 frame->prev = ao_scheme_frame_poly(prev);
129 const struct ao_scheme_type ao_scheme_frame_type = {
137 ao_scheme_frame_write(ao_poly p)
139 struct ao_scheme_frame *frame = ao_scheme_poly_frame(p);
140 struct ao_scheme_frame_vals *vals = ao_scheme_poly_frame_vals(frame->vals);
145 if (frame->type & AO_SCHEME_FRAME_PRINT)
146 printf("recurse...");
148 frame->type |= AO_SCHEME_FRAME_PRINT;
149 for (f = 0; f < frame->num; f++) {
152 ao_scheme_poly_write(vals->vals[f].atom);
154 ao_scheme_poly_write(vals->vals[f].val);
157 ao_scheme_poly_write(frame->prev);
158 frame->type &= ~AO_SCHEME_FRAME_PRINT;
165 ao_scheme_frame_find(struct ao_scheme_frame *frame, int top, ao_poly atom)
167 struct ao_scheme_frame_vals *vals = ao_scheme_poly_frame_vals(frame->vals);
172 int m = (l + r) >> 1;
173 if (vals->vals[m].atom < atom)
182 ao_scheme_frame_ref(struct ao_scheme_frame *frame, ao_poly atom)
184 struct ao_scheme_frame_vals *vals = ao_scheme_poly_frame_vals(frame->vals);
185 int l = ao_scheme_frame_find(frame, frame->num, atom);
190 if (vals->vals[l].atom != atom)
192 return &vals->vals[l].val;
195 struct ao_scheme_frame *ao_scheme_frame_free_list[AO_SCHEME_FRAME_FREE];
197 static struct ao_scheme_frame_vals *
198 ao_scheme_frame_vals_new(int num)
200 struct ao_scheme_frame_vals *vals;
202 vals = ao_scheme_alloc(frame_vals_num_size(num));
205 vals->type = AO_SCHEME_FRAME_VALS;
207 memset(vals->vals, '\0', num * sizeof (struct ao_scheme_val));
211 struct ao_scheme_frame *
212 ao_scheme_frame_new(int num)
214 struct ao_scheme_frame *frame;
215 struct ao_scheme_frame_vals *vals;
217 if (num < AO_SCHEME_FRAME_FREE && (frame = ao_scheme_frame_free_list[num])) {
218 ao_scheme_frame_free_list[num] = ao_scheme_poly_frame(frame->prev);
219 vals = ao_scheme_poly_frame_vals(frame->vals);
221 frame = ao_scheme_alloc(sizeof (struct ao_scheme_frame));
224 frame->type = AO_SCHEME_FRAME;
226 frame->prev = AO_SCHEME_NIL;
227 frame->vals = AO_SCHEME_NIL;
228 ao_scheme_frame_stash(0, frame);
229 vals = ao_scheme_frame_vals_new(num);
230 frame = ao_scheme_frame_fetch(0);
233 frame->vals = ao_scheme_frame_vals_poly(vals);
236 frame->prev = AO_SCHEME_NIL;
241 ao_scheme_frame_mark(struct ao_scheme_frame *frame)
244 return AO_SCHEME_NIL;
245 frame->type |= AO_SCHEME_FRAME_MARK;
246 return ao_scheme_frame_poly(frame);
250 ao_scheme_frame_free(struct ao_scheme_frame *frame)
252 if (frame && !ao_scheme_frame_marked(frame)) {
253 int num = frame->num;
254 if (num < AO_SCHEME_FRAME_FREE) {
255 struct ao_scheme_frame_vals *vals;
257 vals = ao_scheme_poly_frame_vals(frame->vals);
258 memset(vals->vals, '\0', vals->size * sizeof (struct ao_scheme_val));
259 frame->prev = ao_scheme_frame_poly(ao_scheme_frame_free_list[num]);
260 ao_scheme_frame_free_list[num] = frame;
265 static struct ao_scheme_frame *
266 ao_scheme_frame_realloc(struct ao_scheme_frame *frame, int new_num)
268 struct ao_scheme_frame_vals *vals;
269 struct ao_scheme_frame_vals *new_vals;
272 if (new_num == frame->num)
274 ao_scheme_frame_stash(0, frame);
275 new_vals = ao_scheme_frame_vals_new(new_num);
276 frame = ao_scheme_frame_fetch(0);
279 vals = ao_scheme_poly_frame_vals(frame->vals);
281 if (copy > frame->num)
283 memcpy(new_vals->vals, vals->vals, copy * sizeof (struct ao_scheme_val));
284 frame->vals = ao_scheme_frame_vals_poly(new_vals);
285 frame->num = new_num;
290 ao_scheme_frame_bind(struct ao_scheme_frame *frame, int num, ao_poly atom, ao_poly val)
292 struct ao_scheme_frame_vals *vals = ao_scheme_poly_frame_vals(frame->vals);
293 int l = ao_scheme_frame_find(frame, num, atom);
295 memmove(&vals->vals[l+1],
297 (num - l) * sizeof (struct ao_scheme_val));
298 vals->vals[l].atom = atom;
299 vals->vals[l].val = val;
303 ao_scheme_frame_add(struct ao_scheme_frame *frame, ao_poly atom, ao_poly val)
305 ao_poly *ref = frame ? ao_scheme_frame_ref(frame, atom) : NULL;
309 ao_scheme_poly_stash(0, atom);
310 ao_scheme_poly_stash(1, val);
311 frame = ao_scheme_frame_realloc(frame, f + 1);
312 val = ao_scheme_poly_fetch(1);
313 atom = ao_scheme_poly_fetch(0);
315 return AO_SCHEME_NIL;
316 ao_scheme_frame_bind(frame, frame->num - 1, atom, val);
322 struct ao_scheme_frame *ao_scheme_frame_global;
323 struct ao_scheme_frame *ao_scheme_frame_current;
326 ao_scheme_frame_init(void)
328 if (!ao_scheme_frame_global)
329 ao_scheme_frame_global = ao_scheme_frame_new(0);