Commit 5d226247 authored by Rusty Russell's avatar Rusty Russell

tdb2: typesafe traverse function

This make sure that the data being handed to the function in tdb_traverse
is the correct type.
parent 95c98eb3
...@@ -85,6 +85,7 @@ int main(int argc, char *argv[]) ...@@ -85,6 +85,7 @@ int main(int argc, char *argv[])
printf("ccan/ilog\n"); printf("ccan/ilog\n");
printf("ccan/failtest\n"); printf("ccan/failtest\n");
printf("ccan/tally\n"); printf("ccan/tally\n");
printf("ccan/typesafe_cb\n");
return 0; return 0;
} }
......
...@@ -40,6 +40,7 @@ extern "C" { ...@@ -40,6 +40,7 @@ extern "C" {
#include <stdint.h> #include <stdint.h>
#endif #endif
#include <ccan/compiler/compiler.h> #include <ccan/compiler/compiler.h>
#include <ccan/typesafe_cb/typesafe_cb.h>
union tdb_attribute; union tdb_attribute;
struct tdb_context; struct tdb_context;
...@@ -249,9 +250,6 @@ enum TDB_ERROR tdb_transaction_commit(struct tdb_context *tdb); ...@@ -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); 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_traverse - traverse a TDB
* @tdb: the tdb context returned from tdb_open() * @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 ...@@ -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 * On success, returns the number of keys iterated. On error returns
* a negative enum TDB_ERROR value. * 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 * tdb_firstkey - get the "first" key in a TDB
......
...@@ -38,9 +38,9 @@ struct trav_data { ...@@ -38,9 +38,9 @@ struct trav_data {
enum TDB_ERROR delete_error; 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; int val;
td->calls++; td->calls++;
...@@ -75,9 +75,8 @@ struct trav_grow_data { ...@@ -75,9 +75,8 @@ struct trav_grow_data {
}; };
static int trav_grow(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf, 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; int val;
unsigned char buffer[128] = { 0 }; unsigned char buffer[128] = { 0 };
......
...@@ -18,7 +18,10 @@ ...@@ -18,7 +18,10 @@
#include "private.h" #include "private.h"
#include <ccan/likely/likely.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; enum TDB_ERROR ecode;
struct traverse_info tinfo; struct traverse_info tinfo;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment