tdb2: tdb_mkdata
authorRusty Russell <rusty@rustcorp.com.au>
Mon, 21 Mar 2011 11:42:17 +0000 (22:12 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Mon, 21 Mar 2011 11:42:17 +0000 (22:12 +1030)
It's common to want to create a struct tdb_data which contains a const
pointer, so add a helper to do so, and use it to clean up other -Wcast-qual
warnings.

20 files changed:
ccan/tdb2/check.c
ccan/tdb2/private.h
ccan/tdb2/tdb.c
ccan/tdb2/tdb2.h
ccan/tdb2/test/external-agent.c
ccan/tdb2/test/run-03-coalesce.c
ccan/tdb2/test/run-04-basichash.c
ccan/tdb2/test/run-10-simple-store.c
ccan/tdb2/test/run-11-simple-fetch.c
ccan/tdb2/test/run-13-delete.c
ccan/tdb2/test/run-15-append.c
ccan/tdb2/test/run-20-growhash.c
ccan/tdb2/test/run-50-multiple-freelists.c
ccan/tdb2/test/run-55-transaction.c
ccan/tdb2/test/run-56-open-during-transaction.c
ccan/tdb2/test/run-57-die-during-transaction.c
ccan/tdb2/test/run-81-seqnum.c
ccan/tdb2/test/run-record-expand.c
ccan/tdb2/test/run-remap-in-read_traverse.c
ccan/tdb2/test/run-simple-delete.c

index b44ae42a680c8923fe24826da080f374e05bcb45..577086689f373f75c1774d9be2fd1175180af950 100644 (file)
@@ -402,22 +402,26 @@ static enum TDB_ERROR check_hash_tree(struct tdb_context *tdb,
                check:
                        if (check) {
                                TDB_DATA key, data;
-                               key.dsize = rec_key_length(&rec);
-                               data.dsize = rec_data_length(&rec);
-                               key.dptr = (void *)tdb_access_read(tdb,
-                                                  off + sizeof(rec),
-                                                  key.dsize + data.dsize,
-                                                  false);
-                               if (TDB_PTR_IS_ERR(key.dptr)) {
-                                       ecode = TDB_PTR_ERR(key.dptr);
+                               const unsigned char *kptr;
+
+                               kptr = tdb_access_read(tdb,
+                                                      off + sizeof(rec),
+                                                      rec_key_length(&rec)
+                                                      + rec_data_length(&rec),
+                                                      false);
+                               if (TDB_PTR_IS_ERR(kptr)) {
+                                       ecode = TDB_PTR_ERR(kptr);
                                        goto fail;
                                }
-                               data.dptr = key.dptr + key.dsize;
+
+                               key = tdb_mkdata(kptr, rec_key_length(&rec));
+                               data = tdb_mkdata(kptr + key.dsize,
+                                                 rec_data_length(&rec));
                                ecode = check(key, data, private_data);
+                               tdb_access_release(tdb, kptr);
                                if (ecode != TDB_SUCCESS) {
                                        goto fail;
                                }
-                               tdb_access_release(tdb, key.dptr);
                        }
                }
        }
index 1e31ec159fbe457cfd570cdf60d6461d62e52dfa..90fe7376b3558665cac13a30d4a864697d3388e6 100644 (file)
@@ -35,7 +35,6 @@
 #include <ccan/tdb2/tdb2.h>
 #include <ccan/likely/likely.h>
 #include <ccan/compiler/compiler.h>
-#include <ccan/cast/cast.h>
 #if HAVE_BYTESWAP_H
 #include <byteswap.h>
 #endif
index 254aac5f25a94e1daae9692ea03a9c42b1a6ddce..09333a26801380fb232a2fc444eb11473de8805e 100644 (file)
@@ -422,7 +422,6 @@ enum TDB_ERROR tdb_parse_record_(struct tdb_context *tdb,
        tdb_off_t off;
        struct tdb_used_record rec;
        struct hash_info h;
-       TDB_DATA data;
        enum TDB_ERROR ecode;
 
        off = find_and_lock(tdb, key, F_RDLCK, &h, &rec, NULL);
@@ -433,16 +432,16 @@ enum TDB_ERROR tdb_parse_record_(struct tdb_context *tdb,
        if (!off) {
                ecode = TDB_ERR_NOEXIST;
        } else {
-               data.dsize = rec_data_length(&rec);
-               data.dptr = (void *)tdb_access_read(tdb,
-                                                   off + sizeof(rec)
-                                                   + key.dsize,
-                                                   data.dsize, false);
-               if (TDB_PTR_IS_ERR(data.dptr)) {
-                       ecode = TDB_PTR_ERR(data.dptr);
+               const void *dptr;
+               dptr = tdb_access_read(tdb, off + sizeof(rec) + key.dsize,
+                                      rec_data_length(&rec), false);
+               if (TDB_PTR_IS_ERR(dptr)) {
+                       ecode = TDB_PTR_ERR(dptr);
                } else {
+                       TDB_DATA data = tdb_mkdata(dptr, rec_data_length(&rec));
+
                        ecode = parse(key, data, p);
-                       tdb_access_release(tdb, data.dptr);
+                       tdb_access_release(tdb, dptr);
                }
        }
 
index 4f5be81950d069fa9dbe5bb8492f0086ed50f708..fa5787c3779acd424e7f3cbde6a4c5e6746eb45d 100644 (file)
@@ -45,6 +45,7 @@ extern "C" {
 #endif
 #include <ccan/compiler/compiler.h>
 #include <ccan/typesafe_cb/typesafe_cb.h>
+#include <ccan/cast/cast.h>
 
 union tdb_attribute;
 struct tdb_context;
@@ -221,6 +222,24 @@ static inline bool tdb_deq(struct tdb_data a, struct tdb_data b)
        return a.dsize == b.dsize && memcmp(a.dptr, b.dptr, a.dsize) == 0;
 }
 
+/**
+ * tdb_mkdata - make a struct tdb_data from const data
+ * @p: the constant pointer
+ * @len: the length
+ *
+ * As the dptr member of struct tdb_data is not constant, you need to
+ * cast it.  This function keeps thost casts in one place, as well as
+ * suppressing the warning some compilers give when casting away a
+ * qualifier (eg. gcc with -Wcast-qual)
+ */
+static inline struct tdb_data tdb_mkdata(const void *p, size_t len)
+{
+       struct tdb_data d;
+       d.dptr = cast_const(void *, p);
+       d.dsize = len;
+       return d;
+}
+
 /**
  * tdb_transaction_start - start a transaction
  * @tdb: the tdb context returned from tdb_open()
index 79c299a088047ccbbc7d495548d89e4c34fade89..df51a0c5197baf2a311950c757a85f6a09bec556 100644 (file)
@@ -31,8 +31,7 @@ static enum agent_return do_operation(enum operation op, const char *name)
 
        diag("external: %s", operation_name(op));
 
-       k.dptr = (void *)name;
-       k.dsize = strlen(name);
+       k = tdb_mkdata(name, strlen(name));
 
        locking_would_block = 0;
        switch (op) {
index c3583d850c369b74af84817d84f623729a851386..0b544f8c1f758f286b0e7a5fd90862e7e47d94a8 100644 (file)
@@ -34,10 +34,8 @@ int main(int argc, char *argv[])
        /* FIXME: Test TDB_CONVERT */
 
        plan_tests(38);
-       data.dptr = (void *)"world";
-       data.dsize = 5;
-       key.dptr = (void *)"hello";
-       key.dsize = 5;
+       data = tdb_mkdata("world", 5);
+       key = tdb_mkdata("hello", 5);
 
        /* No coalescing can be done due to EOF */
        layout = new_tdb_layout("run-03-coalesce.tdb");
index b3a5d1c5bd0b8d2b1d1ffe6d03cee05e429ec110..0f962f82b63bfcb59a2f5b5998bf25b6dccd77af 100644 (file)
@@ -12,7 +12,7 @@
 /* We rig the hash so adjacent-numbered records always clash. */
 static uint64_t clash(const void *key, size_t len, uint64_t seed, void *priv)
 {
-       return ((uint64_t)*(unsigned int *)key)
+       return ((uint64_t)*(const unsigned int *)key)
                << (64 - TDB_TOPLEVEL_HASH_BITS - 1);
 }
 
index daf975556493a406965a5bd3395ae06e873fd4ac..3aeb3816fb09b14e3de5e7e9d1a3f20eea897b35 100644 (file)
@@ -30,8 +30,8 @@ int main(int argc, char *argv[])
        int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
                        TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT, 
                        TDB_NOMMAP|TDB_CONVERT };
-       struct tdb_data key = { (unsigned char *)"key", 3 };
-       struct tdb_data data = { (unsigned char *)"data", 4 };
+       struct tdb_data key = tdb_mkdata("key", 3);
+       struct tdb_data data = tdb_mkdata("data", 4);
 
        failtest_init(argc, argv);
        failtest_hook = suppress_failure;
index ad1933cd3b0566f3e005b6a428efc85374eb04e2..278d726ad20f3e66d8da80ad8f3244ca045f91b1 100644 (file)
@@ -30,8 +30,8 @@ int main(int argc, char *argv[])
        int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
                        TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT, 
                        TDB_NOMMAP|TDB_CONVERT };
-       struct tdb_data key = { (unsigned char *)"key", 3 };
-       struct tdb_data data = { (unsigned char *)"data", 4 };
+       struct tdb_data key = tdb_mkdata("key", 3);
+       struct tdb_data data = tdb_mkdata("data", 4);
 
        failtest_init(argc, argv);
        failtest_hook = suppress_failure;
index aba6fe29448c3fb3fb53d7acf3bad8ef40fc7ca4..c70fec84828059edbb4fb26e0026ef3233857e6a 100644 (file)
@@ -12,7 +12,7 @@
 /* We rig the hash so adjacent-numbered records always clash. */
 static uint64_t clash(const void *key, size_t len, uint64_t seed, void *priv)
 {
-       return ((uint64_t)*(unsigned int *)key)
+       return ((uint64_t)*(const unsigned int *)key)
                << (64 - TDB_TOPLEVEL_HASH_BITS - 1);
 }
 
index e11289e99b8493723f0c4a641b34bd15a2fdd829..70c0d1745a9bdf73f8e277e8b860eec00ab364f3 100644 (file)
@@ -35,7 +35,7 @@ int main(int argc, char *argv[])
        int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
                        TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT, 
                        TDB_NOMMAP|TDB_CONVERT };
-       struct tdb_data key = { (unsigned char *)"key", 3 };
+       struct tdb_data key = tdb_mkdata("key", 3);
        struct tdb_data data;
 
        buffer = malloc(MAX_SIZE);
index b504e4907be88a85b07ba83b66cc5d92a5fda979..1b6e7450e240ec8dacb462c0423bc7d249c33a33 100644 (file)
@@ -11,7 +11,7 @@
 
 static uint64_t myhash(const void *key, size_t len, uint64_t seed, void *priv)
 {
-       return *(uint64_t *)key;
+       return *(const uint64_t *)key;
 }
 
 static void add_bits(uint64_t *val, unsigned new, unsigned new_bits,
index 8220b911bf22d95f026745bf3ff7347b18541211..7a48c3e0eee7726e9fd209587d1f0c50fed59002 100644 (file)
@@ -18,10 +18,8 @@ int main(int argc, char *argv[])
        TDB_DATA key, data;
 
        plan_tests(11);
-       key.dptr = (unsigned char *)"Hello";
-       data.dptr = (unsigned char *)"world";
-       data.dsize = 5;
-       key.dsize = 5;
+       key = tdb_mkdata("Hello", 5);
+       data = tdb_mkdata("world", 5);
 
        /* Create a TDB with three free tables. */
        layout = new_tdb_layout(NULL);
index d7fd951e57046aa5a17fdbc466bebe70c49d0865..1650e40e1f3f2b134fa2293f01da50feda6138c5 100644 (file)
@@ -16,7 +16,7 @@ int main(int argc, char *argv[])
        unsigned char *buffer;
        int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
                        TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT };
-       struct tdb_data key = { (unsigned char *)"key", 3 };
+       struct tdb_data key = tdb_mkdata("key", 3);
        struct tdb_data data;
 
        buffer = malloc(1000);
index b242eab772ed4cbf73a3dcd8df5650f329aeb49d..96107d637e4472b7026b587f26c031191b1c9631 100644 (file)
@@ -160,10 +160,8 @@ int main(int argc, char *argv[])
 
                opened = true;
                ok1(tdb_transaction_start(tdb) == 0);
-               key.dsize = strlen("hi");
-               key.dptr = (void *)"hi";
-               data.dptr = (void *)"world";
-               data.dsize = strlen("world");
+               key = tdb_mkdata("hi", strlen("hi"));
+               data = tdb_mkdata("world", strlen("world"));
 
                ok1(tdb_store(tdb, key, data, TDB_INSERT) == 0);
                ok1(tdb_transaction_commit(tdb) == 0);
index 54dcdca66b5e771899acb1ae33a6f403e543afe8..84f01eb21a8dd009d5a6d738485f96e790822a55 100644 (file)
@@ -208,8 +208,7 @@ reset:
        }
 
        /* Put key for agent to fetch. */
-       key.dsize = strlen(KEY_STRING);
-       key.dptr = (void *)KEY_STRING;
+       key = tdb_mkdata(KEY_STRING, strlen(KEY_STRING));
        if (tdb_store(tdb, key, key, TDB_INSERT) != 0)
                return false;
 
index db42b976ccba81098cd579b1e17138c41a14fe8b..f7308e2dbd50128325ba675186e3aea983294707 100644 (file)
@@ -15,8 +15,8 @@ int main(int argc, char *argv[])
        unsigned int i;
        struct tdb_context *tdb;
        struct tdb_data d = { NULL, 0 }; /* Bogus GCC warning */
-       struct tdb_data key = { (unsigned char *)"key", 3 };
-       struct tdb_data data = { (unsigned char *)"data", 4 };
+       struct tdb_data key = tdb_mkdata("key", 3);
+       struct tdb_data data = tdb_mkdata("data", 4);
        int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
                        TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT,
                        TDB_NOMMAP|TDB_CONVERT };
index 63bbf18d4fc0cae98769dff1c9851aac7b099214..155484cee79d1e3bff9915fdab05ff9fae3cf224 100644 (file)
@@ -19,7 +19,7 @@ int main(int argc, char *argv[])
        int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
                        TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT, 
                        TDB_NOMMAP|TDB_CONVERT };
-       struct tdb_data key = { (unsigned char *)"key", 3 };
+       struct tdb_data key = tdb_mkdata("key", 3);
        struct tdb_data data;
 
        data.dptr = malloc(MAX_SIZE);
index 85df6612a9d5277158de2ac7b91fc927ca265c01..d784ca34074ec7f8b8fe0cee6c957e60f42d578b 100644 (file)
@@ -40,7 +40,7 @@ int main(int argc, char *argv[])
        unsigned int i;
        struct agent *agent;
        struct tdb_context *tdb;
-       struct tdb_data d = { (unsigned char *)"hello", 5 };
+       struct tdb_data d = tdb_mkdata("hello", 5);
        const char filename[] = "run-remap-in-read_traverse.tdb";
 
        plan_tests(4);
index fef79a9b23a8f8825aad6230e7b3db8c48afd38c..3d6a6f2ad8659c5a169639fdf6533a35c30fca53 100644 (file)
@@ -16,8 +16,8 @@ int main(int argc, char *argv[])
        int flags[] = { TDB_INTERNAL, TDB_DEFAULT, TDB_NOMMAP,
                        TDB_INTERNAL|TDB_CONVERT, TDB_CONVERT, 
                        TDB_NOMMAP|TDB_CONVERT };
-       struct tdb_data key = { (unsigned char *)"key", 3 };
-       struct tdb_data data = { (unsigned char *)"data", 4 };
+       struct tdb_data key = tdb_mkdata("key", 3);
+       struct tdb_data data = tdb_mkdata("data", 4);
 
        plan_tests(sizeof(flags) / sizeof(flags[0]) * 7 + 1);
        for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {