]> git.tuebingen.mpg.de Git - adu.git/blobdiff - bloom.c
Rename bloom_init() to bloom_new().
[adu.git] / bloom.c
diff --git a/bloom.c b/bloom.c
index f7a9b1aabd32f89863fa09f4c0121942df1b23f9..2aff535e833f75ba178bfcbf0524e0bf19d0cca7 100644 (file)
--- a/bloom.c
+++ b/bloom.c
@@ -12,6 +12,9 @@
 void *adu_calloc(size_t size)
 {
        void *ret = malloc(size);
+
+       if (!ret)
+               exit(EXIT_FAILURE);
        memset(ret, 0, size);
        return ret;
 }
@@ -114,13 +117,11 @@ int bloom_test_and_insert(const uint8_t *data, size_t len, struct bloom *b)
        return !ret;
 }
 
-int bloom_test_and_insert_string(const char *str, struct bloom *b)
-{
-       uint32_t len = strlen(str);
-
-       return bloom_test_and_insert((const uint8_t *)str, len, b);
-}
-
+/**
+ * Deallocate a bloom filter.
+ *
+ * \param b The filter to deallocate.
+ */
 void bloom_free(struct bloom *b)
 {
        if (!b)
@@ -129,8 +130,16 @@ void bloom_free(struct bloom *b)
        free(b);
 }
 
-int bloom_init(unsigned order, unsigned num_hash_functions,
-               struct bloom **result)
+/**
+ * Allocate and initialize a new bloom filter.
+ *
+ * \param order Use a filter containing 2^order bits.
+ * \param num_hash_functions Set that many bits in the filter per entry.
+ *
+ * \return This function either returns a freshly allocated and initialized
+ * bloom filter or does not return at all (if the underlying malloc() failed).
+ */
+struct bloom *bloom_new(unsigned order, unsigned num_hash_functions)
 {
        struct bloom *b = adu_calloc(sizeof(*b));
 
@@ -139,12 +148,18 @@ int bloom_init(unsigned order, unsigned num_hash_functions,
        b->order = order;
        b->num_hash_functions = num_hash_functions;
        b->filter = adu_calloc(filter_bits(b) / 8);
-       *result = b;
-       return 1;
+       return b;
 }
 
 #ifdef TEST_BLOOM
 
+int bloom_test_and_insert_string(const char *str, struct bloom *b)
+{
+       uint32_t len = strlen(str);
+
+       return bloom_test_and_insert((const uint8_t *)str, len, b);
+}
+
 void add_stdin(struct bloom *b)
 {
        char buf[255];
@@ -173,7 +188,7 @@ int main(int argc, char **argv)
                printf("k: # of hash functions to use\n");
                exit(1);
        }
-       bloom_init(atoi(argv[1]), atoi(argv[2]), &b);
+       b = bloom_new(atoi(argv[1]), atoi(argv[2]));
        add_stdin(b);
        INFO_LOG("%u%% of bits are set\n", (unsigned)
                (b->num_set_bits * 100ULL / filter_bits(b)));