]> git.ozlabs.org Git - ccan/blobdiff - ccan/tdb2/traverse.c
tdb2: add comments to tdb2.h, reorder for maximum readability.
[ccan] / ccan / tdb2 / traverse.c
index 410054d7947a704407dbf3c559a569d22ea27a87..b79cc8b47e7c090e64ee12b69b225e8ba4b33f78 100644 (file)
@@ -1,7 +1,7 @@
- /* 
+ /*
    Trivial Database 2: traverse function.
    Copyright (C) Rusty Russell 2010
-   
+
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
@@ -26,14 +26,18 @@ static int64_t traverse(struct tdb_context *tdb, int ltype,
        struct tdb_data k, d;
        int64_t count = 0;
 
+       k.dptr = NULL;
        for (ret = first_in_hash(tdb, ltype, &tinfo, &k, &d.dsize);
             ret == 1;
             ret = next_in_hash(tdb, ltype, &tinfo, &k, &d.dsize)) {
                d.dptr = k.dptr + k.dsize;
                
                count++;
-               if (fn && fn(tdb, k, d, p))
+               if (fn && fn(tdb, k, d, p)) {
+                       free(k.dptr);
                        break;
+               }
+               free(k.dptr);
        }
 
        if (ret < 0)
@@ -70,7 +74,7 @@ TDB_DATA tdb_firstkey(struct tdb_context *tdb)
        default:
                return tdb_null;
        }
-}              
+}
 
 /* We lock twice, not very efficient.  We could keep last key & tinfo cached. */
 TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA key)
@@ -93,4 +97,4 @@ TDB_DATA tdb_nextkey(struct tdb_context *tdb, TDB_DATA key)
        default:
                return tdb_null;
        }
-}              
+}