summaryrefslogtreecommitdiffstats
path: root/struct.c
diff options
context:
space:
mode:
authorKaz Kylheku <kaz@kylheku.com>2019-04-19 19:03:01 -0700
committerKaz Kylheku <kaz@kylheku.com>2019-04-19 19:03:01 -0700
commit428eb38cf4c0b2c1677a88aed6f7fc2d78903538 (patch)
tree67fd238acc7e73aab6b06a7e280b826838140dcb /struct.c
parent8a1af20f71b6b75dfc25e32c2c190ea1c539d484 (diff)
downloadtxr-428eb38cf4c0b2c1677a88aed6f7fc2d78903538.tar.gz
txr-428eb38cf4c0b2c1677a88aed6f7fc2d78903538.tar.bz2
txr-428eb38cf4c0b2c1677a88aed6f7fc2d78903538.zip
New function: allocate-struct.
* struct.c (struct_init): allocate-struct intrinsic registered. (allocate_struct): New static function. * txr.1: Documented.
Diffstat (limited to 'struct.c')
-rw-r--r--struct.c17
1 files changed, 17 insertions, 0 deletions
diff --git a/struct.c b/struct.c
index 494ae025..5ea77869 100644
--- a/struct.c
+++ b/struct.c
@@ -114,6 +114,7 @@ static val make_struct_type_compat(val name, val super, val slots,
val initfun, val boactor);
static val call_super_method(val inst, val sym, struct args *);
static val call_super_fun(val type, val sym, struct args *);
+static val allocate_struct(val type);
void struct_init(void)
{
@@ -157,6 +158,7 @@ void struct_init(void)
reg_fun(intern(lit("make-lazy-struct"), user_package),
func_n2(make_lazy_struct));
reg_fun(make_struct_lit_s, func_n2(make_struct_lit));
+ reg_fun(intern(lit("allocate-struct"), user_package), func_n1(allocate_struct));
reg_fun(intern(lit("copy-struct"), user_package), func_n1(copy_struct));
reg_fun(intern(lit("replace-struct"), user_package), func_n2(replace_struct));
reg_fun(intern(lit("clear-struct"), user_package), func_n2o(clear_struct, 1));
@@ -514,6 +516,21 @@ static void call_postinitfun_chain(struct struct_type *st, val strct)
}
}
+static val allocate_struct(val type)
+{
+ val self = lit("allocate-struct");
+ struct struct_type *st = stype_handle(&type, self);
+ cnum nslots = st->nslots;
+ size_t size = offsetof(struct struct_inst, slot) + sizeof (val) * nslots;
+ struct struct_inst *si = coerce(struct struct_inst *, chk_calloc(1, size));
+ si->type = st;
+ si->id = st->id;
+ si->lazy = 0;
+ si->dirty = 1;
+ bug_unless (type == st->self);
+ return cobj(coerce(mem_t *, si), st->name, &struct_inst_ops);
+}
+
static val make_struct_impl(val self, val type,
struct args *plist, struct args *args)
{