소스 검색

ncd: store objecte names as strings one after another, instead of with arrays of pointers

ambrop7 13 년 전
부모
커밋
ff16a3b8f3
3개의 변경된 파일30개의 추가작업 그리고 22개의 파일을 삭제
  1. 15 11
      ncd/NCDInterpBlock.c
  2. 3 2
      ncd/NCDInterpBlock.h
  3. 12 9
      ncd/ncd.c

+ 15 - 11
ncd/NCDInterpBlock.c

@@ -36,6 +36,7 @@
 #include <misc/split_string.h>
 #include <misc/hashfun.h>
 #include <misc/maxalign.h>
+#include <misc/strdup.h>
 #include <base/BLog.h>
 
 #include "NCDInterpBlock.h"
@@ -177,6 +178,7 @@ int NCDInterpBlock_Init (NCDInterpBlock *o, NCDBlock *block, NCDProcess *process
         e->name = NCDStatement_Name(s);
         e->cmdname = NCDStatement_RegCmdName(s);
         e->objnames = NULL;
+        e->num_objnames = 0;
         e->alloc_size = 0;
         
         NCDValMem mem;
@@ -203,9 +205,12 @@ int NCDInterpBlock_Init (NCDInterpBlock *o, NCDBlock *block, NCDProcess *process
             goto loop_fail1;
         }
         
-        if (NCDStatement_RegObjName(s) && !(e->objnames = split_string(NCDStatement_RegObjName(s), '.'))) {
-            BLog(BLOG_ERROR, "split_string failed");
-            goto loop_fail2;
+        if (NCDStatement_RegObjName(s)) {
+            if (!(e->objnames = b_strdup(NCDStatement_RegObjName(s)))) {
+                BLog(BLOG_ERROR, "b_strdup failed");
+                goto loop_fail2;
+            }
+            e->num_objnames = split_string_inplace2(e->objnames, '.') + 1;
         }
         
         if (e->name) {
@@ -232,9 +237,7 @@ int NCDInterpBlock_Init (NCDInterpBlock *o, NCDBlock *block, NCDProcess *process
 fail2:
     while (o->num_stmts-- > 0) {
         struct NCDInterpBlock__stmt *e = &o->stmts[o->num_stmts];
-        if (e->objnames) {
-            free_strings(e->objnames);
-        }
+        free(e->objnames);
         BFree(e->arg_data);
         NCDValReplaceProg_Free(&e->arg_prog);
     }
@@ -250,9 +253,7 @@ void NCDInterpBlock_Free (NCDInterpBlock *o)
     
     while (o->num_stmts-- > 0) {
         struct NCDInterpBlock__stmt *e = &o->stmts[o->num_stmts];
-        if (e->objnames) {
-            free_strings(e->objnames);
-        }
+        free(e->objnames);
         BFree(e->arg_data);
         NCDValReplaceProg_Free(&e->arg_prog);
     }
@@ -298,13 +299,16 @@ const char * NCDInterpBlock_StatementCmdName (NCDInterpBlock *o, int i)
     return o->stmts[i].cmdname;
 }
 
-char ** NCDInterpBlock_StatementObjNames (NCDInterpBlock *o, int i)
+void NCDInterpBlock_StatementObjNames (NCDInterpBlock *o, int i, const char **out_objnames, size_t *out_num_objnames)
 {
     DebugObject_Access(&o->d_obj);
     ASSERT(i >= 0)
     ASSERT(i < o->num_stmts)
+    ASSERT(out_objnames)
+    ASSERT(out_num_objnames)
     
-    return o->stmts[i].objnames;
+    *out_objnames = o->stmts[i].objnames;
+    *out_num_objnames = o->stmts[i].num_objnames;
 }
 
 int NCDInterpBlock_CopyStatementArgs (NCDInterpBlock *o, int i, NCDValMem *out_valmem, NCDValRef *out_val, NCDValReplaceProg *out_prog)

+ 3 - 2
ncd/NCDInterpBlock.h

@@ -42,7 +42,8 @@
 struct NCDInterpBlock__stmt {
     const char *name;
     const char *cmdname;
-    char **objnames;
+    char *objnames;
+    size_t num_objnames;
     char *arg_data;
     size_t arg_len;
     NCDValSafeRef arg_ref;
@@ -72,7 +73,7 @@ int NCDInterpBlock_Init (NCDInterpBlock *o, NCDBlock *block, NCDProcess *process
 void NCDInterpBlock_Free (NCDInterpBlock *o);
 int NCDInterpBlock_FindStatement (NCDInterpBlock *o, int from_index, const char *name);
 const char * NCDInterpBlock_StatementCmdName (NCDInterpBlock *o, int i);
-char ** NCDInterpBlock_StatementObjNames (NCDInterpBlock *o, int i);
+void NCDInterpBlock_StatementObjNames (NCDInterpBlock *o, int i, const char **out_objnames, size_t *out_num_objnames);
 int NCDInterpBlock_CopyStatementArgs (NCDInterpBlock *o, int i, NCDValMem *out_valmem, NCDValRef *out_val, NCDValReplaceProg *out_prog) WARN_UNUSED;
 void NCDInterpBlock_StatementBumpAllocSize (NCDInterpBlock *o, int i, int alloc_size);
 int NCDInterpBlock_StatementPreallocSize (NCDInterpBlock *o, int i);

+ 12 - 9
ncd/ncd.c

@@ -171,7 +171,7 @@ static int replace_placeholders_callback (void *arg, int plid, NCDValMem *mem, N
 static void process_advance (struct process *p);
 static void process_wait_timer_handler (struct process *p);
 static int process_find_object (struct process *p, int pos, const char *name, NCDObject *out_object);
-static int process_resolve_object_expr (struct process *p, int pos, char **names, NCDObject *out_object);
+static int process_resolve_object_expr (struct process *p, int pos, const char *names, size_t num_names, NCDObject *out_object);
 static int process_resolve_variable_expr (struct process *p, int pos, const char *names, size_t num_names, NCDValMem *mem, NCDValRef *out_value);
 static void statement_logfunc (struct statement *ps);
 static void statement_log (struct statement *ps, int level, const char *fmt, ...);
@@ -1005,13 +1005,16 @@ void process_advance (struct process *p)
     NCDObject object;
     NCDObject *object_ptr = NULL;
     
-    char **object_names = NCDInterpBlock_StatementObjNames(p->iblock, p->ap);
+    const char *objnames;
+    size_t num_objnames;
+    NCDInterpBlock_StatementObjNames(p->iblock, p->ap, &objnames, &num_objnames);
+    
     const char *type = NCDInterpBlock_StatementCmdName(p->iblock, p->ap);
     
     // if this is a method-like statement, type is really "base_type(object)::method_name"
-    if (object_names) {
+    if (objnames) {
         // get object
-        if (!process_resolve_object_expr(p, p->ap, object_names, &object)) {
+        if (!process_resolve_object_expr(p, p->ap, objnames, num_objnames, &object)) {
             goto fail0;
         }
         object_ptr = &object;
@@ -1131,27 +1134,27 @@ int process_find_object (struct process *p, int pos, const char *name, NCDObject
     return 0;
 }
 
-int process_resolve_object_expr (struct process *p, int pos, char **names, NCDObject *out_object)
+int process_resolve_object_expr (struct process *p, int pos, const char *names, size_t num_names, NCDObject *out_object)
 {
     ASSERT(pos >= 0)
     ASSERT(pos <= p->num_statements)
     ASSERT(names)
-    ASSERT(count_strings(names) > 0)
+    ASSERT(num_names > 0)
     ASSERT(out_object)
     
     NCDObject object;
-    if (!process_find_object(p, pos, names[0], &object)) {
+    if (!process_find_object(p, pos, names, &object)) {
         goto fail;
     }
     
-    if (!NCDObject_ResolveObjExpr(&object, names + 1, out_object)) {
+    if (!NCDObject_ResolveObjExprCompact(&object, names + strlen(names) + 1, num_names - 1, out_object)) {
         goto fail;
     }
     
     return 1;
     
 fail:;
-    char *name = implode_strings(names, '.');
+    char *name = implode_compact_strings(names, num_names, '.');
     process_log(p, BLOG_ERROR, "failed to resolve object (%s) from position %zu", (name ? name : ""), pos);
     free(name);
     return 0;