Bläddra i källkod

ncd/modules/net_ipv4_addr.c: port to func_new2

ambrop7 13 år sedan
förälder
incheckning
544a2838cb
1 ändrade filer med 12 tillägg och 23 borttagningar
  1. 12 23
      ncd/modules/net_ipv4_addr.c

+ 12 - 23
ncd/modules/net_ipv4_addr.c

@@ -56,16 +56,10 @@ struct instance {
     struct ipv4_ifaddr ifaddr;
     struct ipv4_ifaddr ifaddr;
 };
 };
 
 
-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 ifname_arg;
     NCDValRef ifname_arg;
@@ -75,13 +69,13 @@ static void func_new (NCDModuleInst *i)
         !NCDVal_ListRead(i->args, 3, &ifname_arg, &addr_arg, &prefix_arg)
         !NCDVal_ListRead(i->args, 3, &ifname_arg, &addr_arg, &prefix_arg)
     ) {
     ) {
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
         ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        goto fail0;
     }
     }
     if (!NCDVal_IsStringNoNulls(ifname_arg) || !NCDVal_IsStringNoNulls(addr_arg) ||
     if (!NCDVal_IsStringNoNulls(ifname_arg) || !NCDVal_IsStringNoNulls(addr_arg) ||
         (!NCDVal_IsInvalid(prefix_arg) && !NCDVal_IsStringNoNulls(prefix_arg))
         (!NCDVal_IsInvalid(prefix_arg) && !NCDVal_IsStringNoNulls(prefix_arg))
     ) {
     ) {
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     o->ifname = NCDVal_StringValue(ifname_arg);
     o->ifname = NCDVal_StringValue(ifname_arg);
@@ -89,32 +83,30 @@ static void func_new (NCDModuleInst *i)
     if (NCDVal_IsInvalid(prefix_arg)) {
     if (NCDVal_IsInvalid(prefix_arg)) {
         if (!ipaddr_parse_ipv4_ifaddr(NCDVal_StringValue(addr_arg), &o->ifaddr)) {
         if (!ipaddr_parse_ipv4_ifaddr(NCDVal_StringValue(addr_arg), &o->ifaddr)) {
             ModuleLog(o->i, BLOG_ERROR, "wrong CIDR notation address");
             ModuleLog(o->i, BLOG_ERROR, "wrong CIDR notation address");
-            goto fail1;
+            goto fail0;
         }
         }
     } else {
     } else {
         if (!ipaddr_parse_ipv4_addr(NCDVal_StringValue(addr_arg), &o->ifaddr.addr)) {
         if (!ipaddr_parse_ipv4_addr(NCDVal_StringValue(addr_arg), &o->ifaddr.addr)) {
             ModuleLog(o->i, BLOG_ERROR, "wrong address");
             ModuleLog(o->i, BLOG_ERROR, "wrong address");
-            goto fail1;
+            goto fail0;
         }
         }
         
         
         if (!ipaddr_parse_ipv4_prefix(NCDVal_StringValue(prefix_arg), &o->ifaddr.prefix)) {
         if (!ipaddr_parse_ipv4_prefix(NCDVal_StringValue(prefix_arg), &o->ifaddr.prefix)) {
             ModuleLog(o->i, BLOG_ERROR, "wrong prefix");
             ModuleLog(o->i, BLOG_ERROR, "wrong prefix");
-            goto fail1;
+            goto fail0;
         }
         }
     }
     }
     
     
     // add address
     // add address
     if (!NCDIfConfig_add_ipv4_addr(o->ifname, o->ifaddr)) {
     if (!NCDIfConfig_add_ipv4_addr(o->ifname, o->ifaddr)) {
         ModuleLog(o->i, BLOG_ERROR, "failed to add IP address");
         ModuleLog(o->i, BLOG_ERROR, "failed to add IP address");
-        goto fail1;
+        goto fail0;
     }
     }
     
     
     // signal up
     // signal up
     NCDModuleInst_Backend_Up(o->i);
     NCDModuleInst_Backend_Up(o->i);
     return;
     return;
     
     
-fail1:
-    free(o);
 fail0:
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
     NCDModuleInst_Backend_Dead(i);
@@ -123,24 +115,21 @@ 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;
     
     
     // remove address
     // remove address
     if (!NCDIfConfig_remove_ipv4_addr(o->ifname, o->ifaddr)) {
     if (!NCDIfConfig_remove_ipv4_addr(o->ifname, o->ifaddr)) {
         ModuleLog(o->i, BLOG_ERROR, "failed to remove IP address");
         ModuleLog(o->i, BLOG_ERROR, "failed to remove IP address");
     }
     }
     
     
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
+    NCDModuleInst_Backend_Dead(o->i);
 }
 }
 
 
 static const struct NCDModule modules[] = {
 static const struct NCDModule modules[] = {
     {
     {
         .type = "net.ipv4.addr",
         .type = "net.ipv4.addr",
-        .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
     }
     }