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

ncd: modules: port some modules to func_new2

ambrop7 13 лет назад
Родитель
Сommit
641a6bd0ff

+ 12 - 26
ncd/modules/net_backend_badvpn.c

@@ -146,17 +146,9 @@ void timer_handler (struct instance *o)
     try_process(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;
     
     // read arguments
@@ -166,12 +158,12 @@ static void func_new (NCDModuleInst *i)
     NCDValRef args_arg;
     if (!NCDVal_ListRead(o->i->args, 4, &ifname_arg, &user_arg, &exec_arg, &args_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     if (!NCDVal_IsStringNoNulls(ifname_arg) || !NCDVal_IsStringNoNulls(user_arg) ||
         !NCDVal_IsStringNoNulls(exec_arg) || !NCDVal_IsList(args_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     o->ifname = NCDVal_StringValue(ifname_arg);
     o->user = NCDVal_StringValue(user_arg);
@@ -184,20 +176,20 @@ static void func_new (NCDModuleInst *i)
         NCDValRef arg = NCDVal_ListGet(o->args, j);
         if (!NCDVal_IsStringNoNulls(arg)) {
             ModuleLog(o->i, BLOG_ERROR, "wrong type");
-            goto fail1;
+            goto fail0;
         }
     }
     
     // create TAP device
     if (!NCDIfConfig_make_tuntap(o->ifname, o->user, 0)) {
         ModuleLog(o->i, BLOG_ERROR, "failed to create TAP device");
-        goto fail1;
+        goto fail0;
     }
     
     // set device up
     if (!NCDIfConfig_set_up(o->ifname)) {
         ModuleLog(o->i, BLOG_ERROR, "failed to set device up");
-        goto fail2;
+        goto fail1;
     }
     
     // set not dying
@@ -211,15 +203,12 @@ static void func_new (NCDModuleInst *i)
     
     // try starting process
     try_process(o);
-    
     return;
     
-fail2:
+fail1:
     if (!NCDIfConfig_remove_tuntap(o->ifname, 0)) {
         ModuleLog(o->i, BLOG_ERROR, "failed to remove TAP device");
     }
-fail1:
-    free(o);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
@@ -228,7 +217,6 @@ fail0:
 void instance_free (struct instance *o)
 {
     ASSERT(!o->started)
-    NCDModuleInst *i = o->i;
     
     // free timer
     BReactor_RemoveTimer(o->i->iparams->reactor, &o->timer);
@@ -243,10 +231,7 @@ void instance_free (struct instance *o)
         ModuleLog(o->i, BLOG_ERROR, "failed to remove TAP device");
     }
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 
 static void func_die (void *vo)
@@ -269,8 +254,9 @@ static void func_die (void *vo)
 static const struct NCDModule modules[] = {
     {
         .type = "net.backend.badvpn",
-        .func_new = func_new,
-        .func_die = func_die
+        .func_new2 = func_new,
+        .func_die = func_die,
+        .alloc_size = sizeof(struct instance)
     }, {
         .type = NULL
     }

+ 12 - 26
ncd/modules/net_backend_rfkill.c

@@ -130,17 +130,9 @@ static void monitor_handler (struct instance *o, struct rfkill_event event)
     }
 }
 
-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;
     
     // check arguments
@@ -148,11 +140,11 @@ static void func_new (NCDModuleInst *i)
     NCDValRef name_arg;
     if (!NCDVal_ListRead(i->args, 2, &type_arg, &name_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     if (!NCDVal_IsStringNoNulls(type_arg) || !NCDVal_IsStringNoNulls(name_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     const char *type = NCDVal_StringValue(type_arg);
     const char *name = NCDVal_StringValue(name_arg);
@@ -160,33 +152,30 @@ static void func_new (NCDModuleInst *i)
     if (!strcmp(type, "index")) {
         if (sscanf(name, "%"SCNu32, &o->index) != 1) {
             ModuleLog(o->i, BLOG_ERROR, "wrong index argument");
-            goto fail1;
+            goto fail0;
         }
     }
     else if (!strcmp(type, "wlan")) {
         if (!find_wlan_rfill(name, &o->index)) {
             ModuleLog(o->i, BLOG_ERROR, "failed to find rfkill for wlan interface");
-            goto fail1;
+            goto fail0;
         }
     }
     else {
         ModuleLog(o->i, BLOG_ERROR, "unknown type argument");
-        goto fail1;
+        goto fail0;
     }
     
     // init monitor
     if (!NCDRfkillMonitor_Init(&o->monitor, o->i->iparams->reactor, (NCDRfkillMonitor_handler)monitor_handler, o)) {
         ModuleLog(o->i, BLOG_ERROR, "monitor failed");
-        goto fail1;
+        goto fail0;
     }
     
     // set not up
     o->up = 0;
-    
     return;
     
-fail1:
-    free(o);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
@@ -195,22 +184,19 @@ fail0:
 static void func_die (void *vo)
 {
     struct instance *o = vo;
-    NCDModuleInst *i = o->i;
     
     // free monitor
     NCDRfkillMonitor_Free(&o->monitor);
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 
 static const struct NCDModule modules[] = {
     {
         .type = "net.backend.rfkill",
-        .func_new = func_new,
-        .func_die = func_die
+        .func_new2 = func_new,
+        .func_die = func_die,
+        .alloc_size = sizeof(struct instance)
     }, {
         .type = NULL
     }

+ 10 - 23
ncd/modules/net_backend_waitdevice.c

@@ -115,28 +115,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;
     
     // check arguments
     NCDValRef arg;
     if (!NCDVal_ListRead(i->args, 1, &arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     if (!NCDVal_IsStringNoNulls(arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     o->ifname = NCDVal_StringValue(arg);
     
@@ -146,17 +138,15 @@ static void func_new (NCDModuleInst *i)
     // compile regex
     if (regcomp(&o->reg, DEVPATH_REGEX, REG_EXTENDED)) {
         ModuleLog(o->i, BLOG_ERROR, "regcomp failed");
-        goto fail2;
+        goto fail1;
     }
     
     // set no devpath
     o->devpath = NULL;
     return;
     
-fail2:
-    NCDUdevClient_Free(&o->client);
 fail1:
-    free(o);
+    NCDUdevClient_Free(&o->client);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
@@ -165,7 +155,6 @@ fail0:
 static void func_die (void *vo)
 {
     struct instance *o = vo;
-    NCDModuleInst *i = o->i;
     
     // free devpath
     if (o->devpath) {
@@ -178,17 +167,15 @@ static void func_die (void *vo)
     // free client
     NCDUdevClient_Free(&o->client);
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 
 static const struct NCDModule modules[] = {
     {
         .type = "net.backend.waitdevice",
-        .func_new = func_new,
-        .func_die = func_die
+        .func_new2 = func_new,
+        .func_die = func_die,
+        .alloc_size = sizeof(struct instance)
     }, {
         .type = NULL
     }

+ 10 - 22
ncd/modules/net_backend_waitlink.c

@@ -76,26 +76,20 @@ static void monitor_handler_error (struct instance *o)
     instance_free(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;
-    }
+    struct instance *o = vo;
     o->i = i;
-    NCDModuleInst_Backend_SetUser(i, o);
     
     // check arguments
     NCDValRef arg;
     if (!NCDVal_ListRead(i->args, 1, &arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     if (!NCDVal_IsStringNoNulls(arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     const char *ifname = NCDVal_StringValue(arg);
     
@@ -103,21 +97,19 @@ static void func_new (NCDModuleInst *i)
     int ifindex;
     if (!get_iface_info(ifname, NULL, NULL, &ifindex)) {
         ModuleLog(o->i, BLOG_ERROR, "failed to get interface index");
-        goto fail1;
+        goto fail0;
     }
     
     // init monitor
     if (!NCDInterfaceMonitor_Init(&o->monitor, ifindex, NCDIFMONITOR_WATCH_LINK, i->iparams->reactor, o, (NCDInterfaceMonitor_handler)monitor_handler, (NCDInterfaceMonitor_handler_error)monitor_handler_error)) {
         ModuleLog(o->i, BLOG_ERROR, "NCDInterfaceMonitor_Init failed");
-        goto fail1;
+        goto fail0;
     }
     
     // set not up
     o->up = 0;
     return;
     
-fail1:
-    free(o);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
@@ -125,15 +117,10 @@ fail0:
 
 static void instance_free (struct instance *o)
 {
-    NCDModuleInst *i = o->i;
-    
     // free monitor
     NCDInterfaceMonitor_Free(&o->monitor);
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 
 static void func_die (void *vo)
@@ -145,8 +132,9 @@ static void func_die (void *vo)
 static const struct NCDModule modules[] = {
     {
         .type = "net.backend.waitlink",
-        .func_new = func_new,
-        .func_die = func_die
+        .func_new2 = func_new,
+        .func_die = func_die,
+        .alloc_size = sizeof(struct instance)
     }, {
         .type = NULL
     }

+ 15 - 30
ncd/modules/net_backend_wpa_supplicant.c

@@ -397,17 +397,9 @@ void process_pipe_handler_send (struct instance *o, uint8_t *data, int data_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;
-    }
-    NCDModuleInst_Backend_SetUser(i, o);
-    
-    // init arguments
+    struct instance *o = vo;
     o->i = i;
     
     // read arguments
@@ -417,12 +409,12 @@ static void func_new (NCDModuleInst *i)
     NCDValRef args_arg;
     if (!NCDVal_ListRead(o->i->args, 4, &ifname_arg, &conf_arg, &exec_arg, &args_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     if (!NCDVal_IsStringNoNulls(ifname_arg) || !NCDVal_IsStringNoNulls(conf_arg)  ||
         !NCDVal_IsStringNoNulls(exec_arg) || !NCDVal_IsList(args_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     o->ifname = NCDVal_StringValue(ifname_arg);
     o->conf = NCDVal_StringValue(conf_arg);
@@ -439,7 +431,7 @@ static void func_new (NCDModuleInst *i)
     CmdLine c;
     if (!build_cmdline(o, &c)) {
         ModuleLog(o->i, BLOG_ERROR, "failed to build cmdline");
-        goto fail1;
+        goto fail0;
     }
     
     // init process
@@ -448,7 +440,7 @@ static void func_new (NCDModuleInst *i)
                             (BInputProcess_handler_closed)process_handler_closed
     )) {
         ModuleLog(o->i, BLOG_ERROR, "BInputProcess_Init failed");
-        goto fail2;
+        goto fail1;
     }
     
     // init input interface
@@ -457,7 +449,7 @@ static void func_new (NCDModuleInst *i)
     // init buffer
     if (!LineBuffer_Init(&o->pipe_buffer, BInputProcess_GetInput(&o->process), &o->pipe_input, MAX_LINE_LEN, '\n')) {
         ModuleLog(o->i, BLOG_ERROR, "LineBuffer_Init failed");
-        goto fail3;
+        goto fail2;
     }
     
     // set have pipe
@@ -466,25 +458,22 @@ static void func_new (NCDModuleInst *i)
     // start process
     if (!BInputProcess_Start(&o->process, ((char **)c.arr.v)[0], (char **)c.arr.v, NULL)) {
         ModuleLog(o->i, BLOG_ERROR, "BInputProcess_Start failed");
-        goto fail4;
+        goto fail3;
     }
     
     // set not have info
     o->have_info = 0;
     
     CmdLine_Free(&c);
-    
     return;
     
-fail4:
-    LineBuffer_Free(&o->pipe_buffer);
 fail3:
+    LineBuffer_Free(&o->pipe_buffer);
+fail2:
     PacketPassInterface_Free(&o->pipe_input);
     BInputProcess_Free(&o->process);
-fail2:
-    CmdLine_Free(&c);
 fail1:
-    free(o);
+    CmdLine_Free(&c);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
@@ -492,8 +481,6 @@ fail0:
 
 void instance_free (struct instance *o)
 {
-    NCDModuleInst *i = o->i;
-    
     // free info
     if (o->have_info) {
         free_info(o);
@@ -510,10 +497,7 @@ void instance_free (struct instance *o)
     // free process
     BInputProcess_Free(&o->process);
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 
 static void func_die (void *vo)
@@ -565,9 +549,10 @@ static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *o
 static const struct NCDModule modules[] = {
     {
         .type = "net.backend.wpa_supplicant",
-        .func_new = func_new,
+        .func_new2 = func_new,
         .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
         .type = NULL
     }

+ 6 - 17
ncd/modules/net_dns.c

@@ -197,16 +197,10 @@ static int func_globalinit (struct NCDModuleInitParams params)
     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;
-    }
+    struct instance *o = vo;
     o->i = i;
-    NCDModuleInst_Backend_SetUser(i, o);
     
     // init servers list
     LinkedList2_Init(&o->ipv4_dns_servers);
@@ -264,8 +258,6 @@ fail2:
     LinkedList2_Remove(&instances, &o->instances_node);
 fail1:
     remove_ipv4_dns_entries(o);
-    free(o);
-fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
 }
@@ -273,7 +265,6 @@ fail0:
 static void func_die (void *vo)
 {
     struct instance *o = vo;
-    NCDModuleInst *i = o->i;
     
     // remove from instances
     LinkedList2_Remove(&instances, &o->instances_node);
@@ -284,17 +275,15 @@ static void func_die (void *vo)
     // free servers
     remove_ipv4_dns_entries(o);
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 
 static const struct NCDModule modules[] = {
     {
         .type = "net.dns",
-        .func_new = func_new,
-        .func_die = func_die
+        .func_new2 = func_new,
+        .func_die = func_die,
+        .alloc_size = sizeof(struct instance)
     }, {
         .type = NULL
     }

+ 48 - 87
ncd/modules/net_iptables.c

@@ -379,67 +379,52 @@ static void func_globalfree (void)
     BEventLock_Free(&iptables_lock);
 }
 
-static void func_new (NCDModuleInst *i, command_template_build_cmdline build_cmdline)
+static void func_new (void *vo, NCDModuleInst *i, command_template_build_cmdline build_cmdline)
 {
-    // allocate instance
-    struct instance *o = malloc(sizeof(*o));
-    if (!o) {
-        BLog(BLOG_ERROR, "malloc failed");
-        goto fail0;
-    }
+    struct instance *o = vo;
     o->i = i;
-    NCDModuleInst_Backend_SetUser(i, o);
     
     command_template_new(&o->cti, i, build_cmdline, template_free_func, o, BLOG_CURRENT_CHANNEL, &iptables_lock);
-    return;
-    
-fail0:
-    NCDModuleInst_Backend_SetError(i);
-    NCDModuleInst_Backend_Dead(i);
 }
 
 void template_free_func (void *vo, int is_error)
 {
     struct instance *o = vo;
-    NCDModuleInst *i = o->i;
-    
-    // free instance
-    free(o);
     
     if (is_error) {
-        NCDModuleInst_Backend_SetError(i);
+        NCDModuleInst_Backend_SetError(o->i);
     }
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 
-static void append_iptables_func_new (NCDModuleInst *i)
+static void append_iptables_func_new (void *vo, NCDModuleInst *i)
 {
-    func_new(i, build_iptables_append_cmdline);
+    func_new(vo, i, build_iptables_append_cmdline);
 }
 
-static void policy_iptables_func_new (NCDModuleInst *i)
+static void policy_iptables_func_new (void *vo, NCDModuleInst *i)
 {
-    func_new(i, build_iptables_policy_cmdline);
+    func_new(vo, i, build_iptables_policy_cmdline);
 }
 
-static void newchain_iptables_func_new (NCDModuleInst *i)
+static void newchain_iptables_func_new (void *vo, NCDModuleInst *i)
 {
-    func_new(i, build_iptables_newchain_cmdline);
+    func_new(vo, i, build_iptables_newchain_cmdline);
 }
 
-static void append_ebtables_func_new (NCDModuleInst *i)
+static void append_ebtables_func_new (void *vo, NCDModuleInst *i)
 {
-    func_new(i, build_ebtables_append_cmdline);
+    func_new(vo, i, build_ebtables_append_cmdline);
 }
 
-static void policy_ebtables_func_new (NCDModuleInst *i)
+static void policy_ebtables_func_new (void *vo, NCDModuleInst *i)
 {
-    func_new(i, build_ebtables_policy_cmdline);
+    func_new(vo, i, build_ebtables_policy_cmdline);
 }
 
-static void newchain_ebtables_func_new (NCDModuleInst *i)
+static void newchain_ebtables_func_new (void *vo, NCDModuleInst *i)
 {
-    func_new(i, build_ebtables_newchain_cmdline);
+    func_new(vo, i, build_ebtables_newchain_cmdline);
 }
 
 static void func_die (void *vo)
@@ -449,17 +434,9 @@ static void func_die (void *vo)
     command_template_die(&o->cti);
 }
 
-static void lock_func_new (NCDModuleInst *i)
+static void lock_func_new (void *vo, NCDModuleInst *i)
 {
-    // allocate instance
-    struct lock_instance *o = malloc(sizeof(*o));
-    if (!o) {
-        BLog(BLOG_ERROR, "malloc failed");
-        goto fail0;
-    }
-    NCDModuleInst_Backend_SetUser(i, o);
-    
-    // init arguments
+    struct lock_instance *o = vo;
     o->i = i;
     
     // init lock job
@@ -471,17 +448,11 @@ static void lock_func_new (NCDModuleInst *i)
     
     // set state locking
     o->state = LOCK_STATE_LOCKING;
-    return;
-    
-fail0:
-    NCDModuleInst_Backend_SetError(i);
-    NCDModuleInst_Backend_Dead(i);
 }
 
 static void lock_func_die (void *vo)
 {
     struct lock_instance *o = vo;
-    NCDModuleInst *i = o->i;
     
     if (o->state == LOCK_STATE_UNLOCKED) {
         ASSERT(o->unlock)
@@ -500,24 +471,13 @@ static void lock_func_die (void *vo)
     // free lock job
     BEventLockJob_Free(&o->lock_job);
     
-    // free instance
-    free(o);
-    
     // dead
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 
-static void unlock_func_new (NCDModuleInst *i)
+static void unlock_func_new (void *vo, NCDModuleInst *i)
 {
-    // allocate instance
-    struct unlock_instance *o = malloc(sizeof(*o));
-    if (!o) {
-        BLog(BLOG_ERROR, "malloc failed");
-        goto fail0;
-    }
-    NCDModuleInst_Backend_SetUser(i, o);
-    
-    // init arguments
+    struct unlock_instance *o = vo;
     o->i = i;
     
     // get lock lock
@@ -526,13 +486,13 @@ static void unlock_func_new (NCDModuleInst *i)
     // make sure lock doesn't already have an unlock
     if (lock->unlock) {
         BLog(BLOG_ERROR, "lock already has an unlock");
-        goto fail1;
+        goto fail0;
     }
     
     // make sure lock is locked
     if (lock->state != LOCK_STATE_LOCKED) {
         BLog(BLOG_ERROR, "lock is not locked");
-        goto fail1;
+        goto fail0;
     }
     
     // set lock
@@ -551,8 +511,6 @@ static void unlock_func_new (NCDModuleInst *i)
     lock->state = LOCK_STATE_UNLOCKED;
     return;
     
-fail1:
-    free(o);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
@@ -580,47 +538,50 @@ static void unlock_func_die (void *vo)
 
 static void unlock_free (struct unlock_instance *o)
 {
-    NCDModuleInst *i = o->i;
-    
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 
 static const struct NCDModule modules[] = {
     {
         .type = "net.iptables.append",
-        .func_new = append_iptables_func_new,
-        .func_die = func_die
+        .func_new2 = append_iptables_func_new,
+        .func_die = func_die,
+        .alloc_size = sizeof(struct instance)
     }, {
         .type = "net.iptables.policy",
-        .func_new = policy_iptables_func_new,
-        .func_die = func_die
+        .func_new2 = policy_iptables_func_new,
+        .func_die = func_die,
+        .alloc_size = sizeof(struct instance)
     }, {
         .type = "net.iptables.newchain",
-        .func_new = newchain_iptables_func_new,
-        .func_die = func_die
+        .func_new2 = newchain_iptables_func_new,
+        .func_die = func_die,
+        .alloc_size = sizeof(struct instance)
     }, {
         .type = "net.ebtables.append",
-        .func_new = append_ebtables_func_new,
-        .func_die = func_die
+        .func_new2 = append_ebtables_func_new,
+        .func_die = func_die,
+        .alloc_size = sizeof(struct instance)
     }, {
         .type = "net.ebtables.policy",
-        .func_new = policy_ebtables_func_new,
-        .func_die = func_die
+        .func_new2 = policy_ebtables_func_new,
+        .func_die = func_die,
+        .alloc_size = sizeof(struct instance)
     }, {
         .type = "net.ebtables.newchain",
-        .func_new = newchain_ebtables_func_new,
-        .func_die = func_die
+        .func_new2 = newchain_ebtables_func_new,
+        .func_die = func_die,
+        .alloc_size = sizeof(struct instance)
     }, {
         .type = "net.iptables.lock",
-        .func_new = lock_func_new,
-        .func_die = lock_func_die
+        .func_new2 = lock_func_new,
+        .func_die = lock_func_die,
+        .alloc_size = sizeof(struct lock_instance)
     }, {
         .type = "net.iptables.lock::unlock",
-        .func_new = unlock_func_new,
-        .func_die = unlock_func_die
+        .func_new2 = unlock_func_new,
+        .func_die = unlock_func_die,
+        .alloc_size = sizeof(struct unlock_instance)
     }, {
         .type = NULL
     }

+ 10 - 22
ncd/modules/net_ipv4_arp_probe.c

@@ -118,27 +118,21 @@ static void arpprobe_handler (struct instance *o, int event)
     }
 }
 
-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;
-    NCDModuleInst_Backend_SetUser(i, o);
     
     // read arguments
     NCDValRef arg_ifname;
     NCDValRef arg_addr;
     if (!NCDVal_ListRead(i->args, 2, &arg_ifname, &arg_addr)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     if (!NCDVal_IsStringNoNulls(arg_ifname) || !NCDVal_IsStringNoNulls(arg_addr)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     const char *ifname = NCDVal_StringValue(arg_ifname);
     const char *addr_str = NCDVal_StringValue(arg_addr);
@@ -147,21 +141,19 @@ static void func_new (NCDModuleInst *i)
     uint32_t addr;
     if (!ipaddr_parse_ipv4_addr((char *)addr_str, &addr)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong address");
-        goto fail1;
+        goto fail0;
     }
     
     // init arpprobe
     if (!BArpProbe_Init(&o->arpprobe, ifname, addr, i->iparams->reactor, o, (BArpProbe_handler)arpprobe_handler)) {
         ModuleLog(o->i, BLOG_ERROR, "BArpProbe_Init failed");
-        goto fail1;
+        goto fail0;
     }
     
     // set state unknown
     o->state = STATE_UNKNOWN;
     return;
     
-fail1:
-    free(o);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
@@ -169,15 +161,10 @@ fail0:
 
 static void instance_free (struct instance *o)
 {
-    NCDModuleInst *i = o->i;
-    
     // free arpprobe
     BArpProbe_Free(&o->arpprobe);
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 
 static void func_die (void *vo)
@@ -208,9 +195,10 @@ static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *o
 static const struct NCDModule modules[] = {
     {
         .type = "net.ipv4.arp_probe",
-        .func_new = func_new,
+        .func_new2 = func_new,
         .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
         .type = NULL
     }

+ 13 - 25
ncd/modules/net_ipv4_dhcp.c

@@ -99,27 +99,21 @@ static void dhcp_handler (struct instance *o, int event)
     }
 }
 
-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;
-    NCDModuleInst_Backend_SetUser(i, o);
     
     // check arguments
     NCDValRef ifname_arg;
     NCDValRef opts_arg = NCDVal_NewInvalid();
     if (!NCDVal_ListRead(i->args, 1, &ifname_arg) && !NCDVal_ListRead(i->args, 2, &ifname_arg, &opts_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     if (!NCDVal_IsStringNoNulls(ifname_arg) || (!NCDVal_IsInvalid(opts_arg) && !NCDVal_IsList(opts_arg))) {
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     const char *ifname = NCDVal_StringValue(ifname_arg);
     
@@ -133,7 +127,7 @@ static void func_new (NCDModuleInst *i)
         // read name
         if (!NCDVal_IsStringNoNulls(opt)) {
             ModuleLog(o->i, BLOG_ERROR, "wrong option name type");
-            goto fail1;
+            goto fail0;
         }
         const char *optname = NCDVal_StringValue(opt);
         
@@ -141,12 +135,12 @@ static void func_new (NCDModuleInst *i)
             // read value
             if (j == count) {
                 ModuleLog(o->i, BLOG_ERROR, "option value missing");
-                goto fail1;
+                goto fail0;
             }
             NCDValRef val = NCDVal_ListGet(opts_arg, j + 1);
             if (!NCDVal_IsStringNoNulls(val)) {
                 ModuleLog(o->i, BLOG_ERROR, "wrong option value type");
-                goto fail1;
+                goto fail0;
             }
             const char *optval = NCDVal_StringValue(val);
             
@@ -163,22 +157,20 @@ static void func_new (NCDModuleInst *i)
         }
         else {
             ModuleLog(o->i, BLOG_ERROR, "unknown option name");
-            goto fail1;
+            goto fail0;
         }
     }
     
     // init DHCP
     if (!BDHCPClient_Init(&o->dhcp, ifname, opts, o->i->iparams->reactor, (BDHCPClient_handler)dhcp_handler, o)) {
         ModuleLog(o->i, BLOG_ERROR, "BDHCPClient_Init failed");
-        goto fail1;
+        goto fail0;
     }
     
     // set not up
     o->up = 0;
     return;
     
-fail1:
-    free(o);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
@@ -186,15 +178,10 @@ fail0:
 
 static void instance_free (struct instance *o)
 {
-    NCDModuleInst *i = o->i;
-    
     // free DHCP
     BDHCPClient_Free(&o->dhcp);
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 
 static void func_die (void *vo)
@@ -335,9 +322,10 @@ fail:
 static const struct NCDModule modules[] = {
     {
         .type = "net.ipv4.dhcp",
-        .func_new = func_new,
+        .func_new2 = func_new,
         .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
         .type = NULL
     }

+ 10 - 22
ncd/modules/net_ipv6_wait_dynamic_addr.c

@@ -92,26 +92,20 @@ static void monitor_handler_error (struct instance *o)
     instance_free(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;
-    }
+    struct instance *o = vo;
     o->i = i;
-    NCDModuleInst_Backend_SetUser(i, o);
     
     // read arguments
     NCDValRef ifname_arg;
     if (!NCDVal_ListRead(i->args, 1, &ifname_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     if (!NCDVal_IsStringNoNulls(ifname_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     const char *ifname = NCDVal_StringValue(ifname_arg);
     
@@ -119,21 +113,19 @@ static void func_new (NCDModuleInst *i)
     int ifindex;
     if (!get_iface_info(ifname, NULL, NULL, &ifindex)) {
         ModuleLog(o->i, BLOG_ERROR, "failed to get interface index");
-        goto fail1;
+        goto fail0;
     }
     
     // init monitor
     if (!NCDInterfaceMonitor_Init(&o->monitor, ifindex, NCDIFMONITOR_WATCH_IPV6_ADDR, i->iparams->reactor, o, (NCDInterfaceMonitor_handler)monitor_handler, (NCDInterfaceMonitor_handler_error)monitor_handler_error)) {
         ModuleLog(o->i, BLOG_ERROR, "NCDInterfaceMonitor_Init failed");
-        goto fail1;
+        goto fail0;
     }
     
     // set not up
     o->up = 0;
     return;
     
-fail1:
-    free(o);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
@@ -141,15 +133,10 @@ fail0:
 
 static void instance_free (struct instance *o)
 {
-    NCDModuleInst *i = o->i;
-    
     // free monitor
     NCDInterfaceMonitor_Free(&o->monitor);
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 
 static void func_die (void *vo)
@@ -199,9 +186,10 @@ static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *o
 static const struct NCDModule modules[] = {
     {
         .type = "net.ipv6.wait_dynamic_addr",
-        .func_new = func_new,
+        .func_new2 = func_new,
         .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
         .type = NULL
     }

+ 9 - 22
ncd/modules/net_up.c

@@ -49,35 +49,27 @@ struct instance {
     const char *ifname;
 };
 
-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;
     
     // read arguments
     NCDValRef ifname_arg;
     if (!NCDVal_ListRead(o->i->args, 1, &ifname_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     if (!NCDVal_IsStringNoNulls(ifname_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     o->ifname = NCDVal_StringValue(ifname_arg);
     
     // set interface up
     if (!NCDIfConfig_set_up(o->ifname)) {
         ModuleLog(o->i, BLOG_ERROR, "failed to set interface up");
-        goto fail1;
+        goto fail0;
     }
     
     // signal up
@@ -85,8 +77,6 @@ static void func_new (NCDModuleInst *i)
     
     return;
     
-fail1:
-    free(o);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
@@ -95,24 +85,21 @@ fail0:
 static void func_die (void *vo)
 {
     struct instance *o = vo;
-    NCDModuleInst *i = o->i;
     
     // set interface down
     if (!NCDIfConfig_set_down(o->ifname)) {
         ModuleLog(o->i, BLOG_ERROR, "failed to set interface down");
     }
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 
 static const struct NCDModule modules[] = {
     {
         .type = "net.up",
-        .func_new = func_new,
-        .func_die = func_die
+        .func_new2 = func_new,
+        .func_die = func_die,
+        .alloc_size = sizeof(struct instance)
     }, {
         .type = NULL
     }

+ 13 - 27
ncd/modules/net_watch_interfaces.c

@@ -344,23 +344,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;
     
     // check arguments
     if (!NCDVal_ListRead(o->i->args, 0)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     
     // init client
@@ -372,28 +364,26 @@ static void func_new (NCDModuleInst *i)
     // compile regex's
     if (regcomp(&o->reg, DEVPATH_REGEX, REG_EXTENDED)) {
         ModuleLog(o->i, BLOG_ERROR, "regcomp failed");
-        goto fail2;
+        goto fail1;
     }
     if (regcomp(&o->usb_reg, DEVPATH_USB_REGEX, REG_EXTENDED)) {
         ModuleLog(o->i, BLOG_ERROR, "regcomp failed");
-        goto fail3;
+        goto fail2;
     }
     if (regcomp(&o->pci_reg, DEVPATH_PCI_REGEX, REG_EXTENDED)) {
         ModuleLog(o->i, BLOG_ERROR, "regcomp failed");
-        goto fail4;
+        goto fail3;
     }
     
     event_template_new(&o->templ, o->i, BLOG_CURRENT_CHANNEL, 3, o, (event_template_func_free)templ_func_free);
     return;
     
-fail4:
-    regfree(&o->usb_reg);
 fail3:
-    regfree(&o->reg);
+    regfree(&o->usb_reg);
 fail2:
-    NCDUdevClient_Free(&o->client);
+    regfree(&o->reg);
 fail1:
-    free(o);
+    NCDUdevClient_Free(&o->client);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
@@ -401,8 +391,6 @@ fail0:
 
 static void templ_func_free (struct instance *o)
 {
-    NCDModuleInst *i = o->i;
-    
     // free devices
     LinkedList1Node *list_node;
     while (list_node = LinkedList1_GetFirst(&o->devices_list)) {
@@ -418,10 +406,7 @@ static void templ_func_free (struct instance *o)
     // free client
     NCDUdevClient_Free(&o->client);
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 
 static void func_die (void *vo)
@@ -470,9 +455,10 @@ fail0:
 static const struct NCDModule modules[] = {
     {
         .type = "net.watch_interfaces",
-        .func_new = func_new,
+        .func_new2 = func_new,
         .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
         .type = "net.watch_interfaces::nextevent",
         .func_new = nextevent_func_new

+ 23 - 47
ncd/modules/netmask.c

@@ -72,30 +72,19 @@ struct prefix_instance {
     int prefix;
 };
 
-static void addr_func_init_templ (NCDModuleInst *i, uint32_t addr)
+static void addr_func_init_templ (void *vo, NCDModuleInst *i, uint32_t addr)
 {
-    // allocate structure
-    struct addr_instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "malloc failed");
-        goto fail0;
-    }
+    struct addr_instance *o = vo;
     o->i = i;
-    NCDModuleInst_Backend_SetUser(i, o);
     
     // remember address
     o->addr = addr;
     
     // signal up
     NCDModuleInst_Backend_Up(i);
-    return;
-    
-fail0:
-    NCDModuleInst_Backend_SetError(i);
-    NCDModuleInst_Backend_Dead(i);
 }
 
-static void prefix_to_mask_func_init (NCDModuleInst *i)
+static void prefix_to_mask_func_init (void *vo, NCDModuleInst *i)
 {
     // read arguments
     NCDValRef prefix_arg;
@@ -118,7 +107,7 @@ static void prefix_to_mask_func_init (NCDModuleInst *i)
     // make mask
     uint32_t mask = ipaddr_ipv4_mask_from_prefix(prefix);
     
-    addr_func_init_templ(i, mask);
+    addr_func_init_templ(vo, i, mask);
     return;
     
 fail0:
@@ -126,7 +115,7 @@ fail0:
     NCDModuleInst_Backend_Dead(i);
 }
 
-static void ipv4_net_from_addr_and_prefix_func_init (NCDModuleInst *i)
+static void ipv4_net_from_addr_and_prefix_func_init (void *vo, NCDModuleInst *i)
 {
     // read arguments
     NCDValRef addr_arg;
@@ -157,7 +146,7 @@ static void ipv4_net_from_addr_and_prefix_func_init (NCDModuleInst *i)
     // make network
     uint32_t network = (addr & ipaddr_ipv4_mask_from_prefix(prefix));
     
-    addr_func_init_templ(i, network);
+    addr_func_init_templ(vo, i, network);
     return;
     
 fail0:
@@ -168,12 +157,8 @@ fail0:
 static void addr_func_die (void *vo)
 {
     struct addr_instance *o = vo;
-    NCDModuleInst *i = o->i;
     
-    // free structure
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 
 static int addr_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
@@ -194,47 +179,39 @@ static int addr_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValR
     return 0;
 }
 
-static void mask_to_prefix_func_init (NCDModuleInst *i)
+static void mask_to_prefix_func_init (void *vo, NCDModuleInst *i)
 {
-    // allocate structure
-    struct prefix_instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "malloc failed");
-        goto fail0;
-    }
+    struct prefix_instance *o = vo;
     o->i = i;
-    NCDModuleInst_Backend_SetUser(i, o);
     
     // read arguments
     NCDValRef mask_arg;
     if (!NCDVal_ListRead(i->args, 1, &mask_arg)) {
         ModuleLog(i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     if (!NCDVal_IsStringNoNulls(mask_arg)) {
         ModuleLog(i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     
     // parse mask
     uint32_t mask;
     if (!ipaddr_parse_ipv4_addr((char *)NCDVal_StringValue(mask_arg), &mask)) {
         ModuleLog(i, BLOG_ERROR, "bad mask");
-        goto fail1;
+        goto fail0;
     }
     
     // build prefix
     if (!ipaddr_ipv4_prefix_from_mask(mask, &o->prefix)) {
         ModuleLog(i, BLOG_ERROR, "bad mask");
-        goto fail1;
+        goto fail0;
     }
     
     // signal up
     NCDModuleInst_Backend_Up(i);
     return;
     
-fail1:
-    free(o);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
@@ -243,12 +220,8 @@ fail0:
 static void prefix_func_die (void *vo)
 {
     struct prefix_instance *o = vo;
-    NCDModuleInst *i = o->i;
     
-    // free structure
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 
 static int prefix_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
@@ -272,19 +245,22 @@ static int prefix_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDVa
 static const struct NCDModule modules[] = {
     {
         .type = "ipv4_prefix_to_mask",
-        .func_new = prefix_to_mask_func_init,
+        .func_new2 = prefix_to_mask_func_init,
         .func_die = addr_func_die,
-        .func_getvar = addr_func_getvar
+        .func_getvar = addr_func_getvar,
+        .alloc_size = sizeof(struct addr_instance)
     }, {
         .type = "ipv4_mask_to_prefix",
-        .func_new = mask_to_prefix_func_init,
+        .func_new2 = mask_to_prefix_func_init,
         .func_die = prefix_func_die,
-        .func_getvar = prefix_func_getvar
+        .func_getvar = prefix_func_getvar,
+        .alloc_size = sizeof(struct prefix_instance)
     }, {
         .type = "ipv4_net_from_addr_and_prefix",
-        .func_new = ipv4_net_from_addr_and_prefix_func_init,
+        .func_new2 = ipv4_net_from_addr_and_prefix_func_init,
         .func_die = addr_func_die,
-        .func_getvar = addr_func_getvar
+        .func_getvar = addr_func_getvar,
+        .alloc_size = sizeof(struct addr_instance)
     }, {
         .type = NULL
     }

+ 34 - 39
ncd/modules/valuemetic.c

@@ -100,21 +100,16 @@ static int compute_different (NCDValRef v1, NCDValRef v2)
     return NCDVal_Compare(v1, v2) != 0;
 }
 
-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;
-    NCDModuleInst_Backend_SetUser(i, o);
     
     NCDValRef v1_arg;
     NCDValRef v2_arg;
     if (!NCDVal_ListRead(i->args, 2, &v1_arg, &v2_arg)) {
         ModuleLog(i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     
     o->result = cfunc(v1_arg, v2_arg);
@@ -122,8 +117,6 @@ static void new_templ (NCDModuleInst *i, compute_func cfunc)
     NCDModuleInst_Backend_Up(i);
     return;
     
-fail1:
-    free(o);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
@@ -132,12 +125,8 @@ fail0:
 static void func_die (void *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)
@@ -157,67 +146,73 @@ static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *o
     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 const struct NCDModule modules[] = {
     {
         .type = "val_lesser",
-        .func_new = func_new_lesser,
+        .func_new2 = func_new_lesser,
         .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
         .type = "val_greater",
-        .func_new = func_new_greater,
+        .func_new2 = func_new_greater,
         .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
         .type = "val_lesser_equal",
-        .func_new = func_new_lesser_equal,
+        .func_new2 = func_new_lesser_equal,
         .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
         .type = "val_greater_equal",
-        .func_new = func_new_greater_equal,
+        .func_new2 = func_new_greater_equal,
         .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
         .type = "val_equal",
-        .func_new = func_new_equal,
+        .func_new2 = func_new_equal,
         .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
         .type = "val_different",
-        .func_new = func_new_different,
+        .func_new2 = func_new_different,
         .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
         .type = NULL
     }