Quellcode durchsuchen

ncd: NCDInterpProg: lookup processes by string ids

ambrop7 vor 13 Jahren
Ursprung
Commit
5ea5a974f1
4 geänderte Dateien mit 33 neuen und 17 gelöschten Zeilen
  1. 8 5
      ncd/NCDInterpProg.c
  2. 3 3
      ncd/NCDInterpProg.h
  3. 5 5
      ncd/NCDInterpProg_hash.h
  4. 17 4
      ncd/ncd.c

+ 8 - 5
ncd/NCDInterpProg.c

@@ -29,7 +29,6 @@
 
 #include <stdint.h>
 #include <limits.h>
-#include <string.h>
 
 #include <misc/balloc.h>
 #include <misc/hashfun.h>
@@ -71,7 +70,11 @@ int NCDInterpProg_Init (NCDInterpProg *o, NCDProgram *prog, NCDStringIndex *stri
     for (NCDProcess *p = NCDProgram_FirstProcess(prog); p; p = NCDProgram_NextProcess(prog, p)) {
         struct NCDInterpProg__process *e = &o->procs[o->num_procs];
         
-        e->name = NCDProcess_Name(p);
+        e->name = NCDStringIndex_Get(string_index, NCDProcess_Name(p));
+        if (e->name < 0) {
+            BLog(BLOG_ERROR, "NCDStringIndex_Get failed");
+            goto fail2;
+        }
         
         if (!NCDInterpProcess_Init(&e->iprocess, p, string_index, pdb, module_index, method_index)) {
             BLog(BLOG_ERROR, "NCDInterpProcess_Init failed");
@@ -117,17 +120,17 @@ void NCDInterpProg_Free (NCDInterpProg *o)
     BFree(o->procs);
 }
 
-NCDInterpProcess * NCDInterpProg_FindProcess (NCDInterpProg *o, const char *name)
+NCDInterpProcess * NCDInterpProg_FindProcess (NCDInterpProg *o, NCD_string_id_t name)
 {
     DebugObject_Access(&o->d_obj);
-    ASSERT(name)
+    ASSERT(name >= 0)
     
     NCDInterpProg__HashRef ref = NCDInterpProg__Hash_Lookup(&o->hash, o->procs, name);
     if (ref.link == NCDInterpProg__HashNullLink()) {
         return NULL;
     }
     
-    ASSERT(!strcmp(ref.ptr->name, name))
+    ASSERT(ref.ptr->name == name)
     
     return &ref.ptr->iprocess;
 }

+ 3 - 3
ncd/NCDInterpProg.h

@@ -34,16 +34,16 @@
 #include <base/DebugObject.h>
 #include <ncd/NCDAst.h>
 #include <ncd/NCDInterpProcess.h>
+#include <ncd/NCDStringIndex.h>
 #include <structure/CHash.h>
 
 struct NCDInterpProg__process {
-    const char *name;
+    NCD_string_id_t name;
     NCDInterpProcess iprocess;
     int hash_next;
 };
 
 typedef struct NCDInterpProg__process NCDInterpProg__hashentry;
-typedef const char *NCDInterpProg__hashkey;
 typedef struct NCDInterpProg__process *NCDInterpProg__hasharg;
 
 #include "NCDInterpProg_hash.h"
@@ -58,6 +58,6 @@ typedef struct {
 
 int NCDInterpProg_Init (NCDInterpProg *o, NCDProgram *prog, NCDStringIndex *string_index, NCDPlaceholderDb *pdb, NCDModuleIndex *module_index, NCDMethodIndex *method_index) WARN_UNUSED;
 void NCDInterpProg_Free (NCDInterpProg *o);
-NCDInterpProcess * NCDInterpProg_FindProcess (NCDInterpProg *o, const char *name);
+NCDInterpProcess * NCDInterpProg_FindProcess (NCDInterpProg *o, NCD_string_id_t name);
 
 #endif

+ 5 - 5
ncd/NCDInterpProg_hash.h

@@ -1,12 +1,12 @@
 #define CHASH_PARAM_NAME NCDInterpProg__Hash
 #define CHASH_PARAM_ENTRY NCDInterpProg__hashentry
 #define CHASH_PARAM_LINK int
-#define CHASH_PARAM_KEY NCDInterpProg__hashkey
+#define CHASH_PARAM_KEY NCD_string_id_t
 #define CHASH_PARAM_ARG NCDInterpProg__hasharg
 #define CHASH_PARAM_NULL ((int)-1)
 #define CHASH_PARAM_DEREF(arg, link) (&(arg)[(link)])
-#define CHASH_PARAM_ENTRYHASH(arg, entry) (badvpn_djb2_hash((const uint8_t *)(entry).ptr->name))
-#define CHASH_PARAM_KEYHASH(arg, key) (badvpn_djb2_hash((const uint8_t *)(key)))
-#define CHASH_PARAM_COMPARE_ENTRIES(arg, entry1, entry2) (!strcmp((entry1).ptr->name, (entry2).ptr->name))
-#define CHASH_PARAM_COMPARE_KEY_ENTRY(arg, key1, entry2) (!strcmp((key1), (entry2).ptr->name))
+#define CHASH_PARAM_ENTRYHASH(arg, entry) ((size_t)(entry).ptr->name)
+#define CHASH_PARAM_KEYHASH(arg, key) ((size_t)(key))
+#define CHASH_PARAM_COMPARE_ENTRIES(arg, entry1, entry2) ((entry1).ptr->name == (entry2).ptr->name)
+#define CHASH_PARAM_COMPARE_KEY_ENTRY(arg, key1, entry2) ((key1) == (entry2).ptr->name)
 #define CHASH_PARAM_ENTRY_NEXT hash_next

+ 17 - 4
ncd/ncd.c

@@ -411,8 +411,12 @@ int main (int argc, char **argv)
             continue;
         }
         
+        // get string id for process name
+        NCD_string_id_t name_id = NCDStringIndex_Lookup(&string_index, NCDProcess_Name(p));
+        ASSERT(name_id >= 0)
+        
         // find iprocess
-        NCDInterpProcess *iprocess = NCDInterpProg_FindProcess(&iprogram, NCDProcess_Name(p));
+        NCDInterpProcess *iprocess = NCDInterpProg_FindProcess(&iprogram, name_id);
         ASSERT(iprocess)
         
         if (!process_new(iprocess, NULL)) {
@@ -1431,11 +1435,16 @@ int statement_instance_func_getobj (NCDModuleInst *inst, NCD_string_id_t objname
 
 int statement_instance_func_initprocess (NCDModuleProcess *mp, const char *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, template_name);
+    NCDInterpProcess *iprocess = NCDInterpProg_FindProcess(&iprogram, name_id);
     if (!iprocess) {
-        BLog(BLOG_ERROR, "no template named %s", template_name);
-        return 0;
+        goto does_not_exist;
     }
     
     // make sure it's a template
@@ -1453,6 +1462,10 @@ int statement_instance_func_initprocess (NCDModuleProcess *mp, const char *templ
     BLog(BLOG_INFO, "created process from template %s", template_name);
     
     return 1;
+    
+does_not_exist:
+    BLog(BLOG_ERROR, "no template named %s", template_name);
+    return 0;
 }
 
 void statement_instance_logfunc (NCDModuleInst *inst)