Ver código fonte

Refactoring using MemRef.

Ambroz Bizjak 11 anos atrás
pai
commit
b27f9c105b

+ 7 - 0
misc/cmdline.h

@@ -40,6 +40,7 @@
 #include <misc/debug.h>
 #include <misc/exparray.h>
 #include <misc/strdup.h>
+#include <misc/memref.h>
 
 typedef struct {
     struct ExpArray arr;
@@ -50,6 +51,7 @@ static int CmdLine_Init (CmdLine *c);
 static void CmdLine_Free (CmdLine *c);
 static int CmdLine_Append (CmdLine *c, const char *str);
 static int CmdLine_AppendNoNull (CmdLine *c, const char *str, size_t str_len);
+static int CmdLine_AppendNoNullMr (CmdLine *c, MemRef mr);
 static int CmdLine_AppendMulti (CmdLine *c, int num, ...);
 static int CmdLine_Finish (CmdLine *c);
 static char ** CmdLine_Get (CmdLine *c);
@@ -116,6 +118,11 @@ int CmdLine_AppendNoNull (CmdLine *c, const char *str, size_t str_len)
     return 1;
 }
 
+int CmdLine_AppendNoNullMr (CmdLine *c, MemRef mr)
+{
+    return CmdLine_AppendNoNull(c, mr.ptr, mr.len);
+}
+
 int CmdLine_AppendMulti (CmdLine *c, int num, ...)
 {
     int res = 1;

+ 17 - 0
misc/memref.h

@@ -44,6 +44,7 @@ typedef struct {
 } MemRef;
 
 static MemRef MemRef_Make (char const *ptr, size_t len);
+static MemRef MemRef_MakeCstr (char const *ptr);
 static char MemRef_At (MemRef o, size_t pos);
 static void MemRef_AssertRange (MemRef o, size_t offset, size_t length);
 static MemRef MemRef_SubFrom (MemRef o, size_t offset);
@@ -51,6 +52,7 @@ static MemRef MemRef_SubTo (MemRef o, size_t length);
 static MemRef MemRef_Sub (MemRef o, size_t offset, size_t length);
 static char * MemRef_StrDup (MemRef o);
 static void MemRef_CopyOut (MemRef o, char *out);
+static int MemRef_Equal (MemRef o, MemRef other);
 
 #define MEMREF_LOOP_CHARS__BODY(char_rel_pos_var, char_var, body) \
 { \
@@ -86,6 +88,13 @@ static MemRef MemRef_Make (char const *ptr, size_t len)
     return res;
 }
 
+static MemRef MemRef_MakeCstr (char const *ptr)
+{
+    ASSERT(ptr)
+    
+    return MemRef_Make(ptr, strlen(ptr));
+}
+
 static char MemRef_At (MemRef o, size_t pos)
 {
     ASSERT(o.ptr)
@@ -139,4 +148,12 @@ static void MemRef_CopyOut (MemRef o, char *out)
     memcpy(out, o.ptr, o.len);
 }
 
+static int MemRef_Equal (MemRef o, MemRef other)
+{
+    ASSERT(o.ptr)
+    ASSERT(other.ptr)
+    
+    return (o.len == other.len) && !memcmp(o.ptr, other.ptr, o.len);
+}
+
 #endif

+ 5 - 0
ncd/NCDVal.c

@@ -813,6 +813,11 @@ fail:
     return NCDVal_NewInvalid();
 }
 
+NCDValRef NCDVal_NewStringBinMr (NCDValMem *mem, MemRef data)
+{
+    return NCDVal_NewStringBin(mem, (uint8_t const *)data.ptr, data.len);
+}
+
 NCDValRef NCDVal_NewStringUninitialized (NCDValMem *mem, size_t len)
 {
     NCDVal__AssertMem(mem);

+ 5 - 0
ncd/NCDVal.h

@@ -232,6 +232,11 @@ NCDValRef NCDVal_NewString (NCDValMem *mem, const char *data);
  */
 NCDValRef NCDVal_NewStringBin (NCDValMem *mem, const uint8_t *data, size_t len);
 
+/**
+ * See NCDVal_NewStringBin.
+ */
+NCDValRef NCDVal_NewStringBinMr (NCDValMem *mem, MemRef data);
+
 /**
  * Builds a new StoredString of the given length with undefined contents.
  * You can define the contents of the string later by copying to the address

+ 2 - 3
ncd/extra/build_cmdline.c

@@ -85,9 +85,8 @@ int ncd_build_cmdline (NCDModuleInst *i, int log_channel, NCDValRef cmd_arg, cha
             goto fail2;
         }
         
-        MemRef mr = NCDVal_StringMemRef(arg);
-        if (!CmdLine_AppendNoNull(&cl, mr.ptr, mr.len)) {
-            NCDModuleInst_Backend_Log(i, log_channel, BLOG_ERROR, "CmdLine_AppendNoNull failed");
+        if (!CmdLine_AppendNoNullMr(&cl, NCDVal_StringMemRef(arg))) {
+            NCDModuleInst_Backend_Log(i, log_channel, BLOG_ERROR, "CmdLine_AppendNoNullMr failed");
             goto fail2;
         }
     }

+ 2 - 3
ncd/modules/daemon.c

@@ -129,9 +129,8 @@ static int build_cmdline (NCDModuleInst *i, NCDValRef cmd_arg, char **exec, CmdL
             goto fail2;
         }
         
-        MemRef mr = NCDVal_StringMemRef(arg);
-        if (!CmdLine_AppendNoNull(cl, mr.ptr, mr.len)) {
-            ModuleLog(i, BLOG_ERROR, "CmdLine_AppendNoNull failed");
+        if (!CmdLine_AppendNoNullMr(cl, NCDVal_StringMemRef(arg))) {
+            ModuleLog(i, BLOG_ERROR, "CmdLine_AppendNoNullMr failed");
             goto fail2;
         }
     }

+ 10 - 14
ncd/modules/depend.c

@@ -69,8 +69,7 @@
 
 struct provide {
     NCDModuleInst *i;
-    const char *name;
-    size_t name_len;
+    MemRef name;
     int is_queued;
     union {
         struct {
@@ -87,8 +86,7 @@ struct provide {
 
 struct depend {
     NCDModuleInst *i;
-    const char *name;
-    size_t name_len;
+    MemRef name;
     struct provide *p;
     LinkedList1Node node;
 };
@@ -98,13 +96,13 @@ struct global {
     LinkedList1 free_depends;
 };
 
-static struct provide * find_provide (struct global *g, const char *name, size_t name_len)
+static struct provide * find_provide (struct global *g, MemRef name)
 {
     for (LinkedList1Node *n = LinkedList1_GetFirst(&g->provides); n; n = LinkedList1Node_Next(n)) {
         struct provide *p = UPPER_OBJECT(n, struct provide, provides_node);
         ASSERT(!p->is_queued)
         
-        if (p->name_len == name_len && !memcmp(p->name, name, name_len)) {
+        if (MemRef_Equal(p->name, name)) {
             return p;
         }
     }
@@ -115,7 +113,7 @@ static struct provide * find_provide (struct global *g, const char *name, size_t
 static void provide_promote (struct provide *o)
 {
     struct global *g = ModuleGlobal(o->i);
-    ASSERT(!find_provide(g, o->name, o->name_len))
+    ASSERT(!find_provide(g, o->name))
     
     // set not queued
     o->is_queued = 0;
@@ -136,7 +134,7 @@ static void provide_promote (struct provide *o)
         struct depend *d = UPPER_OBJECT(n, struct depend, node);
         ASSERT(!d->p)
         
-        if (d->name_len != o->name_len || memcmp(d->name, o->name, d->name_len)) {
+        if (!MemRef_Equal(d->name, o->name)) {
             n = next;
             continue;
         }
@@ -204,8 +202,7 @@ static void provide_func_new_templ (void *vo, NCDModuleInst *i, const struct NCD
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
         goto fail0;
     }
-    o->name = NCDVal_StringData(name_arg);
-    o->name_len = NCDVal_StringLength(name_arg);
+    o->name = NCDVal_StringMemRef(name_arg);
     
     // signal up.
     // This comes above provide_promote(), so that effects on related depend statements are
@@ -213,7 +210,7 @@ static void provide_func_new_templ (void *vo, NCDModuleInst *i, const struct NCD
     NCDModuleInst_Backend_Up(o->i);
     
     // check for existing provide with this name
-    struct provide *ep = find_provide(g, o->name, o->name_len);
+    struct provide *ep = find_provide(g, o->name);
     if (ep) {
         ASSERT(!ep->is_queued)
         
@@ -322,11 +319,10 @@ static void depend_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleI
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
         goto fail0;
     }
-    o->name = NCDVal_StringData(name_arg);
-    o->name_len = NCDVal_StringLength(name_arg);
+    o->name = NCDVal_StringMemRef(name_arg);
     
     // find a provide with our name
-    struct provide *p = find_provide(g, o->name, o->name_len);
+    struct provide *p = find_provide(g, o->name);
     ASSERT(!p || !p->is_queued)
     
     if (p && !p->dying) {

+ 6 - 8
ncd/modules/dynamic_depend.c

@@ -327,12 +327,11 @@ static void provide_func_new (void *vo, NCDModuleInst *i, const struct NCDModule
         ModuleLog(i, BLOG_ERROR, "wrong type");
         goto fail0;
     }
-    const char *name_str = NCDVal_StringData(name_arg);
-    size_t name_len = NCDVal_StringLength(name_arg);
+    MemRef name = NCDVal_StringMemRef(name_arg);
     
     // find name, create new if needed
-    struct name *n = find_name(g, name_str, name_len);
-    if (!n && !(n = name_init(i, g, name_str, name_len))) {
+    struct name *n = find_name(g, name.ptr, name.len);
+    if (!n && !(n = name_init(i, g, name.ptr, name.len))) {
         goto fail0;
     }
     
@@ -423,12 +422,11 @@ static void depend_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleI
         ModuleLog(i, BLOG_ERROR, "wrong type");
         goto fail0;
     }
-    const char *name_str = NCDVal_StringData(name_arg);
-    size_t name_len = NCDVal_StringLength(name_arg);
+    MemRef name = NCDVal_StringMemRef(name_arg);
     
     // find name, create new if needed
-    struct name *n = find_name(g, name_str, name_len);
-    if (!n && !(n = name_init(i, g, name_str, name_len))) {
+    struct name *n = find_name(g, name.ptr, name.len);
+    if (!n && !(n = name_init(i, g, name.ptr, name.len))) {
         goto fail0;
     }
     

+ 7 - 8
ncd/modules/explode.c

@@ -96,21 +96,20 @@ static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new
         limit = (n <= SIZE_MAX ? n : SIZE_MAX);
     }
     
-    const char *del_data = NCDVal_StringData(delimiter_arg);
-    size_t del_len = NCDVal_StringLength(delimiter_arg);
+    MemRef del = NCDVal_StringMemRef(delimiter_arg);
     
-    if (del_len == 0) {
+    if (del.len == 0) {
         ModuleLog(i, BLOG_ERROR, "delimiter must be nonempty");
         goto fail0;
     }
     
-    size_t *table = BAllocArray(del_len, sizeof(table[0]));
+    size_t *table = BAllocArray(del.len, sizeof(table[0]));
     if (!table) {
         ModuleLog(i, BLOG_ERROR, "ExpArray_init failed");
         goto fail0;
     }
     
-    build_substring_backtrack_table(del_data, del_len, table);
+    build_substring_backtrack_table(del.ptr, del.len, table);
     
     if (!ExpArray_init(&o->arr, sizeof(struct substring), 8)) {
         ModuleLog(i, BLOG_ERROR, "ExpArray_init failed");
@@ -124,7 +123,7 @@ static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new
     while (1) {
         size_t start;
         int is_end = 0;
-        if (limit == 0 || !find_substring(data, len, del_data, del_len, table, &start)) {
+        if (limit == 0 || !find_substring(data, len, del.ptr, del.len, table, &start)) {
             start = len;
             is_end = 1;
         }
@@ -149,8 +148,8 @@ static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new
             break;
         }
         
-        data += start + del_len;
-        len -= start + del_len;
+        data += start + del.len;
+        len -= start + del.len;
         limit--;
     }
     

+ 8 - 12
ncd/modules/net_backend_badvpn.c

@@ -48,10 +48,8 @@
 struct instance {
     NCDModuleInst *i;
     NCDValNullTermString ifname_nts;
-    const char *user;
-    size_t user_len;
-    const char *exec;
-    size_t exec_len;
+    MemRef user;
+    MemRef exec;
     NCDValRef args;
     int dying;
     int started;
@@ -72,7 +70,7 @@ void try_process (struct instance *o)
     }
     
     // append exec
-    if (!CmdLine_AppendNoNull(&c, o->exec, o->exec_len)) {
+    if (!CmdLine_AppendNoNullMr(&c, o->exec)) {
         goto fail1;
     }
     
@@ -85,7 +83,7 @@ void try_process (struct instance *o)
     size_t count = NCDVal_ListCount(o->args);
     for (size_t j = 0; j < count; j++) {
         NCDValRef arg = NCDVal_ListGet(o->args, j);
-        if (!CmdLine_AppendNoNull(&c, NCDVal_StringData(arg), NCDVal_StringLength(arg))) {
+        if (!CmdLine_AppendNoNullMr(&c, NCDVal_StringMemRef(arg))) {
             goto fail1;
         }
     }
@@ -96,7 +94,7 @@ void try_process (struct instance *o)
     }
     
     // start process
-    if (!BProcess_Init(&o->process, o->i->params->iparams->manager, (BProcess_handler)process_handler, o, ((char **)c.arr.v)[0], (char **)c.arr.v, o->user)) {
+    if (!BProcess_Init(&o->process, o->i->params->iparams->manager, (BProcess_handler)process_handler, o, ((char **)c.arr.v)[0], (char **)c.arr.v, o->user.ptr)) {
         ModuleLog(o->i, BLOG_ERROR, "BProcess_Init failed");
         goto fail1;
     }
@@ -167,10 +165,8 @@ static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new
         goto fail0;
     }
     
-    o->user = NCDVal_StringData(user_arg);
-    o->user_len = NCDVal_StringLength(user_arg);
-    o->exec = NCDVal_StringData(exec_arg);
-    o->exec_len = NCDVal_StringLength(exec_arg);
+    o->user = NCDVal_StringMemRef(user_arg);
+    o->exec = NCDVal_StringMemRef(exec_arg);
     o->args = args_arg;
     
     // check arguments
@@ -190,7 +186,7 @@ static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new
     }
     
     // create TAP device
-    if (!NCDIfConfig_make_tuntap(o->ifname_nts.data, o->user, 0)) {
+    if (!NCDIfConfig_make_tuntap(o->ifname_nts.data, o->user.ptr, 0)) {
         ModuleLog(o->i, BLOG_ERROR, "failed to create TAP device");
         goto fail1;
     }

+ 3 - 5
ncd/modules/net_backend_waitdevice.c

@@ -50,8 +50,7 @@
 
 struct instance {
     NCDModuleInst *i;
-    const char *ifname;
-    size_t ifname_len;
+    MemRef ifname;
     NCDUdevClient client;
     regex_t reg;
     char *devpath;
@@ -80,7 +79,7 @@ static void client_handler (struct instance *o, char *devpath, int have_map, BSt
         const char *ifindex_str = BStringMap_Get(cache_map, "IFINDEX");
         
         uintmax_t ifindex;
-        if (!(!match_res && interface && strlen(interface) == o->ifname_len && !memcmp(interface, o->ifname, o->ifname_len) && ifindex_str && parse_unsigned_integer(ifindex_str, &ifindex))) {
+        if (!(!match_res && interface && strlen(interface) == o->ifname.len && !memcmp(interface, o->ifname.ptr, o->ifname.len) && ifindex_str && parse_unsigned_integer(ifindex_str, &ifindex))) {
             goto out;
         }
         
@@ -130,8 +129,7 @@ static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
         goto fail0;
     }
-    o->ifname = NCDVal_StringData(arg);
-    o->ifname_len = NCDVal_StringLength(arg);
+    o->ifname = NCDVal_StringMemRef(arg);
     
     // init client
     NCDUdevClient_Init(&o->client, o->i->params->iparams->umanager, o, (NCDUdevClient_handler)client_handler);

+ 11 - 17
ncd/modules/net_backend_wpa_supplicant.c

@@ -68,12 +68,9 @@
 
 struct instance {
     NCDModuleInst *i;
-    const char *ifname;
-    size_t ifname_len;
-    const char *conf;
-    size_t conf_len;
-    const char *exec;
-    size_t exec_len;
+    MemRef ifname;
+    MemRef conf;
+    MemRef exec;
     NCDValRef args;
     int dying;
     int up;
@@ -221,7 +218,7 @@ int build_cmdline (struct instance *o, CmdLine *c)
     }
     
     // append stdbuf part
-    int res = build_stdbuf_cmdline(c, stdbuf_exec, o->exec, o->exec_len);
+    int res = build_stdbuf_cmdline(c, stdbuf_exec, o->exec.ptr, o->exec.len);
     free(stdbuf_exec);
     if (!res) {
         goto fail1;
@@ -238,18 +235,18 @@ int build_cmdline (struct instance *o, CmdLine *c)
         }
         
         // append argument
-        if (!CmdLine_AppendNoNull(c, NCDVal_StringData(arg), NCDVal_StringLength(arg))) {
+        if (!CmdLine_AppendNoNullMr(c, NCDVal_StringMemRef(arg))) {
             goto fail1;
         }
     }
     
     // append interface name
-    if (!CmdLine_Append(c, "-i") || !CmdLine_AppendNoNull(c, o->ifname, o->ifname_len)) {
+    if (!CmdLine_Append(c, "-i") || !CmdLine_AppendNoNullMr(c, o->ifname)) {
         goto fail1;
     }
     
     // append config file
-    if (!CmdLine_Append(c, "-c") || !CmdLine_AppendNoNull(c, o->conf, o->conf_len)) {
+    if (!CmdLine_Append(c, "-c") || !CmdLine_AppendNoNullMr(c, o->conf)) {
         goto fail1;
     }
     
@@ -351,7 +348,7 @@ void process_pipe_handler_send (struct instance *o, uint8_t *data, int data_len)
     // prefix, so don't fail if there isn't one.
     size_t l1;
     size_t l2;
-    if (o->ifname_len > 0 && (l1 = data_begins_with_bin((char *)data, data_len, o->ifname, o->ifname_len)) && (l2 = data_begins_with((char *)data + l1, data_len - l1, ": "))) {
+    if (o->ifname.len > 0 && (l1 = data_begins_with_bin((char *)data, data_len, o->ifname.ptr, o->ifname.len)) && (l2 = data_begins_with((char *)data + l1, data_len - l1, ": "))) {
         data += l1 + l2;
         data_len -= l1 + l2;
     }
@@ -425,12 +422,9 @@ static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new
         goto fail0;
     }
     
-    o->ifname = NCDVal_StringData(ifname_arg);
-    o->ifname_len = NCDVal_StringLength(ifname_arg);
-    o->conf = NCDVal_StringData(conf_arg);
-    o->conf_len = NCDVal_StringLength(conf_arg);
-    o->exec = NCDVal_StringData(exec_arg);
-    o->exec_len = NCDVal_StringLength(exec_arg);
+    o->ifname = NCDVal_StringMemRef(ifname_arg);
+    o->conf = NCDVal_StringMemRef(conf_arg);
+    o->exec = NCDVal_StringMemRef(exec_arg);
     o->args = args_arg;
     
     // set not dying

+ 16 - 24
ncd/modules/net_iptables.c

@@ -170,10 +170,8 @@ static int build_append_or_insert_cmdline (NCDModuleInst *i, NCDValRef args, con
         ModuleLog(i, BLOG_ERROR, "wrong type");
         goto fail0;
     }
-    const char *table = NCDVal_StringData(table_arg);
-    size_t table_len = NCDVal_StringLength(table_arg);
-    const char *chain = NCDVal_StringData(chain_arg);
-    size_t chain_len = NCDVal_StringLength(chain_arg);
+    MemRef table = NCDVal_StringMemRef(table_arg);
+    MemRef chain = NCDVal_StringMemRef(chain_arg);
     
     // find program
     if (!(*exec = badvpn_find_program(prog))) {
@@ -188,7 +186,7 @@ static int build_append_or_insert_cmdline (NCDModuleInst *i, NCDValRef args, con
     }
     
     // add header
-    if (!CmdLine_Append(cl, *exec) || !CmdLine_Append(cl, "-t") || !CmdLine_AppendNoNull(cl, table, table_len) || !CmdLine_Append(cl, (remove ? "-D" : type)) || !CmdLine_AppendNoNull(cl, chain, chain_len)) {
+    if (!CmdLine_Append(cl, *exec) || !CmdLine_Append(cl, "-t") || !CmdLine_AppendNoNullMr(cl, table) || !CmdLine_Append(cl, (remove ? "-D" : type)) || !CmdLine_AppendNoNullMr(cl, chain)) {
         ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
         goto fail2;
     }
@@ -203,8 +201,8 @@ static int build_append_or_insert_cmdline (NCDModuleInst *i, NCDValRef args, con
             goto fail2;
         }
         
-        if (!CmdLine_AppendNoNull(cl, NCDVal_StringData(arg), NCDVal_StringLength(arg))) {
-            ModuleLog(i, BLOG_ERROR, "CmdLine_AppendNoNull failed");
+        if (!CmdLine_AppendNoNullMr(cl, NCDVal_StringMemRef(arg))) {
+            ModuleLog(i, BLOG_ERROR, "CmdLine_AppendNoNullMr failed");
             goto fail2;
         }
     }
@@ -252,14 +250,10 @@ static int build_policy_cmdline (NCDModuleInst *i, NCDValRef args, const char *p
         ModuleLog(i, BLOG_ERROR, "wrong type");
         goto fail0;
     }
-    const char *table = NCDVal_StringData(table_arg);
-    size_t table_len = NCDVal_StringLength(table_arg);
-    const char *chain = NCDVal_StringData(chain_arg);
-    size_t chain_len = NCDVal_StringLength(chain_arg);
-    const char *target = NCDVal_StringData(target_arg);
-    size_t target_len = NCDVal_StringLength(target_arg);
-    const char *revert_target = NCDVal_StringData(revert_target_arg);
-    size_t revert_target_len = NCDVal_StringLength(revert_target_arg);
+    MemRef table = NCDVal_StringMemRef(table_arg);
+    MemRef chain = NCDVal_StringMemRef(chain_arg);
+    MemRef target = NCDVal_StringMemRef(target_arg);
+    MemRef revert_target = NCDVal_StringMemRef(revert_target_arg);
     
     // find program
     if (!(*exec = badvpn_find_program(prog))) {
@@ -274,9 +268,9 @@ static int build_policy_cmdline (NCDModuleInst *i, NCDValRef args, const char *p
     }
     
     // add arguments
-    if (!CmdLine_Append(cl, *exec) || !CmdLine_Append(cl, "-t") || !CmdLine_AppendNoNull(cl, table, table_len) ||
-        !CmdLine_Append(cl, "-P") || !CmdLine_AppendNoNull(cl, chain, chain_len) ||
-        !CmdLine_AppendNoNull(cl, (remove ? revert_target : target), (remove ? revert_target_len : target_len))
+    if (!CmdLine_Append(cl, *exec) || !CmdLine_Append(cl, "-t") || !CmdLine_AppendNoNullMr(cl, table) ||
+        !CmdLine_Append(cl, "-P") || !CmdLine_AppendNoNullMr(cl, chain) ||
+        !CmdLine_AppendNoNullMr(cl, (remove ? revert_target : target))
     ) {
         ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
         goto fail2;
@@ -311,10 +305,8 @@ static int build_newchain_cmdline (NCDModuleInst *i, NCDValRef args, const char
         ModuleLog(i, BLOG_ERROR, "wrong type");
         goto fail0;
     }
-    const char *table = (NCDVal_IsInvalid(table_arg) ? "filter" : NCDVal_StringData(table_arg));
-    size_t table_len = (NCDVal_IsInvalid(table_arg) ? 6 : NCDVal_StringLength(table_arg));
-    const char *chain = NCDVal_StringData(chain_arg);
-    size_t chain_len = NCDVal_StringLength(chain_arg);
+    MemRef table = NCDVal_IsInvalid(table_arg) ? MemRef_MakeCstr("filter") : NCDVal_StringMemRef(table_arg);
+    MemRef chain = NCDVal_StringMemRef(chain_arg);
     
     // find program
     if (!(*exec = badvpn_find_program(prog))) {
@@ -330,9 +322,9 @@ static int build_newchain_cmdline (NCDModuleInst *i, NCDValRef args, const char
     
     // add arguments
     if (!CmdLine_AppendMulti(cl, 2, *exec, "-t") ||
-        !CmdLine_AppendNoNull(cl, table, table_len) ||
+        !CmdLine_AppendNoNullMr(cl, table) ||
         !CmdLine_Append(cl, (remove ? "-X" : "-N")) ||
-        !CmdLine_AppendNoNull(cl, chain, chain_len)
+        !CmdLine_AppendNoNullMr(cl, chain)
     ) {
         ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
         goto fail2;

+ 12 - 16
ncd/modules/regex_match.c

@@ -83,8 +83,7 @@
 
 struct instance {
     NCDModuleInst *i;
-    const char *input;
-    size_t input_len;
+    MemRef input;
     int succeeded;
     int num_matches;
     regmatch_t matches[MAX_MATCHES];
@@ -92,8 +91,7 @@ struct instance {
 
 struct replace_instance {
     NCDModuleInst *i;
-    char *output;
-    size_t output_len;
+    MemRef output;
 };
 
 static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
@@ -112,11 +110,10 @@ static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
         goto fail0;
     }
-    o->input = NCDVal_StringData(input_arg);
-    o->input_len = NCDVal_StringLength(input_arg);
+    o->input = NCDVal_StringMemRef(input_arg);
     
     // make sure we don't overflow regoff_t
-    if (o->input_len > INT_MAX) {
+    if (o->input.len > INT_MAX) {
         ModuleLog(o->i, BLOG_ERROR, "input string too long");
         goto fail0;
     }
@@ -139,8 +136,8 @@ static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new
     
     // execute match
     o->matches[0].rm_so = 0;
-    o->matches[0].rm_eo = o->input_len;
-    o->succeeded = (regexec(&preg, o->input, MAX_MATCHES, o->matches, REG_STARTEND) == 0);
+    o->matches[0].rm_eo = o->input.len;
+    o->succeeded = (regexec(&preg, o->input.ptr, MAX_MATCHES, o->matches, REG_STARTEND) == 0);
     
     // free regex
     regfree(&preg);
@@ -168,13 +165,13 @@ static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *o
         if (o->succeeded && n < MAX_MATCHES && o->matches[n].rm_so >= 0) {
             regmatch_t *m = &o->matches[n];
             
-            ASSERT(m->rm_so <= o->input_len)
+            ASSERT(m->rm_so <= o->input.len)
             ASSERT(m->rm_eo >= m->rm_so)
-            ASSERT(m->rm_eo <= o->input_len)
+            ASSERT(m->rm_eo <= o->input.len)
             
             size_t len = m->rm_eo - m->rm_so;
             
-            *out = NCDVal_NewStringBin(mem, (uint8_t *)o->input + m->rm_so, len);
+            *out = NCDVal_NewStringBinMr(mem, MemRef_Sub(o->input, m->rm_so, len));
             return 1;
         }
     }
@@ -297,8 +294,7 @@ static void replace_func_new (void *vo, NCDModuleInst *i, const struct NCDModule
     }
     
     // set output
-    o->output = ExpString_Get(&out);
-    o->output_len = ExpString_Length(&out);
+    o->output = MemRef_Make(ExpString_Get(&out), ExpString_Length(&out));
     
     // free compiled regex's
     while (num_done_regex-- > 0) {
@@ -328,7 +324,7 @@ static void replace_func_die (void *vo)
     struct replace_instance *o = vo;
     
     // free output
-    BFree(o->output);
+    BFree((char *)o->output.ptr);
     
     NCDModuleInst_Backend_Dead(o->i);
 }
@@ -338,7 +334,7 @@ static int replace_func_getvar (void *vo, const char *name, NCDValMem *mem, NCDV
     struct replace_instance *o = vo;
     
     if (!strcmp(name, "")) {
-        *out = NCDVal_NewStringBin(mem, (uint8_t *)o->output, o->output_len);
+        *out = NCDVal_NewStringBinMr(mem, o->output);
         return 1;
     }
     

+ 1 - 2
ncd/modules/run.c

@@ -114,8 +114,7 @@ static int build_cmdline (NCDModuleInst *i, NCDValRef args, int remove, char **e
             goto fail2;
         }
         
-        MemRef arg_mr = NCDVal_StringMemRef(arg);
-        if (!CmdLine_AppendNoNull(cl, arg_mr.ptr, arg_mr.len)) {
+        if (!CmdLine_AppendNoNullMr(cl, NCDVal_StringMemRef(arg))) {
             ModuleLog(i, BLOG_ERROR, "CmdLine_AppendNoNull failed");
             goto fail2;
         }

+ 1 - 2
ncd/modules/runonce.c

@@ -124,8 +124,7 @@ static int build_cmdline (NCDModuleInst *i, NCDValRef cmd_arg, char **exec, CmdL
             goto fail2;
         }
         
-        MemRef arg_mr = NCDVal_StringMemRef(arg);
-        if (!CmdLine_AppendNoNull(cl, arg_mr.ptr, arg_mr.len)) {
+        if (!CmdLine_AppendNoNullMr(cl, NCDVal_StringMemRef(arg))) {
             ModuleLog(i, BLOG_ERROR, "CmdLine_AppendNoNull failed");
             goto fail2;
         }

+ 9 - 12
ncd/modules/substr.c

@@ -48,19 +48,17 @@
 
 struct substr_instance {
     NCDModuleInst *i;
-    const char *data;
-    size_t length;
+    MemRef data;
     int is_external;
     BRefTarget *external_ref_target;
 };
 
-static void substr_func_new_common (void *vo, NCDModuleInst *i, const char *data, size_t length, int is_external, BRefTarget *external_ref_target)
+static void substr_func_new_common (void *vo, NCDModuleInst *i, MemRef data, int is_external, BRefTarget *external_ref_target)
 {
     struct substr_instance *o = vo;
     o->i = i;
     
     o->data = data;
-    o->length = length;
     o->is_external = is_external;
     o->external_ref_target = external_ref_target;
     
@@ -73,9 +71,9 @@ static int substr_func_getvar (void *vo, NCD_string_id_t name, NCDValMem *mem, N
     
     if (name == NCD_STRING_EMPTY) {
         if (o->is_external) {
-            *out = NCDVal_NewExternalString(mem, o->data, o->length, o->external_ref_target);
+            *out = NCDVal_NewExternalString(mem, o->data.ptr, o->data.len, o->external_ref_target);
         } else {
-            *out = NCDVal_NewStringBin(mem, (const uint8_t *)o->data, o->length);
+            *out = NCDVal_NewStringBinMr(mem, o->data);
         }
         return 1;
     }
@@ -113,16 +111,15 @@ static void func_new_substr (void *vo, NCDModuleInst *i, const struct NCDModuleI
         }
     }
     
-    const char *str_data = NCDVal_StringData(str_arg);
-    size_t str_length = NCDVal_StringLength(str_arg);
+    MemRef str = NCDVal_StringMemRef(str_arg);
     
-    if (start > str_length) {
+    if (start > str.len) {
         ModuleLog(i, BLOG_ERROR, "start is beyond the end of the string");
         goto fail0;
     }
     
-    const char *sub_data = str_data + start;
-    size_t sub_length = str_length - start;
+    const char *sub_data = str.ptr + start;
+    size_t sub_length = str.len - start;
     if (sub_length > max) {
         sub_length = max;
     }
@@ -138,7 +135,7 @@ static void func_new_substr (void *vo, NCDModuleInst *i, const struct NCDModuleI
         is_external = 1;
     }
     
-    substr_func_new_common(vo, i, sub_data, sub_length, is_external, external_ref_target);
+    substr_func_new_common(vo, i, MemRef_Make(sub_data, sub_length), is_external, external_ref_target);
     return;
     
 fail0:

+ 6 - 8
ncd/modules/sys_watch_input.c

@@ -69,8 +69,7 @@ struct device {
 
 struct instance {
     NCDModuleInst *i;
-    const char *devnode_type;
-    size_t devnode_type_len;
+    MemRef devnode_type;
     NCDUdevClient client;
     LinkedList1 devices_list;
     event_template templ;
@@ -158,7 +157,7 @@ fail1:
     return 0;
 }
 
-static int devname_is_type (const char *devname, const char *devname_type, size_t devname_type_len)
+static int devname_is_type (const char *devname, MemRef devname_type)
 {
     // skip digits at the end
     size_t i;
@@ -169,8 +168,8 @@ static int devname_is_type (const char *devname, const char *devname_type, size_
     }
     
     // check if devname_type precedes skipped digits
-    for (size_t j = devname_type_len; j > 0; j--) {
-        if (!(i > 0 && devname[i - 1] == devname_type[j - 1])) {
+    for (size_t j = devname_type.len; j > 0; j--) {
+        if (!(i > 0 && devname[i - 1] == devname_type.ptr[j - 1])) {
             return 0;
         }
         i--;
@@ -290,7 +289,7 @@ static void client_handler (struct instance *o, char *devpath, int have_map, BSt
     const char *subsystem = BStringMap_Get(cache_map, "SUBSYSTEM");
     const char *devname = BStringMap_Get(cache_map, "DEVNAME");
     
-    if (!(subsystem && !strcmp(subsystem, "input") && devname && devname_is_type(devname, o->devnode_type, o->devnode_type_len))) {
+    if (!(subsystem && !strcmp(subsystem, "input") && devname && devname_is_type(devname, o->devnode_type))) {
         if (ex_device) {
             remove_device(o, ex_device);
         }
@@ -357,8 +356,7 @@ static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new
         ModuleLog(o->i, BLOG_ERROR, "wrong type");
         goto fail0;
     }
-    o->devnode_type = NCDVal_StringData(devnode_type_arg);
-    o->devnode_type_len = NCDVal_StringLength(devnode_type_arg);
+    o->devnode_type = NCDVal_StringMemRef(devnode_type_arg);
     
     // init client
     NCDUdevClient_Init(&o->client, o->i->params->iparams->umanager, o, (NCDUdevClient_handler)client_handler);