12 void *adu_calloc(size_t size)
14 void *ret = malloc(size);
22 #define INFO_LOG printf
25 static inline uint64_t filter_bits(struct bloom *b)
27 return 1ULL << b->order;
31 * SuperFastHash, by Paul Hsieh.
32 * http://www.azillionmonkeys.com/qed/hash.html
35 #if (defined(__GNUC__) && defined(__i386__))
36 #define get16bits(d) (*((const uint16_t *) (d)))
38 #define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\
39 +(uint32_t)(((const uint8_t *)(d))[0]) )
43 static uint32_t super_fast_hash(const uint8_t *data, uint32_t len, uint32_t hash)
50 for (;len > 0; len--) {
51 hash += get16bits (data);
52 tmp = (get16bits (data+2) << 11) ^ hash;
53 hash = (hash << 16) ^ tmp;
54 data += 2*sizeof (uint16_t);
58 /* Handle end cases */
61 hash += get16bits (data);
63 hash ^= data[sizeof (uint16_t)] << 18;
67 hash += get16bits (data);
76 /* Force "avalanching" of final 127 bits */
86 static int test_and_set_bit(uint64_t bitnum, struct bloom *b)
88 uint64_t byte = bitnum / 8;
89 uint8_t bit = 1 << (bitnum % 8);
90 int ret = b->filter[byte] & bit;
92 b->filter[byte] |= bit;
99 int bloom_insert(const uint8_t *data, size_t len, struct bloom *b)
102 uint32_t h = 0xb11924e1; /* some arbitrary value */
104 for (i = 1; i < b->num_hash_functions; i++) {
105 uint64_t bitnum = super_fast_hash(data, len, h);
108 bitnum = bitnum << 32;
109 h = super_fast_hash(data, len, h);
112 bitnum &= filter_bits(b) - 1;
113 ret |= !test_and_set_bit(bitnum, b);
121 * Deallocate a bloom filter.
123 * \param b The filter to deallocate.
125 void bloom_free(struct bloom *b)
134 * Allocate and initialize a new bloom filter.
136 * \param order Use a filter containing 2^order bits.
137 * \param num_hash_functions Set that many bits in the filter per entry.
139 * \return This function either returns a freshly allocated and initialized
140 * bloom filter or does not return at all (if the underlying malloc() failed).
142 struct bloom *bloom_new(unsigned order, unsigned num_hash_functions)
144 struct bloom *b = adu_calloc(sizeof(*b));
146 INFO_LOG("allocating bloom filter (order: %u, %u hash functions)\n",
147 order, num_hash_functions);
149 b->num_hash_functions = num_hash_functions;
150 b->filter = adu_calloc(filter_bits(b) / 8);
156 int bloom_insert_string(const char *str, struct bloom *b)
158 uint32_t len = strlen(str);
160 return bloom_insert((const uint8_t *)str, len, b);
163 void add_stdin(struct bloom *b)
166 unsigned false_positives = 0;
168 while (fgets(buf, sizeof(buf) - 1, stdin)) {
169 size_t len = strlen(buf);
172 if (buf[len - 1] == '\n')
174 if (bloom_insert_string(buf, b))
177 INFO_LOG("filter contains %llu entries\n", b->num_entries);
178 printf("%u possible false positives\n", false_positives);
181 int main(int argc, char **argv)
186 printf("usage: $0 j k\n");
187 printf("j: set bloom filter size to m=2^j bits\n");
188 printf("k: # of hash functions to use\n");
191 b = bloom_new(atoi(argv[1]), atoi(argv[2]));
193 INFO_LOG("%u%% of bits are set\n", (unsigned)
194 (b->num_set_bits * 100ULL / filter_bits(b)));
198 #endif /* TEST_BLOOM */