Просмотр исходного кода

ncd: modules: optimize some boolean reading and making with ID-strings

ambrop7 13 лет назад
Родитель
Сommit
aa4f922a20

+ 4 - 1
ncd/modules/assert.c

@@ -42,6 +42,7 @@
  */
 
 #include <ncd/NCDModule.h>
+#include <ncd/static_strings.h>
 
 #include <generated/blog_channel_ncd_assert.h>
 
@@ -64,7 +65,9 @@ static void func_new_common (NCDModuleInst *i, const struct NCDModuleInst_new_pa
     NCDModuleInst_Backend_Up(i);
     
     // if failed, initiate exit (before up!)
-    if (!NCDVal_StringEquals(cond_arg, (is_false ? "false" : "true"))) {
+    if ((!is_false && !NCDVal_StringEqualsId(cond_arg, NCD_STRING_TRUE, i->params->iparams->string_index)) ||
+        (is_false && !NCDVal_StringEqualsId(cond_arg, NCD_STRING_FALSE, i->params->iparams->string_index))
+    ) {
         ModuleLog(i, BLOG_ERROR, "assertion failed");
         NCDModuleInst_Backend_InterpExit(i, 1);
     }

+ 2 - 1
ncd/modules/choose.c

@@ -45,6 +45,7 @@
 
 #include <ncd/NCDModule.h>
 #include <ncd/static_strings.h>
+#include <ncd/extra/value_utils.h>
 
 #include <generated/blog_channel_ncd_choose.h>
 
@@ -97,7 +98,7 @@ static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new
         }
         
         // update result
-        if (!have_result && NCDVal_StringEquals(c_cond, "true")) {
+        if (!have_result && ncd_read_boolean(c_cond)) {
             o->result = c_result;
             have_result = 1;
         }

+ 3 - 3
ncd/modules/file.c

@@ -88,6 +88,7 @@
 #include <misc/parse_number.h>
 #include <ncd/NCDModule.h>
 #include <ncd/static_strings.h>
+#include <ncd/extra/value_utils.h>
 
 #include <generated/blog_channel_ncd_file.h>
 
@@ -280,10 +281,9 @@ static int stat_func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NC
     struct stat_instance *o = vo;
     
     if (name == strings[STRING_SUCCEEDED].id) {
-        const char *str = (o->succeeded ? "true" : "false");
-        *out = NCDVal_NewString(mem, str);
+        *out = ncd_make_boolean(mem, o->succeeded, o->i->params->iparams->string_index);
         if (NCDVal_IsInvalid(*out)) {
-            ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
+            ModuleLog(o->i, BLOG_ERROR, "ncd_make_boolean failed");
         }
         return 1;
     }

+ 2 - 1
ncd/modules/if.c

@@ -43,6 +43,7 @@
 #include <string.h>
 
 #include <ncd/NCDModule.h>
+#include <ncd/extra/value_utils.h>
 
 #include <generated/blog_channel_ncd_if.h>
 
@@ -62,7 +63,7 @@ static void new_templ (NCDModuleInst *i, const struct NCDModuleInst_new_params *
     }
     
     // compute logical value of argument
-    int c = NCDVal_StringEquals(arg, "true");
+    int c = ncd_read_boolean(arg);
     
     // signal up if needed
     if ((is_not && !c) || (!is_not && c)) {

+ 4 - 8
ncd/modules/list.c

@@ -648,11 +648,9 @@ static int contains_func_getvar (void *vo, const char *name, NCDValMem *mem, NCD
     struct contains_instance *o = vo;
     
     if (!strcmp(name, "")) {
-        const char *value = (o->contains ? "true" : "false");
-        
-        *out = NCDVal_NewString(mem, value);
+        *out = ncd_make_boolean(mem, o->contains, o->i->params->iparams->string_index);
         if (NCDVal_IsInvalid(*out)) {
-            ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
+            ModuleLog(o->i, BLOG_ERROR, "ncd_make_boolean failed");
         }
         return 1;
     }
@@ -725,11 +723,9 @@ static int find_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValR
     }
     
     if (!strcmp(name, "found")) {
-        const char *value = (o->is_found ? "true" : "false");
-        
-        *out = NCDVal_NewString(mem, value);
+        *out = ncd_make_boolean(mem, o->is_found, o->i->params->iparams->string_index);
         if (NCDVal_IsInvalid(*out)) {
-            ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
+            ModuleLog(o->i, BLOG_ERROR, "ncd_make_boolean failed");
         }
         return 1;
     }

+ 5 - 6
ncd/modules/logical.c

@@ -48,6 +48,7 @@
 
 #include <ncd/NCDModule.h>
 #include <ncd/static_strings.h>
+#include <ncd/extra/value_utils.h>
 
 #include <generated/blog_channel_ncd_logical.h>
 
@@ -75,7 +76,7 @@ static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new
             goto fail0;
         }
         
-        o->value = !NCDVal_StringEquals(arg, "true");
+        o->value = !ncd_read_boolean(arg);
     } else {
         o->value = (is_or ? 0 : 1);
         
@@ -89,7 +90,7 @@ static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new
                 goto fail0;
             }
             
-            int this_value = NCDVal_StringEquals(arg, "true");
+            int this_value = ncd_read_boolean(arg);
             if (is_or) {
                 o->value = o->value || this_value;
             } else {
@@ -127,11 +128,9 @@ static int func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValR
     struct instance *o = vo;
     
     if (name == NCD_STRING_EMPTY) {
-        const char *v = (o->value ? "true" : "false");
-        
-        *out = NCDVal_NewString(mem, v);
+        *out = ncd_make_boolean(mem, o->value, o->i->params->iparams->string_index);
         if (NCDVal_IsInvalid(*out)) {
-            ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
+            ModuleLog(o->i, BLOG_ERROR, "ncd_make_boolean failed");
         }
         return 1;
     }

+ 3 - 3
ncd/modules/net_ipv4_addr_in_network.c

@@ -49,6 +49,7 @@
 
 #include <misc/ipaddr.h>
 #include <ncd/NCDModule.h>
+#include <ncd/extra/value_utils.h>
 
 #include <generated/blog_channel_ncd_net_ipv4_addr_in_network.h>
 
@@ -140,10 +141,9 @@ static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *o
     struct instance *o = vo;
     
     if (!strcmp(name, "")) {
-        const char *v = (o->value ? "true" : "false");
-        *out = NCDVal_NewString(mem, v);
+        *out = ncd_make_boolean(mem, o->value, o->i->params->iparams->string_index);
         if (NCDVal_IsInvalid(*out)) {
-            ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
+            ModuleLog(o->i, BLOG_ERROR, "ncd_make_boolean failed");
         }
         return 1;
     }

+ 3 - 4
ncd/modules/net_ipv4_arp_probe.c

@@ -49,6 +49,7 @@
 #include <misc/ipaddr.h>
 #include <arpprobe/BArpProbe.h>
 #include <ncd/NCDModule.h>
+#include <ncd/extra/value_utils.h>
 
 #include <generated/blog_channel_ncd_net_ipv4_arp_probe.h>
 
@@ -187,11 +188,9 @@ static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *o
     ASSERT(o->state == STATE_EXIST || o->state == STATE_NOEXIST)
     
     if (!strcmp(name, "exists")) {
-        const char *str = (o->state == STATE_EXIST ? "true" : "false");
-        
-        *out = NCDVal_NewString(mem, str);
+        *out = ncd_make_boolean(mem, o->state == STATE_EXIST, o->i->params->iparams->string_index);
         if (NCDVal_IsInvalid(*out)) {
-            ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
+            ModuleLog(o->i, BLOG_ERROR, "ncd_make_boolean failed");
         }
         return 1;
     }

+ 3 - 3
ncd/modules/net_ipv6_addr_in_network.c

@@ -49,6 +49,7 @@
 
 #include <misc/ipaddr6.h>
 #include <ncd/NCDModule.h>
+#include <ncd/extra/value_utils.h>
 
 #include <generated/blog_channel_ncd_net_ipv6_addr_in_network.h>
 
@@ -140,10 +141,9 @@ static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *o
     struct instance *o = vo;
     
     if (!strcmp(name, "")) {
-        const char *v = (o->value ? "true" : "false");
-        *out = NCDVal_NewString(mem, v);
+        *out = ncd_make_boolean(mem, o->value, o->i->params->iparams->string_index);
         if (NCDVal_IsInvalid(*out)) {
-            ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
+            ModuleLog(o->i, BLOG_ERROR, "ncd_make_boolean failed");
         }
         return 1;
     }

+ 6 - 9
ncd/modules/parse.c

@@ -205,10 +205,9 @@ static int func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValR
     struct instance *o = vo;
     
     if (name == strings[STRING_SUCCEEDED].id) {
-        const char *str = o->succeeded ? "true" : "false";
-        *out = NCDVal_NewString(mem, str);
+        *out = ncd_make_boolean(mem, o->succeeded, o->i->params->iparams->string_index);
         if (NCDVal_IsInvalid(*out)) {
-            ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
+            ModuleLog(o->i, BLOG_ERROR, "ncd_make_boolean failed");
         }
         return 1;
     }
@@ -274,10 +273,9 @@ static int ipv4_cidr_addr_func_getvar2 (void *vo, NCD_string_id_t name, NCDValMe
     struct ipv4_cidr_instance *o = vo;
     
     if (name == strings[STRING_SUCCEEDED].id) {
-        const char *str = o->succeeded ? "true" : "false";
-        *out = NCDVal_NewString(mem, str);
+        *out = ncd_make_boolean(mem, o->succeeded, o->i->params->iparams->string_index);
         if (NCDVal_IsInvalid(*out)) {
-            ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
+            ModuleLog(o->i, BLOG_ERROR, "ncd_make_boolean failed");
         }
         return 1;
     }
@@ -338,10 +336,9 @@ static int ipv6_cidr_addr_func_getvar2 (void *vo, NCD_string_id_t name, NCDValMe
     struct ipv6_cidr_instance *o = vo;
     
     if (name == strings[STRING_SUCCEEDED].id) {
-        const char *str = o->succeeded ? "true" : "false";
-        *out = NCDVal_NewString(mem, str);
+        *out = ncd_make_boolean(mem, o->succeeded, o->i->params->iparams->string_index);
         if (NCDVal_IsInvalid(*out)) {
-            ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
+            ModuleLog(o->i, BLOG_ERROR, "ncd_make_boolean failed");
         }
         return 1;
     }

+ 3 - 3
ncd/modules/regex_match.c

@@ -75,6 +75,7 @@
 #include <misc/debug.h>
 #include <misc/balloc.h>
 #include <ncd/NCDModule.h>
+#include <ncd/extra/value_utils.h>
 
 #include <generated/blog_channel_ncd_regex_match.h>
 
@@ -160,10 +161,9 @@ static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *o
     struct instance *o = vo;
     
     if (!strcmp(name, "succeeded")) {
-        const char *str = o->succeeded ? "true" : "false";
-        *out = NCDVal_NewString(mem, str);
+        *out = ncd_make_boolean(mem, o->succeeded, o->i->params->iparams->string_index);
         if (NCDVal_IsInvalid(*out)) {
-            ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
+            ModuleLog(o->i, BLOG_ERROR, "ncd_make_boolean failed");
         }
         return 1;
     }

+ 3 - 3
ncd/modules/strcmp.c

@@ -40,6 +40,7 @@
 
 #include <ncd/NCDModule.h>
 #include <ncd/static_strings.h>
+#include <ncd/extra/value_utils.h>
 
 #include <generated/blog_channel_ncd_strcmp.h>
 
@@ -84,10 +85,9 @@ static int func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValR
     struct instance *o = vo;
     
     if (name == NCD_STRING_EMPTY) {
-        const char *v = o->result ? "true" : "false";
-        *out = NCDVal_NewString(mem, v);
+        *out = ncd_make_boolean(mem, o->result, o->i->params->iparams->string_index);
         if (NCDVal_IsInvalid(*out)) {
-            ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
+            ModuleLog(o->i, BLOG_ERROR, "ncd_make_boolean failed");
         }
         return 1;
     }

+ 3 - 3
ncd/modules/try.c

@@ -61,6 +61,7 @@
 
 #include <misc/offset.h>
 #include <ncd/NCDModule.h>
+#include <ncd/extra/value_utils.h>
 
 #include <generated/blog_channel_ncd_try.h>
 
@@ -236,10 +237,9 @@ static int func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValR
     ASSERT(!o->dying)
     
     if (name == strings[STRING_SUCCEEDED].id) {
-        const char *str = (o->succeeded ? "true" : "false");
-        *out = NCDVal_NewString(mem, str);
+        *out = ncd_make_boolean(mem, o->succeeded, o->i->params->iparams->string_index);
         if (NCDVal_IsInvalid(*out)) {
-            ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
+            ModuleLog(o->i, BLOG_ERROR, "ncd_make_boolean failed");
         }
         return 1;
     }

+ 2 - 3
ncd/modules/value.c

@@ -1108,10 +1108,9 @@ static int func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValR
     struct value *v = valref_val(&o->ref);
     
     if (name == strings[STRING_EXISTS].id) {
-        const char *str = v ? "true" : "false";
-        *out = NCDVal_NewString(mem, str);
+        *out = ncd_make_boolean(mem, !!v, o->i->params->iparams->string_index);
         if (NCDVal_IsInvalid(*out)) {
-            ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
+            ModuleLog(o->i, BLOG_ERROR, "ncd_make_boolean failed");
         }
         return 1;
     }

+ 3 - 4
ncd/modules/valuemetic.c

@@ -59,6 +59,7 @@
 
 #include <ncd/NCDModule.h>
 #include <ncd/static_strings.h>
+#include <ncd/extra/value_utils.h>
 
 #include <generated/blog_channel_ncd_valuemetic.h>
 
@@ -135,11 +136,9 @@ static int func_getvar2 (void *vo, NCD_string_id_t name, NCDValMem *mem, NCDValR
     struct instance *o = vo;
     
     if (name == NCD_STRING_EMPTY) {
-        const char *str = o->result ? "true" : "false";
-        
-        *out = NCDVal_NewString(mem, str);
+        *out = ncd_make_boolean(mem, o->result, o->i->params->iparams->string_index);
         if (NCDVal_IsInvalid(*out)) {
-            ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewString failed");
+            ModuleLog(o->i, BLOG_ERROR, "ncd_make_boolean failed");
         }
         return 1;
     }