ソースを参照

ncd: Refactoring using NCDModuleRef.

Ambroz Bizjak 11 年 前
コミット
2400f4c697
4 ファイル変更63 行追加18 行削除
  1. 29 0
      ncd/NCDModule.c
  2. 26 0
      ncd/NCDModule.h
  3. 4 9
      ncd/modules/backtrack.c
  4. 4 9
      ncd/modules/try.c

+ 29 - 0
ncd/NCDModule.c

@@ -389,6 +389,35 @@ int NCDModuleInst_Backend_InterpLoadGroup (NCDModuleInst *n, const struct NCDMod
     return n->params->iparams->func_loadgroup(n->params->iparams->user, group);
 }
 
+void NCDModuleRef_Init (NCDModuleRef *o, NCDModuleInst *inst)
+{
+    ASSERT(!inst->pass_mem_to_methods)
+    
+    NCDObject obj = NCDModuleInst_Object(inst);
+    NCDObjRef_Init(&o->objref, NCDObject_Pobj(&obj));
+    
+    DebugObject_Init(&o->d_obj);
+}
+
+void NCDModuleRef_Free (NCDModuleRef *o)
+{
+    DebugObject_Free(&o->d_obj);
+    
+    NCDObjRef_Free(&o->objref);
+}
+
+NCDModuleInst * NCDModuleRef_Deref (NCDModuleRef *o)
+{
+    DebugObject_Access(&o->d_obj);
+    
+    NCDModuleInst *res = NULL;
+    NCDObject obj;
+    if (NCDObjRef_Deref(&o->objref, &obj)) {
+        res = NCDObject_MethodUser(&obj);
+    }
+    return res;
+}
+
 int NCDModuleProcess_InitId (NCDModuleProcess *o, NCDModuleInst *n, NCD_string_id_t template_name, NCDValRef args, NCDModuleProcess_handler_event handler_event)
 {
     DebugObject_Access(&n->d_obj);

+ 26 - 0
ncd/NCDModule.h

@@ -382,6 +382,14 @@ typedef struct NCDModuleInst_s {
     DebugObject d_obj;
 } NCDModuleInst;
 
+/**
+ * Weak NCDModuleInst reference.
+ */
+typedef struct {
+    NCDObjRef objref;
+    DebugObject d_obj;
+} NCDModuleRef;
+
 /**
  * Process created from a process template on behalf of a module backend
  * instance, implemented by the interpreter.
@@ -613,6 +621,24 @@ btime_t NCDModuleInst_Backend_InterpGetRetryTime (NCDModuleInst *n);
  */
 int NCDModuleInst_Backend_InterpLoadGroup (NCDModuleInst *n, const struct NCDModuleGroup *group);
 
+/**
+ * Initializes a weak reference to a module instance.
+ * The instane must no have had NCDModuleInst_Backend_PassMemToMethods
+ * called.
+ */
+void NCDModuleRef_Init (NCDModuleRef *o, NCDModuleInst *inst);
+
+/**
+ * Frees the reference.
+ */
+void NCDModuleRef_Free (NCDModuleRef *o);
+
+/**
+ * Dereferences the reference.
+ * If the reference was broken, returns NULL.
+ */
+NCDModuleInst * NCDModuleRef_Deref (NCDModuleRef *o);
+
 /**
  * Initializes a process in the interpreter from a process template.
  * This must be called on behalf of a module backend instance.

+ 4 - 9
ncd/modules/backtrack.c

@@ -50,7 +50,7 @@
 
 struct rgo_instance {
     NCDModuleInst *i;
-    NCDObjRef bp_ref;
+    NCDModuleRef bp_ref;
 };
 
 static void func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
@@ -106,8 +106,7 @@ static void rgo_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst
     NCDModuleInst *backtrack_point_inst = params->method_user;
     
     // init object reference to the backtrack_point
-    NCDObject obj = NCDModuleInst_Object(backtrack_point_inst);
-    NCDObjRef_Init(&o->bp_ref, NCDObject_Pobj(&obj));
+    NCDModuleRef_Init(&o->bp_ref, backtrack_point_inst);
     
     // go up
     NCDModuleInst_Backend_Up(i);
@@ -122,14 +121,10 @@ static void rgo_func_die (void *vo)
     struct rgo_instance *o = vo;
     
     // deref backtrack_point
-    NCDModuleInst *backtrack_point_inst = NULL;
-    NCDObject rgo_obj;
-    if (NCDObjRef_Deref(&o->bp_ref, &rgo_obj)) {
-        backtrack_point_inst = NCDObject_MethodUser(&rgo_obj);
-    }
+    NCDModuleInst *backtrack_point_inst = NCDModuleRef_Deref(&o->bp_ref);
     
     // free object reference
-    NCDObjRef_Free(&o->bp_ref);
+    NCDModuleRef_Free(&o->bp_ref);
     
     // die
     NCDModuleInst_Backend_Dead(o->i);

+ 4 - 9
ncd/modules/try.c

@@ -106,7 +106,7 @@ struct instance {
 
 struct rbreak_instance {
     NCDModuleInst *i;
-    NCDObjRef objref;
+    NCDModuleRef ref;
 };
 
 enum {STATE_INIT, STATE_DEINIT, STATE_FINISHED, STATE_WAITINT};
@@ -456,8 +456,7 @@ static void rbreak_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleI
     }
     
     // init object reference
-    NCDObject obj = NCDModuleInst_Object(((struct instance *)params->method_user)->i);
-    NCDObjRef_Init(&o->objref, NCDObject_Pobj(&obj));
+    NCDModuleRef_Init(&o->ref, ((struct instance *)params->method_user)->i);
     
     // go up
     NCDModuleInst_Backend_Up(i);
@@ -472,14 +471,10 @@ static void rbreak_func_die (void *vo)
     struct rbreak_instance *o = vo;
     
     // deref backtrack_point
-    NCDModuleInst *inst = NULL;
-    NCDObject obj;
-    if (NCDObjRef_Deref(&o->objref, &obj)) {
-        inst = NCDObject_MethodUser(&obj);
-    }
+    NCDModuleInst *inst = NCDModuleRef_Deref(&o->ref);
     
     // free object reference
-    NCDObjRef_Free(&o->objref);
+    NCDModuleRef_Free(&o->ref);
     
     // die
     NCDModuleInst_Backend_Dead(o->i);