瀏覽代碼

ncd: port some modules to use preallocation

ambrop7 13 年之前
父節點
當前提交
42d543212b

+ 61 - 79
ncd/modules/arithmetic.c

@@ -174,59 +174,41 @@ static int compute_modulo (NCDModuleInst *i, uintmax_t n1, uintmax_t n2, char *o
     return 1;
     return 1;
 }
 }
 
 
-static void new_templ (NCDModuleInst *i, compute_func cfunc)
+static void new_templ (void *vo, NCDModuleInst *i, compute_func cfunc)
 {
 {
-    struct instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "malloc failed");
-        goto fail0;
-    }
+    struct instance *o = vo;
     o->i = i;
     o->i = i;
-    NCDModuleInst_Backend_SetUser(i, o);
     
     
     NCDValRef n1_arg;
     NCDValRef n1_arg;
     NCDValRef n2_arg;
     NCDValRef n2_arg;
     if (!NCDVal_ListRead(i->args, 2, &n1_arg, &n2_arg)) {
     if (!NCDVal_ListRead(i->args, 2, &n1_arg, &n2_arg)) {
         ModuleLog(i, BLOG_ERROR, "wrong arity");
         ModuleLog(i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     }
     if (!NCDVal_IsStringNoNulls(n1_arg) || !NCDVal_IsStringNoNulls(n2_arg)) {
     if (!NCDVal_IsStringNoNulls(n1_arg) || !NCDVal_IsStringNoNulls(n2_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     uintmax_t n1;
     uintmax_t n1;
     uintmax_t n2;
     uintmax_t n2;
     if (!parse_unsigned_integer(NCDVal_StringValue(n1_arg), &n1) || !parse_unsigned_integer(NCDVal_StringValue(n2_arg), &n2)) {
     if (!parse_unsigned_integer(NCDVal_StringValue(n1_arg), &n1) || !parse_unsigned_integer(NCDVal_StringValue(n2_arg), &n2)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong value");
         ModuleLog(o->i, BLOG_ERROR, "wrong value");
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     if (!cfunc(i, n1, n2, o->value)) {
     if (!cfunc(i, n1, n2, o->value)) {
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     NCDModuleInst_Backend_Up(i);
     NCDModuleInst_Backend_Up(i);
     return;
     return;
     
     
-fail1:
-    free(o);
 fail0:
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
 }
 }
 
 
-static void func_die (void *vo)
-{
-    struct instance *o = vo;
-    NCDModuleInst *i = o->i;
-    
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
-}
-
 static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
 static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
 {
 {
     struct instance *o = vo;
     struct instance *o = vo;
@@ -242,117 +224,117 @@ static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *o
     return 0;
     return 0;
 }
 }
 
 
-static void func_new_lesser (NCDModuleInst *i)
+static void func_new_lesser (void *vo, NCDModuleInst *i)
 {
 {
-    new_templ(i, compute_lesser);
+    new_templ(vo, i, compute_lesser);
 }
 }
 
 
-static void func_new_greater (NCDModuleInst *i)
+static void func_new_greater (void *vo, NCDModuleInst *i)
 {
 {
-    new_templ(i, compute_greater);
+    new_templ(vo, i, compute_greater);
 }
 }
 
 
-static void func_new_lesser_equal (NCDModuleInst *i)
+static void func_new_lesser_equal (void *vo, NCDModuleInst *i)
 {
 {
-    new_templ(i, compute_lesser_equal);
+    new_templ(vo, i, compute_lesser_equal);
 }
 }
 
 
-static void func_new_greater_equal (NCDModuleInst *i)
+static void func_new_greater_equal (void *vo, NCDModuleInst *i)
 {
 {
-    new_templ(i, compute_greater_equal);
+    new_templ(vo, i, compute_greater_equal);
 }
 }
 
 
-static void func_new_equal (NCDModuleInst *i)
+static void func_new_equal (void *vo, NCDModuleInst *i)
 {
 {
-    new_templ(i, compute_equal);
+    new_templ(vo, i, compute_equal);
 }
 }
 
 
-static void func_new_different (NCDModuleInst *i)
+static void func_new_different (void *vo, NCDModuleInst *i)
 {
 {
-    new_templ(i, compute_different);
+    new_templ(vo, i, compute_different);
 }
 }
 
 
-static void func_new_add (NCDModuleInst *i)
+static void func_new_add (void *vo, NCDModuleInst *i)
 {
 {
-    new_templ(i, compute_add);
+    new_templ(vo, i, compute_add);
 }
 }
 
 
-static void func_new_subtract (NCDModuleInst *i)
+static void func_new_subtract (void *vo, NCDModuleInst *i)
 {
 {
-    new_templ(i, compute_subtract);
+    new_templ(vo, i, compute_subtract);
 }
 }
 
 
-static void func_new_multiply (NCDModuleInst *i)
+static void func_new_multiply (void *vo, NCDModuleInst *i)
 {
 {
-    new_templ(i, compute_multiply);
+    new_templ(vo, i, compute_multiply);
 }
 }
 
 
-static void func_new_divide (NCDModuleInst *i)
+static void func_new_divide (void *vo, NCDModuleInst *i)
 {
 {
-    new_templ(i, compute_divide);
+    new_templ(vo, i, compute_divide);
 }
 }
 
 
-static void func_new_modulo (NCDModuleInst *i)
+static void func_new_modulo (void *vo, NCDModuleInst *i)
 {
 {
-    new_templ(i, compute_modulo);
+    new_templ(vo, i, compute_modulo);
 }
 }
 
 
 static const struct NCDModule modules[] = {
 static const struct NCDModule modules[] = {
     {
     {
         .type = "num_lesser",
         .type = "num_lesser",
-        .func_new = func_new_lesser,
-        .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_new2 = func_new_lesser,
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "num_greater",
         .type = "num_greater",
-        .func_new = func_new_greater,
-        .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_new2 = func_new_greater,
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "num_lesser_equal",
         .type = "num_lesser_equal",
-        .func_new = func_new_lesser_equal,
-        .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_new2 = func_new_lesser_equal,
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "num_greater_equal",
         .type = "num_greater_equal",
-        .func_new = func_new_greater_equal,
-        .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_new2 = func_new_greater_equal,
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "num_equal",
         .type = "num_equal",
-        .func_new = func_new_equal,
-        .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_new2 = func_new_equal,
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "num_different",
         .type = "num_different",
-        .func_new = func_new_different,
-        .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_new2 = func_new_different,
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "num_add",
         .type = "num_add",
-        .func_new = func_new_add,
-        .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_new2 = func_new_add,
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "num_subtract",
         .type = "num_subtract",
-        .func_new = func_new_subtract,
-        .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_new2 = func_new_subtract,
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "num_multiply",
         .type = "num_multiply",
-        .func_new = func_new_multiply,
-        .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_new2 = func_new_multiply,
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "num_divide",
         .type = "num_divide",
-        .func_new = func_new_divide,
-        .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_new2 = func_new_divide,
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "num_modulo",
         .type = "num_modulo",
-        .func_new = func_new_modulo,
-        .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_new2 = func_new_modulo,
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = NULL
         .type = NULL
     }
     }

+ 20 - 55
ncd/modules/blocker.c

@@ -102,21 +102,15 @@ struct use_instance {
     LinkedList2Node blocker_node;
     LinkedList2Node blocker_node;
 };
 };
 
 
-static void func_new (NCDModuleInst *i)
+static void func_new (void *vo, NCDModuleInst *i)
 {
 {
-    // allocate instance
-    struct instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
+    struct instance *o = vo;
     o->i = i;
     o->i = i;
-    NCDModuleInst_Backend_SetUser(i, o);
     
     
     // check arguments
     // check arguments
     if (!NCDVal_ListRead(o->i->args, 0)) {
     if (!NCDVal_ListRead(o->i->args, 0)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // init users list
     // init users list
@@ -135,8 +129,6 @@ static void func_new (NCDModuleInst *i)
     NCDModuleInst_Backend_Up(o->i);
     NCDModuleInst_Backend_Up(o->i);
     return;
     return;
     
     
-fail1:
-    free(o);
 fail0:
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
@@ -145,7 +137,6 @@ fail0:
 static void instance_free (struct instance *o)
 static void instance_free (struct instance *o)
 {
 {
     ASSERT(LinkedList2_IsEmpty(&o->users))
     ASSERT(LinkedList2_IsEmpty(&o->users))
-    NCDModuleInst *i = o->i;
     
     
     // break any rdownups
     // break any rdownups
     LinkedList0Node *ln;
     LinkedList0Node *ln;
@@ -156,10 +147,7 @@ static void instance_free (struct instance *o)
         rdu->blocker = NULL;
         rdu->blocker = NULL;
     }
     }
     
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 }
 
 
 static void func_die (void *vo)
 static void func_die (void *vo)
@@ -237,21 +225,15 @@ static void downup_func_new (NCDModuleInst *i)
     updown_func_new_templ(i, 1, 1);
     updown_func_new_templ(i, 1, 1);
 }
 }
 
 
-static void rdownup_func_new (NCDModuleInst *i)
+static void rdownup_func_new (void *vo, NCDModuleInst *i)
 {
 {
-    // allocate structure
-    struct rdownup_instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "malloc failed");
-        goto fail0;
-    }
+    struct rdownup_instance *o = vo;
     o->i = i;
     o->i = i;
-    NCDModuleInst_Backend_SetUser(i, o);
     
     
     // check arguments
     // check arguments
     if (!NCDVal_ListRead(i->args, 0)) {
     if (!NCDVal_ListRead(i->args, 0)) {
         ModuleLog(i, BLOG_ERROR, "wrong arity");
         ModuleLog(i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // get blocker
     // get blocker
@@ -267,8 +249,6 @@ static void rdownup_func_new (NCDModuleInst *i)
     NCDModuleInst_Backend_Up(i);
     NCDModuleInst_Backend_Up(i);
     return;
     return;
     
     
-fail1:
-    free(o);
 fail0:
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
@@ -277,7 +257,6 @@ fail0:
 static void rdownup_func_die (void *vo)
 static void rdownup_func_die (void *vo)
 {
 {
     struct rdownup_instance *o = vo;
     struct rdownup_instance *o = vo;
-    NCDModuleInst *i = o->i;
     
     
     struct instance *blk = o->blocker;
     struct instance *blk = o->blocker;
     
     
@@ -299,29 +278,18 @@ static void rdownup_func_die (void *vo)
         blk->up = 1;
         blk->up = 1;
     }
     }
     
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 }
 
 
-static void use_func_new (NCDModuleInst *i)
+static void use_func_new (void *vo, NCDModuleInst *i)
 {
 {
-    // allocate instance
-    struct use_instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
-    NCDModuleInst_Backend_SetUser(i, o);
-    
-    // init arguments
+    struct use_instance *o = vo;
     o->i = i;
     o->i = i;
     
     
     // check arguments
     // check arguments
     if (!NCDVal_ListRead(o->i->args, 0)) {
     if (!NCDVal_ListRead(o->i->args, 0)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // set blocker
     // set blocker
@@ -337,8 +305,6 @@ static void use_func_new (NCDModuleInst *i)
     
     
     return;
     return;
     
     
-fail1:
-    free(o);
 fail0:
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
@@ -347,7 +313,6 @@ fail0:
 static void use_func_die (void *vo)
 static void use_func_die (void *vo)
 {
 {
     struct use_instance *o = vo;
     struct use_instance *o = vo;
-    NCDModuleInst *i = o->i;
     
     
     // remove from blocker's list
     // remove from blocker's list
     LinkedList2_Remove(&o->blocker->users, &o->blocker_node);
     LinkedList2_Remove(&o->blocker->users, &o->blocker_node);
@@ -357,17 +322,15 @@ static void use_func_die (void *vo)
         instance_free(o->blocker);
         instance_free(o->blocker);
     }
     }
     
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 }
 
 
 static const struct NCDModule modules[] = {
 static const struct NCDModule modules[] = {
     {
     {
         .type = "blocker",
         .type = "blocker",
-        .func_new = func_new,
-        .func_die = func_die
+        .func_new2 = func_new,
+        .func_die = func_die,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "blocker::up",
         .type = "blocker::up",
         .func_new = up_func_new
         .func_new = up_func_new
@@ -379,12 +342,14 @@ static const struct NCDModule modules[] = {
         .func_new = downup_func_new
         .func_new = downup_func_new
     }, {
     }, {
         .type = "blocker::rdownup",
         .type = "blocker::rdownup",
-        .func_new = rdownup_func_new,
-        .func_die = rdownup_func_die
+        .func_new2 = rdownup_func_new,
+        .func_die = rdownup_func_die,
+        .alloc_size = sizeof(struct rdownup_instance)
     }, {
     }, {
         .type = "blocker::use",
         .type = "blocker::use",
-        .func_new = use_func_new,
+        .func_new2 = use_func_new,
         .func_die = use_func_die,
         .func_die = use_func_die,
+        .alloc_size = sizeof(struct use_instance)
     }, {
     }, {
         .type = NULL
         .type = NULL
     }
     }

+ 19 - 46
ncd/modules/call.c

@@ -149,17 +149,9 @@ static int process_func_getspecialobj (struct instance *o, const char *name, NCD
     return 0;
     return 0;
 }
 }
 
 
-static void callrefhere_func_new (NCDModuleInst *i)
+static void callrefhere_func_new (void *vo, NCDModuleInst *i)
 {
 {
-    // allocate instance
-    struct callrefhere_instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
-    NCDModuleInst_Backend_SetUser(i, o);
-    
-    // set arguments
+    struct callrefhere_instance *o = vo;
     o->i = i;
     o->i = i;
     
     
     // init calls list
     // init calls list
@@ -167,17 +159,11 @@ static void callrefhere_func_new (NCDModuleInst *i)
     
     
     // signal up
     // signal up
     NCDModuleInst_Backend_Up(o->i);
     NCDModuleInst_Backend_Up(o->i);
-    return;
-    
-fail0:
-    NCDModuleInst_Backend_SetError(i);
-    NCDModuleInst_Backend_Dead(i);
 }
 }
 
 
 static void callrefhere_func_die (void *vo)
 static void callrefhere_func_die (void *vo)
 {
 {
     struct callrefhere_instance *o = vo;
     struct callrefhere_instance *o = vo;
-    NCDModuleInst *i = o->i;
     
     
     // disconnect calls
     // disconnect calls
     while (!LinkedList0_IsEmpty(&o->calls_list)) {
     while (!LinkedList0_IsEmpty(&o->calls_list)) {
@@ -187,33 +173,24 @@ static void callrefhere_func_die (void *vo)
         inst->crh = NULL;
         inst->crh = NULL;
     }
     }
     
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 }
 
 
-static void func_new (NCDModuleInst *i)
+static void func_new (void *vo, NCDModuleInst *i)
 {
 {
-    // allocate instance
-    struct instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
+    struct instance *o = vo;
     o->i = i;
     o->i = i;
-    NCDModuleInst_Backend_SetUser(i, o);
     
     
     // check arguments
     // check arguments
     NCDValRef template_name_arg;
     NCDValRef template_name_arg;
     NCDValRef args_arg;
     NCDValRef args_arg;
     if (!NCDVal_ListRead(i->args, 2, &template_name_arg, &args_arg)) {
     if (!NCDVal_ListRead(i->args, 2, &template_name_arg, &args_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     }
     if (!NCDVal_IsStringNoNulls(template_name_arg) || !NCDVal_IsList(args_arg)) {
     if (!NCDVal_IsStringNoNulls(template_name_arg) || !NCDVal_IsList(args_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     }
     const char *template_name = NCDVal_StringValue(template_name_arg);
     const char *template_name = NCDVal_StringValue(template_name_arg);
     
     
@@ -233,14 +210,14 @@ static void func_new (NCDModuleInst *i)
         if (NCDVal_IsInvalid(args)) {
         if (NCDVal_IsInvalid(args)) {
             ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewCopy failed");
             ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewCopy failed");
             NCDValMem_Free(&o->args_mem);
             NCDValMem_Free(&o->args_mem);
-            goto fail1;
+            goto fail0;
         }
         }
         
         
         // create process
         // create process
         if (!NCDModuleProcess_Init(&o->process, o->i, template_name, args, o, (NCDModuleProcess_handler_event)process_handler_event)) {
         if (!NCDModuleProcess_Init(&o->process, o->i, template_name, args, o, (NCDModuleProcess_handler_event)process_handler_event)) {
             ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
             ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
             NCDValMem_Free(&o->args_mem);
             NCDValMem_Free(&o->args_mem);
-            goto fail1;
+            goto fail0;
         }
         }
         
         
         // set special functions
         // set special functions
@@ -260,8 +237,6 @@ static void func_new (NCDModuleInst *i)
     }
     }
     return;
     return;
     
     
-fail1:
-    free(o);
 fail0:
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
@@ -269,8 +244,6 @@ fail0:
 
 
 void instance_free (struct instance *o)
 void instance_free (struct instance *o)
 {
 {
-    NCDModuleInst *i = o->i;
-    
     if (o->state != STATE_NONE) {
     if (o->state != STATE_NONE) {
         // remove from callrefhere's calls list
         // remove from callrefhere's calls list
         if (o->crh) {
         if (o->crh) {
@@ -284,10 +257,7 @@ void instance_free (struct instance *o)
         NCDModuleProcess_Free(&o->process);
         NCDModuleProcess_Free(&o->process);
     }
     }
     
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 }
 
 
 static void func_die (void *vo)
 static void func_die (void *vo)
@@ -350,22 +320,25 @@ static int ref_obj_func_getobj (struct instance *o, const char *name, NCDObject
 static const struct NCDModule modules[] = {
 static const struct NCDModule modules[] = {
     {
     {
         .type = "callrefhere",
         .type = "callrefhere",
-        .func_new = callrefhere_func_new,
-        .func_die = callrefhere_func_die
+        .func_new2 = callrefhere_func_new,
+        .func_die = callrefhere_func_die,
+        .alloc_size = sizeof(struct callrefhere_instance)
     }, {
     }, {
         .type = "call",
         .type = "call",
-        .func_new = func_new,
+        .func_new2 = func_new,
         .func_die = func_die,
         .func_die = func_die,
         .func_clean = func_clean,
         .func_clean = func_clean,
         .func_getobj = func_getobj,
         .func_getobj = func_getobj,
-        .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN
+        .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "callrefhere::call",
         .type = "callrefhere::call",
-        .func_new = func_new,
+        .func_new2 = func_new,
         .func_die = func_die,
         .func_die = func_die,
         .func_clean = func_clean,
         .func_clean = func_clean,
         .func_getobj = func_getobj,
         .func_getobj = func_getobj,
-        .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN
+        .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = NULL
         .type = NULL
     }
     }

+ 40 - 46
ncd/modules/call2.c

@@ -64,7 +64,7 @@ struct instance {
 static void process_handler_event (struct instance *o, int event);
 static void process_handler_event (struct instance *o, int event);
 static int process_func_getspecialobj (struct instance *o, const char *name, NCDObject *out_object);
 static int process_func_getspecialobj (struct instance *o, const char *name, NCDObject *out_object);
 static int caller_obj_func_getobj (struct instance *o, const char *name, NCDObject *out_object);
 static int caller_obj_func_getobj (struct instance *o, const char *name, NCDObject *out_object);
-static void func_new_templ (NCDModuleInst *i, const char *template_name, NCDValRef args, int embed);
+static void func_new_templ (void *vo, NCDModuleInst *i, const char *template_name, NCDValRef args, int embed);
 static void instance_free (struct instance *o);
 static void instance_free (struct instance *o);
 
 
 static void process_handler_event (struct instance *o, int event)
 static void process_handler_event (struct instance *o, int event)
@@ -121,19 +121,13 @@ static int caller_obj_func_getobj (struct instance *o, const char *name, NCDObje
     return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
     return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
 }
 }
 
 
-static void func_new_templ (NCDModuleInst *i, const char *template_name, NCDValRef args, int embed)
+static void func_new_templ (void *vo, NCDModuleInst *i, const char *template_name, NCDValRef args, int embed)
 {
 {
     ASSERT(NCDVal_IsInvalid(args) || NCDVal_IsList(args))
     ASSERT(NCDVal_IsInvalid(args) || NCDVal_IsList(args))
     ASSERT(embed == !!embed)
     ASSERT(embed == !!embed)
     
     
-    // allocate instance
-    struct instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
+    struct instance *o = vo;
     o->i = i;
     o->i = i;
-    NCDModuleInst_Backend_SetUser(i, o);
     
     
     // remember embed
     // remember embed
     o->embed = embed;
     o->embed = embed;
@@ -148,7 +142,7 @@ static void func_new_templ (NCDModuleInst *i, const char *template_name, NCDValR
         // create process
         // create process
         if (!NCDModuleProcess_Init(&o->process, o->i, template_name, args, o, (NCDModuleProcess_handler_event)process_handler_event)) {
         if (!NCDModuleProcess_Init(&o->process, o->i, template_name, args, o, (NCDModuleProcess_handler_event)process_handler_event)) {
             ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
             ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
-            goto fail1;
+            goto fail0;
         }
         }
         
         
         // set special functions
         // set special functions
@@ -160,8 +154,6 @@ static void func_new_templ (NCDModuleInst *i, const char *template_name, NCDValR
     
     
     return;
     return;
     
     
-fail1:
-    free(o);
 fail0:
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
@@ -169,20 +161,15 @@ fail0:
 
 
 static void instance_free (struct instance *o)
 static void instance_free (struct instance *o)
 {
 {
-    NCDModuleInst *i = o->i;
-    
     // free process
     // free process
     if (o->state != STATE_NONE) {
     if (o->state != STATE_NONE) {
         NCDModuleProcess_Free(&o->process);
         NCDModuleProcess_Free(&o->process);
     }
     }
     
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 }
 
 
-static void func_new_call (NCDModuleInst *i)
+static void func_new_call (void *vo, NCDModuleInst *i)
 {
 {
     NCDValRef template_arg;
     NCDValRef template_arg;
     NCDValRef args_arg;
     NCDValRef args_arg;
@@ -195,7 +182,7 @@ static void func_new_call (NCDModuleInst *i)
         goto fail0;
         goto fail0;
     }
     }
     
     
-    func_new_templ(i, NCDVal_StringValue(template_arg), args_arg, 0);
+    func_new_templ(vo, i, NCDVal_StringValue(template_arg), args_arg, 0);
     return;
     return;
     
     
 fail0:
 fail0:
@@ -203,7 +190,7 @@ fail0:
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
 }
 }
 
 
-static void func_new_embcall (NCDModuleInst *i)
+static void func_new_embcall (void *vo, NCDModuleInst *i)
 {
 {
     NCDValRef template_arg;
     NCDValRef template_arg;
     if (!NCDVal_ListRead(i->args, 1, &template_arg)) {
     if (!NCDVal_ListRead(i->args, 1, &template_arg)) {
@@ -215,7 +202,7 @@ static void func_new_embcall (NCDModuleInst *i)
         goto fail0;
         goto fail0;
     }
     }
     
     
-    func_new_templ(i, NCDVal_StringValue(template_arg), NCDVal_NewInvalid(), 1);
+    func_new_templ(vo, i, NCDVal_StringValue(template_arg), NCDVal_NewInvalid(), 1);
     return;
     return;
     
     
 fail0:
 fail0:
@@ -223,7 +210,7 @@ fail0:
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
 }
 }
 
 
-static void func_new_call_if (NCDModuleInst *i)
+static void func_new_call_if (void *vo, NCDModuleInst *i)
 {
 {
     NCDValRef cond_arg;
     NCDValRef cond_arg;
     NCDValRef template_arg;
     NCDValRef template_arg;
@@ -243,7 +230,7 @@ static void func_new_call_if (NCDModuleInst *i)
         template_name = NCDVal_StringValue(template_arg);
         template_name = NCDVal_StringValue(template_arg);
     }
     }
     
     
-    func_new_templ(i, template_name, args_arg, 0);
+    func_new_templ(vo, i, template_name, args_arg, 0);
     return;
     return;
     
     
 fail0:
 fail0:
@@ -251,7 +238,7 @@ fail0:
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
 }
 }
 
 
-static void func_new_embcall_if (NCDModuleInst *i)
+static void func_new_embcall_if (void *vo, NCDModuleInst *i)
 {
 {
     NCDValRef cond_arg;
     NCDValRef cond_arg;
     NCDValRef template_arg;
     NCDValRef template_arg;
@@ -270,7 +257,7 @@ static void func_new_embcall_if (NCDModuleInst *i)
         template_name = NCDVal_StringValue(template_arg);
         template_name = NCDVal_StringValue(template_arg);
     }
     }
     
     
-    func_new_templ(i, template_name, NCDVal_NewInvalid(), 1);
+    func_new_templ(vo, i, template_name, NCDVal_NewInvalid(), 1);
     return;
     return;
     
     
 fail0:
 fail0:
@@ -278,7 +265,7 @@ fail0:
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
 }
 }
 
 
-static void func_new_call_ifelse (NCDModuleInst *i)
+static void func_new_call_ifelse (void *vo, NCDModuleInst *i)
 {
 {
     NCDValRef cond_arg;
     NCDValRef cond_arg;
     NCDValRef template_arg;
     NCDValRef template_arg;
@@ -301,7 +288,7 @@ static void func_new_call_ifelse (NCDModuleInst *i)
         template_name = NCDVal_StringValue(else_template_arg);
         template_name = NCDVal_StringValue(else_template_arg);
     }
     }
     
     
-    func_new_templ(i, template_name, args_arg, 0);
+    func_new_templ(vo, i, template_name, args_arg, 0);
     return;
     return;
     
     
 fail0:
 fail0:
@@ -309,7 +296,7 @@ fail0:
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
 }
 }
 
 
-static void func_new_embcall_ifelse (NCDModuleInst *i)
+static void func_new_embcall_ifelse (void *vo, NCDModuleInst *i)
 {
 {
     NCDValRef cond_arg;
     NCDValRef cond_arg;
     NCDValRef template_arg;
     NCDValRef template_arg;
@@ -331,7 +318,7 @@ static void func_new_embcall_ifelse (NCDModuleInst *i)
         template_name = NCDVal_StringValue(else_template_arg);
         template_name = NCDVal_StringValue(else_template_arg);
     }
     }
     
     
-    func_new_templ(i, template_name, NCDVal_NewInvalid(), 1);
+    func_new_templ(vo, i, template_name, NCDVal_NewInvalid(), 1);
     return;
     return;
     
     
 fail0:
 fail0:
@@ -339,7 +326,7 @@ fail0:
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
 }
 }
 
 
-static void func_new_embcall_multif (NCDModuleInst *i)
+static void func_new_embcall_multif (void *vo, NCDModuleInst *i)
 {
 {
     const char *template_name = NULL;
     const char *template_name = NULL;
     
     
@@ -374,7 +361,7 @@ static void func_new_embcall_multif (NCDModuleInst *i)
         j += 2;
         j += 2;
     }
     }
     
     
-    func_new_templ(i, template_name, NCDVal_NewInvalid(), 1);
+    func_new_templ(vo, i, template_name, NCDVal_NewInvalid(), 1);
     return;
     return;
     
     
 fail0:
 fail0:
@@ -428,53 +415,60 @@ static int func_getobj (void *vo, const char *name, NCDObject *out_object)
 static const struct NCDModule modules[] = {
 static const struct NCDModule modules[] = {
     {
     {
         .type = "call2",
         .type = "call2",
-        .func_new = func_new_call,
+        .func_new2 = func_new_call,
         .func_die = func_die,
         .func_die = func_die,
         .func_clean = func_clean,
         .func_clean = func_clean,
         .func_getobj = func_getobj,
         .func_getobj = func_getobj,
-        .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN
+        .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "call2_if",
         .type = "call2_if",
-        .func_new = func_new_call_if,
+        .func_new2 = func_new_call_if,
         .func_die = func_die,
         .func_die = func_die,
         .func_clean = func_clean,
         .func_clean = func_clean,
         .func_getobj = func_getobj,
         .func_getobj = func_getobj,
-        .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN
+        .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "call2_ifelse",
         .type = "call2_ifelse",
-        .func_new = func_new_call_ifelse,
+        .func_new2 = func_new_call_ifelse,
         .func_die = func_die,
         .func_die = func_die,
         .func_clean = func_clean,
         .func_clean = func_clean,
         .func_getobj = func_getobj,
         .func_getobj = func_getobj,
-        .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN
+        .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "embcall2",
         .type = "embcall2",
-        .func_new = func_new_embcall,
+        .func_new2 = func_new_embcall,
         .func_die = func_die,
         .func_die = func_die,
         .func_clean = func_clean,
         .func_clean = func_clean,
         .func_getobj = func_getobj,
         .func_getobj = func_getobj,
-        .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN
+        .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "embcall2_if",
         .type = "embcall2_if",
-        .func_new = func_new_embcall_if,
+        .func_new2 = func_new_embcall_if,
         .func_die = func_die,
         .func_die = func_die,
         .func_clean = func_clean,
         .func_clean = func_clean,
         .func_getobj = func_getobj,
         .func_getobj = func_getobj,
-        .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN
+        .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "embcall2_ifelse",
         .type = "embcall2_ifelse",
-        .func_new = func_new_embcall_ifelse,
+        .func_new2 = func_new_embcall_ifelse,
         .func_die = func_die,
         .func_die = func_die,
         .func_clean = func_clean,
         .func_clean = func_clean,
         .func_getobj = func_getobj,
         .func_getobj = func_getobj,
-        .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN
+        .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "embcall2_multif",
         .type = "embcall2_multif",
-        .func_new = func_new_embcall_multif,
+        .func_new2 = func_new_embcall_multif,
         .func_die = func_die,
         .func_die = func_die,
         .func_clean = func_clean,
         .func_clean = func_clean,
         .func_getobj = func_getobj,
         .func_getobj = func_getobj,
-        .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN
+        .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = NULL
         .type = NULL
     }
     }

+ 10 - 29
ncd/modules/choose.c

@@ -54,27 +54,21 @@ struct instance {
     NCDValRef result;
     NCDValRef result;
 };
 };
 
 
-static void func_new (NCDModuleInst *i)
+static void func_new (void *vo, NCDModuleInst *i)
 {
 {
-    // allocate instance
-    struct instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
+    struct instance *o = vo;
     o->i = i;
     o->i = i;
-    NCDModuleInst_Backend_SetUser(i, o);
     
     
     // read arguments
     // read arguments
     NCDValRef arg_choices;
     NCDValRef arg_choices;
     NCDValRef arg_default_result;
     NCDValRef arg_default_result;
     if (!NCDVal_ListRead(i->args, 2, &arg_choices, &arg_default_result)) {
     if (!NCDVal_ListRead(i->args, 2, &arg_choices, &arg_default_result)) {
         ModuleLog(i, BLOG_ERROR, "wrong arity");
         ModuleLog(i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     }
     if (!NCDVal_IsList(arg_choices)) {
     if (!NCDVal_IsList(arg_choices)) {
         ModuleLog(i, BLOG_ERROR, "wrong type");
         ModuleLog(i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // iterate choices
     // iterate choices
@@ -86,7 +80,7 @@ static void func_new (NCDModuleInst *i)
         // check choice type
         // check choice type
         if (!NCDVal_IsList(c)) {
         if (!NCDVal_IsList(c)) {
             ModuleLog(i, BLOG_ERROR, "wrong choice type");
             ModuleLog(i, BLOG_ERROR, "wrong choice type");
-            goto fail1;
+            goto fail0;
         }
         }
         
         
         // read choice
         // read choice
@@ -94,11 +88,11 @@ static void func_new (NCDModuleInst *i)
         NCDValRef c_result;
         NCDValRef c_result;
         if (!NCDVal_ListRead(c, 2, &c_cond, &c_result)) {
         if (!NCDVal_ListRead(c, 2, &c_cond, &c_result)) {
             ModuleLog(i, BLOG_ERROR, "wrong choice contents arity");
             ModuleLog(i, BLOG_ERROR, "wrong choice contents arity");
-            goto fail1;
+            goto fail0;
         }
         }
         if (!NCDVal_IsString(c_cond)) {
         if (!NCDVal_IsString(c_cond)) {
             ModuleLog(i, BLOG_ERROR, "wrong choice condition type");
             ModuleLog(i, BLOG_ERROR, "wrong choice condition type");
-            goto fail1;
+            goto fail0;
         }
         }
         
         
         // update result
         // update result
@@ -117,24 +111,11 @@ static void func_new (NCDModuleInst *i)
     NCDModuleInst_Backend_Up(o->i);
     NCDModuleInst_Backend_Up(o->i);
     return;
     return;
     
     
-fail1:
-    free(o);
 fail0:
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
 }
 }
 
 
-static void func_die (void *vo)
-{
-    struct instance *o = vo;
-    NCDModuleInst *i = o->i;
-    
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
-}
-
 static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
 static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
 {
 {
     struct instance *o = vo;
     struct instance *o = vo;
@@ -153,9 +134,9 @@ static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *o
 static const struct NCDModule modules[] = {
 static const struct NCDModule modules[] = {
     {
     {
         .type = "choose",
         .type = "choose",
-        .func_new = func_new,
-        .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_new2 = func_new,
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = NULL
         .type = NULL
     }
     }

+ 10 - 21
ncd/modules/concat.c

@@ -50,22 +50,16 @@ struct instance {
     size_t len;
     size_t len;
 };
 };
 
 
-static void func_new (NCDModuleInst *i)
+static void func_new2 (void *vo, NCDModuleInst *i)
 {
 {
-    // allocate instance
-    struct instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
+    struct instance *o = vo;
     o->i = i;
     o->i = i;
-    NCDModuleInst_Backend_SetUser(i, o);
     
     
     // init string
     // init string
     ExpString s;
     ExpString s;
     if (!ExpString_Init(&s)) {
     if (!ExpString_Init(&s)) {
         ModuleLog(i, BLOG_ERROR, "ExpString_Init failed");
         ModuleLog(i, BLOG_ERROR, "ExpString_Init failed");
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // append arguments
     // append arguments
@@ -75,12 +69,12 @@ static void func_new (NCDModuleInst *i)
         
         
         if (!NCDVal_IsString(arg)) {
         if (!NCDVal_IsString(arg)) {
             ModuleLog(i, BLOG_ERROR, "wrong type");
             ModuleLog(i, BLOG_ERROR, "wrong type");
-            goto fail2;
+            goto fail1;
         }
         }
         
         
         if (!ExpString_AppendBinary(&s, (const uint8_t *)NCDVal_StringValue(arg), NCDVal_StringLength(arg))) {
         if (!ExpString_AppendBinary(&s, (const uint8_t *)NCDVal_StringValue(arg), NCDVal_StringLength(arg))) {
             ModuleLog(i, BLOG_ERROR, "ExpString_Append failed");
             ModuleLog(i, BLOG_ERROR, "ExpString_Append failed");
-            goto fail2;
+            goto fail1;
         }
         }
     }
     }
     
     
@@ -92,10 +86,8 @@ static void func_new (NCDModuleInst *i)
     NCDModuleInst_Backend_Up(o->i);
     NCDModuleInst_Backend_Up(o->i);
     return;
     return;
     
     
-fail2:
-    ExpString_Free(&s);
 fail1:
 fail1:
-    free(o);
+    ExpString_Free(&s);
 fail0:
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
@@ -104,15 +96,11 @@ fail0:
 static void func_die (void *vo)
 static void func_die (void *vo)
 {
 {
     struct instance *o = vo;
     struct instance *o = vo;
-    NCDModuleInst *i = o->i;
     
     
     // free string
     // free string
     free(o->string);
     free(o->string);
     
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 }
 
 
 static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
 static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
@@ -133,9 +121,10 @@ static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *o
 static const struct NCDModule modules[] = {
 static const struct NCDModule modules[] = {
     {
     {
         .type = "concat",
         .type = "concat",
-        .func_new = func_new,
+        .func_new2 = func_new2,
         .func_die = func_die,
         .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = NULL
         .type = NULL
     }
     }

+ 12 - 23
ncd/modules/concatv.c

@@ -50,33 +50,27 @@ struct instance {
     size_t len;
     size_t len;
 };
 };
 
 
-static void func_new (NCDModuleInst *i)
+static void func_new (void *vo, NCDModuleInst *i)
 {
 {
-    // allocate instance
-    struct instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
+    struct instance *o = vo;
     o->i = i;
     o->i = i;
-    NCDModuleInst_Backend_SetUser(i, o);
     
     
     // read arguments
     // read arguments
     NCDValRef strings_arg;
     NCDValRef strings_arg;
     if (!NCDVal_ListRead(o->i->args, 1, &strings_arg)) {
     if (!NCDVal_ListRead(o->i->args, 1, &strings_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     }
     if (!NCDVal_IsList(strings_arg)) {
     if (!NCDVal_IsList(strings_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // init string
     // init string
     ExpString s;
     ExpString s;
     if (!ExpString_Init(&s)) {
     if (!ExpString_Init(&s)) {
         ModuleLog(i, BLOG_ERROR, "ExpString_Init failed");
         ModuleLog(i, BLOG_ERROR, "ExpString_Init failed");
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // append arguments
     // append arguments
@@ -86,12 +80,12 @@ static void func_new (NCDModuleInst *i)
         
         
         if (!NCDVal_IsString(arg)) {
         if (!NCDVal_IsString(arg)) {
             ModuleLog(i, BLOG_ERROR, "wrong type");
             ModuleLog(i, BLOG_ERROR, "wrong type");
-            goto fail2;
+            goto fail1;
         }
         }
         
         
         if (!ExpString_AppendBinary(&s, (const uint8_t *)NCDVal_StringValue(arg), NCDVal_StringLength(arg))) {
         if (!ExpString_AppendBinary(&s, (const uint8_t *)NCDVal_StringValue(arg), NCDVal_StringLength(arg))) {
             ModuleLog(i, BLOG_ERROR, "ExpString_Append failed");
             ModuleLog(i, BLOG_ERROR, "ExpString_Append failed");
-            goto fail2;
+            goto fail1;
         }
         }
     }
     }
     
     
@@ -103,10 +97,8 @@ static void func_new (NCDModuleInst *i)
     NCDModuleInst_Backend_Up(o->i);
     NCDModuleInst_Backend_Up(o->i);
     return;
     return;
     
     
-fail2:
-    ExpString_Free(&s);
 fail1:
 fail1:
-    free(o);
+    ExpString_Free(&s);
 fail0:
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
@@ -115,15 +107,11 @@ fail0:
 static void func_die (void *vo)
 static void func_die (void *vo)
 {
 {
     struct instance *o = vo;
     struct instance *o = vo;
-    NCDModuleInst *i = o->i;
     
     
     // free string
     // free string
     free(o->string);
     free(o->string);
     
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 }
 
 
 static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
 static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
@@ -144,9 +132,10 @@ static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *o
 static const struct NCDModule modules[] = {
 static const struct NCDModule modules[] = {
     {
     {
         .type = "concatv",
         .type = "concatv",
-        .func_new = func_new,
+        .func_new2 = func_new,
         .func_die = func_die,
         .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = NULL
         .type = NULL
     }
     }

+ 20 - 39
ncd/modules/logical.c

@@ -57,27 +57,21 @@ struct instance {
     int value;
     int value;
 };
 };
 
 
-static void func_new (NCDModuleInst *i, int is_not, int is_or)
+static void func_new (void *vo, NCDModuleInst *i, int is_not, int is_or)
 {
 {
-    // allocate instance
-    struct instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
+    struct instance *o = vo;
     o->i = i;
     o->i = i;
-    NCDModuleInst_Backend_SetUser(i, o);
     
     
     // compute value from arguments
     // compute value from arguments
     if (is_not) {
     if (is_not) {
         NCDValRef arg;
         NCDValRef arg;
         if (!NCDVal_ListRead(i->args, 1, &arg)) {
         if (!NCDVal_ListRead(i->args, 1, &arg)) {
             ModuleLog(o->i, BLOG_ERROR, "wrong arity");
             ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-            goto fail1;
+            goto fail0;
         }
         }
         if (!NCDVal_IsString(arg)) {
         if (!NCDVal_IsString(arg)) {
             ModuleLog(o->i, BLOG_ERROR, "wrong type");
             ModuleLog(o->i, BLOG_ERROR, "wrong type");
-            goto fail1;
+            goto fail0;
         }
         }
         
         
         o->value = !NCDVal_StringEquals(arg, "true");
         o->value = !NCDVal_StringEquals(arg, "true");
@@ -91,7 +85,7 @@ static void func_new (NCDModuleInst *i, int is_not, int is_or)
             
             
             if (!NCDVal_IsString(arg)) {
             if (!NCDVal_IsString(arg)) {
                 ModuleLog(o->i, BLOG_ERROR, "wrong type");
                 ModuleLog(o->i, BLOG_ERROR, "wrong type");
-                goto fail1;
+                goto fail0;
             }
             }
             
             
             int this_value = NCDVal_StringEquals(arg, "true");
             int this_value = NCDVal_StringEquals(arg, "true");
@@ -107,37 +101,24 @@ static void func_new (NCDModuleInst *i, int is_not, int is_or)
     NCDModuleInst_Backend_Up(o->i);
     NCDModuleInst_Backend_Up(o->i);
     return;
     return;
     
     
-fail1:
-    free(o);
 fail0:
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
 }
 }
 
 
-static void func_new_not (NCDModuleInst *i)
-{
-    func_new(i, 1, 0);
-}
-
-static void func_new_or (NCDModuleInst *i)
+static void func_new_not (void *vo, NCDModuleInst *i)
 {
 {
-    func_new(i, 0, 1);
+    func_new(vo, i, 1, 0);
 }
 }
 
 
-static void func_new_and (NCDModuleInst *i)
+static void func_new_or (void *vo, NCDModuleInst *i)
 {
 {
-    func_new(i, 0, 0);
+    func_new(vo, i, 0, 1);
 }
 }
 
 
-static void func_die (void *vo)
+static void func_new_and (void *vo, NCDModuleInst *i)
 {
 {
-    struct instance *o = vo;
-    NCDModuleInst *i = o->i;
-    
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    func_new(vo, i, 0, 0);
 }
 }
 
 
 static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
 static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
@@ -160,19 +141,19 @@ static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *o
 static const struct NCDModule modules[] = {
 static const struct NCDModule modules[] = {
     {
     {
         .type = "not",
         .type = "not",
-        .func_new = func_new_not,
-        .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_new2 = func_new_not,
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "or",
         .type = "or",
-        .func_new = func_new_or,
-        .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_new2 = func_new_or,
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "and",
         .type = "and",
-        .func_new = func_new_and,
-        .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_new2 = func_new_and,
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = NULL
         .type = NULL
     }
     }

+ 61 - 56
ncd/modules/print.c

@@ -60,12 +60,26 @@
 
 
 #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
 #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
 
 
-struct instance {
+struct rprint_instance {
     NCDModuleInst *i;
     NCDModuleInst *i;
     int ln;
     int ln;
-    int rev;
 };
 };
 
 
+static int check_args (NCDModuleInst *i)
+{
+    size_t num_args = NCDVal_ListCount(i->args);
+    
+    for (size_t j = 0; j < num_args; j++) {
+        NCDValRef arg = NCDVal_ListGet(i->args, j);
+        if (!NCDVal_IsString(arg)) {
+            ModuleLog(i, BLOG_ERROR, "wrong type");
+            return 0;
+        }
+    }
+    
+    return 1;
+}
+
 static void do_print (NCDModuleInst *i, int ln)
 static void do_print (NCDModuleInst *i, int ln)
 {
 {
     size_t num_args = NCDVal_ListCount(i->args);
     size_t num_args = NCDVal_ListCount(i->args);
@@ -94,101 +108,92 @@ static void do_print (NCDModuleInst *i, int ln)
     }
     }
 }
 }
 
 
-static void func_new_temp (NCDModuleInst *i, int ln, int rev)
+static void rprint_func_new_common (void *vo, NCDModuleInst *i, int ln)
 {
 {
-    // allocate instance
-    struct instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
-    NCDModuleInst_Backend_SetUser(i, o);
-    
-    // init arguments
+    struct rprint_instance *o = vo;
     o->i = i;
     o->i = i;
     o->ln = ln;
     o->ln = ln;
-    o->rev = rev;
     
     
-    // check arguments
-    size_t num_args = NCDVal_ListCount(i->args);
-    for (size_t j = 0; j < num_args; j++) {
-        NCDValRef arg = NCDVal_ListGet(i->args, j);
-        if (!NCDVal_IsString(arg)) {
-            ModuleLog(o->i, BLOG_ERROR, "wrong type");
-            goto fail1;
-        }
-    }
-    
-    // print
-    if (!o->rev) {
-        do_print(o->i, o->ln);
+    if (!check_args(i)) {
+        goto fail0;
     }
     }
     
     
-    // signal up
-    NCDModuleInst_Backend_Up(o->i);
-    
+    NCDModuleInst_Backend_Up(i);
     return;
     return;
     
     
-fail1:
-    free(o);
 fail0:
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
 }
 }
 
 
-static void func_die (void *vo)
+static void rprint_func_die (void *vo)
 {
 {
-    struct instance *o = vo;
-    NCDModuleInst *i = o->i;
-    
-    // print
-    if (o->rev) {
-        do_print(o->i, o->ln);
-    }
+    struct rprint_instance *o = vo;
     
     
-    // free instance
-    free(o);
+    do_print(o->i, o->ln);
     
     
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 }
 
 
 static void print_func_new (NCDModuleInst *i)
 static void print_func_new (NCDModuleInst *i)
 {
 {
-    return func_new_temp(i, 0, 0);
+    if (!check_args(i)) {
+        goto fail0;
+    }
+    
+    do_print(i, 0);
+    
+    NCDModuleInst_Backend_Up(i);
+    return;
+    
+fail0:
+    NCDModuleInst_Backend_SetError(i);
+    NCDModuleInst_Backend_Dead(i);
 }
 }
 
 
 static void println_func_new (NCDModuleInst *i)
 static void println_func_new (NCDModuleInst *i)
 {
 {
-    return func_new_temp(i, 1, 0);
+    if (!check_args(i)) {
+        goto fail0;
+    }
+    
+    do_print(i, 1);
+    
+    NCDModuleInst_Backend_Up(i);
+    return;
+    
+fail0:
+    NCDModuleInst_Backend_SetError(i);
+    NCDModuleInst_Backend_Dead(i);
 }
 }
 
 
-static void rprint_func_new (NCDModuleInst *i)
+static void rprint_func_new (void *vo, NCDModuleInst *i)
 {
 {
-    return func_new_temp(i, 0, 1);
+    return rprint_func_new_common(vo, i, 0);
 }
 }
 
 
-static void rprintln_func_new (NCDModuleInst *i)
+static void rprintln_func_new (void *vo, NCDModuleInst *i)
 {
 {
-    return func_new_temp(i, 1, 1);
+    return rprint_func_new_common(vo, i, 1);
 }
 }
 
 
 static const struct NCDModule modules[] = {
 static const struct NCDModule modules[] = {
     {
     {
         .type = "print",
         .type = "print",
-        .func_new = print_func_new,
-        .func_die = func_die
+        .func_new = print_func_new
     }, {
     }, {
         .type = "println",
         .type = "println",
-        .func_new = println_func_new,
-        .func_die = func_die
+        .func_new = println_func_new
     }, {
     }, {
         .type = "rprint",
         .type = "rprint",
-        .func_new = rprint_func_new,
-        .func_die = func_die
+        .func_new2 = rprint_func_new,
+        .func_die = rprint_func_die,
+        .alloc_size = sizeof(struct rprint_instance)
      }, {
      }, {
         .type = "rprintln",
         .type = "rprintln",
-        .func_new = rprintln_func_new,
-        .func_die = func_die
+        .func_new2 = rprintln_func_new,
+        .func_die = rprint_func_die,
+        .alloc_size = sizeof(struct rprint_instance)
     }, {
     }, {
         .type = NULL
         .type = NULL
     }
     }

+ 7 - 21
ncd/modules/process_manager.c

@@ -413,23 +413,15 @@ int process_set_params (struct process *p, const char *template_name, NCDValMem
     return 1;
     return 1;
 }
 }
 
 
-static void func_new (NCDModuleInst *i)
+static void func_new (void *vo, NCDModuleInst *i)
 {
 {
-    // allocate instance
-    struct instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
-    NCDModuleInst_Backend_SetUser(i, o);
-    
-    // init arguments
+    struct instance *o = vo;
     o->i = i;
     o->i = i;
     
     
     // check arguments
     // check arguments
     if (!NCDVal_ListRead(o->i->args, 0)) {
     if (!NCDVal_ListRead(o->i->args, 0)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // init processes list
     // init processes list
@@ -440,11 +432,8 @@ static void func_new (NCDModuleInst *i)
     
     
     // signal up
     // signal up
     NCDModuleInst_Backend_Up(o->i);
     NCDModuleInst_Backend_Up(o->i);
-    
     return;
     return;
     
     
-fail1:
-    free(o);
 fail0:
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
@@ -453,12 +442,8 @@ fail0:
 void instance_free (struct instance *o)
 void instance_free (struct instance *o)
 {
 {
     ASSERT(LinkedList2_IsEmpty(&o->processes_list))
     ASSERT(LinkedList2_IsEmpty(&o->processes_list))
-    NCDModuleInst *i = o->i;
     
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 }
 
 
 static void func_die (void *vo)
 static void func_die (void *vo)
@@ -580,8 +565,9 @@ fail0:
 static const struct NCDModule modules[] = {
 static const struct NCDModule modules[] = {
     {
     {
         .type = "process_manager",
         .type = "process_manager",
-        .func_new = func_new,
-        .func_die = func_die
+        .func_new2 = func_new,
+        .func_die = func_die,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "process_manager::start",
         .type = "process_manager::start",
         .func_new = start_func_new
         .func_new = start_func_new

+ 7 - 22
ncd/modules/run.c

@@ -134,17 +134,9 @@ fail0:
     return 0;
     return 0;
 }
 }
 
 
-static void func_new (NCDModuleInst *i)
+static void func_new (void *vo, NCDModuleInst *i)
 {
 {
-    // allocate instance
-    struct instance *o = malloc(sizeof(*o));
-    if (!o) {
-        BLog(BLOG_ERROR, "malloc failed");
-        goto fail0;
-    }
-    NCDModuleInst_Backend_SetUser(i, o);
-    
-    // init arguments
+    struct instance *o = vo;
     o->i = i;
     o->i = i;
     
     
     // init dummy event lock
     // init dummy event lock
@@ -152,27 +144,19 @@ static void func_new (NCDModuleInst *i)
     
     
     command_template_new(&o->cti, i, build_cmdline, template_free_func, o, BLOG_CURRENT_CHANNEL, &o->lock);
     command_template_new(&o->cti, i, build_cmdline, template_free_func, o, BLOG_CURRENT_CHANNEL, &o->lock);
     return;
     return;
-    
-fail0:
-    NCDModuleInst_Backend_SetError(i);
-    NCDModuleInst_Backend_Dead(i);
 }
 }
 
 
 void template_free_func (void *vo, int is_error)
 void template_free_func (void *vo, int is_error)
 {
 {
     struct instance *o = vo;
     struct instance *o = vo;
-    NCDModuleInst *i = o->i;
     
     
     // free dummy event lock
     // free dummy event lock
     BEventLock_Free(&o->lock);
     BEventLock_Free(&o->lock);
     
     
-    // free instance
-    free(o);
-    
     if (is_error) {
     if (is_error) {
-        NCDModuleInst_Backend_SetError(i);
+        NCDModuleInst_Backend_SetError(o->i);
     }
     }
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 }
 
 
 static void func_die (void *vo)
 static void func_die (void *vo)
@@ -185,8 +169,9 @@ static void func_die (void *vo)
 static const struct NCDModule modules[] = {
 static const struct NCDModule modules[] = {
     {
     {
         .type = "run",
         .type = "run",
-        .func_new = func_new,
-        .func_die = func_die
+        .func_new2 = func_new,
+        .func_die = func_die,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = NULL
         .type = NULL
     }
     }

+ 13 - 26
ncd/modules/runonce.c

@@ -164,18 +164,12 @@ static void process_handler (struct instance *o, int normally, uint8_t normally_
     NCDModuleInst_Backend_Up(o->i);
     NCDModuleInst_Backend_Up(o->i);
 }
 }
 
 
-static void func_new (NCDModuleInst *i)
+static void func_new (void *vo, NCDModuleInst *i)
 {
 {
-    // allocate instance
-    struct instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
-    NCDModuleInst_Backend_SetUser(i, o);
+    struct instance *o = vo;
+    o->i = i;
     
     
     // init arguments
     // init arguments
-    o->i = i;
     o->term_on_deinit = 0;
     o->term_on_deinit = 0;
     
     
     // read arguments
     // read arguments
@@ -183,11 +177,11 @@ static void func_new (NCDModuleInst *i)
     NCDValRef opts_arg = NCDVal_NewInvalid();
     NCDValRef opts_arg = NCDVal_NewInvalid();
     if (!NCDVal_ListRead(i->args, 1, &cmd_arg) && !NCDVal_ListRead(i->args, 2, &cmd_arg, &opts_arg)) {
     if (!NCDVal_ListRead(i->args, 1, &cmd_arg) && !NCDVal_ListRead(i->args, 2, &cmd_arg, &opts_arg)) {
         ModuleLog(i, BLOG_ERROR, "wrong arity");
         ModuleLog(i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     }
     if (!NCDVal_IsInvalid(opts_arg) && !NCDVal_IsList(opts_arg)) {
     if (!NCDVal_IsInvalid(opts_arg) && !NCDVal_IsList(opts_arg)) {
         ModuleLog(i, BLOG_ERROR, "wrong type");
         ModuleLog(i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     int keep_stdout = 0;
     int keep_stdout = 0;
@@ -202,7 +196,7 @@ static void func_new (NCDModuleInst *i)
         // read name
         // read name
         if (!NCDVal_IsStringNoNulls(opt)) {
         if (!NCDVal_IsStringNoNulls(opt)) {
             ModuleLog(o->i, BLOG_ERROR, "wrong option name type");
             ModuleLog(o->i, BLOG_ERROR, "wrong option name type");
-            goto fail1;
+            goto fail0;
         }
         }
         const char *optname = NCDVal_StringValue(opt);
         const char *optname = NCDVal_StringValue(opt);
         
         
@@ -220,7 +214,7 @@ static void func_new (NCDModuleInst *i)
         }
         }
         else {
         else {
             ModuleLog(o->i, BLOG_ERROR, "unknown option name");
             ModuleLog(o->i, BLOG_ERROR, "unknown option name");
-            goto fail1;
+            goto fail0;
         }
         }
     }
     }
     
     
@@ -228,7 +222,7 @@ static void func_new (NCDModuleInst *i)
     char *exec;
     char *exec;
     CmdLine cl;
     CmdLine cl;
     if (!build_cmdline(o->i, cmd_arg, &exec, &cl)) {
     if (!build_cmdline(o->i, cmd_arg, &exec, &cl)) {
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // build fd mapping
     // build fd mapping
@@ -257,7 +251,7 @@ static void func_new (NCDModuleInst *i)
         ModuleLog(i, BLOG_ERROR, "BProcess_Init failed");
         ModuleLog(i, BLOG_ERROR, "BProcess_Init failed");
         CmdLine_Free(&cl);
         CmdLine_Free(&cl);
         free(exec);
         free(exec);
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     CmdLine_Free(&cl);
     CmdLine_Free(&cl);
@@ -265,11 +259,8 @@ static void func_new (NCDModuleInst *i)
     
     
     // set state
     // set state
     o->state = STATE_RUNNING;
     o->state = STATE_RUNNING;
-    
     return;
     return;
     
     
-fail1:
-    free(o);
 fail0:
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
@@ -277,12 +268,7 @@ fail0:
 
 
 static void instance_free (struct instance *o)
 static void instance_free (struct instance *o)
 {
 {
-    NCDModuleInst *i = o->i;
-    
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 }
 
 
 static void func_die (void *vo)
 static void func_die (void *vo)
@@ -325,9 +311,10 @@ static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *o
 static const struct NCDModule modules[] = {
 static const struct NCDModule modules[] = {
     {
     {
         .type = "runonce",
         .type = "runonce",
-        .func_new = func_new,
+        .func_new2 = func_new,
         .func_die = func_die,
         .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = NULL
         .type = NULL
     }
     }

+ 10 - 22
ncd/modules/sleep.c

@@ -68,35 +68,29 @@ static void timer_handler (void *vo)
     }
     }
 }
 }
 
 
-static void func_new (NCDModuleInst *i)
+static void func_new (void *vo, NCDModuleInst *i)
 {
 {
-    // allocate instance
-    struct instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
+    struct instance *o = vo;
     o->i = i;
     o->i = i;
-    NCDModuleInst_Backend_SetUser(i, o);
     
     
     // check arguments
     // check arguments
     NCDValRef ms_start_arg;
     NCDValRef ms_start_arg;
     NCDValRef ms_stop_arg;
     NCDValRef ms_stop_arg;
     if (!NCDVal_ListRead(i->args, 2, &ms_start_arg, &ms_stop_arg)) {
     if (!NCDVal_ListRead(i->args, 2, &ms_start_arg, &ms_stop_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     }
     if (!NCDVal_IsStringNoNulls(ms_start_arg) || !NCDVal_IsStringNoNulls(ms_stop_arg)) {
     if (!NCDVal_IsStringNoNulls(ms_start_arg) || !NCDVal_IsStringNoNulls(ms_stop_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     }
     if (sscanf(NCDVal_StringValue(ms_start_arg), "%"SCNi64, &o->ms_start) != 1) {
     if (sscanf(NCDVal_StringValue(ms_start_arg), "%"SCNi64, &o->ms_start) != 1) {
         ModuleLog(o->i, BLOG_ERROR, "wrong time");
         ModuleLog(o->i, BLOG_ERROR, "wrong time");
-        goto fail1;
+        goto fail0;
     }
     }
     if (sscanf(NCDVal_StringValue(ms_stop_arg), "%"SCNi64, &o->ms_stop) != 1) {
     if (sscanf(NCDVal_StringValue(ms_stop_arg), "%"SCNi64, &o->ms_stop) != 1) {
         ModuleLog(o->i, BLOG_ERROR, "wrong time");
         ModuleLog(o->i, BLOG_ERROR, "wrong time");
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // init timer
     // init timer
@@ -109,8 +103,6 @@ static void func_new (NCDModuleInst *i)
     BReactor_SetTimerAfter(o->i->iparams->reactor, &o->timer, o->ms_start);
     BReactor_SetTimerAfter(o->i->iparams->reactor, &o->timer, o->ms_start);
     return;
     return;
     
     
-fail1:
-    free(o);
 fail0:
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
@@ -118,15 +110,10 @@ fail0:
 
 
 void instance_free (struct instance *o)
 void instance_free (struct instance *o)
 {
 {
-    NCDModuleInst *i = o->i;
-    
     // free timer
     // free timer
     BReactor_RemoveTimer(o->i->iparams->reactor, &o->timer);
     BReactor_RemoveTimer(o->i->iparams->reactor, &o->timer);
     
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 }
 
 
 static void func_die (void *vo)
 static void func_die (void *vo)
@@ -143,8 +130,9 @@ static void func_die (void *vo)
 static const struct NCDModule modules[] = {
 static const struct NCDModule modules[] = {
     {
     {
         .type = "sleep",
         .type = "sleep",
-        .func_new = func_new,
-        .func_die = func_die
+        .func_new2 = func_new,
+        .func_die = func_die,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = NULL
         .type = NULL
     }
     }

+ 17 - 12
ncd/modules/strcmp.c

@@ -44,10 +44,15 @@
 
 
 #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
 #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
 
 
-static void func_new (NCDModuleInst *i)
+struct instance {
+    NCDModuleInst *i;
+    int result;
+};
+
+static void func_new (void *vo, NCDModuleInst *i)
 {
 {
-    // set argument
-    NCDModuleInst_Backend_SetUser(i, i);
+    struct instance *o = vo;
+    o->i = i;
     
     
     // check arguments
     // check arguments
     NCDValRef str1_arg;
     NCDValRef str1_arg;
@@ -61,6 +66,9 @@ static void func_new (NCDModuleInst *i)
         goto fail0;
         goto fail0;
     }
     }
     
     
+    // compare
+    o->result = (NCDVal_Compare(str1_arg, str2_arg) == 0);
+    
     // signal up
     // signal up
     NCDModuleInst_Backend_Up(i);
     NCDModuleInst_Backend_Up(i);
     return;
     return;
@@ -72,18 +80,14 @@ fail0:
 
 
 static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
 static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
 {
 {
-    NCDModuleInst *i = vo;
+    struct instance *o = vo;
     
     
     if (!strcmp(name, "")) {
     if (!strcmp(name, "")) {
-        NCDValRef str1_arg;
-        NCDValRef str2_arg;
-        NCDVal_ListRead(i->args, 2, &str1_arg, &str2_arg);
-        int result = NCDVal_Compare(str1_arg, str2_arg) == 0;
-        const char *v = result ? "true" : "false";
+        const char *v = o->result ? "true" : "false";
         
         
         *out = NCDVal_NewString(mem, v);
         *out = NCDVal_NewString(mem, v);
         if (NCDVal_IsInvalid(*out)) {
         if (NCDVal_IsInvalid(*out)) {
-            ModuleLog(i, BLOG_ERROR, "NCDVal_NewString failed");
+            ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
         }
         }
         return 1;
         return 1;
     }
     }
@@ -94,8 +98,9 @@ static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *o
 static const struct NCDModule modules[] = {
 static const struct NCDModule modules[] = {
     {
     {
         .type = "strcmp",
         .type = "strcmp",
-        .func_new = func_new,
-        .func_getvar = func_getvar
+        .func_new2 = func_new,
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = NULL
         .type = NULL
     }
     }

+ 10 - 22
ncd/modules/try.c

@@ -156,34 +156,28 @@ static void start_terminating (struct instance *o)
     o->state = STATE_DEINIT;
     o->state = STATE_DEINIT;
 }
 }
 
 
-static void func_new (NCDModuleInst *i)
+static void func_new (void *vo, NCDModuleInst *i)
 {
 {
-    // allocate structure
-    struct instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
+    struct instance *o = vo;
     o->i = i;
     o->i = i;
-    NCDModuleInst_Backend_SetUser(i, o);
     
     
     // check arguments
     // check arguments
     NCDValRef template_name_arg;
     NCDValRef template_name_arg;
     NCDValRef args_arg;
     NCDValRef args_arg;
     if (!NCDVal_ListRead(i->args, 2, &template_name_arg, &args_arg)) {
     if (!NCDVal_ListRead(i->args, 2, &template_name_arg, &args_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     }
     if (!NCDVal_IsStringNoNulls(template_name_arg) || !NCDVal_IsList(args_arg)) {
     if (!NCDVal_IsStringNoNulls(template_name_arg) || !NCDVal_IsList(args_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     }
     const char *template_name = NCDVal_StringValue(template_name_arg);
     const char *template_name = NCDVal_StringValue(template_name_arg);
     
     
     // start process
     // start process
     if (!NCDModuleProcess_Init(&o->process, i, template_name, args_arg, o, (NCDModuleProcess_handler_event)process_handler_event)) {
     if (!NCDModuleProcess_Init(&o->process, i, template_name, args_arg, o, (NCDModuleProcess_handler_event)process_handler_event)) {
         ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
         ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // set special object function
     // set special object function
@@ -195,21 +189,14 @@ static void func_new (NCDModuleInst *i)
     o->succeeded = 1;
     o->succeeded = 1;
     return;
     return;
     
     
-fail1:
-    free(o);
 fail0:
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
 }
 }
 
 
 static void instance_free (struct instance *o)
 static void instance_free (struct instance *o)
-{
-    NCDModuleInst *i = o->i;
-    
-    // free structure
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+{   
+    NCDModuleInst_Backend_Dead(o->i);
 }
 }
 
 
 static void func_die (void *vo)
 static void func_die (void *vo)
@@ -291,9 +278,10 @@ fail1:
 static const struct NCDModule modules[] = {
 static const struct NCDModule modules[] = {
     {
     {
         .type = "try",
         .type = "try",
-        .func_new = func_new,
+        .func_new2 = func_new,
         .func_die = func_die,
         .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "try.try::assert",
         .type = "try.try::assert",
         .func_new = assert_func_new
         .func_new = assert_func_new

+ 39 - 50
ncd/modules/value.c

@@ -866,16 +866,10 @@ static void valref_break (struct valref *r)
     r->v = NULL;
     r->v = NULL;
 }
 }
 
 
-static void func_new_common (NCDModuleInst *i, struct value *v, value_deinit_func deinit_func, void *deinit_data)
+static void func_new_common (void *vo, NCDModuleInst *i, struct value *v, value_deinit_func deinit_func, void *deinit_data)
 {
 {
-    // allocate instance
-    struct instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
+    struct instance *o = vo;
     o->i = i;
     o->i = i;
-    NCDModuleInst_Backend_SetUser(i, o);
     
     
     // init value references
     // init value references
     valref_init(&o->ref, v);
     valref_init(&o->ref, v);
@@ -886,33 +880,21 @@ static void func_new_common (NCDModuleInst *i, struct value *v, value_deinit_fun
     
     
     NCDModuleInst_Backend_Up(i);
     NCDModuleInst_Backend_Up(i);
     return;
     return;
-    
-fail0:
-    value_cleanup(v);
-    if (deinit_func) {
-        deinit_func(deinit_data, i);
-    }
-    NCDModuleInst_Backend_SetError(i);
-    NCDModuleInst_Backend_Dead(i);
 }
 }
 
 
 static void func_die (void *vo)
 static void func_die (void *vo)
 {
 {
     struct instance *o = vo;
     struct instance *o = vo;
-    NCDModuleInst *i = o->i;
     
     
     // deinit
     // deinit
     if (o->deinit_func) {
     if (o->deinit_func) {
-        o->deinit_func(o->deinit_data, i);
+        o->deinit_func(o->deinit_data, o->i);
     }
     }
     
     
     // free value reference
     // free value reference
     valref_free(&o->ref);
     valref_free(&o->ref);
     
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 }
 
 
 static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
 static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
@@ -1008,7 +990,7 @@ fail:
     return 1;
     return 1;
 }
 }
 
 
-static void func_new_value (NCDModuleInst *i)
+static void func_new_value (void *vo, NCDModuleInst *i)
 {
 {
     NCDValRef value_arg;
     NCDValRef value_arg;
     if (!NCDVal_ListRead(i->args, 1, &value_arg)) {
     if (!NCDVal_ListRead(i->args, 1, &value_arg)) {
@@ -1021,7 +1003,7 @@ static void func_new_value (NCDModuleInst *i)
         goto fail0;
         goto fail0;
     }
     }
     
     
-    func_new_common(i, v, NULL, NULL);
+    func_new_common(vo, i, v, NULL, NULL);
     return;
     return;
     
     
 fail0:
 fail0:
@@ -1029,7 +1011,7 @@ fail0:
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
 }
 }
 
 
-static void func_new_get (NCDModuleInst *i)
+static void func_new_get (void *vo, NCDModuleInst *i)
 {
 {
     NCDValRef where_arg;
     NCDValRef where_arg;
     if (!NCDVal_ListRead(i->args, 1, &where_arg)) {
     if (!NCDVal_ListRead(i->args, 1, &where_arg)) {
@@ -1050,7 +1032,7 @@ static void func_new_get (NCDModuleInst *i)
         goto fail0;
         goto fail0;
     }
     }
     
     
-    func_new_common(i, v, NULL, NULL);
+    func_new_common(vo, i, v, NULL, NULL);
     return;
     return;
     
     
 fail0:
 fail0:
@@ -1058,7 +1040,7 @@ fail0:
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
 }
 }
 
 
-static void func_new_try_get (NCDModuleInst *i)
+static void func_new_try_get (void *vo, NCDModuleInst *i)
 {
 {
     NCDValRef where_arg;
     NCDValRef where_arg;
     if (!NCDVal_ListRead(i->args, 1, &where_arg)) {
     if (!NCDVal_ListRead(i->args, 1, &where_arg)) {
@@ -1076,7 +1058,7 @@ static void func_new_try_get (NCDModuleInst *i)
     
     
     struct value *v = value_get(i, mov, where_arg, 1);
     struct value *v = value_get(i, mov, where_arg, 1);
     
     
-    func_new_common(i, v, NULL, NULL);
+    func_new_common(vo, i, v, NULL, NULL);
     return;
     return;
     
     
 fail0:
 fail0:
@@ -1084,7 +1066,7 @@ fail0:
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
 }
 }
 
 
-static void func_new_getpath (NCDModuleInst *i)
+static void func_new_getpath (void *vo, NCDModuleInst *i)
 {
 {
     NCDValRef path_arg;
     NCDValRef path_arg;
     if (!NCDVal_ListRead(i->args, 1, &path_arg)) {
     if (!NCDVal_ListRead(i->args, 1, &path_arg)) {
@@ -1109,7 +1091,7 @@ static void func_new_getpath (NCDModuleInst *i)
         goto fail0;
         goto fail0;
     }
     }
     
     
-    func_new_common(i, v, NULL, NULL);
+    func_new_common(vo, i, v, NULL, NULL);
     return;
     return;
     
     
 fail0:
 fail0:
@@ -1117,7 +1099,7 @@ fail0:
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
 }
 }
 
 
-static void func_new_insert (NCDModuleInst *i)
+static void func_new_insert (void *vo, NCDModuleInst *i)
 {
 {
     NCDValRef where_arg;
     NCDValRef where_arg;
     NCDValRef what_arg;
     NCDValRef what_arg;
@@ -1139,7 +1121,7 @@ static void func_new_insert (NCDModuleInst *i)
         goto fail0;
         goto fail0;
     }
     }
     
     
-    func_new_common(i, v, NULL, NULL);
+    func_new_common(vo, i, v, NULL, NULL);
     return;
     return;
     
     
 fail0:
 fail0:
@@ -1193,7 +1175,7 @@ static void insert_undo_deinit_func (struct insert_undo_deinit_data *data, NCDMo
     free(data);
     free(data);
 }
 }
 
 
-static void func_new_insert_undo (NCDModuleInst *i)
+static void func_new_insert_undo (void *vo, NCDModuleInst *i)
 {
 {
     NCDValRef where_arg;
     NCDValRef where_arg;
     NCDValRef what_arg;
     NCDValRef what_arg;
@@ -1225,7 +1207,7 @@ static void func_new_insert_undo (NCDModuleInst *i)
     valref_init(&data->val_ref, v);
     valref_init(&data->val_ref, v);
     valref_init(&data->oldval_ref, oldv);
     valref_init(&data->oldval_ref, oldv);
     
     
-    func_new_common(i, v, (value_deinit_func)insert_undo_deinit_func, data);
+    func_new_common(vo, i, v, (value_deinit_func)insert_undo_deinit_func, data);
     return;
     return;
     
     
 fail1:
 fail1:
@@ -1235,7 +1217,7 @@ fail0:
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
 }
 }
 
 
-static void func_new_substr (NCDModuleInst *i)
+static void func_new_substr (void *vo, NCDModuleInst *i)
 {
 {
     NCDValRef start_arg;
     NCDValRef start_arg;
     NCDValRef length_arg = NCDVal_NewInvalid();
     NCDValRef length_arg = NCDVal_NewInvalid();
@@ -1287,7 +1269,7 @@ static void func_new_substr (NCDModuleInst *i)
         goto fail0;
         goto fail0;
     }
     }
     
     
-    func_new_common(i, v, NULL, NULL);
+    func_new_common(vo, i, v, NULL, NULL);
     return;
     return;
     
     
 fail0:
 fail0:
@@ -1351,39 +1333,45 @@ fail0:
 static const struct NCDModule modules[] = {
 static const struct NCDModule modules[] = {
     {
     {
         .type = "value",
         .type = "value",
-        .func_new = func_new_value,
+        .func_new2 = func_new_value,
         .func_die = func_die,
         .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "value::get",
         .type = "value::get",
         .base_type = "value",
         .base_type = "value",
-        .func_new = func_new_get,
+        .func_new2 = func_new_get,
         .func_die = func_die,
         .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "value::try_get",
         .type = "value::try_get",
         .base_type = "value",
         .base_type = "value",
-        .func_new = func_new_try_get,
+        .func_new2 = func_new_try_get,
         .func_die = func_die,
         .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "value::getpath",
         .type = "value::getpath",
         .base_type = "value",
         .base_type = "value",
-        .func_new = func_new_getpath,
+        .func_new2 = func_new_getpath,
         .func_die = func_die,
         .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "value::insert",
         .type = "value::insert",
         .base_type = "value",
         .base_type = "value",
-        .func_new = func_new_insert,
+        .func_new2 = func_new_insert,
         .func_die = func_die,
         .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "value::insert_undo",
         .type = "value::insert_undo",
         .base_type = "value",
         .base_type = "value",
-        .func_new = func_new_insert_undo,
+        .func_new2 = func_new_insert_undo,
         .func_die = func_die,
         .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "value::remove",
         .type = "value::remove",
         .func_new = remove_func_new
         .func_new = remove_func_new
@@ -1393,9 +1381,10 @@ static const struct NCDModule modules[] = {
     }, {
     }, {
         .type = "value::substr",
         .type = "value::substr",
         .base_type = "value",
         .base_type = "value",
-        .func_new = func_new_substr,
+        .func_new2 = func_new_substr,
         .func_die = func_die,
         .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = NULL
         .type = NULL
     }
     }

+ 9 - 20
ncd/modules/var.c

@@ -52,22 +52,16 @@ struct instance {
     NCDValRef value;
     NCDValRef value;
 };
 };
 
 
-static void func_new (NCDModuleInst *i)
+static void func_new (void *vo, NCDModuleInst *i)
 {
 {
-    // allocate instance
-    struct instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
+    struct instance *o = vo;
     o->i = i;
     o->i = i;
-    NCDModuleInst_Backend_SetUser(i, o);
     
     
     // read argument
     // read argument
     NCDValRef value_arg;
     NCDValRef value_arg;
     if (!NCDVal_ListRead(i->args, 1, &value_arg)) {
     if (!NCDVal_ListRead(i->args, 1, &value_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // init mem
     // init mem
@@ -77,17 +71,15 @@ static void func_new (NCDModuleInst *i)
     o->value = NCDVal_NewCopy(&o->mem, value_arg);
     o->value = NCDVal_NewCopy(&o->mem, value_arg);
     if (NCDVal_IsInvalid(o->value)) {
     if (NCDVal_IsInvalid(o->value)) {
         ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewCopy failed");
         ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewCopy failed");
-        goto fail2;
+        goto fail1;
     }
     }
     
     
     // signal up
     // signal up
     NCDModuleInst_Backend_Up(o->i);
     NCDModuleInst_Backend_Up(o->i);
     return;
     return;
     
     
-fail2:
-    NCDValMem_Free(&o->mem);
 fail1:
 fail1:
-    free(o);
+    NCDValMem_Free(&o->mem);
 fail0:
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
@@ -96,15 +88,11 @@ fail0:
 static void func_die (void *vo)
 static void func_die (void *vo)
 {
 {
     struct instance *o = vo;
     struct instance *o = vo;
-    NCDModuleInst *i = o->i;
     
     
     // free mem
     // free mem
     NCDValMem_Free(&o->mem);
     NCDValMem_Free(&o->mem);
     
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 }
 
 
 static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
 static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
@@ -164,9 +152,10 @@ fail0:
 static const struct NCDModule modules[] = {
 static const struct NCDModule modules[] = {
     {
     {
         .type = "var",
         .type = "var",
-        .func_new = func_new,
+        .func_new2 = func_new,
         .func_die = func_die,
         .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "var::set",
         .type = "var::set",
         .func_new = set_func_new
         .func_new = set_func_new