Эх сурвалжийг харах

ncd: modules: port some more more modules to func_new2

ambrop7 13 жил өмнө
parent
commit
429b5eaed0

+ 9 - 20
ncd/modules/file.c

@@ -105,40 +105,32 @@ struct stat_instance {
     struct stat result;
 };
 
-static void read_func_new (NCDModuleInst *i)
+static void read_func_new (void *vo, NCDModuleInst *i)
 {
-    // allocate instance
-    struct read_instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
+    struct read_instance *o = vo;
     o->i = i;
-    NCDModuleInst_Backend_SetUser(i, o);
     
     // read arguments
     NCDValRef filename_arg;
     if (!NCDVal_ListRead(i->args, 1, &filename_arg)) {
         ModuleLog(i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     if (!NCDVal_IsStringNoNulls(filename_arg)) {
         ModuleLog(i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     
     // read file
     if (!read_file(NCDVal_StringValue(filename_arg), &o->file_data, &o->file_len)) {
         ModuleLog(i, BLOG_ERROR, "failed to read file");
-        goto fail1;
+        goto fail0;
     }
     
     // signal up
     NCDModuleInst_Backend_Up(i);
     return;
     
-fail1:
-    free(o);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
@@ -147,15 +139,11 @@ fail0:
 static void read_func_die (void *vo)
 {
     struct read_instance *o = vo;
-    NCDModuleInst *i = o->i;
     
     // free data
     free(o->file_data);
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 
 static int read_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
@@ -320,9 +308,10 @@ static int stat_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValR
 static const struct NCDModule modules[] = {
     {
         .type = "file_read",
-        .func_new = read_func_new,
+        .func_new2 = read_func_new,
         .func_die = read_func_die,
-        .func_getvar = read_func_getvar
+        .func_getvar = read_func_getvar,
+        .alloc_size = sizeof(struct read_instance)
     }, {
         .type = "file_write",
         .func_new = write_func_new

+ 10 - 21
ncd/modules/from_string.c

@@ -50,26 +50,20 @@ struct instance {
     NCDValRef val;
 };
 
-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 str_arg;
     if (!NCDVal_ListRead(i->args, 1, &str_arg)) {
         ModuleLog(i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     if (!NCDVal_IsStringNoNulls(str_arg)) {
         ModuleLog(i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     const char *str = NCDVal_StringValue(str_arg);
     
@@ -79,17 +73,15 @@ static void func_new (NCDModuleInst *i)
     // parse value string
     if (!NCDValParser_Parse(str, strlen(str), &o->mem, &o->val)) {
         ModuleLog(i, BLOG_ERROR, "failed to parse");
-        goto fail2;
+        goto fail1;
     }
     
     // signal up
     NCDModuleInst_Backend_Up(i);
     return;
     
-fail2:
-    NCDValMem_Free(&o->mem);
 fail1:
-    free(o);
+    NCDValMem_Free(&o->mem);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
@@ -98,15 +90,11 @@ fail0:
 static void func_die (void *vo)
 {
     struct instance *o = vo;
-    NCDModuleInst *i = o->i;
     
     // free mem
     NCDValMem_Free(&o->mem);
     
-    // 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)
@@ -127,9 +115,10 @@ static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *o
 static const struct NCDModule modules[] = {
     {
         .type = "from_string",
-        .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 - 24
ncd/modules/implode.c

@@ -52,34 +52,28 @@ struct instance {
     size_t result_len;
 };
 
-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;
-    NCDModuleInst_Backend_SetUser(i, o);
     
     // read arguments
     NCDValRef glue_arg;
     NCDValRef pieces_arg;
     if (!NCDVal_ListRead(i->args, 2, &glue_arg, &pieces_arg)) {
         ModuleLog(i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     if (!NCDVal_IsString(glue_arg) || !NCDVal_IsList(pieces_arg)) {
         ModuleLog(i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     
     // init result string
     ExpString str;
     if (!ExpString_Init(&str)) {
         ModuleLog(i, BLOG_ERROR, "ExpString_Init failed");
-        goto fail1;
+        goto fail0;
     }
     
     size_t count = NCDVal_ListCount(pieces_arg);
@@ -89,21 +83,21 @@ static void func_new (NCDModuleInst *i)
         // check piece type
         if (!NCDVal_IsString(piece)) {
             ModuleLog(i, BLOG_ERROR, "wrong piece type");
-            goto fail2;
+            goto fail1;
         }
         
         // append glue
         if (j > 0) {
             if (!ExpString_AppendBinary(&str, (const uint8_t *)NCDVal_StringValue(glue_arg), NCDVal_StringLength(glue_arg))) {
                 ModuleLog(i, BLOG_ERROR, "ExpString_AppendBinary failed");
-                goto fail2;
+                goto fail1;
             }
         }
         
         // append piece
         if (!ExpString_AppendBinary(&str, (const uint8_t *)NCDVal_StringValue(piece), NCDVal_StringLength(piece))) {
             ModuleLog(i, BLOG_ERROR, "ExpString_AppendBinary failed");
-            goto fail2;
+            goto fail1;
         }
     }
     
@@ -115,10 +109,8 @@ static void func_new (NCDModuleInst *i)
     NCDModuleInst_Backend_Up(i);
     return;
     
-fail2:
-    ExpString_Free(&str);
 fail1:
-    free(o);
+    ExpString_Free(&str);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
@@ -127,15 +119,11 @@ fail0:
 static void func_die (void *vo)
 {
     struct instance *o = vo;
-    NCDModuleInst *i = o->i;
     
     // free result
     free(o->result);
     
-    // 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)
@@ -156,9 +144,10 @@ static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *o
 static const struct NCDModule modules[] = {
     {
         .type = "implode",
-        .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
     }

+ 11 - 22
ncd/modules/ip_in_network.c

@@ -51,16 +51,10 @@ struct instance {
     int value;
 };
 
-static void func_new (NCDModuleInst *i)
+static void func_new (void *vo, NCDModuleInst *i)
 {
-    // allocate instance
-    struct instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
+    struct instance *o = vo;
     o->i = i;
-    NCDModuleInst_Backend_SetUser(i, o);
     
     // read arguments
     NCDValRef arg_addr1;
@@ -68,11 +62,11 @@ static void func_new (NCDModuleInst *i)
     NCDValRef arg_netprefix;
     if (!NCDVal_ListRead(o->i->args, 3, &arg_addr1, &arg_addr2, &arg_netprefix)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     if (!NCDVal_IsStringNoNulls(arg_addr1) || !NCDVal_IsStringNoNulls(arg_addr2) || !NCDVal_IsStringNoNulls(arg_netprefix)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     
     // parse
@@ -81,15 +75,15 @@ static void func_new (NCDModuleInst *i)
     int netprefix;
     if (!ipaddr_parse_ipv4_addr((char *)NCDVal_StringValue(arg_addr1), &addr1)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong addr1");
-        goto fail1;
+        goto fail0;
     }
     if (!ipaddr_parse_ipv4_addr((char *)NCDVal_StringValue(arg_addr2), &addr2)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong addr2");
-        goto fail1;
+        goto fail0;
     }
     if (!ipaddr_parse_ipv4_prefix((char *)NCDVal_StringValue(arg_netprefix), &netprefix)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong netprefix");
-        goto fail1;
+        goto fail0;
     }
     
     // test
@@ -99,8 +93,6 @@ static void func_new (NCDModuleInst *i)
     NCDModuleInst_Backend_Up(o->i);
     return;
     
-fail1:
-    free(o);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
@@ -109,12 +101,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)
@@ -137,9 +125,10 @@ static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *o
 static const struct NCDModule modules[] = {
     {
         .type = "ip_in_network",
-        .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
     }

+ 20 - 29
ncd/modules/parse.c

@@ -126,26 +126,20 @@ static int parse_ipv4_addr (NCDModuleInst *i, const char *str, NCDValMem *mem, N
     return 1;
 }
 
-static void new_templ (NCDModuleInst *i, parse_func pfunc)
+static void new_templ (void *vo, NCDModuleInst *i, parse_func pfunc)
 {
-    // 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;
-    NCDModuleInst_Backend_SetUser(i, o);
     
     // read arguments
     NCDValRef str_arg;
     if (!NCDVal_ListRead(i->args, 1, &str_arg)) {
         ModuleLog(i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     if (!NCDVal_IsString(str_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     
     // init mem
@@ -163,8 +157,6 @@ static void new_templ (NCDModuleInst *i, parse_func pfunc)
     NCDModuleInst_Backend_Up(i);
     return;
     
-fail1:
-    free(o);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
@@ -173,15 +165,11 @@ fail0:
 static void func_die (void *vo)
 {
     struct instance *o = vo;
-    NCDModuleInst *i = o->i;
     
     // free mem
     NCDValMem_Free(&o->mem);
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 
 static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
@@ -208,19 +196,19 @@ static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *o
     return 0;
 }
 
-static void func_new_parse_number (NCDModuleInst *i)
+static void func_new_parse_number (void *vo, NCDModuleInst *i)
 {
-    new_templ(i, parse_number);
+    new_templ(vo, i, parse_number);
 }
 
-static void func_new_parse_value (NCDModuleInst *i)
+static void func_new_parse_value (void *vo, NCDModuleInst *i)
 {
-    new_templ(i, parse_value);
+    new_templ(vo, i, parse_value);
 }
 
-static void func_new_parse_ipv4_addr (NCDModuleInst *i)
+static void func_new_parse_ipv4_addr (void *vo, NCDModuleInst *i)
 {
-    new_templ(i, parse_ipv4_addr);
+    new_templ(vo, i, parse_ipv4_addr);
 }
 
 static void ipv4_cidr_addr_func_new (void *vo, NCDModuleInst *i)
@@ -290,19 +278,22 @@ static int ipv4_cidr_addr_func_getvar (void *vo, const char *name, NCDValMem *me
 static const struct NCDModule modules[] = {
     {
         .type = "parse_number",
-        .func_new = func_new_parse_number,
+        .func_new2 = func_new_parse_number,
         .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
         .type = "parse_value",
-        .func_new = func_new_parse_value,
+        .func_new2 = func_new_parse_value,
         .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
         .type = "parse_ipv4_addr",
-        .func_new = func_new_parse_ipv4_addr,
+        .func_new2 = func_new_parse_ipv4_addr,
         .func_die = func_die,
-        .func_getvar = func_getvar
+        .func_getvar = func_getvar,
+        .alloc_size = sizeof(struct instance)
     }, {
         .type = "parse_ipv4_cidr_addr",
         .func_new2 = ipv4_cidr_addr_func_new,

+ 13 - 28
ncd/modules/sys_evdev.c

@@ -141,59 +141,48 @@ static void device_nextevent (struct instance *o)
     NCDModuleInst_Backend_Down(o->i);
 }
 
-static void func_new (NCDModuleInst *i)
+static void func_new (void *vo, NCDModuleInst *i)
 {
-    // allocate instance
-    struct instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
-    NCDModuleInst_Backend_SetUser(i, o);
-    
-    // init arguments
+    struct instance *o = vo;
     o->i = i;
     
     // check arguments
     NCDValRef device_arg;
     if (!NCDVal_ListRead(o->i->args, 1, &device_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     if (!NCDVal_IsStringNoNulls(device_arg)) {
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     
     // open device
     if ((o->evdev_fd = open(NCDVal_StringValue(device_arg), O_RDONLY)) < 0) {
         ModuleLog(o->i, BLOG_ERROR, "open failed");
-        goto fail1;
+        goto fail0;
     }
     
     // set non-blocking
     if (!badvpn_set_nonblocking(o->evdev_fd)) {
         ModuleLog(o->i, BLOG_ERROR, "badvpn_set_nonblocking failed");
-        goto fail2;
+        goto fail1;
     }
     
     // init BFileDescriptor
     BFileDescriptor_Init(&o->bfd, o->evdev_fd, (BFileDescriptor_handler)device_handler, o);
     if (!BReactor_AddFileDescriptor(o->i->iparams->reactor, &o->bfd)) {
         ModuleLog(o->i, BLOG_ERROR, "BReactor_AddFileDescriptor failed");
-        goto fail2;
+        goto fail1;
     }
     BReactor_SetFileDescriptorEvents(o->i->iparams->reactor, &o->bfd, BREACTOR_READ);
     
     // set not processing
     o->processing = 0;
-    
     return;
     
-fail2:
-    ASSERT_FORCE(close(o->evdev_fd) == 0)
 fail1:
-    free(o);
+    ASSERT_FORCE(close(o->evdev_fd) == 0)
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
@@ -201,8 +190,6 @@ fail0:
 
 void instance_free (struct instance *o, int is_error)
 {
-    NCDModuleInst *i = o->i;
-    
     // free BFileDescriptor
     BReactor_RemoveFileDescriptor(o->i->iparams->reactor, &o->bfd);
     
@@ -212,13 +199,10 @@ void instance_free (struct instance *o, int is_error)
         ModuleLog(o->i, BLOG_ERROR, "close failed");
     }
     
-    // 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 func_die (void *vo)
@@ -342,9 +326,10 @@ fail0:
 static const struct NCDModule modules[] = {
     {
         .type = "sys.evdev",
-        .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 = "sys.evdev::nextevent",
         .func_new = nextevent_func_new