فهرست منبع

ncd: NCDModule: pass template name for process creation as a string ID

ambrop7 13 سال پیش
والد
کامیت
fcc39d3581
3فایلهای تغییر یافته به همراه83 افزوده شده و 22 حذف شده
  1. 54 2
      ncd/NCDModule.c
  2. 15 3
      ncd/NCDModule.h
  3. 14 17
      ncd/ncd.c

+ 54 - 2
ncd/NCDModule.c

@@ -322,13 +322,13 @@ btime_t NCDModuleInst_Backend_InterpGetRetryTime (NCDModuleInst *n)
     return n->params->iparams->func_interp_getretrytime();
 }
 
-int NCDModuleProcess_Init (NCDModuleProcess *o, NCDModuleInst *n, const char *template_name, NCDValRef args, void *user, NCDModuleProcess_handler_event handler_event)
+int NCDModuleProcess_InitId (NCDModuleProcess *o, NCDModuleInst *n, NCD_string_id_t template_name, NCDValRef args, void *user, NCDModuleProcess_handler_event handler_event)
 {
     DebugObject_Access(&n->d_obj);
     ASSERT(n->state == STATE_DOWN_UNCLEAN || n->state == STATE_DOWN_CLEAN ||
            n->state == STATE_UP ||
            n->state == STATE_DYING)
-    ASSERT(template_name)
+    ASSERT(template_name >= 0)
     ASSERT(NCDVal_IsInvalid(args) || NCDVal_IsList(args))
     ASSERT(handler_event)
     
@@ -368,6 +368,58 @@ fail1:
     return 0;
 }
 
+int NCDModuleProcess_InitValue (NCDModuleProcess *o, NCDModuleInst *n, NCDValRef template_name, NCDValRef args, void *user, NCDModuleProcess_handler_event handler_event)
+{
+    DebugObject_Access(&n->d_obj);
+    ASSERT(n->state == STATE_DOWN_UNCLEAN || n->state == STATE_DOWN_CLEAN ||
+           n->state == STATE_UP ||
+           n->state == STATE_DYING)
+    ASSERT(NCDVal_IsString(template_name))
+    ASSERT(NCDVal_IsInvalid(args) || NCDVal_IsList(args))
+    ASSERT(handler_event)
+    
+    NCD_string_id_t template_name_id;
+    
+    if (NCDVal_IsIdString(template_name)) {
+        template_name_id = NCDVal_IdStringId(template_name);
+    } else {
+        const char *str = NCDVal_StringValue(template_name);
+        size_t len = NCDVal_StringLength(template_name);
+        
+        if (strlen(str) != len) {
+            BLog(BLOG_ERROR, "template name cannot have nulls");
+            return 0;
+        }
+        
+        template_name_id = NCDStringIndex_Get(n->params->iparams->string_index, str);
+        if (template_name_id < 0) {
+            BLog(BLOG_ERROR, "NCDStringIndex_Get failed");
+            return 0;
+        }
+    }
+    
+    return NCDModuleProcess_InitId(o, n, template_name_id, args, user, handler_event);
+}
+
+int NCDModuleProcess_Init (NCDModuleProcess *o, NCDModuleInst *n, const char *template_name, NCDValRef args, void *user, NCDModuleProcess_handler_event handler_event)
+{
+    DebugObject_Access(&n->d_obj);
+    ASSERT(n->state == STATE_DOWN_UNCLEAN || n->state == STATE_DOWN_CLEAN ||
+           n->state == STATE_UP ||
+           n->state == STATE_DYING)
+    ASSERT(template_name)
+    ASSERT(NCDVal_IsInvalid(args) || NCDVal_IsList(args))
+    ASSERT(handler_event)
+    
+    NCD_string_id_t template_name_id = NCDStringIndex_Get(n->params->iparams->string_index, template_name);
+    if (template_name_id < 0) {
+        BLog(BLOG_ERROR, "NCDStringIndex_Get failed");
+        return 0;
+    }
+    
+    return NCDModuleProcess_InitId(o, n, template_name_id, args, user, handler_event);
+}
+
 void NCDModuleProcess_Free (NCDModuleProcess *o)
 {
     DebugObject_Free(&o->d_obj);

+ 15 - 3
ncd/NCDModule.h

@@ -103,10 +103,11 @@ typedef int (*NCDModuleInst_func_getobj) (struct NCDModuleInst_s *inst, NCD_stri
  * from within this function.
  * 
  * @param p handle for the new process backend
- * @param template_name name of the template to create the process from
+ * @param template_name name of the template to create the process from,
+ *                      as an {@link NCDStringIndex} identifier
  * @return 1 on success, 0 on failure
  */
-typedef int (*NCDModuleInst_func_initprocess) (struct NCDModuleProcess_s *p, const char *template_name);
+typedef int (*NCDModuleInst_func_initprocess) (struct NCDModuleProcess_s *p, NCD_string_id_t template_name);
 
 /**
  * Function called when the module instance wants the interpreter to
@@ -527,7 +528,7 @@ btime_t NCDModuleInst_Backend_InterpGetRetryTime (NCDModuleInst *n);
  * 
  * @param o the process
  * @param n backend instance whose interpreter will be providing the process
- * @param template_name name of the process template
+ * @param template_name name of the process template as an {@link NCDStringIndex} identifier
  * @param args arguments to the process. Must be an invalid value or a list value.
  *             The value must be available and unchanged while the process exists.
  * @param user argument to handlers
@@ -535,6 +536,17 @@ btime_t NCDModuleInst_Backend_InterpGetRetryTime (NCDModuleInst *n);
  *                      interpreter
  * @return 1 on success, 0 on failure
  */
+int NCDModuleProcess_InitId (NCDModuleProcess *o, NCDModuleInst *n, NCD_string_id_t template_name, NCDValRef args, void *user, NCDModuleProcess_handler_event handler_event) WARN_UNUSED;
+
+/**
+ * Wrapper around {@link NCDModuleProcess_InitId} which takes the template name as an
+ * {@link NCDValRef}, which must point to a string value.
+ */
+int NCDModuleProcess_InitValue (NCDModuleProcess *o, NCDModuleInst *n, NCDValRef template_name, NCDValRef args, void *user, NCDModuleProcess_handler_event handler_event) WARN_UNUSED;
+
+/**
+ * Wrapper around {@link NCDModuleProcess_InitId} which takes the template name as a char pointer.
+ */
 int NCDModuleProcess_Init (NCDModuleProcess *o, NCDModuleInst *n, const char *template_name, NCDValRef args, void *user, NCDModuleProcess_handler_event handler_event) WARN_UNUSED;
 
 /**

+ 14 - 17
ncd/ncd.c

@@ -193,7 +193,7 @@ 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 (NCDModuleInst *inst, int event);
 static int statement_instance_func_getobj (NCDModuleInst *inst, NCD_string_id_t objname, NCDObject *out_object);
-static int statement_instance_func_initprocess (NCDModuleProcess *mp, const char *template_name);
+static int statement_instance_func_initprocess (NCDModuleProcess *mp, NCD_string_id_t 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);
@@ -1433,39 +1433,36 @@ int statement_instance_func_getobj (NCDModuleInst *inst, NCD_string_id_t objname
     return process_find_object(statement_process(ps), ps->i, objname, out_object);
 }
 
-int statement_instance_func_initprocess (NCDModuleProcess *mp, const char *template_name)
+int statement_instance_func_initprocess (NCDModuleProcess* mp, NCD_string_id_t template_name)
 {
-    // get string id for process name
-    NCD_string_id_t name_id = NCDStringIndex_Lookup(&string_index, template_name);
-    if (name_id < 0) {
-        goto does_not_exist;
-    }
-    
     // find process
-    NCDInterpProcess *iprocess = NCDInterpProg_FindProcess(&iprogram, name_id);
+    NCDInterpProcess *iprocess = NCDInterpProg_FindProcess(&iprogram, template_name);
     if (!iprocess) {
-        goto does_not_exist;
+        const char *str = NCDStringIndex_Value(&string_index, template_name);
+        BLog(BLOG_ERROR, "no template named %s", str);
+        return 0;
     }
     
     // make sure it's a template
     if (!NCDInterpProcess_IsTemplate(iprocess)) {
-        BLog(BLOG_ERROR, "need template to create a process, but %s is a process", template_name);
+        const char *str = NCDStringIndex_Value(&string_index, template_name);
+        BLog(BLOG_ERROR, "need template to create a process, but %s is a process", str);
         return 0;
     }
     
     // create process
     if (!process_new(iprocess, mp)) {
-        BLog(BLOG_ERROR, "failed to create process from template %s", template_name);
+        const char *str = NCDStringIndex_Value(&string_index, template_name);
+        BLog(BLOG_ERROR, "failed to create process from template %s", str);
         return 0;
     }
     
-    BLog(BLOG_INFO, "created process from template %s", template_name);
+    if (BLog_WouldLog(BLOG_INFO, BLOG_CURRENT_CHANNEL)) {
+        const char *str = NCDStringIndex_Value(&string_index, template_name);
+        BLog(BLOG_INFO, "created process from template %s", str);
+    }
     
     return 1;
-    
-does_not_exist:
-    BLog(BLOG_ERROR, "no template named %s", template_name);
-    return 0;
 }
 
 void statement_instance_logfunc (NCDModuleInst *inst)