altos/scheme: Replace per-type indexed stash with poly stash heap
authorKeith Packard <keithp@keithp.com>
Tue, 19 Dec 2017 20:39:20 +0000 (12:39 -0800)
committerKeith Packard <keithp@keithp.com>
Tue, 19 Dec 2017 20:39:20 +0000 (12:39 -0800)
Instead of having a random set of stash arrays with explicit indices
used by callers, just have a general heap. Less error prone, and less code.

Signed-off-by: Keith Packard <keithp@keithp.com>
src/scheme/ao_scheme.h
src/scheme/ao_scheme_atom.c
src/scheme/ao_scheme_builtin.c
src/scheme/ao_scheme_cons.c
src/scheme/ao_scheme_frame.c
src/scheme/ao_scheme_lambda.c
src/scheme/ao_scheme_mem.c
src/scheme/ao_scheme_stack.c
src/scheme/ao_scheme_string.c
src/scheme/ao_scheme_vector.c

index 5cae0bda0558dc17f630be0e8dda60c9d58480ee..d4c9bc05051d15c3222c8ed9564ba71ccb177e68 100644 (file)
@@ -595,38 +595,72 @@ ao_scheme_cons_check(struct ao_scheme_cons *cons);
 #endif
 
 void
-ao_scheme_cons_stash(int id, struct ao_scheme_cons *cons);
+ao_scheme_poly_stash(ao_poly poly);
 
-struct ao_scheme_cons *
-ao_scheme_cons_fetch(int id);
+ao_poly
+ao_scheme_poly_fetch(void);
 
-void
-ao_scheme_poly_stash(int id, ao_poly poly);
+static inline void
+ao_scheme_cons_stash(struct ao_scheme_cons *cons) {
+       ao_scheme_poly_stash(ao_scheme_cons_poly(cons));
+}
 
-ao_poly
-ao_scheme_poly_fetch(int id);
+static inline struct ao_scheme_cons *
+ao_scheme_cons_fetch(void) {
+       return ao_scheme_poly_cons(ao_scheme_poly_fetch());
+}
 
-void
-ao_scheme_string_stash(int id, struct ao_scheme_string *string);
+static inline void
+ao_scheme_atom_stash(struct ao_scheme_atom *atom) {
+       ao_scheme_poly_stash(ao_scheme_atom_poly(atom));
+}
 
-struct ao_scheme_string *
-ao_scheme_string_fetch(int id);
+static inline struct ao_scheme_atom *
+ao_scheme_atom_fetch(void) {
+       return ao_scheme_poly_atom(ao_scheme_poly_fetch());
+}
+
+static inline void
+ao_scheme_string_stash(struct ao_scheme_string *string) {
+       ao_scheme_poly_stash(ao_scheme_string_poly(string));
+}
 
+static inline struct ao_scheme_string *
+ao_scheme_string_fetch(void) {
+       return ao_scheme_poly_string(ao_scheme_poly_fetch());
+}
+
+#ifdef AO_SCHEME_FEATURE_VECTOR
 static inline void
-ao_scheme_stack_stash(int id, struct ao_scheme_stack *stack) {
-       ao_scheme_poly_stash(id, ao_scheme_stack_poly(stack));
+ao_scheme_vector_stash(struct ao_scheme_vector *vector) {
+       ao_scheme_poly_stash(ao_scheme_vector_poly(vector));
+}
+
+static inline struct ao_scheme_vector *
+ao_scheme_vector_fetch(void) {
+       return ao_scheme_poly_vector(ao_scheme_poly_fetch());
+}
+#endif
+
+static inline void
+ao_scheme_stack_stash(struct ao_scheme_stack *stack) {
+       ao_scheme_poly_stash(ao_scheme_stack_poly(stack));
 }
 
 static inline struct ao_scheme_stack *
-ao_scheme_stack_fetch(int id) {
-       return ao_scheme_poly_stack(ao_scheme_poly_fetch(id));
+ao_scheme_stack_fetch(void) {
+       return ao_scheme_poly_stack(ao_scheme_poly_fetch());
 }
 
-void
-ao_scheme_frame_stash(int id, struct ao_scheme_frame *frame);
+static inline void
+ao_scheme_frame_stash(struct ao_scheme_frame *frame) {
+       ao_scheme_poly_stash(ao_scheme_frame_poly(frame));
+}
 
-struct ao_scheme_frame *
-ao_scheme_frame_fetch(int id);
+static inline struct ao_scheme_frame *
+ao_scheme_frame_fetch(void) {
+       return ao_scheme_poly_frame(ao_scheme_poly_fetch());
+}
 
 /* bool */
 
index 8989cefda35a1f052bf9ff4d8b8e42053bb400fb..c72a2b27e1f803f2c066ecb04a77ffd0a2262c9a 100644 (file)
@@ -107,9 +107,9 @@ ao_scheme_string_to_atom(struct ao_scheme_string *string)
 
        if (atom)
                return atom;
-       ao_scheme_string_stash(0, string);
+       ao_scheme_string_stash(string);
        atom = ao_scheme_alloc(name_size(string->val));
-       string = ao_scheme_string_fetch(0);
+       string = ao_scheme_string_fetch();
        ao_scheme_atom_init(atom, string->val);
        return atom;
 }
index 8438243412ac7fb4483171c56e8c066f585dd015..81fd901058fa7967332328d773e1c5297148a48c 100644 (file)
@@ -321,7 +321,7 @@ ao_scheme_math(struct ao_scheme_cons *orig_cons, enum ao_scheme_builtin_id op)
 
                if (cons == orig_cons) {
                        ret = car;
-                       ao_scheme_cons_stash(0, cons);
+                       ao_scheme_cons_stash(cons);
                        if (cons->cdr == AO_SCHEME_NIL) {
                                switch (op) {
                                case builtin_minus:
@@ -349,7 +349,7 @@ ao_scheme_math(struct ao_scheme_cons *orig_cons, enum ao_scheme_builtin_id op)
                                        break;
                                }
                        }
-                       cons = ao_scheme_cons_fetch(0);
+                       cons = ao_scheme_cons_fetch();
                } else if (ao_scheme_integer_typep(rt) && ao_scheme_integer_typep(ct)) {
                        int32_t r = ao_scheme_poly_integer(ret, NULL);
                        int32_t c = ao_scheme_poly_integer(car, NULL);
@@ -413,9 +413,9 @@ ao_scheme_math(struct ao_scheme_cons *orig_cons, enum ao_scheme_builtin_id op)
                        default:
                                break;
                        }
-                       ao_scheme_cons_stash(0, cons);
+                       ao_scheme_cons_stash(cons);
                        ret = ao_scheme_integer_poly(r);
-                       cons = ao_scheme_cons_fetch(0);
+                       cons = ao_scheme_cons_fetch();
 #ifdef AO_SCHEME_FEATURE_FLOAT
                } else if (ao_scheme_number_typep(rt) && ao_scheme_number_typep(ct)) {
                        float r, c;
@@ -442,16 +442,16 @@ ao_scheme_math(struct ao_scheme_cons *orig_cons, enum ao_scheme_builtin_id op)
                        default:
                                break;
                        }
-                       ao_scheme_cons_stash(0, cons);
+                       ao_scheme_cons_stash(cons);
                        ret = ao_scheme_float_get(r);
-                       cons = ao_scheme_cons_fetch(0);
+                       cons = ao_scheme_cons_fetch();
 #endif
                }
                else if (rt == AO_SCHEME_STRING && ct == AO_SCHEME_STRING && op == builtin_plus) {
-                       ao_scheme_cons_stash(0, cons);
+                       ao_scheme_cons_stash(cons);
                        ret = ao_scheme_string_poly(ao_scheme_string_cat(ao_scheme_poly_string(ret),
                                                                         ao_scheme_poly_string(car)));
-                       cons = ao_scheme_cons_fetch(0);
+                       cons = ao_scheme_cons_fetch();
                        if (!ret)
                                return ret;
                }
index 1a2de8233ffa19b98e3221dd69ff66ea5d0c06d8..a9ff5acdb1a5735a1f07c7fa3e0bb0e3ae5d2524 100644 (file)
@@ -92,11 +92,11 @@ ao_scheme_cons_cons(ao_poly car, ao_poly cdr)
                cons = ao_scheme_cons_free_list;
                ao_scheme_cons_free_list = ao_scheme_poly_cons(cons->cdr);
        } else {
-               ao_scheme_poly_stash(0, car);
-               ao_scheme_poly_stash(1, cdr);
+               ao_scheme_poly_stash(car);
+               ao_scheme_poly_stash(cdr);
                cons = ao_scheme_alloc(sizeof (struct ao_scheme_cons));
-               cdr = ao_scheme_poly_fetch(1);
-               car = ao_scheme_poly_fetch(0);
+               cdr = ao_scheme_poly_fetch();
+               car = ao_scheme_poly_fetch();
                if (!cons)
                        return NULL;
        }
@@ -134,13 +134,13 @@ ao_scheme_cons_copy(struct ao_scheme_cons *cons)
                struct ao_scheme_cons   *new;
                ao_poly cdr;
 
-               ao_scheme_cons_stash(0, cons);
-               ao_scheme_cons_stash(1, head);
-               ao_scheme_poly_stash(0, ao_scheme_cons_poly(tail));
+               ao_scheme_cons_stash(cons);
+               ao_scheme_cons_stash(head);
+               ao_scheme_cons_stash(tail);
                new = ao_scheme_alloc(sizeof (struct ao_scheme_cons));
-               cons = ao_scheme_cons_fetch(0);
-               head = ao_scheme_cons_fetch(1);
-               tail = ao_scheme_poly_cons(ao_scheme_poly_fetch(0));
+               tail = ao_scheme_cons_fetch();
+               head = ao_scheme_cons_fetch();
+               cons = ao_scheme_cons_fetch();
                if (!new)
                        return AO_SCHEME_NIL;
                new->car = cons->car;
index a7e5153f5257f22d2594cdce596e94ed9bfcc62c..16da62fb922ff000679964f7a28ce258b1234c2e 100644 (file)
@@ -250,9 +250,9 @@ ao_scheme_frame_new(int num)
                frame->num = 0;
                frame->prev = AO_SCHEME_NIL;
                frame->vals = AO_SCHEME_NIL;
-               ao_scheme_frame_stash(0, frame);
+               ao_scheme_frame_stash(frame);
                vals = ao_scheme_frame_vals_new(num);
-               frame = ao_scheme_frame_fetch(0);
+               frame = ao_scheme_frame_fetch();
                if (!vals)
                        return NULL;
                frame->vals = ao_scheme_frame_vals_poly(vals);
@@ -296,9 +296,9 @@ ao_scheme_frame_realloc(struct ao_scheme_frame *frame, int new_num)
 
        if (new_num == frame->num)
                return frame;
-       ao_scheme_frame_stash(0, frame);
+       ao_scheme_frame_stash(frame);
        new_vals = ao_scheme_frame_vals_new(new_num);
-       frame = ao_scheme_frame_fetch(0);
+       frame = ao_scheme_frame_fetch();
        if (!new_vals)
                return NULL;
        vals = ao_scheme_poly_frame_vals(frame->vals);
@@ -331,11 +331,11 @@ ao_scheme_frame_add(struct ao_scheme_frame *frame, ao_poly atom, ao_poly val)
 
        if (!ref) {
                int f = frame->num;
-               ao_scheme_poly_stash(0, atom);
-               ao_scheme_poly_stash(1, val);
+               ao_scheme_poly_stash(atom);
+               ao_scheme_poly_stash(val);
                frame = ao_scheme_frame_realloc(frame, f + 1);
-               val = ao_scheme_poly_fetch(1);
-               atom = ao_scheme_poly_fetch(0);
+               val = ao_scheme_poly_fetch();
+               atom = ao_scheme_poly_fetch();
                if (!frame)
                        return AO_SCHEME_NIL;
                ao_scheme_frame_bind(frame, frame->num - 1, atom, val);
index e8ce0710258df5c10b3a319ea4961679208c22a6..e818d7b04bd5dcbcfe6a57431d27a165c1b89b99 100644 (file)
@@ -89,9 +89,9 @@ ao_scheme_lambda_alloc(struct ao_scheme_cons *code, int args)
                }
        }
 
-       ao_scheme_cons_stash(0, code);
+       ao_scheme_cons_stash(code);
        lambda = ao_scheme_alloc(sizeof (struct ao_scheme_lambda));
-       code = ao_scheme_cons_fetch(0);
+       code = ao_scheme_cons_fetch();
        if (!lambda)
                return AO_SCHEME_NIL;
 
@@ -160,9 +160,9 @@ ao_scheme_lambda_eval(void)
                        return ao_scheme_error(AO_SCHEME_INVALID, "need at least %d args, got %d", args_wanted, args_provided);
        }
 
-       ao_scheme_poly_stash(1, varargs);
+       ao_scheme_poly_stash(varargs);
        next_frame = ao_scheme_frame_new(args_wanted + (varargs != AO_SCHEME_NIL));
-       varargs = ao_scheme_poly_fetch(1);
+       varargs = ao_scheme_poly_fetch();
        if (!next_frame)
                return AO_SCHEME_NIL;
 
index 55872b6294ab3f246f0e1aa2f2033771fc073163..c92150722d1e8dbe2439544f1b3e2293248094e6 100644 (file)
@@ -184,43 +184,34 @@ struct ao_scheme_root {
        void                            **addr;
 };
 
-static struct ao_scheme_cons   *save_cons[2];
-static struct ao_scheme_string *save_string[2];
-static struct ao_scheme_frame  *save_frame[1];
-static ao_poly                 save_poly[3];
+#define AO_SCHEME_NUM_STASH    6
+static ao_poly                 stash_poly[AO_SCHEME_NUM_STASH];
+static int                     stash_poly_ptr;
 
 static const struct ao_scheme_root     ao_scheme_root[] = {
        {
-               .type = &ao_scheme_cons_type,
-               .addr = (void **) &save_cons[0],
-       },
-       {
-               .type = &ao_scheme_cons_type,
-               .addr = (void **) &save_cons[1],
-       },
-       {
-               .type = &ao_scheme_string_type,
-               .addr = (void **) &save_string[0],
+               .type = NULL,
+               .addr = (void **) (void *) &stash_poly[0]
        },
        {
-               .type = &ao_scheme_string_type,
-               .addr = (void **) &save_string[1],
+               .type = NULL,
+               .addr = (void **) (void *) &stash_poly[1]
        },
        {
-               .type = &ao_scheme_frame_type,
-               .addr = (void **) &save_frame[0],
+               .type = NULL,
+               .addr = (void **) (void *) &stash_poly[2]
        },
        {
                .type = NULL,
-               .addr = (void **) (void *) &save_poly[0]
+               .addr = (void **) (void *) &stash_poly[3]
        },
        {
                .type = NULL,
-               .addr = (void **) (void *) &save_poly[1]
+               .addr = (void **) (void *) &stash_poly[4]
        },
        {
                .type = NULL,
-               .addr = (void **) (void *) &save_poly[2]
+               .addr = (void **) (void *) &stash_poly[5]
        },
        {
                .type = &ao_scheme_atom_type,
@@ -991,63 +982,21 @@ ao_scheme_alloc(int size)
 }
 
 void
-ao_scheme_cons_stash(int id, struct ao_scheme_cons *cons)
-{
-       assert(save_cons[id] == 0);
-       save_cons[id] = cons;
-}
-
-struct ao_scheme_cons *
-ao_scheme_cons_fetch(int id)
-{
-       struct ao_scheme_cons *cons = save_cons[id];
-       save_cons[id] = NULL;
-       return cons;
-}
-
-void
-ao_scheme_poly_stash(int id, ao_poly poly)
+ao_scheme_poly_stash(ao_poly p)
 {
-       assert(save_poly[id] == AO_SCHEME_NIL);
-       save_poly[id] = poly;
+       assert(stash_poly_ptr < AO_SCHEME_NUM_STASH);
+       stash_poly[stash_poly_ptr++] = p;
 }
 
 ao_poly
-ao_scheme_poly_fetch(int id)
-{
-       ao_poly poly = save_poly[id];
-       save_poly[id] = AO_SCHEME_NIL;
-       return poly;
-}
-
-void
-ao_scheme_string_stash(int id, struct ao_scheme_string *string)
+ao_scheme_poly_fetch(void)
 {
-       assert(save_string[id] == NULL);
-       save_string[id] = string;
-}
+       ao_poly p;
 
-struct ao_scheme_string *
-ao_scheme_string_fetch(int id)
-{
-       struct ao_scheme_string *string = save_string[id];
-       save_string[id] = NULL;
-       return string;
-}
-
-void
-ao_scheme_frame_stash(int id, struct ao_scheme_frame *frame)
-{
-       assert(save_frame[id] == NULL);
-       save_frame[id] = frame;
-}
-
-struct ao_scheme_frame *
-ao_scheme_frame_fetch(int id)
-{
-       struct ao_scheme_frame *frame = save_frame[id];
-       save_frame[id] = NULL;
-       return frame;
+       assert (stash_poly_ptr > 0);
+       p = stash_poly[--stash_poly_ptr];
+       stash_poly[stash_poly_ptr] = AO_SCHEME_NIL;
+       return p;
 }
 
 int
index e29e2b687f7c7f0907b38994a32876ea0accde9b..863df3ca9f0dafe49ff7a0a8391963a1db817dd5 100644 (file)
@@ -199,13 +199,13 @@ ao_scheme_stack_copy(struct ao_scheme_stack *old)
        struct ao_scheme_stack *n, *prev = NULL;
 
        while (old) {
-               ao_scheme_stack_stash(0, old);
-               ao_scheme_stack_stash(1, new);
-               ao_scheme_stack_stash(2, prev);
+               ao_scheme_stack_stash(old);
+               ao_scheme_stack_stash(new);
+               ao_scheme_stack_stash(prev);
                n = ao_scheme_stack_new();
-               prev = ao_scheme_stack_fetch(2);
-               new = ao_scheme_stack_fetch(1);
-               old = ao_scheme_stack_fetch(0);
+               prev = ao_scheme_stack_fetch();
+               new = ao_scheme_stack_fetch();
+               old = ao_scheme_stack_fetch();
                if (!n)
                        return NULL;
 
index b00ef276815be1b04f6476603b1924ce56a1eaa4..dfc749663ed6b073b6756f2e837a22e5e04df845 100644 (file)
@@ -60,9 +60,9 @@ ao_scheme_string_copy(struct ao_scheme_string *a)
        int                     alen = strlen(a->val);
        struct ao_scheme_string *r;
 
-       ao_scheme_string_stash(0, a);
+       ao_scheme_string_stash(a);
        r = ao_scheme_string_alloc(alen);
-       a = ao_scheme_string_fetch(0);
+       a = ao_scheme_string_fetch();
        if (!r)
                return NULL;
        strcpy(r->val, a->val);
@@ -87,9 +87,9 @@ ao_scheme_atom_to_string(struct ao_scheme_atom *a)
        int                     alen = strlen(a->name);
        struct ao_scheme_string *r;
 
-       ao_scheme_poly_stash(0, ao_scheme_atom_poly(a));
+       ao_scheme_atom_stash(a);
        r = ao_scheme_string_alloc(alen);
-       a = ao_scheme_poly_atom(ao_scheme_poly_fetch(0));
+       a = ao_scheme_atom_fetch();
        if (!r)
                return NULL;
        strcpy(r->val, a->name);
@@ -103,11 +103,11 @@ ao_scheme_string_cat(struct ao_scheme_string *a, struct ao_scheme_string *b)
        int                             blen = strlen(b->val);
        struct ao_scheme_string         *r;
 
-       ao_scheme_string_stash(0, a);
-       ao_scheme_string_stash(1, b);
+       ao_scheme_string_stash(a);
+       ao_scheme_string_stash(b);
        r = ao_scheme_string_alloc(alen + blen);
-       a = ao_scheme_string_fetch(0);
-       b = ao_scheme_string_fetch(1);
+       b = ao_scheme_string_fetch();
+       a = ao_scheme_string_fetch();
        if (!r)
                return NULL;
        strcpy(r->val, a->val);
@@ -123,9 +123,9 @@ ao_scheme_string_pack(struct ao_scheme_cons *cons)
        int                     len;
 
        len = ao_scheme_cons_length(cons);
-       ao_scheme_cons_stash(0, cons);
+       ao_scheme_cons_stash(cons);
        r = ao_scheme_string_alloc(len);
-       cons = ao_scheme_cons_fetch(0);
+       cons = ao_scheme_cons_fetch();
        if (!r)
                return AO_SCHEME_NIL;
        rval = r->val;
@@ -151,13 +151,13 @@ ao_scheme_string_unpack(struct ao_scheme_string *a)
 
        for (i = 0; (c = a->val[i]); i++) {
                struct ao_scheme_cons   *n;
-               ao_scheme_cons_stash(0, cons);
-               ao_scheme_cons_stash(1, tail);
-               ao_scheme_string_stash(0, a);
+               ao_scheme_cons_stash(cons);
+               ao_scheme_cons_stash(tail);
+               ao_scheme_string_stash(a);
                n = ao_scheme_cons_cons(ao_scheme_int_poly(c), AO_SCHEME_NIL);
-               a = ao_scheme_string_fetch(0);
-               cons = ao_scheme_cons_fetch(0);
-               tail = ao_scheme_cons_fetch(1);
+               a = ao_scheme_string_fetch();
+               tail = ao_scheme_cons_fetch();
+               cons = ao_scheme_cons_fetch();
 
                if (!n) {
                        cons = NULL;
index 419d6765aeafd4877098e3b5c778fd5eedd816e4..afdc89a86e880087f98fd2842d3c29bf7d307019 100644 (file)
@@ -145,9 +145,9 @@ ao_scheme_list_to_vector(struct ao_scheme_cons *cons)
        if (ao_scheme_exception)
                return NULL;
 
-       ao_scheme_cons_stash(0, cons);
+       ao_scheme_cons_stash(cons);
        vector = ao_scheme_vector_alloc(length, AO_SCHEME_NIL);
-       cons = ao_scheme_cons_fetch(0);
+       cons = ao_scheme_cons_fetch();
        if (!vector)
                return NULL;
        i = 0;
@@ -166,9 +166,9 @@ ao_scheme_vector_to_list(struct ao_scheme_vector *vector)
        struct ao_scheme_cons   *cons = NULL;
 
        for (i = length; i-- > 0;) {
-               ao_scheme_poly_stash(2, ao_scheme_vector_poly(vector));
+               ao_scheme_vector_stash(vector);
                cons = ao_scheme_cons_cons(vector->vals[i], ao_scheme_cons_poly(cons));
-               vector = ao_scheme_poly_vector(ao_scheme_poly_fetch(2));
+               vector = ao_scheme_vector_fetch();
                if (!cons)
                        return NULL;
        }