1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-01-18 12:52:44 +01:00

Merge pull request #3758 from cgundogan/pr/bloom/static

sys/bloom: Pass buffer to bloom instead of using m/calloc
This commit is contained in:
Cenk Gündoğan 2015-10-08 16:35:28 +02:00
commit bcb0d54028
5 changed files with 59 additions and 70 deletions

View File

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

View File

@ -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.

View File

@ -1,6 +1,8 @@
APPLICATION = bloom_bytes
include ../Makefile.tests_common
BOARD_INSUFFICIENT_MEMORY := chronos msb-430 msb-430h telosb wsn430-v1_3b wsn430-v1_4 z1
USEMODULE += hashes
USEMODULE += bloom
USEMODULE += random

View File

@ -27,7 +27,10 @@
#include "hashes.h"
#include "bloom.h"
#include "random.h"
#include "bitfield.h"
#define BLOOM_BITS (1UL << 12)
#define BLOOM_HASHF (8)
#define lenB 512
#define lenA (10 * 1000)
@ -38,6 +41,13 @@
#define BUF_SIZE 50
static uint32_t buf[BUF_SIZE];
static bloom_t bloom;
BITFIELD(bf, BLOOM_BITS);
hashfp_t hashes[BLOOM_HASHF] = {
(hashfp_t) fnv_hash, (hashfp_t) sax_hash, (hashfp_t) sdbm_hash,
(hashfp_t) djb2_hash, (hashfp_t) kr_hash, (hashfp_t) dek_hash,
(hashfp_t) rotating_hash, (hashfp_t) one_at_a_time_hash,
};
static void buf_fill(uint32_t *buf, int len)
{
@ -50,12 +60,11 @@ int main(void)
{
xtimer_init();
bloom_t *bloom = bloom_new(1 << 12, 8, fnv_hash, sax_hash, sdbm_hash,
djb2_hash, kr_hash, dek_hash, rotating_hash, one_at_a_time_hash);
bloom_init(&bloom, BLOOM_BITS, bf, hashes, BLOOM_HASHF);
printf("Testing Bloom filter.\n\n");
printf("m: %" PRIu32 " k: %" PRIu32 "\n\n", (uint32_t) bloom->m,
(uint32_t) bloom->k);
printf("m: %" PRIu32 " k: %" PRIu32 "\n\n", (uint32_t) bloom.m,
(uint32_t) bloom.k);
genrand_init(myseed);
@ -64,7 +73,7 @@ int main(void)
for (int i = 0; i < lenB; i++) {
buf_fill(buf, BUF_SIZE);
buf[0] = MAGIC_B;
bloom_add(bloom,
bloom_add(&bloom,
(uint8_t *) buf,
BUF_SIZE * sizeof(uint32_t) / sizeof(uint8_t));
}
@ -82,7 +91,7 @@ int main(void)
buf_fill(buf, BUF_SIZE);
buf[0] = MAGIC_A;
if (bloom_check(bloom,
if (bloom_check(&bloom,
(uint8_t *) buf,
BUF_SIZE * sizeof(uint32_t) / sizeof(uint8_t))) {
in++;
@ -102,7 +111,7 @@ int main(void)
double false_positive_rate = (double) in / (double) lenA;
printf("%f false positive rate.\n", false_positive_rate);
bloom_del(bloom);
bloom_del(&bloom);
printf("\nAll done!\n");
return 0;
}

View File

@ -13,6 +13,7 @@
#include "hashes.h"
#include "bloom.h"
#include "bitfield.h"
#include "tests-bloom-sets.h"
@ -22,33 +23,40 @@
#define TESTS_BLOOM_NOT_IN_FILTER (996)
#define TESTS_BLOOM_FALSE_POS_RATE_THR (0.005)
static bloom_t *bloom;
static bloom_t bloom;
BITFIELD(bf, TESTS_BLOOM_BITS);
hashfp_t hashes[TESTS_BLOOM_HASHF] = {
(hashfp_t) fnv_hash,
(hashfp_t) sax_hash,
(hashfp_t) sdbm_hash,
(hashfp_t) djb2_hash,
(hashfp_t) kr_hash,
(hashfp_t) dek_hash,
};
static void load_dictionary_fixture(void)
{
for (int i = 0; i < lenB; i++)
{
bloom_add(bloom, (const uint8_t *) B[i], strlen(B[i]));
bloom_add(&bloom, (const uint8_t *) B[i], strlen(B[i]));
}
}
static void set_up_bloom(void)
{
bloom = bloom_new(TESTS_BLOOM_BITS, TESTS_BLOOM_HASHF, fnv_hash, sax_hash,
sdbm_hash, djb2_hash, kr_hash, dek_hash, rotating_hash,
one_at_a_time_hash);
bloom_init(&bloom, TESTS_BLOOM_BITS, bf, hashes, TESTS_BLOOM_HASHF);
}
static void tear_down_bloom(void)
{
bloom_del(bloom);
bloom_del(&bloom);
}
static void test_bloom_parameters_bytes_hashf(void)
{
TEST_ASSERT_EQUAL_INT(TESTS_BLOOM_BITS, bloom->m);
TEST_ASSERT_EQUAL_INT(TESTS_BLOOM_HASHF, bloom->k);
TEST_ASSERT_EQUAL_INT(TESTS_BLOOM_BITS, bloom.m);
TEST_ASSERT_EQUAL_INT(TESTS_BLOOM_HASHF, bloom.k);
}
static void test_bloom_based_on_dictionary_fixture(void)
@ -61,7 +69,7 @@ static void test_bloom_based_on_dictionary_fixture(void)
for (int i = 0; i < lenA; i++)
{
if (bloom_check(bloom, (const uint8_t *) A[i], strlen(A[i])))
if (bloom_check(&bloom, (const uint8_t *) A[i], strlen(A[i])))
{
in++;
}