tdb2: typesafe traverse function
authorRusty Russell <rusty@rustcorp.com.au>
Thu, 17 Mar 2011 11:42:21 +0000 (22:12 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Thu, 17 Mar 2011 11:42:21 +0000 (22:12 +1030)
This make sure that the data being handed to the function in tdb_traverse
is the correct type.

ccan/tdb2/_info
ccan/tdb2/tdb2.h
ccan/tdb2/test/run-traverse.c
ccan/tdb2/traverse.c

index 67ad3f6c340189d18074ca93fd665fe82743a772..0e894e5b3ad0bba51ef08979bfd4d41e982bc7eb 100644 (file)
@@ -85,6 +85,7 @@ int main(int argc, char *argv[])
                printf("ccan/ilog\n");
                printf("ccan/failtest\n");
                printf("ccan/tally\n");
+               printf("ccan/typesafe_cb\n");
                return 0;
        }
 
index c0ab8eb07529d521b359874d8cdcf541783714c3..52c26959d02cb412496b7f00a5e8eff542ed2667 100644 (file)
@@ -40,6 +40,7 @@ extern "C" {
 #include <stdint.h>
 #endif
 #include <ccan/compiler/compiler.h>
+#include <ccan/typesafe_cb/typesafe_cb.h>
 
 union tdb_attribute;
 struct tdb_context;
@@ -249,9 +250,6 @@ enum TDB_ERROR tdb_transaction_commit(struct tdb_context *tdb);
  */
 enum TDB_ERROR tdb_transaction_prepare_commit(struct tdb_context *tdb);
 
-/* FIXME: Make typesafe */
-typedef int (*tdb_traverse_func)(struct tdb_context *, TDB_DATA, TDB_DATA, void *);
-
 /**
  * tdb_traverse - traverse a TDB
  * @tdb: the tdb context returned from tdb_open()
@@ -269,7 +267,14 @@ typedef int (*tdb_traverse_func)(struct tdb_context *, TDB_DATA, TDB_DATA, void
  * On success, returns the number of keys iterated.  On error returns
  * a negative enum TDB_ERROR value.
  */
-int64_t tdb_traverse(struct tdb_context *tdb, tdb_traverse_func fn, void *p);
+#define tdb_traverse(tdb, fn, p)                                       \
+       tdb_traverse_(tdb, typesafe_cb_preargs(int, (fn), (p),          \
+                                              struct tdb_context *,    \
+                                              TDB_DATA, TDB_DATA), (p))
+
+int64_t tdb_traverse_(struct tdb_context *tdb,
+                     int (*fn)(struct tdb_context *,
+                               TDB_DATA, TDB_DATA, void *), void *p);
 
 /**
  * tdb_firstkey - get the "first" key in a TDB
index 0a6310027beaf3c4c6a3770e5b0b57eae1587eb0..13c3d4573d8ab0358d2d2d0f9685271d4c37cf61 100644 (file)
@@ -38,9 +38,9 @@ struct trav_data {
        enum TDB_ERROR delete_error;
 };
 
-static int trav(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, void *p)
+static int trav(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
+               struct trav_data *td)
 {
-       struct trav_data *td = p;
        int val;
 
        td->calls++;
@@ -75,9 +75,8 @@ struct trav_grow_data {
 };
 
 static int trav_grow(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
-                    void *p)
+                    struct trav_grow_data *tgd)             
 {
-       struct trav_grow_data *tgd = p;
        int val;
        unsigned char buffer[128] = { 0 };
 
index 5217f63b80b9b68a463c0f85174f59b362ba0070..9736e9f5a1d2c9667fb9d05c4966e9e5081f9457 100644 (file)
 #include "private.h"
 #include <ccan/likely/likely.h>
 
-int64_t tdb_traverse(struct tdb_context *tdb, tdb_traverse_func fn, void *p)
+int64_t tdb_traverse_(struct tdb_context *tdb,
+                     int (*fn)(struct tdb_context *,
+                               TDB_DATA, TDB_DATA, void *),
+                     void *p)
 {
        enum TDB_ERROR ecode;
        struct traverse_info tinfo;