summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2016-01-09 15:50:04 -0800
committerKaz Kylheku <kaz@kylheku.com>2016-01-09 15:50:04 -0800
commit1020794b7ff51a4f168a73343f0674e71b0b279b (patch)
tree8520c2e17e3c4b2d4752658fe0c0bee6f5d72829
parent4e119128f89115dc5e1b00c00c2567ad550a04c3 (diff)
downloadtxr-1020794b7ff51a4f168a73343f0674e71b0b279b.tar.gz
txr-1020794b7ff51a4f168a73343f0674e71b0b279b.tar.bz2
txr-1020794b7ff51a4f168a73343f0674e71b0b279b.zip
Use struct instead of cons for lazy string fields.
* gc.c (finalize): Must free the dynamic structure attached to the LSTR type now. (mark_obj): Must mark interior of LSTR type's props structure. * lib.c (lazy_sub_str, copy_lazy_str): Copy props structure. (lazy_str): Allocate and initialize props structure. (lazy_str_force, lazy_str_put, lazy_str_force_upto, lazy_str_get_trailing_list, out_lazy_str): Follow representation change. * lib.h (struct lazy_string_props): New struct type. (strut lazy_string): Member opts replaced with props pointer to struct lazy_string_props.
-rw-r--r--gc.c8
-rw-r--r--lib.c41
-rw-r--r--lib.h8
3 files changed, 37 insertions, 20 deletions
diff --git a/gc.c b/gc.c
index 2615b025..cc5e9597 100644
--- a/gc.c
+++ b/gc.c
@@ -230,7 +230,6 @@ static void finalize(val obj)
case PKG:
case FUN:
case LCONS:
- case LSTR:
case ENV:
case FLNUM:
case RNG:
@@ -243,6 +242,10 @@ static void finalize(val obj)
free(obj->st.str);
obj->st.str = 0;
return;
+ case LSTR:
+ free(obj->ls.props);
+ obj->ls.props = 0;
+ return;
case VEC:
free(obj->v.vec-2);
obj->v.vec = 0;
@@ -351,7 +354,8 @@ tail_call:
mark_obj_tail(obj->lc.cdr);
case LSTR:
mark_obj(obj->ls.prefix);
- mark_obj(obj->ls.opts);
+ mark_obj(obj->ls.props->limit);
+ mark_obj(obj->ls.props->term);
mark_obj_tail(obj->ls.list);
case COBJ:
obj->co.ops->mark(obj);
diff --git a/lib.c b/lib.c
index 7c45907f..1d034edf 100644
--- a/lib.c
+++ b/lib.c
@@ -3187,8 +3187,9 @@ static val lazy_sub_str(val lstr, val from, val to)
lsub->ls.type = LSTR;
lsub->ls.prefix = pfxsub;
lsub->ls.list = lstr->ls.list;
- lsub->ls.opts = lstr->ls.opts;
-
+ lsub->ls.props = coerce(struct lazy_string_props *,
+ chk_copy_obj(coerce(mem_t *, lstr->ls.props),
+ sizeof *lstr->ls.props));
return lsub;
}
}
@@ -6107,7 +6108,9 @@ val lazy_str(val lst, val term, val limit)
obj->ls.type = LSTR;
/* Must init before calling something that can gc! */
- obj->ls.opts = obj->ls.list = obj->ls.prefix = nil;
+ obj->ls.list = obj->ls.prefix = nil;
+ obj->ls.props = coerce(struct lazy_string_props *,
+ chk_calloc(1, sizeof *obj->ls.props));
term = default_arg(term, lit("\n"));
limit = default_bool_arg(limit);
@@ -6121,7 +6124,9 @@ val lazy_str(val lst, val term, val limit)
limit = if2(limit, minus(limit, one));
}
- set(mkloc(obj->ls.opts, obj), cons(term, limit));
+
+ set(mkloc(obj->ls.props->term, obj), term);
+ set(mkloc(obj->ls.props->limit, obj), limit);
return obj;
}
@@ -6130,10 +6135,12 @@ static val copy_lazy_str(val lstr)
{
val obj = make_obj();
obj->ls.type = LSTR;
- obj->ls.opts = obj->ls.list = obj->ls.prefix = nil;
+ obj->ls.list = obj->ls.prefix = nil;
obj->ls.prefix = copy_str(lstr->ls.prefix);
obj->ls.list = lstr->ls.list;
- obj->ls.opts = lstr->ls.opts;
+ obj->ls.props = coerce(struct lazy_string_props *,
+ chk_copy_obj(coerce(mem_t *, lstr->ls.props),
+ sizeof *lstr->ls.props));
return obj;
}
@@ -6142,8 +6149,8 @@ val lazy_str_force(val lstr)
val lim, term;
list_collect_decl (strlist, ptail);
type_check(lstr, LSTR);
- lim = cdr(lstr->ls.opts);
- term = car(lstr->ls.opts);
+ lim = lstr->ls.props->limit;
+ term = lstr->ls.props->term;
while ((!lim || gt(lim, zero)) && lstr->ls.list) {
val next = pop(&lstr->ls.list);
@@ -6156,7 +6163,7 @@ val lazy_str_force(val lstr)
}
if (lim)
- set(cdr_l(lstr->ls.opts), lim);
+ set(mkloc(lstr->ls.props->limit, lstr), lim);
if (strlist) {
push(lstr->ls.prefix, &strlist);
@@ -6170,8 +6177,8 @@ val lazy_str_put(val lstr, val stream)
{
val lim, term, iter;
type_check(lstr, LSTR);
- lim = cdr(lstr->ls.opts);
- term = car(lstr->ls.opts);
+ lim = lstr->ls.props->limit;
+ term = lstr->ls.props->term;
put_string(lstr->ls.prefix, stream);
@@ -6194,8 +6201,8 @@ val lazy_str_force_upto(val lstr, val index)
val lim, term, ltrm, len, effidx = index;
list_collect_decl (strlist, ptail);
type_check(lstr, LSTR);
- lim = cdr(lstr->ls.opts);
- term = car(lstr->ls.opts);
+ lim = lstr->ls.props->limit;
+ term = lstr->ls.props->term;
ltrm = length_str(term);
len = length_str(lstr->ls.prefix);
@@ -6220,7 +6227,7 @@ val lazy_str_force_upto(val lstr, val index)
}
if (lim)
- set(cdr_l(lstr->ls.opts), lim);
+ set(mkloc(lstr->ls.props->limit, lstr), lim);
if (strlist) {
push(lstr->ls.prefix, &strlist);
@@ -6329,7 +6336,7 @@ val lazy_str_get_trailing_list(val lstr, val index)
{
uses_or2;
val split_suffix = split_str(sub_str(lstr->ls.prefix, index, nil),
- or2(car(lstr->ls.opts), lit("\n")));
+ or2(lstr->ls.props->term, lit("\n")));
if (!cdr(split_suffix) && equal(car(split_suffix), null_string))
return lstr->ls.list;
@@ -8237,8 +8244,8 @@ static void out_lazy_str(val lstr, val out)
const wchar_t *wcterm;
type_check(lstr, LSTR);
- lim = cdr(lstr->ls.opts);
- term = car(lstr->ls.opts);
+ lim = lstr->ls.props->limit;
+ term = lstr->ls.props->term;
wcterm = c_str(term);
put_char(chr('"'), out);
diff --git a/lib.h b/lib.h
index 7b2a4f0f..93ab96bb 100644
--- a/lib.h
+++ b/lib.h
@@ -201,11 +201,17 @@ struct lazy_cons {
* Lazy string: virtual string which dynamically grows as a catentation
* of a list of strings.
*/
+
+struct lazy_string_props {
+ val term;
+ val limit;
+};
+
struct lazy_string {
obj_common;
val prefix; /* actual string part */
val list; /* remaining list */
- val opts; /* ( separator . limit ) */
+ struct lazy_string_props *props;
};
struct cobj {