Просмотр исходного кода

ncd: modules: port the rest of modules to func_new2

ambrop7 13 лет назад
Родитель
Сommit
630ff9fa6a

+ 19 - 44
ncd/modules/dynamic_depend.c

@@ -268,35 +268,27 @@ static int func_globalinit (struct NCDModuleInitParams params)
     return 1;
     return 1;
 }
 }
 
 
-static void provide_func_new (NCDModuleInst *i)
+static void provide_func_new (void *vo, NCDModuleInst *i)
 {
 {
-    // allocate instance
-    struct provide *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
-    NCDModuleInst_Backend_SetUser(i, o);
-    
-    // init arguments
+    struct provide *o = vo;
     o->i = i;
     o->i = i;
     
     
     // read arguments
     // read arguments
     NCDValRef name_arg;
     NCDValRef name_arg;
     if (!NCDVal_ListRead(i->args, 2, &name_arg, &o->order_value)) {
     if (!NCDVal_ListRead(i->args, 2, &name_arg, &o->order_value)) {
         ModuleLog(i, BLOG_ERROR, "wrong arity");
         ModuleLog(i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     }
     if (!NCDVal_IsStringNoNulls(name_arg)) {
     if (!NCDVal_IsStringNoNulls(name_arg)) {
         ModuleLog(i, BLOG_ERROR, "wrong type");
         ModuleLog(i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     }
     const char *name_str = NCDVal_StringValue(name_arg);
     const char *name_str = NCDVal_StringValue(name_arg);
     
     
     // find name, create new if needed
     // find name, create new if needed
     struct name *n = find_name(name_str);
     struct name *n = find_name(name_str);
     if (!n && !(n = name_init(i, name_str))) {
     if (!n && !(n = name_init(i, name_str))) {
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // set name
     // set name
@@ -305,7 +297,7 @@ static void provide_func_new (NCDModuleInst *i)
     // check for order value conflict
     // check for order value conflict
     if (BAVL_LookupExact(&n->provides_tree, &o->order_value)) {
     if (BAVL_LookupExact(&n->provides_tree, &o->order_value)) {
         ModuleLog(i, BLOG_ERROR, "order value already exists");
         ModuleLog(i, BLOG_ERROR, "order value already exists");
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // add to name's provides tree
     // add to name's provides tree
@@ -329,8 +321,6 @@ static void provide_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);
@@ -342,15 +332,10 @@ static void provide_free (struct provide *o)
     ASSERT(o->dying)
     ASSERT(o->dying)
     ASSERT(o != n->cur_p)
     ASSERT(o != n->cur_p)
     
     
-    NCDModuleInst *i = o->i;
-    
     // remove from name's provides tree
     // remove from name's provides tree
     BAVL_Remove(&n->provides_tree, &o->provides_tree_node);
     BAVL_Remove(&n->provides_tree, &o->provides_tree_node);
     
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 }
 
 
 static void provide_func_die (void *vo)
 static void provide_func_die (void *vo)
@@ -378,33 +363,27 @@ static void provide_func_die (void *vo)
     name_start_resetting(n);
     name_start_resetting(n);
 }
 }
 
 
-static void depend_func_new (NCDModuleInst *i)
+static void depend_func_new (void *vo, NCDModuleInst *i)
 {
 {
-    // allocate instance
-    struct depend *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
+    struct depend *o = vo;
     o->i = i;
     o->i = i;
-    NCDModuleInst_Backend_SetUser(i, o);
     
     
     // read arguments
     // read arguments
     NCDValRef name_arg;
     NCDValRef name_arg;
     if (!NCDVal_ListRead(i->args, 1, &name_arg)) {
     if (!NCDVal_ListRead(i->args, 1, &name_arg)) {
         ModuleLog(i, BLOG_ERROR, "wrong arity");
         ModuleLog(i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     }
     if (!NCDVal_IsStringNoNulls(name_arg)) {
     if (!NCDVal_IsStringNoNulls(name_arg)) {
         ModuleLog(i, BLOG_ERROR, "wrong type");
         ModuleLog(i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     }
     const char *name_str = NCDVal_StringValue(name_arg);
     const char *name_str = NCDVal_StringValue(name_arg);
     
     
     // find name, create new if needed
     // find name, create new if needed
     struct name *n = find_name(name_str);
     struct name *n = find_name(name_str);
     if (!n && !(n = name_init(i, name_str))) {
     if (!n && !(n = name_init(i, name_str))) {
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // set name
     // set name
@@ -429,8 +408,6 @@ static void depend_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);
@@ -439,7 +416,6 @@ fail0:
 static void depend_func_die (void *vo)
 static void depend_func_die (void *vo)
 {
 {
     struct depend *o = vo;
     struct depend *o = vo;
-    NCDModuleInst *i = o->i;
     struct name *n = o->n;
     struct name *n = o->n;
     
     
     if (o->is_bound) {
     if (o->is_bound) {
@@ -460,10 +436,7 @@ static void depend_func_die (void *vo)
         name_free_if_unused(n);
         name_free_if_unused(n);
     }
     }
     
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 }
 
 
 static void depend_func_clean (void *vo)
 static void depend_func_clean (void *vo)
@@ -505,15 +478,17 @@ static int depend_func_getobj (void *vo, const char *objname, NCDObject *out_obj
 static const struct NCDModule modules[] = {
 static const struct NCDModule modules[] = {
     {
     {
         .type = "dynamic_provide",
         .type = "dynamic_provide",
-        .func_new = provide_func_new,
-        .func_die = provide_func_die
+        .func_new2 = provide_func_new,
+        .func_die = provide_func_die,
+        .alloc_size = sizeof(struct provide)
     }, {
     }, {
         .type = "dynamic_depend",
         .type = "dynamic_depend",
-        .func_new = depend_func_new,
+        .func_new2 = depend_func_new,
         .func_die = depend_func_die,
         .func_die = depend_func_die,
         .func_clean = depend_func_clean,
         .func_clean = depend_func_clean,
         .func_getobj = depend_func_getobj,
         .func_getobj = depend_func_getobj,
-        .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN
+        .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
+        .alloc_size = sizeof(struct depend)
     }, {
     }, {
         .type = NULL
         .type = NULL
     }
     }

+ 10 - 22
ncd/modules/imperative.c

@@ -224,16 +224,10 @@ static void deinit_timer_handler (struct instance *o)
     o->state = STATE_DEINIT_CLEANING;
     o->state = STATE_DEINIT_CLEANING;
 }
 }
 
 
-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 init_template_arg;
     NCDValRef init_template_arg;
@@ -242,13 +236,13 @@ static void func_new (NCDModuleInst *i)
     NCDValRef deinit_timeout_arg;
     NCDValRef deinit_timeout_arg;
     if (!NCDVal_ListRead(i->args, 5, &init_template_arg, &init_args, &deinit_template_arg, &o->deinit_args, &deinit_timeout_arg)) {
     if (!NCDVal_ListRead(i->args, 5, &init_template_arg, &init_args, &deinit_template_arg, &o->deinit_args, &deinit_timeout_arg)) {
         ModuleLog(i, BLOG_ERROR, "wrong arity");
         ModuleLog(i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     }
     if (!NCDVal_IsStringNoNulls(init_template_arg) || !NCDVal_IsList(init_args)  ||
     if (!NCDVal_IsStringNoNulls(init_template_arg) || !NCDVal_IsList(init_args)  ||
         !NCDVal_IsStringNoNulls(deinit_template_arg) || !NCDVal_IsList(o->deinit_args) ||
         !NCDVal_IsStringNoNulls(deinit_template_arg) || !NCDVal_IsList(o->deinit_args) ||
         !NCDVal_IsStringNoNulls(deinit_timeout_arg)) {
         !NCDVal_IsStringNoNulls(deinit_timeout_arg)) {
         ModuleLog(i, BLOG_ERROR, "wrong type");
         ModuleLog(i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     }
     const char *init_template = NCDVal_StringValue(init_template_arg);
     const char *init_template = NCDVal_StringValue(init_template_arg);
     o->deinit_template = NCDVal_StringValue(deinit_template_arg);
     o->deinit_template = NCDVal_StringValue(deinit_template_arg);
@@ -257,7 +251,7 @@ static void func_new (NCDModuleInst *i)
     uintmax_t timeout;
     uintmax_t timeout;
     if (!parse_unsigned_integer(NCDVal_StringValue(deinit_timeout_arg), &timeout) || timeout > UINT64_MAX) {
     if (!parse_unsigned_integer(NCDVal_StringValue(deinit_timeout_arg), &timeout) || timeout > UINT64_MAX) {
         ModuleLog(i, BLOG_ERROR, "wrong timeout");
         ModuleLog(i, BLOG_ERROR, "wrong timeout");
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // init timer
     // init timer
@@ -272,7 +266,7 @@ static void func_new (NCDModuleInst *i)
     } else {
     } else {
         // start init process
         // start init process
         if (!start_process(o, init_template, init_args, (NCDModuleProcess_handler_event)init_process_handler_event)) {
         if (!start_process(o, init_template, init_args, (NCDModuleProcess_handler_event)init_process_handler_event)) {
-            goto fail1;
+            goto fail0;
         }
         }
         
         
         // set state init working
         // set state init working
@@ -283,8 +277,6 @@ static void func_new (NCDModuleInst *i)
     o->dying = 0;
     o->dying = 0;
     return;
     return;
     
     
-fail1:
-    free(o);
 fail0:
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
@@ -292,12 +284,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)
@@ -317,8 +304,9 @@ static void func_die (void *vo)
 static const struct NCDModule modules[] = {
 static const struct NCDModule modules[] = {
     {
     {
         .type = "imperative",
         .type = "imperative",
-        .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/index.c

@@ -60,30 +60,19 @@ struct instance {
     size_t value;
     size_t value;
 };
 };
 
 
-static void func_new_templ (NCDModuleInst *i, size_t value)
+static void func_new_templ (void *vo, NCDModuleInst *i, size_t value)
 {
 {
-    // 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);
     
     
     // set value
     // set value
     o->value = value;
     o->value = value;
     
     
     // 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 func_new_from_value (NCDModuleInst *i)
+static void func_new_from_value (void *vo, NCDModuleInst *i)
 {
 {
     // read arguments
     // read arguments
     NCDValRef arg_value;
     NCDValRef arg_value;
@@ -109,7 +98,7 @@ static void func_new_from_value (NCDModuleInst *i)
         goto fail0;
         goto fail0;
     }
     }
     
     
-    func_new_templ(i, value);
+    func_new_templ(vo, i, value);
     return;
     return;
     
     
 fail0:
 fail0:
@@ -117,7 +106,7 @@ fail0:
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
 }
 }
 
 
-static void func_new_from_index (NCDModuleInst *i)
+static void func_new_from_index (void *vo, NCDModuleInst *i)
 {
 {
     struct instance *index = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
     struct instance *index = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
     
     
@@ -127,7 +116,7 @@ static void func_new_from_index (NCDModuleInst *i)
         goto fail0;
         goto fail0;
     }
     }
     
     
-    func_new_templ(i, index->value + 1);
+    func_new_templ(vo, i, index->value + 1);
     return;
     return;
     
     
 fail0:
 fail0:
@@ -138,12 +127,8 @@ 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 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)
@@ -167,15 +152,17 @@ static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *o
 static const struct NCDModule modules[] = {
 static const struct NCDModule modules[] = {
     {
     {
         .type = "index",
         .type = "index",
-        .func_new = func_new_from_value,
+        .func_new2 = func_new_from_value,
         .func_die = func_die,
         .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "index::next",
         .type = "index::next",
         .base_type = "index",
         .base_type = "index",
-        .func_new = func_new_from_index,
+        .func_new2 = func_new_from_index,
         .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
     }
     }

+ 49 - 118
ncd/modules/list.c

@@ -316,16 +316,10 @@ fail:
     return 0;
     return 0;
 }
 }
 
 
-static void func_new_list (NCDModuleInst *i)
+static void func_new_list (void *vo, NCDModuleInst *i)
 {
 {
-    // allocate instance
-    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);
     
     
     // init list
     // init list
     IndexedList_Init(&o->il);
     IndexedList_Init(&o->il);
@@ -341,22 +335,14 @@ static void func_new_list (NCDModuleInst *i)
     
     
 fail1:
 fail1:
     cut_list_front(o, 0);
     cut_list_front(o, 0);
-    free(o);
-fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
 }
 }
 
 
-static void func_new_listfrom (NCDModuleInst *i)
+static void func_new_listfrom (void *vo, NCDModuleInst *i)
 {
 {
-    // allocate instance
-    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);
     
     
     // init list
     // init list
     IndexedList_Init(&o->il);
     IndexedList_Init(&o->il);
@@ -372,8 +358,6 @@ static void func_new_listfrom (NCDModuleInst *i)
     
     
 fail1:
 fail1:
     cut_list_front(o, 0);
     cut_list_front(o, 0);
-    free(o);
-fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
 }
 }
@@ -381,15 +365,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 list elements
     // free list elements
     cut_list_front(o, 0);
     cut_list_front(o, 0);
     
     
-    // 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)
@@ -474,23 +454,15 @@ fail0:
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
 }
 }
 
 
-static void length_func_new (NCDModuleInst *i)
+static void length_func_new (void *vo, NCDModuleInst *i)
 {
 {
-    // allocate instance
-    struct length_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 length_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;
     }
     }
     
     
     // get method object
     // get method object
@@ -504,8 +476,6 @@ static void length_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);
@@ -514,12 +484,8 @@ fail0:
 static void length_func_die (void *vo)
 static void length_func_die (void *vo)
 {
 {
     struct length_instance *o = vo;
     struct length_instance *o = vo;
-    NCDModuleInst *i = o->i;
     
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 }
 
 
 static int length_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
 static int length_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
@@ -540,33 +506,25 @@ static int length_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDVa
     return 0;
     return 0;
 }
 }
     
     
-static void get_func_new (NCDModuleInst *i)
+static void get_func_new (void *vo, NCDModuleInst *i)
 {
 {
-    // allocate instance
-    struct get_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 get_instance *o = vo;
     o->i = i;
     o->i = i;
     
     
     // check arguments
     // check arguments
     NCDValRef index_arg;
     NCDValRef index_arg;
     if (!NCDVal_ListRead(o->i->args, 1, &index_arg)) {
     if (!NCDVal_ListRead(o->i->args, 1, &index_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     }
     if (!NCDVal_IsStringNoNulls(index_arg)) {
     if (!NCDVal_IsStringNoNulls(index_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     }
     uintmax_t index;
     uintmax_t index;
     if (!parse_unsigned_integer(NCDVal_StringValue(index_arg), &index)) {
     if (!parse_unsigned_integer(NCDVal_StringValue(index_arg), &index)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong value");
         ModuleLog(o->i, BLOG_ERROR, "wrong value");
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // get method object
     // get method object
@@ -575,7 +533,7 @@ static void get_func_new (NCDModuleInst *i)
     // check index
     // check index
     if (index >= list_count(mo)) {
     if (index >= list_count(mo)) {
         ModuleLog(o->i, BLOG_ERROR, "no element at index %"PRIuMAX, index);
         ModuleLog(o->i, BLOG_ERROR, "no element at index %"PRIuMAX, index);
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // get element
     // get element
@@ -588,17 +546,15 @@ static void get_func_new (NCDModuleInst *i)
     o->val = NCDVal_NewCopy(&o->mem, e->val);
     o->val = NCDVal_NewCopy(&o->mem, e->val);
     if (NCDVal_IsInvalid(o->val)) {
     if (NCDVal_IsInvalid(o->val)) {
         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);
@@ -607,15 +563,11 @@ fail0:
 static void get_func_die (void *vo)
 static void get_func_die (void *vo)
 {
 {
     struct get_instance *o = vo;
     struct get_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 get_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
 static int get_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
@@ -662,24 +614,16 @@ fail0:
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
 }
 }
 
 
-static void contains_func_new (NCDModuleInst *i)
+static void contains_func_new (void *vo, NCDModuleInst *i)
 {
 {
-    // allocate instance
-    struct contains_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 contains_instance *o = vo;
     o->i = i;
     o->i = i;
     
     
     // read arguments
     // read arguments
     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;
     }
     }
     
     
     // get method object
     // get method object
@@ -692,8 +636,6 @@ static void contains_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);
@@ -702,12 +644,8 @@ fail0:
 static void contains_func_die (void *vo)
 static void contains_func_die (void *vo)
 {
 {
     struct contains_instance *o = vo;
     struct contains_instance *o = vo;
-    NCDModuleInst *i = o->i;
     
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 }
 
 
 static int contains_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
 static int contains_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
@@ -727,17 +665,9 @@ static int contains_func_getvar (void *vo, const char *name, NCDValMem *mem, NCD
     return 0;
     return 0;
 }
 }
 
 
-static void find_func_new (NCDModuleInst *i)
+static void find_func_new (void *vo, NCDModuleInst *i)
 {
 {
-    // allocate instance
-    struct find_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 find_instance *o = vo;
     o->i = i;
     o->i = i;
     
     
     // read arguments
     // read arguments
@@ -745,18 +675,18 @@ static void find_func_new (NCDModuleInst *i)
     NCDValRef value_arg;
     NCDValRef value_arg;
     if (!NCDVal_ListRead(i->args, 2, &start_pos_arg, &value_arg)) {
     if (!NCDVal_ListRead(i->args, 2, &start_pos_arg, &value_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     }
     if (!NCDVal_IsStringNoNulls(start_pos_arg)) {
     if (!NCDVal_IsStringNoNulls(start_pos_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // read start position
     // read start position
     uintmax_t start_pos;
     uintmax_t start_pos;
     if (!parse_unsigned_integer(NCDVal_StringValue(start_pos_arg), &start_pos) || start_pos > UINT64_MAX) {
     if (!parse_unsigned_integer(NCDVal_StringValue(start_pos_arg), &start_pos) || start_pos > UINT64_MAX) {
         ModuleLog(o->i, BLOG_ERROR, "wrong start pos");
         ModuleLog(o->i, BLOG_ERROR, "wrong start pos");
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // get method object
     // get method object
@@ -769,8 +699,6 @@ static void find_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);
@@ -779,12 +707,8 @@ fail0:
 static void find_func_die (void *vo)
 static void find_func_die (void *vo)
 {
 {
     struct find_instance *o = vo;
     struct find_instance *o = vo;
-    NCDModuleInst *i = o->i;
-    
-    // free instance
-    free(o);
     
     
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 }
 
 
 static int find_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
 static int find_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
@@ -920,21 +844,24 @@ fail0:
 static const struct NCDModule modules[] = {
 static const struct NCDModule modules[] = {
     {
     {
         .type = "list",
         .type = "list",
-        .func_new = func_new_list,
+        .func_new2 = func_new_list,
         .func_die = func_die,
         .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "listfrom",
         .type = "listfrom",
         .base_type = "list",
         .base_type = "list",
-        .func_new = func_new_listfrom,
+        .func_new2 = func_new_listfrom,
         .func_die = func_die,
         .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "concatlist", // alias for listfrom
         .type = "concatlist", // alias for listfrom
         .base_type = "list",
         .base_type = "list",
-        .func_new = func_new_listfrom,
+        .func_new2 = func_new_listfrom,
         .func_die = func_die,
         .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "list::append",
         .type = "list::append",
         .func_new = append_func_new
         .func_new = append_func_new
@@ -943,27 +870,31 @@ static const struct NCDModule modules[] = {
         .func_new = appendv_func_new
         .func_new = appendv_func_new
     }, {
     }, {
         .type = "list::length",
         .type = "list::length",
-        .func_new = length_func_new,
+        .func_new2 = length_func_new,
         .func_die = length_func_die,
         .func_die = length_func_die,
-        .func_getvar = length_func_getvar
+        .func_getvar = length_func_getvar,
+        .alloc_size = sizeof(struct length_instance)
     }, {
     }, {
         .type = "list::get",
         .type = "list::get",
-        .func_new = get_func_new,
+        .func_new2 = get_func_new,
         .func_die = get_func_die,
         .func_die = get_func_die,
-        .func_getvar = get_func_getvar
+        .func_getvar = get_func_getvar,
+        .alloc_size = sizeof(struct get_instance)
     }, {
     }, {
         .type = "list::shift",
         .type = "list::shift",
         .func_new = shift_func_new
         .func_new = shift_func_new
     }, {
     }, {
         .type = "list::contains",
         .type = "list::contains",
-        .func_new = contains_func_new,
+        .func_new2 = contains_func_new,
         .func_die = contains_func_die,
         .func_die = contains_func_die,
-        .func_getvar = contains_func_getvar
+        .func_getvar = contains_func_getvar,
+        .alloc_size = sizeof(struct contains_instance)
     }, {
     }, {
         .type = "list::find",
         .type = "list::find",
-        .func_new = find_func_new,
+        .func_new2 = find_func_new,
         .func_die = find_func_die,
         .func_die = find_func_die,
-        .func_getvar = find_func_getvar
+        .func_getvar = find_func_getvar,
+        .alloc_size = sizeof(struct find_instance)
     }, {
     }, {
         .type = "list::remove_at",
         .type = "list::remove_at",
         .func_new = removeat_func_new
         .func_new = removeat_func_new

+ 17 - 44
ncd/modules/ondemand.c

@@ -208,17 +208,9 @@ static void ondemand_process_handler (struct ondemand *o, int event)
     }
     }
 }
 }
 
 
-static void ondemand_func_new (NCDModuleInst *i)
+static void ondemand_func_new (void *vo, NCDModuleInst *i)
 {
 {
-    // allocate instance
-    struct ondemand *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
-    NCDModuleInst_Backend_SetUser(i, o);
-    
-    // init arguments
+    struct ondemand *o = vo;
     o->i = i;
     o->i = i;
     
     
     // read arguments
     // read arguments
@@ -226,11 +218,11 @@ static void ondemand_func_new (NCDModuleInst *i)
     NCDValRef arg_args;
     NCDValRef arg_args;
     if (!NCDVal_ListRead(i->args, 2, &arg_template_name, &arg_args)) {
     if (!NCDVal_ListRead(i->args, 2, &arg_template_name, &arg_args)) {
         ModuleLog(i, BLOG_ERROR, "wrong arity");
         ModuleLog(i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     }
     if (!NCDVal_IsStringNoNulls(arg_template_name) || !NCDVal_IsList(arg_args)) {
     if (!NCDVal_IsStringNoNulls(arg_template_name) || !NCDVal_IsList(arg_args)) {
         ModuleLog(i, BLOG_ERROR, "wrong type");
         ModuleLog(i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     }
     o->template_name = NCDVal_StringValue(arg_template_name);
     o->template_name = NCDVal_StringValue(arg_template_name);
     o->args = arg_args;
     o->args = arg_args;
@@ -248,8 +240,6 @@ static void ondemand_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);
@@ -258,7 +248,6 @@ fail0:
 static void ondemand_free (struct ondemand *o)
 static void ondemand_free (struct ondemand *o)
 {
 {
     ASSERT(!o->have_process)
     ASSERT(!o->have_process)
-    NCDModuleInst *i = o->i;
     
     
     // die demands
     // die demands
     while (!LinkedList1_IsEmpty(&o->demands_list)) {
     while (!LinkedList1_IsEmpty(&o->demands_list)) {
@@ -267,10 +256,7 @@ static void ondemand_free (struct ondemand *o)
         demand_free(demand);
         demand_free(demand);
     }
     }
     
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 }
 
 
 static void ondemand_func_die (void *vo)
 static void ondemand_func_die (void *vo)
@@ -293,23 +279,15 @@ static void ondemand_func_die (void *vo)
     }
     }
 }
 }
 
 
-static void demand_func_new (NCDModuleInst *i)
+static void demand_func_new (void *vo, NCDModuleInst *i)
 {
 {
-    // allocate instance
-    struct demand *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
-    NCDModuleInst_Backend_SetUser(i, o);
-    
-    // init arguments
+    struct demand *o = vo;
     o->i = i;
     o->i = i;
     
     
     // read arguments
     // read 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;
     }
     }
     
     
     // set ondemand
     // set ondemand
@@ -323,7 +301,7 @@ static void demand_func_new (NCDModuleInst *i)
         ASSERT(!o->od->dying)
         ASSERT(!o->od->dying)
         
         
         if (!ondemand_start_process(o->od)) {
         if (!ondemand_start_process(o->od)) {
-            goto fail2;
+            goto fail1;
         }
         }
     }
     }
     
     
@@ -334,10 +312,8 @@ static void demand_func_new (NCDModuleInst *i)
     
     
     return;
     return;
     
     
-fail2:
-    LinkedList1_Remove(&o->od->demands_list, &o->demands_list_node);
 fail1:
 fail1:
-    free(o);
+    LinkedList1_Remove(&o->od->demands_list, &o->demands_list_node);
 fail0:
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
@@ -345,8 +321,6 @@ fail0:
 
 
 static void demand_free (struct demand *o)
 static void demand_free (struct demand *o)
 {
 {
-    NCDModuleInst *i = o->i;
-    
     // remove from ondemand's demands list
     // remove from ondemand's demands list
     LinkedList1_Remove(&o->od->demands_list, &o->demands_list_node);
     LinkedList1_Remove(&o->od->demands_list, &o->demands_list_node);
     
     
@@ -355,10 +329,7 @@ static void demand_free (struct demand *o)
         ondemand_terminate_process(o->od);
         ondemand_terminate_process(o->od);
     }
     }
     
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 }
 
 
 static void demand_func_die (void *vo)
 static void demand_func_die (void *vo)
@@ -380,13 +351,15 @@ static int demand_func_getobj (void *vo, const char *objname, NCDObject *out_obj
 static const struct NCDModule modules[] = {
 static const struct NCDModule modules[] = {
     {
     {
         .type = "ondemand",
         .type = "ondemand",
-        .func_new = ondemand_func_new,
-        .func_die = ondemand_func_die
+        .func_new2 = ondemand_func_new,
+        .func_die = ondemand_func_die,
+        .alloc_size = sizeof(struct ondemand)
     }, {
     }, {
         .type = "ondemand::demand",
         .type = "ondemand::demand",
-        .func_new = demand_func_new,
+        .func_new2 = demand_func_new,
         .func_die = demand_func_die,
         .func_die = demand_func_die,
-        .func_getobj = demand_func_getobj
+        .func_getobj = demand_func_getobj,
+        .alloc_size = sizeof(struct demand)
     }, {
     }, {
         .type = NULL
         .type = NULL
     }
     }

+ 21 - 47
ncd/modules/ref.c

@@ -67,23 +67,15 @@ struct ref_instance {
 
 
 static void ref_instance_free (struct ref_instance *o);
 static void ref_instance_free (struct ref_instance *o);
 
 
-static void refhere_func_new (NCDModuleInst *i)
+static void refhere_func_new (void *vo, NCDModuleInst *i)
 {
 {
-    // allocate instance
-    struct refhere_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 refhere_instance *o = vo;
     o->i = i;
     o->i = i;
     
     
     // check arguments
     // check arguments
     if (!NCDVal_ListRead(i->args, 0)) {
     if (!NCDVal_ListRead(i->args, 0)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // init refs list
     // init refs list
@@ -93,8 +85,6 @@ static void refhere_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);
@@ -103,7 +93,6 @@ fail0:
 static void refhere_func_die (void *vo)
 static void refhere_func_die (void *vo)
 {
 {
     struct refhere_instance *o = vo;
     struct refhere_instance *o = vo;
-    NCDModuleInst *i = o->i;
     
     
     // die refs
     // die refs
     while (!LinkedList0_IsEmpty(&o->refs_list)) {
     while (!LinkedList0_IsEmpty(&o->refs_list)) {
@@ -112,10 +101,7 @@ static void refhere_func_die (void *vo)
         ref_instance_free(ref);
         ref_instance_free(ref);
     }
     }
     
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 }
 
 
 static int refhere_func_getobj (void *vo, const char *objname, NCDObject *out_object)
 static int refhere_func_getobj (void *vo, const char *objname, NCDObject *out_object)
@@ -131,23 +117,15 @@ static int refhere_func_getobj (void *vo, const char *objname, NCDObject *out_ob
     return NCDModuleInst_Backend_GetObj(o->i, objname, out_object);
     return NCDModuleInst_Backend_GetObj(o->i, objname, out_object);
 }
 }
 
 
-static void ref_func_new_templ (NCDModuleInst *i, struct refhere_instance *rh)
+static void ref_func_new_templ (void *vo, NCDModuleInst *i, struct refhere_instance *rh)
 {
 {
-    // allocate instance
-    struct ref_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 ref_instance *o = vo;
     o->i = i;
     o->i = i;
     
     
     // check arguments
     // check arguments
     if (!NCDVal_ListRead(i->args, 0)) {
     if (!NCDVal_ListRead(i->args, 0)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // set refhere
     // set refhere
@@ -160,38 +138,31 @@ static void ref_func_new_templ (NCDModuleInst *i, struct refhere_instance *rh)
     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 ref_func_new_from_refhere (NCDModuleInst *i)
+static void ref_func_new_from_refhere (void *vo, NCDModuleInst *i)
 {
 {
     struct refhere_instance *rh = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
     struct refhere_instance *rh = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
     
     
-    return ref_func_new_templ(i, rh);
+    return ref_func_new_templ(vo, i, rh);
 }
 }
 
 
-static void ref_func_new_from_ref (NCDModuleInst *i)
+static void ref_func_new_from_ref (void *vo, NCDModuleInst *i)
 {
 {
     struct ref_instance *ref = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
     struct ref_instance *ref = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
     
     
-    return ref_func_new_templ(i, ref->rh);
+    return ref_func_new_templ(vo, i, ref->rh);
 }
 }
 
 
 static void ref_instance_free (struct ref_instance *o)
 static void ref_instance_free (struct ref_instance *o)
 {
 {
-    NCDModuleInst *i = o->i;
-    
     // remove from refhere's reft list
     // remove from refhere's reft list
     LinkedList0_Remove(&o->rh->refs_list, &o->refs_list_node);
     LinkedList0_Remove(&o->rh->refs_list, &o->refs_list_node);
     
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 }
 
 
 static void ref_func_die (void *vo)
 static void ref_func_die (void *vo)
@@ -217,21 +188,24 @@ static int ref_func_getobj (void *vo, const char *objname, NCDObject *out_object
 static const struct NCDModule modules[] = {
 static const struct NCDModule modules[] = {
     {
     {
         .type = "refhere",
         .type = "refhere",
-        .func_new = refhere_func_new,
+        .func_new2 = refhere_func_new,
         .func_die = refhere_func_die,
         .func_die = refhere_func_die,
-        .func_getobj = refhere_func_getobj
+        .func_getobj = refhere_func_getobj,
+        .alloc_size = sizeof(struct refhere_instance)
     }, {
     }, {
         .type = "refhere::ref",
         .type = "refhere::ref",
         .base_type = "ref",
         .base_type = "ref",
-        .func_new = ref_func_new_from_refhere,
+        .func_new2 = ref_func_new_from_refhere,
         .func_die = ref_func_die,
         .func_die = ref_func_die,
-        .func_getobj = ref_func_getobj
+        .func_getobj = ref_func_getobj,
+        .alloc_size = sizeof(struct ref_instance)
     }, {
     }, {
         .type = "ref::ref",
         .type = "ref::ref",
         .base_type = "ref",
         .base_type = "ref",
-        .func_new = ref_func_new_from_ref,
+        .func_new2 = ref_func_new_from_ref,
         .func_die = ref_func_die,
         .func_die = ref_func_die,
-        .func_getobj = ref_func_getobj
+        .func_getobj = ref_func_getobj,
+        .alloc_size = sizeof(struct ref_instance)
     }, {
     }, {
         .type = NULL
         .type = NULL
     }
     }

+ 22 - 45
ncd/modules/sys_request_client.c

@@ -541,28 +541,22 @@ bad:
     return 0;
     return 0;
 }
 }
 
 
-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, "malloc failed");
-        goto fail0;
-    }
+    struct instance *o = vo;
     o->i = i;
     o->i = i;
-    NCDModuleInst_Backend_SetUser(i, o);
     
     
     // check arguments
     // check arguments
     NCDValRef connect_addr_arg;
     NCDValRef connect_addr_arg;
     if (!NCDVal_ListRead(i->args, 1, &connect_addr_arg)) {
     if (!NCDVal_ListRead(i->args, 1, &connect_addr_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // get address
     // get address
     struct NCDRequestClient_addr addr;
     struct NCDRequestClient_addr addr;
     if (!get_connect_addr(o, connect_addr_arg, &addr)) {
     if (!get_connect_addr(o, connect_addr_arg, &addr)) {
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // init client
     // init client
@@ -570,7 +564,7 @@ static void func_new (NCDModuleInst *i)
         (NCDRequestClient_handler_error)client_handler_error,
         (NCDRequestClient_handler_error)client_handler_error,
         (NCDRequestClient_handler_connected)client_handler_connected)) {
         (NCDRequestClient_handler_connected)client_handler_connected)) {
         ModuleLog(o->i, BLOG_ERROR, "NCDRequestClient_Init failed");
         ModuleLog(o->i, BLOG_ERROR, "NCDRequestClient_Init failed");
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // init requests list
     // init requests list
@@ -580,8 +574,6 @@ static void func_new (NCDModuleInst *i)
     o->state = CSTATE_CONNECTING;
     o->state = CSTATE_CONNECTING;
     return;
     return;
     
     
-fail1:
-    free(o);
 fail0:
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
@@ -589,8 +581,6 @@ fail0:
 
 
 static void instance_free (struct instance *o, int with_error)
 static void instance_free (struct instance *o, int with_error)
 {
 {
-    NCDModuleInst *i = o->i;
-    
     // deal with requests
     // deal with requests
     LinkedList0Node *ln;
     LinkedList0Node *ln;
     while (ln = LinkedList0_GetFirst(&o->requests_list)) {
     while (ln = LinkedList0_GetFirst(&o->requests_list)) {
@@ -601,13 +591,10 @@ static void instance_free (struct instance *o, int with_error)
     // free client
     // free client
     NCDRequestClient_Free(&o->client);
     NCDRequestClient_Free(&o->client);
     
     
-    // free structure
-    free(o);
-    
     if (with_error) {
     if (with_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)
@@ -617,16 +604,10 @@ static void func_die (void *vo)
     instance_free(o, 0);
     instance_free(o, 0);
 }
 }
 
 
-static void request_func_new (NCDModuleInst *i)
+static void request_func_new (void *vo, NCDModuleInst *i)
 {
 {
-    // allocate structure
-    struct request_instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "malloc failed");
-        goto fail0;
-    }
+    struct request_instance *o = vo;
     o->i = i;
     o->i = i;
-    NCDModuleInst_Backend_SetUser(i, o);
     
     
     // check arguments
     // check arguments
     NCDValRef request_data_arg;
     NCDValRef request_data_arg;
@@ -635,13 +616,13 @@ static void request_func_new (NCDModuleInst *i)
     NCDValRef args_arg;
     NCDValRef args_arg;
     if (!NCDVal_ListRead(i->args, 4, &request_data_arg, &reply_handler_arg, &finished_handler_arg, &args_arg)) {
     if (!NCDVal_ListRead(i->args, 4, &request_data_arg, &reply_handler_arg, &finished_handler_arg, &args_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     }
     if (!NCDVal_IsStringNoNulls(reply_handler_arg) || !NCDVal_IsStringNoNulls(finished_handler_arg) ||
     if (!NCDVal_IsStringNoNulls(reply_handler_arg) || !NCDVal_IsStringNoNulls(finished_handler_arg) ||
         !NCDVal_IsList(args_arg)
         !NCDVal_IsList(args_arg)
     ) {
     ) {
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     }
     o->reply_handler = NCDVal_StringValue(reply_handler_arg);
     o->reply_handler = NCDVal_StringValue(reply_handler_arg);
     o->finished_handler = NCDVal_StringValue(finished_handler_arg);
     o->finished_handler = NCDVal_StringValue(finished_handler_arg);
@@ -654,14 +635,14 @@ static void request_func_new (NCDModuleInst *i)
     // check client state
     // check client state
     if (client->state != CSTATE_CONNECTED) {
     if (client->state != CSTATE_CONNECTED) {
         ModuleLog(o->i, BLOG_ERROR, "client is not connected");
         ModuleLog(o->i, BLOG_ERROR, "client is not connected");
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // convert argument to old format
     // convert argument to old format
     NCDValue request_data_compat;
     NCDValue request_data_compat;
     if (!NCDValCompat_ValToValue(request_data_arg, &request_data_compat)) {
     if (!NCDValCompat_ValToValue(request_data_arg, &request_data_compat)) {
         ModuleLog(o->i, BLOG_ERROR, "NCDValCompat_ValToValue failed");
         ModuleLog(o->i, BLOG_ERROR, "NCDValCompat_ValToValue failed");
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // init request
     // init request
@@ -671,7 +652,7 @@ static void request_func_new (NCDModuleInst *i)
         (NCDRequestClientRequest_handler_finished)request_handler_finished)) {
         (NCDRequestClientRequest_handler_finished)request_handler_finished)) {
         ModuleLog(o->i, BLOG_ERROR, "NCDRequestClientRequest_Init failed");
         ModuleLog(o->i, BLOG_ERROR, "NCDRequestClientRequest_Init failed");
         NCDValue_Free(&request_data_compat);
         NCDValue_Free(&request_data_compat);
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     NCDValue_Free(&request_data_compat);
     NCDValue_Free(&request_data_compat);
@@ -688,8 +669,6 @@ static void request_func_new (NCDModuleInst *i)
     o->dstate = RDSTATE_NONE;
     o->dstate = RDSTATE_NONE;
     return;
     return;
     
     
-fail1:
-    free(o);
 fail0:
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
@@ -698,7 +677,6 @@ fail0:
 static void request_instance_free (struct request_instance *o, int with_error)
 static void request_instance_free (struct request_instance *o, int with_error)
 {
 {
     ASSERT(o->pstate == RPSTATE_NONE)
     ASSERT(o->pstate == RPSTATE_NONE)
-    NCDModuleInst *i = o->i;
     
     
     // free replies
     // free replies
     LinkedList1Node *ln;
     LinkedList1Node *ln;
@@ -712,13 +690,10 @@ static void request_instance_free (struct request_instance *o, int with_error)
         request_gone(o, 1);
         request_gone(o, 1);
     }
     }
     
     
-    // free structure
-    free(o);
-    
     if (with_error) {
     if (with_error) {
-        NCDModuleInst_Backend_SetError(i);
+        NCDModuleInst_Backend_SetError(o->i);
     }
     }
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 }
 
 
 static void request_func_die (void *vo)
 static void request_func_die (void *vo)
@@ -731,12 +706,14 @@ static void request_func_die (void *vo)
 static const struct NCDModule modules[] = {
 static const struct NCDModule modules[] = {
     {
     {
         .type = "sys.request_client",
         .type = "sys.request_client",
-        .func_new = func_new,
-        .func_die = func_die
+        .func_new2 = func_new,
+        .func_die = func_die,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "sys.request_client::request",
         .type = "sys.request_client::request",
-        .func_new = request_func_new,
-        .func_die = request_func_die
+        .func_new2 = request_func_new,
+        .func_die = request_func_die,
+        .alloc_size = sizeof(struct request_instance)
     }, {
     }, {
         .type = NULL
         .type = NULL
     }
     }

+ 9 - 20
ncd/modules/sys_request_server.c

@@ -725,16 +725,10 @@ bad:
     return 0;
     return 0;
 }
 }
 
 
-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, "malloc failed");
-        goto fail0;
-    }
+    struct instance *o = vo;
     o->i = i;
     o->i = i;
-    NCDModuleInst_Backend_SetUser(i, o);
     
     
     // check arguments
     // check arguments
     NCDValRef listen_addr_arg;
     NCDValRef listen_addr_arg;
@@ -742,18 +736,18 @@ static void func_new (NCDModuleInst *i)
     NCDValRef args_arg;
     NCDValRef args_arg;
     if (!NCDVal_ListRead(i->args, 3, &listen_addr_arg, &request_handler_template_arg, &args_arg)) {
     if (!NCDVal_ListRead(i->args, 3, &listen_addr_arg, &request_handler_template_arg, &args_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     }
     if (!NCDVal_IsStringNoNulls(request_handler_template_arg) || !NCDVal_IsList(args_arg)) {
     if (!NCDVal_IsStringNoNulls(request_handler_template_arg) || !NCDVal_IsList(args_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     }
     o->request_handler_template = NCDVal_StringValue(request_handler_template_arg);
     o->request_handler_template = NCDVal_StringValue(request_handler_template_arg);
     o->args = args_arg;
     o->args = args_arg;
     
     
     // init listener
     // init listener
     if (!init_listen(o, listen_addr_arg)) {
     if (!init_listen(o, listen_addr_arg)) {
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // init connections list
     // init connections list
@@ -766,8 +760,6 @@ static void 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);
@@ -775,14 +767,10 @@ fail0:
 
 
 static void instance_free (struct instance *o)
 static void instance_free (struct instance *o)
 {
 {
-    NCDModuleInst *i = o->i;
     ASSERT(o->dying)
     ASSERT(o->dying)
     ASSERT(LinkedList0_IsEmpty(&o->connections_list))
     ASSERT(LinkedList0_IsEmpty(&o->connections_list))
     
     
-    // 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)
@@ -882,8 +870,9 @@ fail:
 static const struct NCDModule modules[] = {
 static const struct NCDModule modules[] = {
     {
     {
         .type = "sys.request_server",
         .type = "sys.request_server",
-        .func_new = func_new,
-        .func_die = func_die
+        .func_new2 = func_new,
+        .func_die = func_die,
+        .alloc_size = sizeof(struct instance)
     }, {
     }, {
         .type = "sys.request_server.request::reply",
         .type = "sys.request_server.request::reply",
         .func_new = reply_func_new
         .func_new = reply_func_new

+ 16 - 31
ncd/modules/sys_watch_directory.c

@@ -235,60 +235,52 @@ static void next_event (struct instance *o)
     }
     }
 }
 }
 
 
-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
     NCDValRef dir_arg;
     NCDValRef dir_arg;
     if (!NCDVal_ListRead(o->i->args, 1, &dir_arg)) {
     if (!NCDVal_ListRead(o->i->args, 1, &dir_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     }
     if (!NCDVal_IsStringNoNulls(dir_arg)) {
     if (!NCDVal_IsStringNoNulls(dir_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     }
     o->dir = NCDVal_StringValue(dir_arg);
     o->dir = NCDVal_StringValue(dir_arg);
     
     
     // open inotify
     // open inotify
     if ((o->inotify_fd = inotify_init()) < 0) {
     if ((o->inotify_fd = inotify_init()) < 0) {
         ModuleLog(o->i, BLOG_ERROR, "inotify_init failed");
         ModuleLog(o->i, BLOG_ERROR, "inotify_init failed");
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // add watch
     // add watch
     if (inotify_add_watch(o->inotify_fd, o->dir, IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVED_FROM | IN_MOVED_TO) < 0) {
     if (inotify_add_watch(o->inotify_fd, o->dir, IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVED_FROM | IN_MOVED_TO) < 0) {
         ModuleLog(o->i, BLOG_ERROR, "inotify_add_watch failed");
         ModuleLog(o->i, BLOG_ERROR, "inotify_add_watch failed");
-        goto fail2;
+        goto fail1;
     }
     }
     
     
     // set non-blocking
     // set non-blocking
     if (!badvpn_set_nonblocking(o->inotify_fd)) {
     if (!badvpn_set_nonblocking(o->inotify_fd)) {
         ModuleLog(o->i, BLOG_ERROR, "badvpn_set_nonblocking failed");
         ModuleLog(o->i, BLOG_ERROR, "badvpn_set_nonblocking failed");
-        goto fail2;
+        goto fail1;
     }
     }
     
     
     // init BFileDescriptor
     // init BFileDescriptor
     BFileDescriptor_Init(&o->bfd, o->inotify_fd, (BFileDescriptor_handler)inotify_fd_handler, o);
     BFileDescriptor_Init(&o->bfd, o->inotify_fd, (BFileDescriptor_handler)inotify_fd_handler, o);
     if (!BReactor_AddFileDescriptor(o->i->iparams->reactor, &o->bfd)) {
     if (!BReactor_AddFileDescriptor(o->i->iparams->reactor, &o->bfd)) {
         ModuleLog(o->i, BLOG_ERROR, "BReactor_AddFileDescriptor failed");
         ModuleLog(o->i, BLOG_ERROR, "BReactor_AddFileDescriptor failed");
-        goto fail2;
+        goto fail1;
     }
     }
     
     
     // open directory
     // open directory
     if (!(o->dir_handle = opendir(o->dir))) {
     if (!(o->dir_handle = opendir(o->dir))) {
         ModuleLog(o->i, BLOG_ERROR, "opendir failed");
         ModuleLog(o->i, BLOG_ERROR, "opendir failed");
-        goto fail3;
+        goto fail2;
     }
     }
     
     
     // set not processing
     // set not processing
@@ -298,15 +290,12 @@ static void func_new (NCDModuleInst *i)
     next_dir_event(o);
     next_dir_event(o);
     return;
     return;
     
     
-fail3:
-    // free BFileDescriptor
-    BReactor_RemoveFileDescriptor(o->i->iparams->reactor, &o->bfd);
 fail2:
 fail2:
+    BReactor_RemoveFileDescriptor(o->i->iparams->reactor, &o->bfd);
+fail1:
     if (close(o->inotify_fd) < 0) {
     if (close(o->inotify_fd) < 0) {
         ModuleLog(o->i, BLOG_ERROR, "close failed");
         ModuleLog(o->i, BLOG_ERROR, "close failed");
     }
     }
-fail1:
-    free(o);
 fail0:
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
@@ -314,8 +303,6 @@ fail0:
 
 
 void instance_free (struct instance *o, int is_error)
 void instance_free (struct instance *o, int is_error)
 {
 {
-    NCDModuleInst *i = o->i;
-    
     // close directory
     // close directory
     if (o->dir_handle) {
     if (o->dir_handle) {
         if (closedir(o->dir_handle) < 0) {
         if (closedir(o->dir_handle) < 0) {
@@ -331,13 +318,10 @@ void instance_free (struct instance *o, int is_error)
         ModuleLog(o->i, BLOG_ERROR, "close failed");
         ModuleLog(o->i, BLOG_ERROR, "close failed");
     }
     }
     
     
-    // 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)
@@ -424,9 +408,10 @@ fail0:
 static const struct NCDModule modules[] = {
 static const struct NCDModule modules[] = {
     {
     {
         .type = "sys.watch_directory",
         .type = "sys.watch_directory",
-        .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 = "sys.watch_directory::nextevent",
         .type = "sys.watch_directory::nextevent",
         .func_new = nextevent_func_new
         .func_new = nextevent_func_new

+ 8 - 22
ncd/modules/sys_watch_input.c

@@ -342,28 +342,20 @@ out:
     }
     }
 }
 }
 
 
-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
     NCDValRef devnode_type_arg;
     NCDValRef devnode_type_arg;
     if (!NCDVal_ListRead(o->i->args, 1, &devnode_type_arg)) {
     if (!NCDVal_ListRead(o->i->args, 1, &devnode_type_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     }
     if (!NCDVal_IsStringNoNulls(devnode_type_arg)) {
     if (!NCDVal_IsStringNoNulls(devnode_type_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     }
     o->devnode_type = NCDVal_StringValue(devnode_type_arg);
     o->devnode_type = NCDVal_StringValue(devnode_type_arg);
     
     
@@ -376,8 +368,6 @@ static void func_new (NCDModuleInst *i)
     event_template_new(&o->templ, o->i, BLOG_CURRENT_CHANNEL, 3, o, (event_template_func_free)templ_func_free);
     event_template_new(&o->templ, o->i, BLOG_CURRENT_CHANNEL, 3, o, (event_template_func_free)templ_func_free);
     return;
     return;
     
     
-fail1:
-    free(o);
 fail0:
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
@@ -385,8 +375,6 @@ fail0:
 
 
 static void templ_func_free (struct instance *o)
 static void templ_func_free (struct instance *o)
 {
 {
-    NCDModuleInst *i = o->i;
-    
     // free devices
     // free devices
     LinkedList1Node *list_node;
     LinkedList1Node *list_node;
     while (list_node = LinkedList1_GetFirst(&o->devices_list)) {
     while (list_node = LinkedList1_GetFirst(&o->devices_list)) {
@@ -397,10 +385,7 @@ static void templ_func_free (struct instance *o)
     // free client
     // free client
     NCDUdevClient_Free(&o->client);
     NCDUdevClient_Free(&o->client);
     
     
-    // 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)
@@ -449,9 +434,10 @@ fail0:
 static const struct NCDModule modules[] = {
 static const struct NCDModule modules[] = {
     {
     {
         .type = "sys.watch_input",
         .type = "sys.watch_input",
-        .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 = "sys.watch_input::nextevent",
         .type = "sys.watch_input::nextevent",
         .func_new = nextevent_func_new
         .func_new = nextevent_func_new

+ 7 - 21
ncd/modules/sys_watch_usb.c

@@ -317,23 +317,15 @@ out:
     }
     }
 }
 }
 
 
-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(i->args, 0)) {
     if (!NCDVal_ListRead(i->args, 0)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // init client
     // init client
@@ -345,8 +337,6 @@ static void func_new (NCDModuleInst *i)
     event_template_new(&o->templ, o->i, BLOG_CURRENT_CHANNEL, 3, o, (event_template_func_free)templ_func_free);
     event_template_new(&o->templ, o->i, BLOG_CURRENT_CHANNEL, 3, o, (event_template_func_free)templ_func_free);
     return;
     return;
     
     
-fail1:
-    free(o);
 fail0:
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
@@ -354,8 +344,6 @@ fail0:
 
 
 static void templ_func_free (struct instance *o)
 static void templ_func_free (struct instance *o)
 {
 {
-    NCDModuleInst *i = o->i;
-    
     // free devices
     // free devices
     while (!LinkedList1_IsEmpty(&o->devices_list)) {
     while (!LinkedList1_IsEmpty(&o->devices_list)) {
         struct device *device = UPPER_OBJECT(LinkedList1_GetFirst(&o->devices_list), struct device, devices_list_node);
         struct device *device = UPPER_OBJECT(LinkedList1_GetFirst(&o->devices_list), struct device, devices_list_node);
@@ -365,10 +353,7 @@ static void templ_func_free (struct instance *o)
     // free client
     // free client
     NCDUdevClient_Free(&o->client);
     NCDUdevClient_Free(&o->client);
     
     
-    // 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)
@@ -417,9 +402,10 @@ fail0:
 static const struct NCDModule modules[] = {
 static const struct NCDModule modules[] = {
     {
     {
         .type = "sys.watch_usb",
         .type = "sys.watch_usb",
-        .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 = "sys.watch_usb::nextevent",
         .type = "sys.watch_usb::nextevent",
         .func_new = nextevent_func_new
         .func_new = nextevent_func_new

+ 8 - 19
ncd/modules/to_string.c

@@ -49,36 +49,28 @@ struct instance {
     char *str;
     char *str;
 };
 };
 
 
-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 value_arg;
     NCDValRef value_arg;
     if (!NCDVal_ListRead(i->args, 1, &value_arg)) {
     if (!NCDVal_ListRead(i->args, 1, &value_arg)) {
         ModuleLog(i, BLOG_ERROR, "wrong arity");
         ModuleLog(i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // convert to string
     // convert to string
     if (!(o->str = NCDValGenerator_Generate(value_arg))) {
     if (!(o->str = NCDValGenerator_Generate(value_arg))) {
         ModuleLog(i, BLOG_ERROR, "NCDValGenerator_Generate failed");
         ModuleLog(i, BLOG_ERROR, "NCDValGenerator_Generate failed");
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // signal up
     // signal up
     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);
@@ -87,15 +79,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->str);
     free(o->str);
     
     
-    // free structure
-    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)
@@ -116,9 +104,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 = "to_string",
         .type = "to_string",
-        .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
     }
     }