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.
18 #define DBG(...) printf(__VA_ARGS__)
24 frame_num_size(int num)
26 return sizeof (struct ao_lisp_frame) + num * sizeof (struct ao_lisp_val);
30 frame_size(void *addr)
32 struct ao_lisp_frame *frame = addr;
33 return frame_num_size(frame->num);
36 #define OFFSET(a) ((uint8_t *) (ao_lisp_ref(a)) - ao_lisp_const)
39 frame_mark(void *addr)
41 struct ao_lisp_frame *frame = addr;
47 for (f = 0; f < frame->num; f++) {
48 struct ao_lisp_val *v = &frame->vals[f];
50 ao_lisp_poly_mark(v->atom);
51 ao_lisp_poly_mark(v->val);
52 DBG ("\tframe mark atom %s %d val %d at %d\n", ao_lisp_poly_atom(v->atom)->name, OFFSET(v->atom), OFFSET(v->val), f);
54 frame = ao_lisp_poly_frame(frame->next);
57 if (ao_lisp_mark_memory(frame, frame_size(frame)))
63 frame_move(void *addr)
65 struct ao_lisp_frame *frame = addr;
69 struct ao_lisp_frame *next;
72 for (f = 0; f < frame->num; f++) {
73 struct ao_lisp_val *v = &frame->vals[f];
76 t = ao_lisp_poly_move(v->atom);
77 DBG("\t\tatom %s %d -> %d\n", ao_lisp_poly_atom(t)->name, OFFSET(v->atom), OFFSET(t));
79 t = ao_lisp_poly_move(v->val);
80 DBG("\t\tval %d -> %d\n", OFFSET(v->val), OFFSET(t));
83 next = ao_lisp_poly_frame(frame->next);
86 next = ao_lisp_move_memory(next, frame_size(next));
87 frame->next = ao_lisp_frame_poly(next);
92 const struct ao_lisp_type ao_lisp_frame_type = {
99 ao_lisp_frame_ref(struct ao_lisp_frame *frame, ao_poly atom)
102 for (f = 0; f < frame->num; f++)
103 if (frame->vals[f].atom == atom)
104 return &frame->vals[f].val;
109 ao_lisp_frame_set(struct ao_lisp_frame *frame, ao_poly atom, ao_poly val)
112 if (!frame->readonly) {
113 ao_poly *ref = ao_lisp_frame_ref(frame, atom);
119 frame = ao_lisp_poly_frame(frame->next);
125 ao_lisp_frame_get(struct ao_lisp_frame *frame, ao_poly atom)
128 ao_poly *ref = ao_lisp_frame_ref(frame, atom);
131 frame = ao_lisp_poly_frame(frame->next);
136 struct ao_lisp_frame *
137 ao_lisp_frame_new(int num, int readonly)
139 struct ao_lisp_frame *frame = ao_lisp_alloc(frame_num_size(num));
144 frame->readonly = readonly;
145 frame->next = AO_LISP_NIL;
146 memset(frame->vals, '\0', num * sizeof (struct ao_lisp_val));
150 static struct ao_lisp_frame *
151 ao_lisp_frame_realloc(struct ao_lisp_frame *frame, int new_num, int readonly)
153 struct ao_lisp_frame *new;
156 if (new_num == frame->num)
158 new = ao_lisp_frame_new(new_num, readonly);
162 if (copy > frame->num)
164 memcpy(new->vals, frame->vals, copy * sizeof (struct ao_lisp_val));
166 new->next = frame->next;
170 struct ao_lisp_frame *
171 ao_lisp_frame_add(struct ao_lisp_frame *frame, ao_poly atom, ao_poly val)
173 ao_poly *ref = frame ? ao_lisp_frame_ref(frame, atom) : NULL;
178 frame = ao_lisp_frame_realloc(frame, f + 1, frame->readonly);
181 frame = ao_lisp_frame_new(1, 0);
185 DBG ("add atom %s %d, val %d at %d\n", ao_lisp_poly_atom(atom)->name, OFFSET(atom), OFFSET(val), f);
186 frame->vals[f].atom = atom;
187 ref = &frame->vals[f].val;