From e783191b2e15f3f6b3437233ef88cfb01eec3435 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cenk=20G=C3=BCndo=C4=9Fan?= Date: Tue, 1 Sep 2015 21:42:33 +0200 Subject: [PATCH] sys/bloom: pass bitfield to bloom instead of allocating memory --- sys/bloom/bloom.c | 61 +++++++++++---------------------------------- sys/include/bloom.h | 17 +++++++------ 2 files changed, 24 insertions(+), 54 deletions(-) diff --git a/sys/bloom/bloom.c b/sys/bloom/bloom.c index b393aa02f9..e394087815 100644 --- a/sys/bloom/bloom.c +++ b/sys/bloom/bloom.c @@ -18,66 +18,35 @@ #include #include "bloom.h" +#include "bitfield.h" +#include "string.h" -#define SETBIT(a,n) (a[n/CHAR_BIT] |= (1<<(n%CHAR_BIT))) -#define GETBIT(a,n) (a[n/CHAR_BIT] & (1<<(n%CHAR_BIT))) #define ROUND(size) ((size + CHAR_BIT - 1) / CHAR_BIT) -bloom_t *bloom_new(size_t size, size_t num_hashes, ...) +void bloom_init(bloom_t *bloom, size_t size, uint8_t *bitfield, hashfp_t *hashes, int hashes_numof) { - bloom_t *bloom; - va_list hashes; - size_t n; - - /* Allocate Bloom filter container */ - if (!(bloom = malloc(sizeof(bloom_t)))) { - return NULL; - } - - /* Allocate Bloom array */ - if (!(bloom->a = calloc(ROUND(size), sizeof(uint8_t)))) { - free(bloom); - return NULL; - } - - /* Allocate Bloom filter hash function pointers */ - if (!(bloom->hash = (hashfp_t *)malloc(num_hashes * sizeof(hashfp_t)))) { - free(bloom->a); - free(bloom); - return NULL; - } - - /* Assign hash functions to pointers in the Bloom filter */ - va_start(hashes, num_hashes); - - for (n = 0; n < num_hashes; n++) { - bloom->hash[n] = va_arg(hashes, hashfp_t); - } - - va_end(hashes); - - /* - * Record the number of hash functions (k) and the number of bytes - * in the Bloom array (m). - */ - bloom->k = num_hashes; bloom->m = size; - - return bloom; + bloom->a = bitfield; + bloom->hash = hashes; + bloom->k = hashes_numof; } void bloom_del(bloom_t *bloom) { - free(bloom->a); - free(bloom->hash); - free(bloom); + if (bloom->a) { + memset(bloom->a, 0, ROUND(bloom->m)); + } + bloom->a = NULL; + bloom->m = 0; + bloom->hash = NULL; + bloom->k = 0; } void bloom_add(bloom_t *bloom, const uint8_t *buf, size_t len) { for (size_t n = 0; n < bloom->k; n++) { uint32_t hash = bloom->hash[n](buf, len); - SETBIT(bloom->a, (hash % bloom->m)); + bf_set(bloom->a, (hash % bloom->m)); } } @@ -86,7 +55,7 @@ bool bloom_check(bloom_t *bloom, const uint8_t *buf, size_t len) for (size_t n = 0; n < bloom->k; n++) { uint32_t hash = bloom->hash[n](buf, len); - if (!(GETBIT(bloom->a, (hash % bloom->m)))) { + if (!(bf_isset(bloom->a, (hash % bloom->m)))) { return false; } } diff --git a/sys/include/bloom.h b/sys/include/bloom.h index 7ac5fd214e..d669cbb49a 100644 --- a/sys/include/bloom.h +++ b/sys/include/bloom.h @@ -151,18 +151,19 @@ typedef struct { } bloom_t; /** - * @brief Allocate and return a pointer to a new Bloom filter. + * @brief Initialize a Bloom Filter. * - * For best results, make 'size' a power of 2. + * @note For best results, make 'size' a power of 2. * - * @param size size of the bit array of the filter in bits - * @param num_hashes the number of hash functions - * @param ... varg function pointers, use hashfp_t - * - * @return An allocated bloom filter + * @param bloom bloom_t to initialize + * @param size size of the bloom filter in bits + * @param bitfield underlying bitfield of the bloom filter + * @param hashes array of hashes + * @param hashes_numof number of elements in hashes * + * @pre @p bitfield MUST be large enough to hold @p size bits. */ -bloom_t *bloom_new(size_t size, size_t num_hashes, ...); +void bloom_init(bloom_t *bloom, size_t size, uint8_t *bitfield, hashfp_t *hashes, int hashes_numof); /** * @brief Delete a Bloom filter.