Răsfoiți Sursa

ncd: NCDInterpProcess: don't depend on NCDProcess to exist after being initialized

ambrop7 13 ani în urmă
părinte
comite
30d5552546
3 a modificat fișierele cu 57 adăugiri și 18 ștergeri
  1. 45 8
      ncd/NCDInterpProcess.c
  2. 7 4
      ncd/NCDInterpProcess.h
  3. 5 6
      ncd/ncd.c

+ 45 - 8
ncd/NCDInterpProcess.c

@@ -170,20 +170,35 @@ int NCDInterpProcess_Init (NCDInterpProcess *o, NCDProcess *process, NCDPlacehol
         goto fail1;
         goto fail1;
     }
     }
     
     
+    if (!(o->name = b_strdup(NCDProcess_Name(process)))) {
+        BLog(BLOG_ERROR, "b_strdup failed");
+        goto fail2;
+    }
+    
     o->num_stmts = 0;
     o->num_stmts = 0;
     o->prealloc_size = -1;
     o->prealloc_size = -1;
-    o->process = process;
+    o->is_template = NCDProcess_IsTemplate(process);
     
     
     for (NCDStatement *s = NCDBlock_FirstStatement(block); s; s = NCDBlock_NextStatement(block, s)) {
     for (NCDStatement *s = NCDBlock_FirstStatement(block); s; s = NCDBlock_NextStatement(block, s)) {
         ASSERT(NCDStatement_Type(s) == NCDSTATEMENT_REG)
         ASSERT(NCDStatement_Type(s) == NCDSTATEMENT_REG)
         struct NCDInterpProcess__stmt *e = &o->stmts[o->num_stmts];
         struct NCDInterpProcess__stmt *e = &o->stmts[o->num_stmts];
         
         
-        e->name = NCDStatement_Name(s);
-        e->cmdname = NCDStatement_RegCmdName(s);
+        e->name = NULL;
+        e->cmdname = NULL;
         e->objnames = NULL;
         e->objnames = NULL;
         e->num_objnames = 0;
         e->num_objnames = 0;
         e->alloc_size = 0;
         e->alloc_size = 0;
         
         
+        if (NCDStatement_Name(s) && !(e->name = b_strdup(NCDStatement_Name(s)))) {
+            BLog(BLOG_ERROR, "b_strdup failed");
+            goto loop_fail0;
+        }
+        
+        if (!(e->cmdname = b_strdup(NCDStatement_RegCmdName(s)))) {
+            BLog(BLOG_ERROR, "b_strdup failed");
+            goto loop_fail0;
+        }
+        
         NCDValMem mem;
         NCDValMem mem;
         NCDValMem_Init(&mem);
         NCDValMem_Init(&mem);
         
         
@@ -247,7 +262,9 @@ int NCDInterpProcess_Init (NCDInterpProcess *o, NCDProcess *process, NCDPlacehol
     loop_fail1:
     loop_fail1:
         NCDValReplaceProg_Free(&e->arg_prog);
         NCDValReplaceProg_Free(&e->arg_prog);
     loop_fail0:
     loop_fail0:
-        goto fail2;
+        free(e->cmdname);
+        free(e->name);
+        goto fail3;
     }
     }
     
     
     ASSERT(o->num_stmts == num_stmts)
     ASSERT(o->num_stmts == num_stmts)
@@ -255,13 +272,17 @@ int NCDInterpProcess_Init (NCDInterpProcess *o, NCDProcess *process, NCDPlacehol
     DebugObject_Init(&o->d_obj);
     DebugObject_Init(&o->d_obj);
     return 1;
     return 1;
     
     
-fail2:
+fail3:
     while (o->num_stmts-- > 0) {
     while (o->num_stmts-- > 0) {
         struct NCDInterpProcess__stmt *e = &o->stmts[o->num_stmts];
         struct NCDInterpProcess__stmt *e = &o->stmts[o->num_stmts];
         free(e->objnames);
         free(e->objnames);
         BFree(e->arg_data);
         BFree(e->arg_data);
         NCDValReplaceProg_Free(&e->arg_prog);
         NCDValReplaceProg_Free(&e->arg_prog);
+        free(e->cmdname);
+        free(e->name);
     }
     }
+    free(o->name);
+fail2:
     NCDInterpProcess__Trie_Free(&o->trie);
     NCDInterpProcess__Trie_Free(&o->trie);
 fail1:
 fail1:
     BFree(o->stmts);
     BFree(o->stmts);
@@ -278,10 +299,12 @@ void NCDInterpProcess_Free (NCDInterpProcess *o)
         free(e->objnames);
         free(e->objnames);
         BFree(e->arg_data);
         BFree(e->arg_data);
         NCDValReplaceProg_Free(&e->arg_prog);
         NCDValReplaceProg_Free(&e->arg_prog);
+        free(e->cmdname);
+        free(e->name);
     }
     }
     
     
+    free(o->name);
     NCDInterpProcess__Trie_Free(&o->trie);
     NCDInterpProcess__Trie_Free(&o->trie);
-    
     BFree(o->stmts);
     BFree(o->stmts);
 }
 }
 
 
@@ -418,9 +441,23 @@ int NCDInterpProcess_StatementPreallocOffset (NCDInterpProcess *o, int i)
     return o->stmts[i].prealloc_offset;
     return o->stmts[i].prealloc_offset;
 }
 }
 
 
-NCDProcess * NCDInterpProcess_Process (NCDInterpProcess *o)
+const char * NCDInterpProcess_Name (NCDInterpProcess *o)
+{
+    DebugObject_Access(&o->d_obj);
+    
+    return o->name;
+}
+
+int NCDInterpProcess_IsTemplate (NCDInterpProcess *o)
+{
+    DebugObject_Access(&o->d_obj);
+    
+    return o->is_template;
+}
+
+int NCDInterpProcess_NumStatements (NCDInterpProcess *o)
 {
 {
     DebugObject_Access(&o->d_obj);
     DebugObject_Access(&o->d_obj);
     
     
-    return o->process;
+    return o->num_stmts;
 }
 }

+ 7 - 4
ncd/NCDInterpProcess.h

@@ -46,8 +46,8 @@
 #include <structure/CStringTrie_decl.h>
 #include <structure/CStringTrie_decl.h>
 
 
 struct NCDInterpProcess__stmt {
 struct NCDInterpProcess__stmt {
-    const char *name;
-    const char *cmdname;
+    char *name;
+    char *cmdname;
     char *objnames;
     char *objnames;
     size_t num_objnames;
     size_t num_objnames;
     char *arg_data;
     char *arg_data;
@@ -65,10 +65,11 @@ struct NCDInterpProcess__stmt {
 
 
 typedef struct {
 typedef struct {
     struct NCDInterpProcess__stmt *stmts;
     struct NCDInterpProcess__stmt *stmts;
+    char *name;
     int num_stmts;
     int num_stmts;
     int prealloc_size;
     int prealloc_size;
+    int is_template;
     NCDInterpProcess__Trie trie;
     NCDInterpProcess__Trie trie;
-    NCDProcess *process;
     DebugObject d_obj;
     DebugObject d_obj;
 } NCDInterpProcess;
 } NCDInterpProcess;
 
 
@@ -84,6 +85,8 @@ void NCDInterpProcess_StatementBumpAllocSize (NCDInterpProcess *o, int i, int al
 int NCDInterpProcess_StatementPreallocSize (NCDInterpProcess *o, int i);
 int NCDInterpProcess_StatementPreallocSize (NCDInterpProcess *o, int i);
 int NCDInterpProcess_PreallocSize (NCDInterpProcess *o);
 int NCDInterpProcess_PreallocSize (NCDInterpProcess *o);
 int NCDInterpProcess_StatementPreallocOffset (NCDInterpProcess *o, int i);
 int NCDInterpProcess_StatementPreallocOffset (NCDInterpProcess *o, int i);
-NCDProcess * NCDInterpProcess_Process (NCDInterpProcess *o);
+const char * NCDInterpProcess_Name (NCDInterpProcess *o);
+int NCDInterpProcess_IsTemplate (NCDInterpProcess *o);
+int NCDInterpProcess_NumStatements (NCDInterpProcess *o);
 
 
 #endif
 #endif

+ 5 - 6
ncd/ncd.c

@@ -374,7 +374,6 @@ int main (int argc, char **argv)
         // find iblock
         // find iblock
         NCDInterpProcess *iprocess = NCDInterpProg_FindProcess(&iprogram, NCDProcess_Name(p));
         NCDInterpProcess *iprocess = NCDInterpProg_FindProcess(&iprogram, NCDProcess_Name(p));
         ASSERT(iprocess)
         ASSERT(iprocess)
-        ASSERT(NCDInterpProcess_Process(iprocess) == p)
         
         
         if (!process_new(iprocess, NULL)) {
         if (!process_new(iprocess, NULL)) {
             BLog(BLOG_ERROR, "failed to initialize process, exiting");
             BLog(BLOG_ERROR, "failed to initialize process, exiting");
@@ -648,8 +647,8 @@ int process_new (NCDInterpProcess *iblock, NCDModuleProcess *module_process)
     ASSERT(iblock)
     ASSERT(iblock)
     
     
     // get num statements
     // get num statements
-    size_t num_statements = NCDBlock_NumStatements(NCDProcess_Block(NCDInterpProcess_Process(iblock)));
-    if (num_statements > INT_MAX) {
+    int num_statements = NCDInterpProcess_NumStatements(iblock);
+    if (num_statements > SIZE_MAX) {
         BLog(BLOG_ERROR, "too many statements");
         BLog(BLOG_ERROR, "too many statements");
         goto fail0;
         goto fail0;
     }
     }
@@ -713,7 +712,7 @@ int process_new (NCDInterpProcess *iblock, NCDModuleProcess *module_process)
     return 1;
     return 1;
     
     
 fail0:
 fail0:
-    BLog(BLOG_ERROR, "failed to initialize process %s", NCDProcess_Name(NCDInterpProcess_Process(iblock)));
+    BLog(BLOG_ERROR, "failed to initialize process %s", NCDInterpProcess_Name(iblock));
     return 0;
     return 0;
 }
 }
 
 
@@ -797,7 +796,7 @@ void process_assert_pointers (struct process *p)
 
 
 void process_logfunc (struct process *p)
 void process_logfunc (struct process *p)
 {
 {
-    BLog_Append("process %s: ", NCDProcess_Name(NCDInterpProcess_Process(p->iblock)));
+    BLog_Append("process %s: ", NCDInterpProcess_Name(p->iblock));
 }
 }
 
 
 void process_log (struct process *p, int level, const char *fmt, ...)
 void process_log (struct process *p, int level, const char *fmt, ...)
@@ -1306,7 +1305,7 @@ int statement_instance_func_initprocess (struct statement *ps, NCDModuleProcess
     }
     }
     
     
     // make sure it's a template
     // make sure it's a template
-    if (!NCDProcess_IsTemplate(NCDInterpProcess_Process(iprocess))) {
+    if (!NCDInterpProcess_IsTemplate(iprocess)) {
         statement_log(ps, BLOG_ERROR, "need template to create a process, but %s is a process", template_name);
         statement_log(ps, BLOG_ERROR, "need template to create a process, but %s is a process", template_name);
         return 0;
         return 0;
     }
     }