Skip to content

Commit

Permalink
sys/bloom: pass bitfield to bloom instead of allocating memory
Browse files Browse the repository at this point in the history
  • Loading branch information
cgundogan committed Oct 5, 2015
1 parent 1c68c8f commit e783191
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 54 deletions.
61 changes: 15 additions & 46 deletions sys/bloom/bloom.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,66 +18,35 @@
#include <stdbool.h>

#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));
}
}

Expand All @@ -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;
}
}
Expand Down
17 changes: 9 additions & 8 deletions sys/include/bloom.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit e783191

Please sign in to comment.