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

ncd: NCDModuleInst: don't store a user pointer (to the struct statement), and instead pass a pointer to NCDModuleInst to
callbacks

ambrop7 13 лет назад
Родитель
Сommit
9a7f7e0e1c
3 измененных файлов с 47 добавлено и 58 удалено
  1. 8 9
      ncd/NCDModule.c
  2. 9 14
      ncd/NCDModule.h
  3. 30 35
      ncd/ncd.c

+ 8 - 9
ncd/NCDModule.c

@@ -58,7 +58,7 @@ static int process_arg_object_func_getvar2 (NCDModuleProcess *o, void *n_ptr, co
 
 static void frontend_event (NCDModuleInst *n, int event)
 {
-    n->params->func_event(n->user, event);
+    n->params->func_event(n, event);
 }
 
 static void inst_assert_backend (NCDModuleInst *n)
@@ -68,7 +68,7 @@ static void inst_assert_backend (NCDModuleInst *n)
            n->state == STATE_DYING)
 }
 
-void NCDModuleInst_Init (NCDModuleInst *n, const struct NCDModule *m, void *mem, const NCDObject *method_object, NCDValRef args, void *user, const struct NCDModuleInst_params *params, const struct NCDModuleInst_iparams *iparams)
+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->alloc_size >= 0)
@@ -88,7 +88,6 @@ void NCDModuleInst_Init (NCDModuleInst *n, const struct NCDModule *m, void *mem,
     n->m = m;
     n->method_user = (method_object ? method_object->user : NULL);
     n->args = args;
-    n->user = user;
     n->params = params;
     n->iparams = iparams;
     
@@ -265,7 +264,7 @@ int NCDModuleInst_Backend_GetObj (NCDModuleInst *n, const char *name, NCDObject
     ASSERT(name)
     ASSERT(out_object)
     
-    int res = n->params->func_getobj(n->user, name, out_object);
+    int res = n->params->func_getobj(n, name, out_object);
     ASSERT(res == 0 || res == 1)
     
     return res;
@@ -277,7 +276,7 @@ void NCDModuleInst_Backend_Log (NCDModuleInst *n, int channel, int level, const
     
     va_list vl;
     va_start(vl, fmt);
-    BLog_LogViaFuncVarArg(n->params->logfunc, n->user, channel, level, fmt, vl);
+    BLog_LogViaFuncVarArg(n->params->logfunc, n, channel, level, fmt, vl);
     va_end(vl);
 }
 
@@ -297,7 +296,7 @@ void NCDModuleInst_Backend_InterpExit (NCDModuleInst *n, int exit_code)
     DebugObject_Access(&n->d_obj);
     inst_assert_backend(n);
     
-    n->iparams->func_interp_exit(n->user, exit_code);
+    n->iparams->func_interp_exit(exit_code);
 }
 
 int NCDModuleInst_Backend_InterpGetArgs (NCDModuleInst *n, NCDValMem *mem, NCDValRef *out_value)
@@ -307,7 +306,7 @@ int NCDModuleInst_Backend_InterpGetArgs (NCDModuleInst *n, NCDValMem *mem, NCDVa
     ASSERT(mem)
     ASSERT(out_value)
     
-    int res = n->iparams->func_interp_getargs(n->user, mem, out_value);
+    int res = n->iparams->func_interp_getargs(mem, out_value);
     ASSERT(res == 0 || res == 1)
     ASSERT(res == 0 || (NCDVal_Assert(*out_value), 1))
     
@@ -319,7 +318,7 @@ btime_t NCDModuleInst_Backend_InterpGetRetryTime (NCDModuleInst *n)
     DebugObject_Access(&n->d_obj);
     inst_assert_backend(n);
     
-    return n->iparams->func_interp_getretrytime(n->user);
+    return n->iparams->func_interp_getretrytime();
 }
 
 int NCDModuleProcess_Init (NCDModuleProcess *o, NCDModuleInst *n, const char *template_name, NCDValRef args, void *user, NCDModuleProcess_handler_event handler_event)
@@ -348,7 +347,7 @@ int NCDModuleProcess_Init (NCDModuleProcess *o, NCDModuleInst *n, const char *te
     o->interp_func_getobj = NULL;
     
     // init interpreter part
-    if (!(n->iparams->func_initprocess(n->user, o, template_name))) {
+    if (!(n->iparams->func_initprocess(o, template_name))) {
         goto fail1;
     }
     

+ 9 - 14
ncd/NCDModule.h

@@ -70,10 +70,10 @@ struct NCDModuleProcess_s;
  * from within this function. The only exception is that it may free the
  * instance from within the NCDMODULE_EVENT_DEAD event.
  * 
- * @param user as in {@link NCDModuleInst_Init}
+ * @param inst the module instance
  * @param event event number
  */
-typedef void (*NCDModuleInst_func_event) (void *user, int event);
+typedef void (*NCDModuleInst_func_event) (struct NCDModuleInst_s *inst, int event);
 
 /**
  * Function called when the module instance wants the interpreter to
@@ -81,12 +81,12 @@ typedef void (*NCDModuleInst_func_event) (void *user, int event);
  * The instance will not be in dead state.
  * This function must not have any side effects.
  * 
- * @param user as in {@link NCDModuleInst_Init}
+ * @param inst the module instance
  * @param name name of the object
  * @param out_object the object will be returned here
  * @return 1 on success, 0 on failure
  */
-typedef int (*NCDModuleInst_func_getobj) (void *user, const char *name, NCDObject *out_object);
+typedef int (*NCDModuleInst_func_getobj) (struct NCDModuleInst_s *inst, const char *name, NCDObject *out_object);
 
 /**
  * Function called when the module instance wants the interpreter to
@@ -101,44 +101,40 @@ typedef int (*NCDModuleInst_func_getobj) (void *user, const char *name, NCDObjec
  * only update its internal state, and visibly react only via jobs that it pushes
  * from within this function.
  * 
- * @param user as in {@link NCDModuleInst_Init}
  * @param p handle for the new process backend
  * @param template_name name of the template to create the process from
  * @return 1 on success, 0 on failure
  */
-typedef int (*NCDModuleInst_func_initprocess) (void *user, struct NCDModuleProcess_s *p, const char *template_name);
+typedef int (*NCDModuleInst_func_initprocess) (struct NCDModuleProcess_s *p, const char *template_name);
 
 /**
  * Function called when the module instance wants the interpreter to
  * initiate termination, as if it received an external terminatio request (signal).
  * 
- * @param user as in {@link NCDModuleInst_Init}
  * @param exit_code exit code to return the the operating system. This overrides any previously
  *                  set exit code, and will be overriden by a signal to the value 1.
  *   
  */
-typedef void (*NCDModuleInst_func_interp_exit) (void *user, int exit_code);
+typedef void (*NCDModuleInst_func_interp_exit) (int exit_code);
 
 /**
  * Function called when the module instance wants the interpreter to
  * provide its extra command line arguments.
  * 
- * @param user as in {@link NCDModuleInst_Init}
  * @param mem value memory to use
  * @param out_value write value reference here on success
  * @return 1 if available, 0 if not available. If available, but out of memory, return 1
  *         and an invalid value.
  */
-typedef int (*NCDModuleInst_func_interp_getargs) (void *user, NCDValMem *mem, NCDValRef *out_value);
+typedef int (*NCDModuleInst_func_interp_getargs) (NCDValMem *mem, NCDValRef *out_value);
 
 /**
  * Function called when the module instance wants the interpreter to
  * provide its retry time.
  * 
- * @param user as in {@link NCDModuleInst_Init}
  * @return retry time in milliseconds
  */
-typedef btime_t (*NCDModuleInst_func_interp_getretrytime) (void *user);
+typedef btime_t (*NCDModuleInst_func_interp_getretrytime) (void);
 
 #define NCDMODULEPROCESS_EVENT_UP 1
 #define NCDMODULEPROCESS_EVENT_DOWN 2
@@ -297,7 +293,6 @@ typedef struct NCDModuleInst_s {
     const struct NCDModule *m;
     void *method_user;
     NCDValRef args;
-    void *user;
     const struct NCDModuleInst_params *params;
     const struct NCDModuleInst_iparams *iparams;
     void *inst_user;
@@ -345,7 +340,7 @@ typedef struct NCDModuleProcess_s {
  * @param params more parameters, see {@link NCDModuleInst_params}
  * @param iparams more parameters, see {@link NCDModuleInst_iparams}
  */
-void NCDModuleInst_Init (NCDModuleInst *n, const struct NCDModule *m, void *mem, const NCDObject *method_object, NCDValRef args, void *user, const struct NCDModuleInst_params *params, const struct NCDModuleInst_iparams *iparams);
+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);
 
 /**
  * Frees the instance.

+ 30 - 35
ncd/ncd.c

@@ -185,13 +185,13 @@ static struct process * statement_process (struct statement *ps);
 static int statement_mem_is_allocated (struct statement *ps);
 static int statement_mem_size (struct statement *ps);
 static int statement_allocate_memory (struct statement *ps, int alloc_size);
-static void statement_instance_func_event (struct statement *ps, int event);
-static int statement_instance_func_getobj (struct statement *ps, const char *objname, NCDObject *out_object);
-static int statement_instance_func_initprocess (struct statement *ps, NCDModuleProcess *mp, const char *template_name);
-static void statement_instance_logfunc (struct statement *ps);
-static void statement_instance_func_interp_exit (struct statement *ps, int exit_code);
-static int statement_instance_func_interp_getargs (struct statement *ps, NCDValMem *mem, NCDValRef *out_value);
-static btime_t statement_instance_func_interp_getretrytime (struct statement *ps);
+static void statement_instance_func_event (NCDModuleInst *inst, int event);
+static int statement_instance_func_getobj (NCDModuleInst *inst, const char *objname, NCDObject *out_object);
+static int statement_instance_func_initprocess (NCDModuleProcess *mp, const char *template_name);
+static void statement_instance_logfunc (NCDModuleInst *inst);
+static void statement_instance_func_interp_exit (int exit_code);
+static int statement_instance_func_interp_getargs (NCDValMem *mem, NCDValRef *out_value);
+static btime_t statement_instance_func_interp_getretrytime (void);
 static void process_moduleprocess_func_event (struct process *p, int event);
 static int process_moduleprocess_func_getobj (struct process *p, const char *name, NCDObject *out_object);
 
@@ -368,17 +368,17 @@ int main (int argc, char **argv)
     }
     
     // init common module params
-    module_params.func_event = (NCDModuleInst_func_event)statement_instance_func_event;
-    module_params.func_getobj = (NCDModuleInst_func_getobj)statement_instance_func_getobj;
+    module_params.func_event = statement_instance_func_event;
+    module_params.func_getobj = statement_instance_func_getobj;
     module_params.logfunc = (BLog_logfunc)statement_instance_logfunc;
     module_iparams.reactor = &reactor;
     module_iparams.manager = &manager;
     module_iparams.umanager = &umanager;
     module_iparams.random2 = &random2;
-    module_iparams.func_initprocess = (NCDModuleInst_func_initprocess)statement_instance_func_initprocess;
-    module_iparams.func_interp_exit = (NCDModuleInst_func_interp_exit)statement_instance_func_interp_exit;
-    module_iparams.func_interp_getargs = (NCDModuleInst_func_interp_getargs)statement_instance_func_interp_getargs;
-    module_iparams.func_interp_getretrytime = (NCDModuleInst_func_interp_getretrytime)statement_instance_func_interp_getretrytime;
+    module_iparams.func_initprocess = statement_instance_func_initprocess;
+    module_iparams.func_interp_exit = statement_instance_func_interp_exit;
+    module_iparams.func_interp_getargs = statement_instance_func_interp_getargs;
+    module_iparams.func_interp_getretrytime = statement_instance_func_interp_getretrytime;
     
     // init processes list
     LinkedList1_Init(&processes);
@@ -1109,7 +1109,7 @@ void process_advance (struct process *p)
     process_assert_pointers(p);
     
     // initialize module instance
-    NCDModuleInst_Init(&ps->inst, module, mem, object_ptr, args, ps, &module_params, &module_iparams);
+    NCDModuleInst_Init(&ps->inst, module, mem, object_ptr, args, &module_params, &module_iparams);
     return;
     
 fail1:
@@ -1274,8 +1274,9 @@ int statement_allocate_memory (struct statement *ps, int alloc_size)
     return 1;
 }
 
-void statement_instance_func_event (struct statement *ps, int event)
+void statement_instance_func_event (NCDModuleInst *inst, int event)
 {
+    struct statement *ps = UPPER_OBJECT(inst, struct statement, inst);
     ASSERT(ps->state == SSTATE_CHILD || ps->state == SSTATE_ADULT || ps->state == SSTATE_DYING)
     
     struct process *p = statement_process(ps);
@@ -1349,70 +1350,66 @@ void statement_instance_func_event (struct statement *ps, int event)
     }
 }
 
-int statement_instance_func_getobj (struct statement *ps, const char *objname, NCDObject *out_object)
+int statement_instance_func_getobj (NCDModuleInst *inst, const char *objname, NCDObject *out_object)
 {
+    struct statement *ps = UPPER_OBJECT(inst, struct statement, inst);
     ASSERT(ps->state != SSTATE_FORGOTTEN)
     
     return process_find_object(statement_process(ps), ps->i, objname, out_object);
 }
 
-int statement_instance_func_initprocess (struct statement *ps, NCDModuleProcess *mp, const char *template_name)
+int statement_instance_func_initprocess (NCDModuleProcess *mp, const char *template_name)
 {
-    ASSERT(ps->state != SSTATE_FORGOTTEN)
-    
     // find process
     NCDInterpProcess *iprocess = NCDInterpProg_FindProcess(&iprogram, template_name);
     if (!iprocess) {
-        statement_log(ps, BLOG_ERROR, "no template named %s", template_name);
+        BLog(BLOG_ERROR, "no template named %s", template_name);
         return 0;
     }
     
     // make sure it's a template
     if (!NCDInterpProcess_IsTemplate(iprocess)) {
-        statement_log(ps, BLOG_ERROR, "need template to create a process, but %s is a process", template_name);
+        BLog(BLOG_ERROR, "need template to create a process, but %s is a process", template_name);
         return 0;
     }
     
     // create process
     if (!process_new(iprocess, mp)) {
-        statement_log(ps, BLOG_ERROR, "failed to create process from template %s", template_name);
+        BLog(BLOG_ERROR, "failed to create process from template %s", template_name);
         return 0;
     }
     
-    statement_log(ps, BLOG_INFO, "created process from template %s", template_name);
+    BLog(BLOG_INFO, "created process from template %s", template_name);
     
     return 1;
 }
 
-void statement_instance_logfunc (struct statement *ps)
+void statement_instance_logfunc (NCDModuleInst *inst)
 {
+    struct statement *ps = UPPER_OBJECT(inst, struct statement, inst);
     ASSERT(ps->state != SSTATE_FORGOTTEN)
     
     statement_logfunc(ps);
     BLog_Append("module: ");
 }
 
-void statement_instance_func_interp_exit (struct statement *ps, int exit_code)
+void statement_instance_func_interp_exit (int exit_code)
 {
-    ASSERT(ps->state != SSTATE_FORGOTTEN)
-    
     start_terminate(exit_code);
 }
 
-int statement_instance_func_interp_getargs (struct statement *ps, NCDValMem *mem, NCDValRef *out_value)
+int statement_instance_func_interp_getargs (NCDValMem *mem, NCDValRef *out_value)
 {
-    ASSERT(ps->state != SSTATE_FORGOTTEN)
-    
     *out_value = NCDVal_NewList(mem, options.num_extra_args);
     if (NCDVal_IsInvalid(*out_value)) {
-        statement_log(ps, BLOG_ERROR, "NCDVal_NewList failed");
+        BLog(BLOG_ERROR, "NCDVal_NewList failed");
         goto fail;
     }
     
     for (int i = 0; i < options.num_extra_args; i++) {
         NCDValRef arg = NCDVal_NewString(mem, options.extra_args[i]);
         if (NCDVal_IsInvalid(arg)) {
-            statement_log(ps, BLOG_ERROR, "NCDVal_NewString failed");
+            BLog(BLOG_ERROR, "NCDVal_NewString failed");
             goto fail;
         }
         
@@ -1426,10 +1423,8 @@ fail:
     return 1;
 }
 
-btime_t statement_instance_func_interp_getretrytime (struct statement *ps)
+btime_t statement_instance_func_interp_getretrytime (void)
 {
-    ASSERT(ps->state != SSTATE_FORGOTTEN)
-    
     return options.retry_time;
 }