瀏覽代碼

Refactoring using MemRef.

Ambroz Bizjak 11 年之前
父節點
當前提交
24e6edd26f
共有 8 個文件被更改,包括 38 次插入25 次删除
  1. 13 0
      misc/expstring.h
  2. 1 1
      ncd/NCDModule.c
  3. 5 0
      ncd/NCDStringIndex.c
  4. 2 0
      ncd/NCDStringIndex.h
  5. 3 7
      ncd/extra/value_utils.c
  6. 3 4
      ncd/modules/basic_functions.c
  7. 8 10
      ncd/modules/implode.c
  8. 3 3
      ncd/modules/regex_match.c

+ 13 - 0
misc/expstring.h

@@ -35,6 +35,7 @@
 #include <misc/debug.h>
 #include <misc/exparray.h>
 #include <misc/bsize.h>
+#include <misc/memref.h>
 
 typedef struct {
     struct ExpArray arr;
@@ -47,9 +48,11 @@ static int ExpString_Append (ExpString *c, const char *str);
 static int ExpString_AppendChar (ExpString *c, char ch);
 static int ExpString_AppendByte (ExpString *c, uint8_t x);
 static int ExpString_AppendBinary (ExpString *c, const uint8_t *data, size_t len);
+static int ExpString_AppendBinaryMr (ExpString *c, MemRef data);
 static int ExpString_AppendZeros (ExpString *c, size_t len);
 static char * ExpString_Get (ExpString *c);
 static size_t ExpString_Length (ExpString *c);
+static MemRef ExpString_GetMr (ExpString *c);
 
 int ExpString_Init (ExpString *c)
 {
@@ -133,6 +136,11 @@ int ExpString_AppendBinary (ExpString *c, const uint8_t *data, size_t len)
     return 1;
 }
 
+int ExpString_AppendBinaryMr (ExpString *c, MemRef data)
+{
+    return ExpString_AppendBinary(c, (uint8_t const *)data.ptr, data.len);
+}
+
 int ExpString_AppendZeros (ExpString *c, size_t len)
 {
     bsize_t newsize = bsize_add(bsize_fromsize(c->n), bsize_add(bsize_fromsize(len), bsize_fromint(1)));
@@ -158,4 +166,9 @@ size_t ExpString_Length (ExpString *c)
     return c->n;
 }
 
+MemRef ExpString_GetMr (ExpString *c)
+{
+    return MemRef_Make((char const *)c->arr.v, c->n);
+}
+
 #endif

+ 1 - 1
ncd/NCDModule.c

@@ -448,7 +448,7 @@ int NCDModuleProcess_InitValue (NCDModuleProcess *o, NCDModuleInst *n, NCDValRef
     if (NCDVal_IsIdString(template_name)) {
         template_name_id = NCDVal_IdStringId(template_name);
     } else {
-        template_name_id = NCDStringIndex_GetBin(n->params->iparams->string_index, NCDVal_StringData(template_name), NCDVal_StringLength(template_name));
+        template_name_id = NCDStringIndex_GetBinMr(n->params->iparams->string_index, NCDVal_StringMemRef(template_name));
         if (template_name_id < 0) {
             BLog(BLOG_ERROR, "NCDStringIndex_GetBin failed");
             return 0;

+ 5 - 0
ncd/NCDStringIndex.c

@@ -216,6 +216,11 @@ NCD_string_id_t NCDStringIndex_GetBin (NCDStringIndex *o, const char *str, size_
     return do_get(o, str, str_len);
 }
 
+NCD_string_id_t NCDStringIndex_GetBinMr (NCDStringIndex *o, MemRef str)
+{
+    return NCDStringIndex_GetBin(o, str.ptr, str.len);
+}
+
 const char * NCDStringIndex_Value (NCDStringIndex *o, NCD_string_id_t id)
 {
     DebugObject_Access(&o->d_obj);

+ 2 - 0
ncd/NCDStringIndex.h

@@ -33,6 +33,7 @@
 #include <limits.h>
 
 #include <misc/debug.h>
+#include <misc/memref.h>
 #include <structure/CHash.h>
 #include <base/DebugObject.h>
 
@@ -75,6 +76,7 @@ NCD_string_id_t NCDStringIndex_Lookup (NCDStringIndex *o, const char *str);
 NCD_string_id_t NCDStringIndex_LookupBin (NCDStringIndex *o, const char *str, size_t str_len);
 NCD_string_id_t NCDStringIndex_Get (NCDStringIndex *o, const char *str);
 NCD_string_id_t NCDStringIndex_GetBin (NCDStringIndex *o, const char *str, size_t str_len);
+NCD_string_id_t NCDStringIndex_GetBinMr (NCDStringIndex *o, MemRef str);
 const char * NCDStringIndex_Value (NCDStringIndex *o, NCD_string_id_t id);
 size_t NCDStringIndex_Length (NCDStringIndex *o, NCD_string_id_t id);
 int NCDStringIndex_HasNulls (NCDStringIndex *o, NCD_string_id_t id);

+ 3 - 7
ncd/extra/value_utils.c

@@ -86,9 +86,7 @@ int ncd_read_uintmax (NCDValRef val, uintmax_t *out)
         return 0;
     }
     
-    size_t length = NCDVal_StringLength(val);
-    
-    return parse_unsigned_integer_bin(NCDVal_StringData(val), length, out);
+    return parse_unsigned_integer_bin(NCDVal_StringData(val), NCDVal_StringLength(val), out);
 }
 
 int ncd_read_time (NCDValRef val, btime_t *out)
@@ -117,7 +115,7 @@ NCD_string_id_t ncd_get_string_id (NCDValRef string, NCDStringIndex *string_inde
         return NCDVal_IdStringId(string);
     }
     
-    return NCDStringIndex_GetBin(string_index, NCDVal_StringData(string), NCDVal_StringLength(string));
+    return NCDStringIndex_GetBinMr(string_index, NCDVal_StringMemRef(string));
 }
 
 NCDValRef ncd_make_uintmax (NCDValMem *mem, uintmax_t value)
@@ -140,9 +138,7 @@ char * ncd_strdup (NCDValRef stringnonulls)
 {
     ASSERT(NCDVal_IsStringNoNulls(stringnonulls))
     
-    size_t length = NCDVal_StringLength(stringnonulls);
-    
-    return b_strdup_bin(NCDVal_StringData(stringnonulls), length);
+    return MemRef_StrDup(NCDVal_StringMemRef(stringnonulls));
 }
 
 int ncd_eval_func_args_ext (NCDCall const *call, size_t start, size_t count, NCDValMem *mem, NCDValRef *out)

+ 3 - 4
ncd/modules/basic_functions.c

@@ -180,9 +180,8 @@ DEFINE_VALUE_COMPARE(different, (cmp != 0))
 static int concat_recurser (ExpString *estr, NCDValRef arg, NCDCall const *call)
 {
     if (NCDVal_IsString(arg)) {
-        MemRef mr = NCDVal_StringMemRef(arg);
-        if (!ExpString_AppendBinary(estr, (uint8_t const *)mr.ptr, mr.len)) {
-            FunctionLog(call, BLOG_ERROR, "ExpString_AppendBinary failed");
+        if (!ExpString_AppendBinaryMr(estr, NCDVal_StringMemRef(arg))) {
+            FunctionLog(call, BLOG_ERROR, "ExpString_AppendBinaryMr failed");
             return 0;
         }
     } else if (NCDVal_IsList(arg)) {
@@ -216,7 +215,7 @@ static void concat_eval (NCDCall call)
             goto fail1;
         }
     }
-    NCDCall_SetResult(&call, NCDVal_NewStringBin(NCDCall_ResMem(&call), (uint8_t const *)ExpString_Get(&estr), ExpString_Length(&estr)));
+    NCDCall_SetResult(&call, NCDVal_NewStringBinMr(NCDCall_ResMem(&call), ExpString_GetMr(&estr)));
 fail1:
     ExpString_Free(&estr);
 fail0:

+ 8 - 10
ncd/modules/implode.c

@@ -47,8 +47,7 @@
 
 struct instance {
     NCDModuleInst *i;
-    char *result;
-    size_t result_len;
+    MemRef result;
 };
 
 static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
@@ -87,22 +86,21 @@ static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new
         
         // append glue
         if (j > 0) {
-            if (!ExpString_AppendBinary(&str, (const uint8_t *)NCDVal_StringData(glue_arg), NCDVal_StringLength(glue_arg))) {
-                ModuleLog(i, BLOG_ERROR, "ExpString_AppendBinary failed");
+            if (!ExpString_AppendBinaryMr(&str, NCDVal_StringMemRef(glue_arg))) {
+                ModuleLog(i, BLOG_ERROR, "ExpString_AppendBinaryMr failed");
                 goto fail1;
             }
         }
         
         // append piece
-        if (!ExpString_AppendBinary(&str, (const uint8_t *)NCDVal_StringData(piece), NCDVal_StringLength(piece))) {
-            ModuleLog(i, BLOG_ERROR, "ExpString_AppendBinary failed");
+        if (!ExpString_AppendBinaryMr(&str, NCDVal_StringMemRef(piece))) {
+            ModuleLog(i, BLOG_ERROR, "ExpString_AppendBinaryMr failed");
             goto fail1;
         }
     }
     
     // store result
-    o->result = ExpString_Get(&str);
-    o->result_len = ExpString_Length(&str);
+    o->result = ExpString_GetMr(&str);
     
     // signal up
     NCDModuleInst_Backend_Up(i);
@@ -119,7 +117,7 @@ static void func_die (void *vo)
     struct instance *o = vo;
     
     // free result
-    free(o->result);
+    free((char *)o->result.ptr);
     
     NCDModuleInst_Backend_Dead(o->i);
 }
@@ -129,7 +127,7 @@ static int func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValR
     struct instance *o = vo;
     
     if (name == NCD_STRING_EMPTY) {
-        *out = NCDVal_NewStringBin(mem, (uint8_t *)o->result, o->result_len);
+        *out = NCDVal_NewStringBinMr(mem, o->result);
         return 1;
     }
     

+ 3 - 3
ncd/modules/regex_match.c

@@ -285,8 +285,8 @@ static void replace_func_new (void *vo, NCDModuleInst *i, const struct NCDModule
         
         // append replacement data
         NCDValRef replace = NCDVal_ListGet(replace_arg, match_regex);
-        if (!ExpString_AppendBinary(&out, (const uint8_t *)NCDVal_StringData(replace), NCDVal_StringLength(replace))) {
-            ModuleLog(i, BLOG_ERROR, "ExpString_AppendBinary failed");
+        if (!ExpString_AppendBinaryMr(&out, NCDVal_StringMemRef(replace))) {
+            ModuleLog(i, BLOG_ERROR, "ExpString_AppendBinaryMr failed");
             goto fail3;
         }
         
@@ -294,7 +294,7 @@ static void replace_func_new (void *vo, NCDModuleInst *i, const struct NCDModule
     }
     
     // set output
-    o->output = MemRef_Make(ExpString_Get(&out), ExpString_Length(&out));
+    o->output = ExpString_GetMr(&out);
     
     // free compiled regex's
     while (num_done_regex-- > 0) {