Quellcode durchsuchen

ncd: NCDModule: remove func_new and port all modules that used it to func_new2

ambrop7 vor 13 Jahren
Ursprung
Commit
0b235e7b6d

+ 2 - 5
ncd/NCDModule.c

@@ -71,6 +71,7 @@ static void inst_assert_backend (NCDModuleInst *n)
 void NCDModuleInst_Init (NCDModuleInst *n, const struct NCDModule *m, void *mem, const NCDObject *method_object, NCDValRef args, const struct NCDModuleInst_params *params, const struct NCDModuleInst_iparams *iparams)
 {
     ASSERT(m)
+    ASSERT(m->func_new2)
     ASSERT(m->alloc_size >= 0)
     ASSERT(!!mem == m->alloc_size > 0)
     ASSERT(NCDVal_IsList(args))
@@ -102,11 +103,7 @@ void NCDModuleInst_Init (NCDModuleInst *n, const struct NCDModule *m, void *mem,
     
     DebugObject_Init(&n->d_obj);
     
-    if (n->m->func_new2) {
-        n->m->func_new2(n->inst_user, n);
-    } else {
-        n->m->func_new(n);
-    }
+    n->m->func_new2(n->inst_user, n);
 }
 
 void NCDModuleInst_Free (NCDModuleInst *n)

+ 7 - 29
ncd/NCDModule.h

@@ -647,27 +647,16 @@ typedef void (*NCDModule_func_globalfree) (void);
  * Handler called to create an new module backend instance.
  * The backend is initialized in down state.
  * 
- * This handler should call {@link NCDModuleInst_Backend_SetUser} to provide
- * an argument for handlers of this backend instance if it needs to keep any
- * kind of state. Alternatively, the module can have the interpreter preallocate
- * a predefined amount of memory for each instance, in which case the
- * {@link NCDModule_func_new2} init function should be used instead of this one
- * (see alloc_size in the {@link NCDModule} structure).
- * 
  * If the backend fails initialization, this function should report the backend
  * instance to have died with error by calling {@link NCDModuleInst_Backend_SetError}
  * and {@link NCDModuleInst_Backend_Dead}.
  * 
+ * @param o if the module specifies a positive alloc_size value in the {@link NCDModule}
+ *          structure, this will point to the allocated memory that can be used by the
+ *          module instance while it exists. Otherwise, it will be NULL.
  * @param i module backend instance handler. The backend may only use this handle via
  *          the Backend functions of {@link NCDModuleInst}.
  */
-typedef void (*NCDModule_func_new) (NCDModuleInst *i);
-
-/**
- * Like {@link NCDModule_func_new}, but with the extra user argument, as in other module
- * instance handlers. The initial value of the argument, as used here, is a pointer to
- * preallocated memory of alloc_size bytes (from {@link NCDModule}), or NULL if alloc_size==0.
- */
 typedef void (*NCDModule_func_new2) (void *o, NCDModuleInst *i);
 
 /**
@@ -740,14 +729,6 @@ struct NCDModule {
     
     /**
      * Function called to create an new backend instance.
-     * Only one of the two possible init functions must be set.
-     */
-    NCDModule_func_new func_new;
-    
-    /**
-     * Function called to create an new backend instance, to be used with memory
-     * preallocation.
-     * Only one of the two possible init functions must be set.
      */
     NCDModule_func_new2 func_new2;
     
@@ -788,13 +769,10 @@ struct NCDModule {
     /**
      * The amount of memory to preallocate for each module instance.
      * Preallocation can be used to avoid having to allocate memory from
-     * module initialization. To make use of preallocated memory, use the
-     * {@link NCDModule_func_new2} variant of the init function.
-     * The memory will be available from the point the init function is
-     * called to the point the instance calls {@link NCDModuleInst_Backend_Dead}.
-     * If alloc_size is >0, there is no need to call
-     * {@link NCDModuleInst_Backend_SetUser}, as the the user argument will
-     * automatically be set to a pointer to the preallocated memory.
+     * module initialization. The memory can be accessed via the first
+     * argument to {@link NCDModule_func_new2} and other calls (except if
+     * the callback pointer is changed subsequently using
+     * {@link NCDModuleInst_Backend_SetUser}).
      */
     int alloc_size;
 };

+ 4 - 4
ncd/modules/assert.c

@@ -76,12 +76,12 @@ fail0:
     NCDModuleInst_Backend_Dead(i);
 }
 
-static void func_new (NCDModuleInst *i)
+static void func_new (void *unused, NCDModuleInst *i)
 {
     func_new_common(i, 0);
 }
 
-static void func_new_false (NCDModuleInst *i)
+static void func_new_false (void *unused, NCDModuleInst *i)
 {
     func_new_common(i, 1);
 }
@@ -89,10 +89,10 @@ static void func_new_false (NCDModuleInst *i)
 static const struct NCDModule modules[] = {
     {
         .type = "assert",
-        .func_new = func_new
+        .func_new2 = func_new
     }, {
         .type = "assert_false",
-        .func_new = func_new_false
+        .func_new2 = func_new_false
     }, {
         .type = NULL
     }

+ 6 - 6
ncd/modules/blocker.c

@@ -207,17 +207,17 @@ fail0:
     NCDModuleInst_Backend_Dead(i);
 }
 
-static void up_func_new (NCDModuleInst *i)
+static void up_func_new (void *unused, NCDModuleInst *i)
 {
     updown_func_new_templ(i, 1, 0);
 }
 
-static void down_func_new (NCDModuleInst *i)
+static void down_func_new (void *unused, NCDModuleInst *i)
 {
     updown_func_new_templ(i, 0, 0);
 }
 
-static void downup_func_new (NCDModuleInst *i)
+static void downup_func_new (void *unused, NCDModuleInst *i)
 {
     updown_func_new_templ(i, 1, 1);
 }
@@ -330,13 +330,13 @@ static const struct NCDModule modules[] = {
         .alloc_size = sizeof(struct instance)
     }, {
         .type = "blocker::up",
-        .func_new = up_func_new
+        .func_new2 = up_func_new
     }, {
         .type = "blocker::down",
-        .func_new = down_func_new
+        .func_new2 = down_func_new
     }, {
         .type = "blocker::downup",
-        .func_new = downup_func_new
+        .func_new2 = downup_func_new
     }, {
         .type = "blocker::rdownup",
         .func_new2 = rdownup_func_new,

+ 2 - 2
ncd/modules/exit.c

@@ -46,7 +46,7 @@
 
 #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
 
-static void func_new (NCDModuleInst *i)
+static void func_new (void *unused, NCDModuleInst *i)
 {
     // check arguments
     NCDValRef exit_code_arg;
@@ -81,7 +81,7 @@ fail0:
 static const struct NCDModule modules[] = {
     {
         .type = "exit",
-        .func_new = func_new
+        .func_new2 = func_new
     }, {
         .type = NULL
     }

+ 2 - 2
ncd/modules/file.c

@@ -161,7 +161,7 @@ static int read_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValR
     return 0;
 }
 
-static void write_func_new (NCDModuleInst *i)
+static void write_func_new (void *unused, NCDModuleInst *i)
 {
     // read arguments
     NCDValRef filename_arg;
@@ -314,7 +314,7 @@ static const struct NCDModule modules[] = {
         .alloc_size = sizeof(struct read_instance)
     }, {
         .type = "file_write",
-        .func_new = write_func_new
+        .func_new2 = write_func_new
     }, {
         .type = "file_stat",
         .func_new2 = stat_func_new,

+ 2 - 2
ncd/modules/getargs.c

@@ -43,7 +43,7 @@
 
 #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
 
-static void func_new (NCDModuleInst *i)
+static void func_new (void *unused, NCDModuleInst *i)
 {
     NCDModuleInst_Backend_SetUser(i, i);
     
@@ -80,7 +80,7 @@ static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *o
 static const struct NCDModule modules[] = {
     {
         .type = "getargs",
-        .func_new = func_new,
+        .func_new2 = func_new,
         .func_getvar = func_getvar
     }, {
         .type = NULL

+ 4 - 4
ncd/modules/if.c

@@ -76,12 +76,12 @@ fail0:
     NCDModuleInst_Backend_Dead(i);
 }
 
-static void func_new (NCDModuleInst *i)
+static void func_new (void *unused, NCDModuleInst *i)
 {
     new_templ(i, 0);
 }
 
-static void func_new_not (NCDModuleInst *i)
+static void func_new_not (void *unused, NCDModuleInst *i)
 {
     new_templ(i, 1);
 }
@@ -89,10 +89,10 @@ static void func_new_not (NCDModuleInst *i)
 static const struct NCDModule modules[] = {
     {
         .type = "if",
-        .func_new = func_new
+        .func_new2 = func_new
     }, {
         .type = "ifnot",
-        .func_new = func_new_not
+        .func_new2 = func_new_not
     }, {
         .type = NULL
     }

+ 12 - 12
ncd/modules/list.c

@@ -398,7 +398,7 @@ static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *o
     return 0;
 }
 
-static void append_func_new (NCDModuleInst *i)
+static void append_func_new (void *unused, NCDModuleInst *i)
 {
     // check arguments
     NCDValRef arg;
@@ -424,7 +424,7 @@ fail0:
     NCDModuleInst_Backend_Dead(i);
 }
 
-static void appendv_func_new (NCDModuleInst *i)
+static void appendv_func_new (void *unused, NCDModuleInst *i)
 {
     // check arguments
     NCDValRef arg;
@@ -585,7 +585,7 @@ static int get_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRe
     return 0;
 }
 
-static void shift_func_new (NCDModuleInst *i)
+static void shift_func_new (void *unused, NCDModuleInst *i)
 {
     // check arguments
     if (!NCDVal_ListRead(i->args, 0)) {
@@ -744,7 +744,7 @@ static int find_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValR
     return 0;
 }
 
-static void removeat_func_new (NCDModuleInst *i)
+static void removeat_func_new (void *unused, NCDModuleInst *i)
 {
     // read arguments
     NCDValRef remove_pos_arg;
@@ -785,7 +785,7 @@ fail0:
     NCDModuleInst_Backend_Dead(i);
 }
 
-static void remove_func_new (NCDModuleInst *i)
+static void remove_func_new (void *unused, NCDModuleInst *i)
 {
     // read arguments
     NCDValRef value_arg;
@@ -816,7 +816,7 @@ fail0:
     NCDModuleInst_Backend_Dead(i);
 }
 
-static void set_func_new (NCDModuleInst *i)
+static void set_func_new (void *unused, NCDModuleInst *i)
 {
     // get method object
     struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)i->method_user);
@@ -864,10 +864,10 @@ static const struct NCDModule modules[] = {
         .alloc_size = sizeof(struct instance)
     }, {
         .type = "list::append",
-        .func_new = append_func_new
+        .func_new2 = append_func_new
     }, {
         .type = "list::appendv",
-        .func_new = appendv_func_new
+        .func_new2 = appendv_func_new
     }, {
         .type = "list::length",
         .func_new2 = length_func_new,
@@ -882,7 +882,7 @@ static const struct NCDModule modules[] = {
         .alloc_size = sizeof(struct get_instance)
     }, {
         .type = "list::shift",
-        .func_new = shift_func_new
+        .func_new2 = shift_func_new
     }, {
         .type = "list::contains",
         .func_new2 = contains_func_new,
@@ -897,13 +897,13 @@ static const struct NCDModule modules[] = {
         .alloc_size = sizeof(struct find_instance)
     }, {
         .type = "list::remove_at",
-        .func_new = removeat_func_new
+        .func_new2 = removeat_func_new
     }, {
         .type = "list::remove",
-        .func_new = remove_func_new
+        .func_new2 = remove_func_new
     }, {
         .type = "list::set",
-        .func_new = set_func_new
+        .func_new2 = set_func_new
     }, {
         .type = NULL
     }

+ 2 - 2
ncd/modules/net_watch_interfaces.c

@@ -421,7 +421,7 @@ static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *o
     return event_template_getvar(&o->templ, name, mem, out);
 }
 
-static void nextevent_func_new (NCDModuleInst *i)
+static void nextevent_func_new (void *unused, NCDModuleInst *i)
 {
     // check arguments
     if (!NCDVal_ListRead(i->args, 0)) {
@@ -461,7 +461,7 @@ static const struct NCDModule modules[] = {
         .alloc_size = sizeof(struct instance)
     }, {
         .type = "net.watch_interfaces::nextevent",
-        .func_new = nextevent_func_new
+        .func_new2 = nextevent_func_new
     }, {
         .type = NULL
     }

+ 4 - 4
ncd/modules/print.c

@@ -135,7 +135,7 @@ static void rprint_func_die (void *vo)
     NCDModuleInst_Backend_Dead(o->i);
 }
 
-static void print_func_new (NCDModuleInst *i)
+static void print_func_new (void *unused, NCDModuleInst *i)
 {
     if (!check_args(i)) {
         goto fail0;
@@ -151,7 +151,7 @@ fail0:
     NCDModuleInst_Backend_Dead(i);
 }
 
-static void println_func_new (NCDModuleInst *i)
+static void println_func_new (void *unused, NCDModuleInst *i)
 {
     if (!check_args(i)) {
         goto fail0;
@@ -180,10 +180,10 @@ static void rprintln_func_new (void *vo, NCDModuleInst *i)
 static const struct NCDModule modules[] = {
     {
         .type = "print",
-        .func_new = print_func_new
+        .func_new2 = print_func_new
     }, {
         .type = "println",
-        .func_new = println_func_new
+        .func_new2 = println_func_new
     }, {
         .type = "rprint",
         .func_new2 = rprint_func_new,

+ 4 - 4
ncd/modules/process_manager.c

@@ -472,7 +472,7 @@ static void func_die (void *vo)
     o->dying = 1;
 }
 
-static void start_func_new (NCDModuleInst *i)
+static void start_func_new (void *unused, NCDModuleInst *i)
 {
     // check arguments
     NCDValRef name_arg;
@@ -525,7 +525,7 @@ fail0:
     NCDModuleInst_Backend_Dead(i);
 }
 
-static void stop_func_new (NCDModuleInst *i)
+static void stop_func_new (void *unused, NCDModuleInst *i)
 {
     // check arguments
     NCDValRef name_arg;
@@ -572,10 +572,10 @@ static const struct NCDModule modules[] = {
         .alloc_size = sizeof(struct instance)
     }, {
         .type = "process_manager::start",
-        .func_new = start_func_new
+        .func_new2 = start_func_new
     }, {
         .type = "process_manager::stop",
-        .func_new = stop_func_new
+        .func_new2 = stop_func_new
     }, {
         .type = NULL
     }

+ 4 - 4
ncd/modules/reboot.c

@@ -42,7 +42,7 @@
 
 #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
 
-static void func_new_hard_reboot (NCDModuleInst *i)
+static void func_new_hard_reboot (void *unused, NCDModuleInst *i)
 {
     // check arguments
     if (!NCDVal_ListRead(i->args, 0)) {
@@ -65,7 +65,7 @@ fail0:
     NCDModuleInst_Backend_Dead(i);
 }
 
-static void func_new_hard_poweroff (NCDModuleInst *i)
+static void func_new_hard_poweroff (void *unused, NCDModuleInst *i)
 {
     // check arguments
     if (!NCDVal_ListRead(i->args, 0)) {
@@ -91,10 +91,10 @@ fail0:
 static const struct NCDModule modules[] = {
     {
         .type = "hard_reboot",
-        .func_new = func_new_hard_reboot
+        .func_new2 = func_new_hard_reboot
     }, {
         .type = "hard_poweroff",
-        .func_new = func_new_hard_poweroff
+        .func_new2 = func_new_hard_poweroff
     }, {
         .type = NULL
     }

+ 2 - 2
ncd/modules/sys_evdev.c

@@ -294,7 +294,7 @@ static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *o
     return 0;
 }
 
-static void nextevent_func_new (NCDModuleInst *i)
+static void nextevent_func_new (void *unused, NCDModuleInst *i)
 {
     // check arguments
     if (!NCDVal_ListRead(i->args, 0)) {
@@ -334,7 +334,7 @@ static const struct NCDModule modules[] = {
         .alloc_size = sizeof(struct instance)
     }, {
         .type = "sys.evdev::nextevent",
-        .func_new = nextevent_func_new
+        .func_new2 = nextevent_func_new
     }, {
         .type = NULL
     }

+ 4 - 4
ncd/modules/sys_request_server.c

@@ -809,7 +809,7 @@ static void func_die (void *vo)
     }
 }
 
-static void reply_func_new (NCDModuleInst *i)
+static void reply_func_new (void *unused, NCDModuleInst *i)
 {
     NCDValRef reply_data;
     if (!NCDVal_ListRead(i->args, 1, &reply_data)) {
@@ -841,7 +841,7 @@ fail:
     NCDModuleInst_Backend_Dead(i);
 }
 
-static void finish_func_new (NCDModuleInst *i)
+static void finish_func_new (void *unused, NCDModuleInst *i)
 {
     if (!NCDVal_ListRead(i->args, 0)) {
         ModuleLog(i, BLOG_ERROR, "wrong arity");
@@ -875,10 +875,10 @@ static const struct NCDModule modules[] = {
         .alloc_size = sizeof(struct instance)
     }, {
         .type = "sys.request_server.request::reply",
-        .func_new = reply_func_new
+        .func_new2 = reply_func_new
     }, {
         .type = "sys.request_server.request::finish",
-        .func_new = finish_func_new
+        .func_new2 = finish_func_new
     }, {
         .type = NULL
     }

+ 2 - 2
ncd/modules/sys_watch_directory.c

@@ -374,7 +374,7 @@ fail:
     return 1;
 }
 
-static void nextevent_func_new (NCDModuleInst *i)
+static void nextevent_func_new (void *unused, NCDModuleInst *i)
 {
     // check arguments
     if (!NCDVal_ListRead(i->args, 0)) {
@@ -414,7 +414,7 @@ static const struct NCDModule modules[] = {
         .alloc_size = sizeof(struct instance)
     }, {
         .type = "sys.watch_directory::nextevent",
-        .func_new = nextevent_func_new
+        .func_new2 = nextevent_func_new
     }, {
         .type = NULL
     }

+ 2 - 2
ncd/modules/sys_watch_input.c

@@ -400,7 +400,7 @@ static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *o
     return event_template_getvar(&o->templ, name, mem, out);
 }
 
-static void nextevent_func_new (NCDModuleInst *i)
+static void nextevent_func_new (void *unused, NCDModuleInst *i)
 {
     // check arguments
     if (!NCDVal_ListRead(i->args, 0)) {
@@ -440,7 +440,7 @@ static const struct NCDModule modules[] = {
         .alloc_size = sizeof(struct instance)
     }, {
         .type = "sys.watch_input::nextevent",
-        .func_new = nextevent_func_new
+        .func_new2 = nextevent_func_new
     }, {
         .type = NULL
     }

+ 2 - 2
ncd/modules/sys_watch_usb.c

@@ -368,7 +368,7 @@ static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *o
     return event_template_getvar(&o->templ, name, mem, out);
 }
 
-static void nextevent_func_new (NCDModuleInst *i)
+static void nextevent_func_new (void *unused, NCDModuleInst *i)
 {
     // check arguments
     if (!NCDVal_ListRead(i->args, 0)) {
@@ -408,7 +408,7 @@ static const struct NCDModule modules[] = {
         .alloc_size = sizeof(struct instance)
     }, {
         .type = "sys.watch_usb::nextevent",
-        .func_new = nextevent_func_new
+        .func_new2 = nextevent_func_new
     }, {
         .type = NULL
     }

+ 2 - 2
ncd/modules/try.c

@@ -238,7 +238,7 @@ static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *o
     return 0;
 }
 
-static void assert_func_new (NCDModuleInst *i)
+static void assert_func_new (void *unused, NCDModuleInst *i)
 {
     // check arguments
     NCDValRef cond_arg;
@@ -284,7 +284,7 @@ static const struct NCDModule modules[] = {
         .alloc_size = sizeof(struct instance)
     }, {
         .type = "try.try::assert",
-        .func_new = assert_func_new
+        .func_new2 = assert_func_new
     }, {
         .type = NULL
     }

+ 6 - 6
ncd/modules/value.c

@@ -1440,7 +1440,7 @@ fail0:
     NCDModuleInst_Backend_Dead(i);
 }
 
-static void remove_func_new (NCDModuleInst *i)
+static void remove_func_new (void *unused, NCDModuleInst *i)
 {
     NCDValRef where_arg;
     if (!NCDVal_ListRead(i->args, 1, &where_arg)) {
@@ -1468,7 +1468,7 @@ fail0:
     NCDModuleInst_Backend_Dead(i);
 }
 
-static void delete_func_new (NCDModuleInst *i)
+static void delete_func_new (void *unused, NCDModuleInst *i)
 {
     if (!NCDVal_ListRead(i->args, 0)) {
         ModuleLog(i, BLOG_ERROR, "wrong arity");
@@ -1493,7 +1493,7 @@ fail0:
     NCDModuleInst_Backend_Dead(i);
 }
 
-static void reset_func_new (NCDModuleInst *i)
+static void reset_func_new (void *unused, NCDModuleInst *i)
 {
     NCDValRef what_arg;
     if (!NCDVal_ListRead(i->args, 1, &what_arg)) {
@@ -1603,13 +1603,13 @@ static const struct NCDModule modules[] = {
         .alloc_size = sizeof(struct instance)
     }, {
         .type = "value::remove",
-        .func_new = remove_func_new
+        .func_new2 = remove_func_new
     }, {
         .type = "value::delete",
-        .func_new = delete_func_new
+        .func_new2 = delete_func_new
     }, {
         .type = "value::reset",
-        .func_new = reset_func_new
+        .func_new2 = reset_func_new
     }, {
         .type = "value::substr",
         .base_type = "value",

+ 2 - 2
ncd/modules/var.c

@@ -110,7 +110,7 @@ static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *o
     return 0;
 }
 
-static void set_func_new (NCDModuleInst *i)
+static void set_func_new (void *unused, NCDModuleInst *i)
 {
     // read arguments
     NCDValRef value_arg;
@@ -158,7 +158,7 @@ static const struct NCDModule modules[] = {
         .alloc_size = sizeof(struct instance)
     }, {
         .type = "var::set",
-        .func_new = set_func_new
+        .func_new2 = set_func_new
     }, {
         .type = NULL
     }