Ver Fonte

ncd: modules: don't allocate an instance structure where this isn't needed

ambrop7 há 14 anos atrás
pai
commit
c9301f20fa

+ 7 - 37
ncd/modules/if.c

@@ -48,32 +48,17 @@
 
 #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
 
-struct instance {
-    NCDModuleInst *i;
-};
-
 static void new_templ (NCDModuleInst *i, int not)
 {
-    // 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
-    o->i = i;
-    
     // check arguments
     NCDValue *arg;
     if (!NCDValue_ListRead(i->args, 1, &arg)) {
-        ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        ModuleLog(i, BLOG_ERROR, "wrong arity");
+        goto fail0;
     }
     if (NCDValue_Type(arg) != NCDVALUE_STRING) {
-        ModuleLog(o->i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        ModuleLog(i, BLOG_ERROR, "wrong type");
+        goto fail0;
     }
     
     // compute logical value of argument
@@ -81,13 +66,11 @@ static void new_templ (NCDModuleInst *i, int not)
     
     // signal up if needed
     if ((not && !c) || (!not && c)) {
-        NCDModuleInst_Backend_Up(o->i);
+        NCDModuleInst_Backend_Up(i);
     }
     
     return;
     
-fail1:
-    free(o);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
@@ -103,26 +86,13 @@ static void func_new_not (NCDModuleInst *i)
     new_templ(i, 1);
 }
 
-static void func_die (void *vo)
-{
-    struct instance *o = vo;
-    NCDModuleInst *i = o->i;
-    
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
-}
-
 static const struct NCDModule modules[] = {
     {
         .type = "if",
-        .func_new = func_new,
-        .func_die = func_die
+        .func_new = func_new
     }, {
         .type = "ifnot",
-        .func_new = func_new_not,
-        .func_die = func_die
+        .func_new = func_new_not
     }, {
         .type = NULL
     }

+ 47 - 223
ncd/modules/list.c

@@ -104,14 +104,6 @@ struct instance {
     NCDValue list;
 };
 
-struct append_instance {
-    NCDModuleInst *i;
-};
-
-struct appendv_instance {
-    NCDModuleInst *i;
-};
-
 struct length_instance {
     NCDModuleInst *i;
     size_t length;
@@ -122,10 +114,6 @@ struct get_instance {
     NCDValue value;
 };
 
-struct shift_instance {
-    NCDModuleInst *i;
-};
-
 struct contains_instance {
     NCDModuleInst *i;
     int contains;
@@ -137,18 +125,6 @@ struct find_instance {
     size_t found_pos;
 };
 
-struct removeat_instance {
-    NCDModuleInst *i;
-};
-
-struct remove_instance {
-    NCDModuleInst *i;
-};
-
-struct set_instance {
-    NCDModuleInst *i;
-};
-
 static int append_list_args (NCDModuleInst *i, NCDValue *list, NCDValue *args)
 {
     ASSERT(NCDValue_Type(list) == NCDVALUE_LIST)
@@ -301,22 +277,11 @@ static int func_getvar (void *vo, const char *name, NCDValue *out)
 
 static void append_func_new (NCDModuleInst *i)
 {
-    // allocate instance
-    struct append_instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
-    NCDModuleInst_Backend_SetUser(i, o);
-    
-    // init arguments
-    o->i = i;
-    
     // check arguments
     NCDValue *arg;
-    if (!NCDValue_ListRead(o->i->args, 1, &arg)) {
-        ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+    if (!NCDValue_ListRead(i->args, 1, &arg)) {
+        ModuleLog(i, BLOG_ERROR, "wrong arity");
+        goto fail0;
     }
     
     // get method object
@@ -325,60 +290,35 @@ static void append_func_new (NCDModuleInst *i)
     // append
     NCDValue v;
     if (!NCDValue_InitCopy(&v, arg)) {
-        ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
-        goto fail1;
+        ModuleLog(i, BLOG_ERROR, "NCDValue_InitCopy failed");
+        goto fail0;
     }
     if (!NCDValue_ListAppend(&mo->list, v)) {
         NCDValue_Free(&v);
-        ModuleLog(o->i, BLOG_ERROR, "NCDValue_ListAppend failed");
-        goto fail1;
+        ModuleLog(i, BLOG_ERROR, "NCDValue_ListAppend failed");
+        goto fail0;
     }
     
     // signal up
-    NCDModuleInst_Backend_Up(o->i);
-    
+    NCDModuleInst_Backend_Up(i);
     return;
     
-fail1:
-    free(o);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
 }
 
-static void append_func_die (void *vo)
-{
-    struct append_instance *o = vo;
-    NCDModuleInst *i = o->i;
-    
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
-}
-
 static void appendv_func_new (NCDModuleInst *i)
 {
-    // allocate instance
-    struct appendv_instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
-    NCDModuleInst_Backend_SetUser(i, o);
-    
-    // init arguments
-    o->i = i;
-    
     // check arguments
     NCDValue *arg;
-    if (!NCDValue_ListRead(o->i->args, 1, &arg)) {
-        ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+    if (!NCDValue_ListRead(i->args, 1, &arg)) {
+        ModuleLog(i, BLOG_ERROR, "wrong arity");
+        goto fail0;
     }
     if (NCDValue_Type(arg) != NCDVALUE_LIST) {
-        ModuleLog(o->i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        ModuleLog(i, BLOG_ERROR, "wrong type");
+        goto fail0;
     }
     
     // get method object
@@ -387,38 +327,24 @@ static void appendv_func_new (NCDModuleInst *i)
     // append
     NCDValue l;
     if (!NCDValue_InitCopy(&l, arg)) {
-        ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
-        goto fail1;
+        ModuleLog(i, BLOG_ERROR, "NCDValue_InitCopy failed");
+        goto fail0;
     }
     if (!NCDValue_ListAppendList(&mo->list, l)) {
-        ModuleLog(o->i, BLOG_ERROR, "NCDValue_ListAppendList failed");
+        ModuleLog(i, BLOG_ERROR, "NCDValue_ListAppendList failed");
         NCDValue_Free(&l);
-        goto fail1;
+        goto fail0;
     }
     
     // signal up
-    NCDModuleInst_Backend_Up(o->i);
-    
+    NCDModuleInst_Backend_Up(i);
     return;
     
-fail1:
-    free(o);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
 }
 
-static void appendv_func_die (void *vo)
-{
-    struct appendv_instance *o = vo;
-    NCDModuleInst *i = o->i;
-    
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
-}
-
 static void length_func_new (NCDModuleInst *i)
 {
     // allocate instance
@@ -574,21 +500,10 @@ static int get_func_getvar (void *vo, const char *name, NCDValue *out)
 
 static void shift_func_new (NCDModuleInst *i)
 {
-    // allocate instance
-    struct shift_instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
-    NCDModuleInst_Backend_SetUser(i, o);
-    
-    // init arguments
-    o->i = i;
-    
     // check arguments
-    if (!NCDValue_ListRead(o->i->args, 0)) {
-        ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+    if (!NCDValue_ListRead(i->args, 0)) {
+        ModuleLog(i, BLOG_ERROR, "wrong arity");
+        goto fail0;
     }
     
     // get method object
@@ -596,35 +511,21 @@ static void shift_func_new (NCDModuleInst *i)
     
     // shift
     if (!NCDValue_ListFirst(&mo->list)) {
-        ModuleLog(o->i, BLOG_ERROR, "list has no elements");
-        goto fail1;
+        ModuleLog(i, BLOG_ERROR, "list has no elements");
+        goto fail0;
     }
     NCDValue v = NCDValue_ListShift(&mo->list);
     NCDValue_Free(&v);
     
     // signal up
-    NCDModuleInst_Backend_Up(o->i);
-    
+    NCDModuleInst_Backend_Up(i);
     return;
     
-fail1:
-    free(o);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
 }
 
-static void shift_func_die (void *vo)
-{
-    struct shift_instance *o = vo;
-    NCDModuleInst *i = o->i;
-    
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
-}
-
 static void contains_func_new (NCDModuleInst *i)
 {
     // allocate instance
@@ -803,33 +704,22 @@ static int find_func_getvar (void *vo, const char *name, NCDValue *out)
 
 static void removeat_func_new (NCDModuleInst *i)
 {
-    // allocate instance
-    struct removeat_instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
-    NCDModuleInst_Backend_SetUser(i, o);
-    
-    // init arguments
-    o->i = i;
-    
     // read arguments
     NCDValue *remove_pos_arg;
     if (!NCDValue_ListRead(i->args, 1, &remove_pos_arg)) {
-        ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        ModuleLog(i, BLOG_ERROR, "wrong arity");
+        goto fail0;
     }
     if (!NCDValue_IsStringNoNulls(remove_pos_arg)) {
-        ModuleLog(o->i, BLOG_ERROR, "wrong type");
-        goto fail1;
+        ModuleLog(i, BLOG_ERROR, "wrong type");
+        goto fail0;
     }
     
     // read position
     uintmax_t remove_pos;
     if (!parse_unsigned_integer(NCDValue_StringValue(remove_pos_arg), &remove_pos)) {
-        ModuleLog(o->i, BLOG_ERROR, "wrong pos");
-        goto fail1;
+        ModuleLog(i, BLOG_ERROR, "wrong pos");
+        goto fail0;
     }
     
     // get method object
@@ -837,8 +727,8 @@ static void removeat_func_new (NCDModuleInst *i)
     
     // check position
     if (remove_pos >= NCDValue_ListCount(&mo->list)) {
-        ModuleLog(o->i, BLOG_ERROR, "pos out of range");
-        goto fail1;
+        ModuleLog(i, BLOG_ERROR, "pos out of range");
+        goto fail0;
     }
     
     // find and remove
@@ -853,45 +743,21 @@ static void removeat_func_new (NCDModuleInst *i)
     }
     
     // signal up
-    NCDModuleInst_Backend_Up(o->i);
+    NCDModuleInst_Backend_Up(i);
     return;
     
-fail1:
-    free(o);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
 }
 
-static void removeat_func_die (void *vo)
-{
-    struct removeat_instance *o = vo;
-    NCDModuleInst *i = o->i;
-    
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
-}
-
 static void remove_func_new (NCDModuleInst *i)
 {
-    // allocate instance
-    struct remove_instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
-    NCDModuleInst_Backend_SetUser(i, o);
-    
-    // init arguments
-    o->i = i;
-    
     // read arguments
     NCDValue *value_arg;
     if (!NCDValue_ListRead(i->args, 1, &value_arg)) {
-        ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+        ModuleLog(i, BLOG_ERROR, "wrong arity");
+        goto fail0;
     }
     
     // get method object
@@ -900,8 +766,8 @@ static void remove_func_new (NCDModuleInst *i)
     // find value
     NCDValue *e = find_in_list(&mo->list, value_arg);
     if (!e) {
-        ModuleLog(o->i, BLOG_ERROR, "value does not exist");
-        goto fail1;
+        ModuleLog(i, BLOG_ERROR, "value does not exist");
+        goto fail0;
     }
     
     // remove it
@@ -909,47 +775,23 @@ static void remove_func_new (NCDModuleInst *i)
     NCDValue_Free(&removed_v);
     
     // signal up
-    NCDModuleInst_Backend_Up(o->i);
+    NCDModuleInst_Backend_Up(i);
     return;
     
-fail1:
-    free(o);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
 }
 
-static void remove_func_die (void *vo)
-{
-    struct remove_instance *o = vo;
-    NCDModuleInst *i = o->i;
-    
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
-}
-
 static void set_func_new (NCDModuleInst *i)
 {
-    // allocate instance
-    struct set_instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
-    NCDModuleInst_Backend_SetUser(i, o);
-    
-    // init arguments
-    o->i = i;
-    
     // init list
     NCDValue list;
     NCDValue_InitList(&list);
     
     // append to list
     if (!append_list_args(i, &list, i->args)) {
-        goto fail2;
+        goto fail1;
     }
     
     // get method object
@@ -960,28 +802,16 @@ static void set_func_new (NCDModuleInst *i)
     mo->list = list;
     
     // signal up
-    NCDModuleInst_Backend_Up(o->i);
+    NCDModuleInst_Backend_Up(i);
     return;
     
-fail2:
+fail1:
     NCDValue_Free(&list);
-    free(o);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
 }
 
-static void set_func_die (void *vo)
-{
-    struct set_instance *o = vo;
-    NCDModuleInst *i = o->i;
-    
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
-}
-
 static const struct NCDModule modules[] = {
     {
         .type = "list",
@@ -1002,12 +832,10 @@ static const struct NCDModule modules[] = {
         .func_getvar = func_getvar
     }, {
         .type = "list::append",
-        .func_new = append_func_new,
-        .func_die = append_func_die
+        .func_new = append_func_new
     }, {
         .type = "list::appendv",
-        .func_new = appendv_func_new,
-        .func_die = appendv_func_die
+        .func_new = appendv_func_new
     }, {
         .type = "list::length",
         .func_new = length_func_new,
@@ -1020,8 +848,7 @@ static const struct NCDModule modules[] = {
         .func_getvar = get_func_getvar
     }, {
         .type = "list::shift",
-        .func_new = shift_func_new,
-        .func_die = shift_func_die
+        .func_new = shift_func_new
     }, {
         .type = "list::contains",
         .func_new = contains_func_new,
@@ -1034,16 +861,13 @@ static const struct NCDModule modules[] = {
         .func_getvar = find_func_getvar
     }, {
         .type = "list::remove_at",
-        .func_new = removeat_func_new,
-        .func_die = removeat_func_die
+        .func_new = removeat_func_new
     }, {
         .type = "list::remove",
-        .func_new = remove_func_new,
-        .func_die = remove_func_die
+        .func_new = remove_func_new
     }, {
         .type = "list::set",
-        .func_new = set_func_new,
-        .func_die = set_func_die
+        .func_new = set_func_new
     }, {
         .type = NULL
     }

+ 7 - 37
ncd/modules/net_watch_interfaces.c

@@ -83,10 +83,6 @@ struct instance {
     event_template templ;
 };
 
-struct nextevent_instance {
-    NCDModuleInst *i;
-};
-
 static void templ_func_free (struct instance *o);
 
 static struct device * find_device_by_ifname (struct instance *o, const char *ifname)
@@ -442,21 +438,10 @@ static int func_getvar (void *vo, const char *name, NCDValue *out)
 
 static void nextevent_func_new (NCDModuleInst *i)
 {
-    // allocate instance
-    struct nextevent_instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
-    NCDModuleInst_Backend_SetUser(i, o);
-    
-    // init arguments
-    o->i = i;
-    
     // check arguments
-    if (!NCDValue_ListRead(o->i->args, 0)) {
-        ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+    if (!NCDValue_ListRead(i->args, 0)) {
+        ModuleLog(i, BLOG_ERROR, "wrong arity");
+        goto fail0;
     }
     
     // get method object
@@ -464,38 +449,24 @@ static void nextevent_func_new (NCDModuleInst *i)
     
     // make sure we are currently reporting an event
     if (!event_template_is_enabled(&mo->templ)) {
-        ModuleLog(o->i, BLOG_ERROR, "not reporting an event");
-        goto fail1;
+        ModuleLog(i, BLOG_ERROR, "not reporting an event");
+        goto fail0;
     }
     
     // signal up.
     // Do it before finishing the event so our process does not advance any further if
     // we would be killed the event provider going down.
-    NCDModuleInst_Backend_Up(o->i);
+    NCDModuleInst_Backend_Up(i);
     
     // wait for next event
     next_event(mo);
-    
     return;
     
-fail1:
-    free(o);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
 }
 
-static void nextevent_func_die (void *vo)
-{
-    struct nextevent_instance *o = vo;
-    NCDModuleInst *i = o->i;
-    
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
-}
-
 static const struct NCDModule modules[] = {
     {
         .type = "net.watch_interfaces",
@@ -504,8 +475,7 @@ static const struct NCDModule modules[] = {
         .func_getvar = func_getvar
     }, {
         .type = "net.watch_interfaces::nextevent",
-        .func_new = nextevent_func_new,
-        .func_die = nextevent_func_die
+        .func_new = nextevent_func_new
     }, {
         .type = NULL
     }

+ 7 - 37
ncd/modules/sys_evdev.c

@@ -75,10 +75,6 @@ struct instance {
     struct input_event event;
 };
 
-struct nextevent_instance {
-    NCDModuleInst *i;
-};
-
 static void instance_free (struct instance *o, int is_error);
 
 #define MAKE_LOOKUP_FUNC(_name_) \
@@ -315,21 +311,10 @@ static int func_getvar (void *vo, const char *name, NCDValue *out)
 
 static void nextevent_func_new (NCDModuleInst *i)
 {
-    // allocate instance
-    struct nextevent_instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
-    NCDModuleInst_Backend_SetUser(i, o);
-    
-    // init arguments
-    o->i = i;
-    
     // check arguments
-    if (!NCDValue_ListRead(o->i->args, 0)) {
-        ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+    if (!NCDValue_ListRead(i->args, 0)) {
+        ModuleLog(i, BLOG_ERROR, "wrong arity");
+        goto fail0;
     }
     
     // get method object
@@ -337,38 +322,24 @@ static void nextevent_func_new (NCDModuleInst *i)
     
     // make sure we are currently reporting an event
     if (!mo->processing) {
-        ModuleLog(o->i, BLOG_ERROR, "not reporting an event");
-        goto fail1;
+        ModuleLog(i, BLOG_ERROR, "not reporting an event");
+        goto fail0;
     }
     
     // signal up.
     // Do it before finishing the event so our process does not advance any further if
     // we would be killed the event provider going down.
-    NCDModuleInst_Backend_Up(o->i);
+    NCDModuleInst_Backend_Up(i);
     
     // wait for next event
     device_nextevent(mo);
-    
     return;
     
-fail1:
-    free(o);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
 }
 
-static void nextevent_func_die (void *vo)
-{
-    struct nextevent_instance *o = vo;
-    NCDModuleInst *i = o->i;
-    
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
-}
-
 static const struct NCDModule modules[] = {
     {
         .type = "sys.evdev",
@@ -377,8 +348,7 @@ static const struct NCDModule modules[] = {
         .func_getvar = func_getvar
     }, {
         .type = "sys.evdev::nextevent",
-        .func_new = nextevent_func_new,
-        .func_die = nextevent_func_die
+        .func_new = nextevent_func_new
     }, {
         .type = NULL
     }

+ 7 - 37
ncd/modules/sys_watch_directory.c

@@ -76,10 +76,6 @@ struct instance {
     const char *processing_type;
 };
 
-struct nextevent_instance {
-    NCDModuleInst *i;
-};
-
 static void instance_free (struct instance *o, int is_error);
 
 static void next_dir_event (struct instance *o)
@@ -387,21 +383,10 @@ static int func_getvar (void *vo, const char *name, NCDValue *out)
 
 static void nextevent_func_new (NCDModuleInst *i)
 {
-    // allocate instance
-    struct nextevent_instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
-    NCDModuleInst_Backend_SetUser(i, o);
-    
-    // init arguments
-    o->i = i;
-    
     // check arguments
-    if (!NCDValue_ListRead(o->i->args, 0)) {
-        ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+    if (!NCDValue_ListRead(i->args, 0)) {
+        ModuleLog(i, BLOG_ERROR, "wrong arity");
+        goto fail0;
     }
     
     // get method object
@@ -409,38 +394,24 @@ static void nextevent_func_new (NCDModuleInst *i)
     
     // make sure we are currently reporting an event
     if (!mo->processing) {
-        ModuleLog(o->i, BLOG_ERROR, "not reporting an event");
-        goto fail1;
+        ModuleLog(i, BLOG_ERROR, "not reporting an event");
+        goto fail0;
     }
     
     // signal up.
     // Do it before finishing the event so our process does not advance any further if
     // we would be killed the event provider going down.
-    NCDModuleInst_Backend_Up(o->i);
+    NCDModuleInst_Backend_Up(i);
     
     // wait for next event
     next_event(mo);
-    
     return;
     
-fail1:
-    free(o);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
 }
 
-static void nextevent_func_die (void *vo)
-{
-    struct nextevent_instance *o = vo;
-    NCDModuleInst *i = o->i;
-    
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
-}
-
 static const struct NCDModule modules[] = {
     {
         .type = "sys.watch_directory",
@@ -449,8 +420,7 @@ static const struct NCDModule modules[] = {
         .func_getvar = func_getvar
     }, {
         .type = "sys.watch_directory::nextevent",
-        .func_new = nextevent_func_new,
-        .func_die = nextevent_func_die
+        .func_new = nextevent_func_new
     }, {
         .type = NULL
     }

+ 7 - 37
ncd/modules/sys_watch_input.c

@@ -76,10 +76,6 @@ struct instance {
     event_template templ;
 };
 
-struct nextevent_instance {
-    NCDModuleInst *i;
-};
-
 static void templ_func_free (struct instance *o);
 
 static struct device * find_device_by_devname (struct instance *o, const char *devname)
@@ -421,21 +417,10 @@ static int func_getvar (void *vo, const char *name, NCDValue *out)
 
 static void nextevent_func_new (NCDModuleInst *i)
 {
-    // allocate instance
-    struct nextevent_instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
-    NCDModuleInst_Backend_SetUser(i, o);
-    
-    // init arguments
-    o->i = i;
-    
     // check arguments
-    if (!NCDValue_ListRead(o->i->args, 0)) {
-        ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+    if (!NCDValue_ListRead(i->args, 0)) {
+        ModuleLog(i, BLOG_ERROR, "wrong arity");
+        goto fail0;
     }
     
     // get method object
@@ -443,38 +428,24 @@ static void nextevent_func_new (NCDModuleInst *i)
     
     // make sure we are currently reporting an event
     if (!event_template_is_enabled(&mo->templ)) {
-        ModuleLog(o->i, BLOG_ERROR, "not reporting an event");
-        goto fail1;
+        ModuleLog(i, BLOG_ERROR, "not reporting an event");
+        goto fail0;
     }
     
     // signal up.
     // Do it before finishing the event so our process does not advance any further if
     // we would be killed the event provider going down.
-    NCDModuleInst_Backend_Up(o->i);
+    NCDModuleInst_Backend_Up(i);
     
     // wait for next event
     next_event(mo);
-    
     return;
     
-fail1:
-    free(o);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
 }
 
-static void nextevent_func_die (void *vo)
-{
-    struct nextevent_instance *o = vo;
-    NCDModuleInst *i = o->i;
-    
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
-}
-
 static const struct NCDModule modules[] = {
     {
         .type = "sys.watch_input",
@@ -483,8 +454,7 @@ static const struct NCDModule modules[] = {
         .func_getvar = func_getvar
     }, {
         .type = "sys.watch_input::nextevent",
-        .func_new = nextevent_func_new,
-        .func_die = nextevent_func_die
+        .func_new = nextevent_func_new
     }, {
         .type = NULL
     }

+ 7 - 37
ncd/modules/sys_watch_usb.c

@@ -78,10 +78,6 @@ struct instance {
     event_template templ;
 };
 
-struct nextevent_instance {
-    NCDModuleInst *i;
-};
-
 static void templ_func_free (struct instance *o);
 
 static struct device * find_device_by_devname (struct instance *o, const char *devname)
@@ -389,21 +385,10 @@ static int func_getvar (void *vo, const char *name, NCDValue *out)
 
 static void nextevent_func_new (NCDModuleInst *i)
 {
-    // allocate instance
-    struct nextevent_instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
-    NCDModuleInst_Backend_SetUser(i, o);
-    
-    // init arguments
-    o->i = i;
-    
     // check arguments
-    if (!NCDValue_ListRead(o->i->args, 0)) {
-        ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+    if (!NCDValue_ListRead(i->args, 0)) {
+        ModuleLog(i, BLOG_ERROR, "wrong arity");
+        goto fail0;
     }
     
     // get method object
@@ -411,38 +396,24 @@ static void nextevent_func_new (NCDModuleInst *i)
     
     // make sure we are currently reporting an event
     if (!event_template_is_enabled(&mo->templ)) {
-        ModuleLog(o->i, BLOG_ERROR, "not reporting an event");
-        goto fail1;
+        ModuleLog(i, BLOG_ERROR, "not reporting an event");
+        goto fail0;
     }
     
     // signal up.
     // Do it before finishing the event so our process does not advance any further if
     // we would be killed the event provider going down.
-    NCDModuleInst_Backend_Up(o->i);
+    NCDModuleInst_Backend_Up(i);
     
     // wait for next event
     next_event(mo);
-    
     return;
     
-fail1:
-    free(o);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
 }
 
-static void nextevent_func_die (void *vo)
-{
-    struct nextevent_instance *o = vo;
-    NCDModuleInst *i = o->i;
-    
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
-}
-
 static const struct NCDModule modules[] = {
     {
         .type = "sys.watch_usb",
@@ -451,8 +422,7 @@ static const struct NCDModule modules[] = {
         .func_getvar = func_getvar
     }, {
         .type = "sys.watch_usb::nextevent",
-        .func_new = nextevent_func_new,
-        .func_die = nextevent_func_die
+        .func_new = nextevent_func_new
     }, {
         .type = NULL
     }

+ 10 - 38
ncd/modules/var.c

@@ -51,10 +51,6 @@ struct instance {
     NCDValue value;
 };
 
-struct set_instance {
-    NCDModuleInst *i;
-};
-
 static void func_new (NCDModuleInst *i)
 {
     // allocate instance
@@ -125,59 +121,36 @@ static int func_getvar (void *vo, const char *name, NCDValue *out)
 
 static void set_func_new (NCDModuleInst *i)
 {
-    // allocate instance
-    struct set_instance *o = malloc(sizeof(*o));
-    if (!o) {
-        ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
-        goto fail0;
-    }
-    NCDModuleInst_Backend_SetUser(i, o);
-    
-    // init arguments
-    o->i = i;
-    
     // read argument
     NCDValue *value_arg;
-    if (!NCDValue_ListRead(o->i->args, 1, &value_arg)) {
-        ModuleLog(o->i, BLOG_ERROR, "wrong arity");
-        goto fail1;
+    if (!NCDValue_ListRead(i->args, 1, &value_arg)) {
+        ModuleLog(i, BLOG_ERROR, "wrong arity");
+        goto fail0;
     }
     
     // get method object
     struct instance *mo = ((NCDModuleInst * )i->method_user)->inst_user;
     
-    // set
+    // copy value
     NCDValue v;
     if (!NCDValue_InitCopy(&v, value_arg)) {
-        ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
-        goto fail1;
+        ModuleLog(i, BLOG_ERROR, "NCDValue_InitCopy failed");
+        goto fail0;
     }
+    
+    // replace value in var
     NCDValue_Free(&mo->value);
     mo->value = v;
     
     // signal up
-    NCDModuleInst_Backend_Up(o->i);
-    
+    NCDModuleInst_Backend_Up(i);
     return;
     
-fail1:
-    free(o);
 fail0:
     NCDModuleInst_Backend_SetError(i);
     NCDModuleInst_Backend_Dead(i);
 }
 
-static void set_func_die (void *vo)
-{
-    struct set_instance *o = vo;
-    NCDModuleInst *i = o->i;
-    
-    // free instance
-    free(o);
-    
-    NCDModuleInst_Backend_Dead(i);
-}
-
 static const struct NCDModule modules[] = {
     {
         .type = "var",
@@ -186,8 +159,7 @@ static const struct NCDModule modules[] = {
         .func_getvar = func_getvar
     }, {
         .type = "var::set",
-        .func_new = set_func_new,
-        .func_die = set_func_die
+        .func_new = set_func_new
     }, {
         .type = NULL
     }