chiark / gitweb /
Remove dead code and unexport some calls
[elogind.git] / src / libsystemd-bus / bus-bloom.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2013 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include "util.h"
23 #include "MurmurHash3.h"
24
25 #include "bus-bloom.h"
26
27 static inline void set_bit(uint64_t filter[], unsigned b) {
28         filter[b >> 6] |= 1ULL << (b & 63);
29 }
30
31 static void bloom_add_data(uint64_t filter[BLOOM_SIZE/8], const void *data, size_t n) {
32         uint16_t hash[8];
33         unsigned k = 0;
34
35         /*
36          * Our bloom filter has the following parameters:
37          *
38          * m=512   (bits in the filter)
39          * k=8     (hash functions)
40          *
41          * We calculate a single 128bit MurmurHash value of which we
42          * use 8 parts of 9 bits as individual hash functions.
43          *
44          */
45
46         MurmurHash3_x64_128(data, n, 0, hash);
47
48         assert_cc(BLOOM_SIZE*8 == 512);
49
50         for (k = 0; k < ELEMENTSOF(hash); k++)
51                 set_bit(filter, hash[k] & 511);
52
53         /* log_debug("bloom: adding <%.*s>", (int) n, (char*) data); */
54 }
55
56 void bloom_add_pair(uint64_t filter[BLOOM_SIZE/8], const char *a, const char *b) {
57         size_t n;
58         char *c;
59
60         assert(filter);
61         assert(a);
62         assert(b);
63
64         n = strlen(a) + 1 + strlen(b);
65         c = alloca(n + 1);
66         strcpy(stpcpy(stpcpy(c, a), ":"), b);
67
68         bloom_add_data(filter, c, n);
69 }
70
71 void bloom_add_prefixes(uint64_t filter[BLOOM_SIZE/8], const char *a, const char *b, char sep) {
72         size_t n;
73         char *c, *p;
74
75         assert(filter);
76         assert(a);
77         assert(b);
78
79         n = strlen(a) + 1 + strlen(b);
80         c = alloca(n + 1);
81
82         p = stpcpy(stpcpy(c, a), ":");
83         strcpy(p, b);
84
85         for (;;) {
86                 char *e;
87
88                 e = strrchr(p, sep);
89                 if (!e || e == p)
90                         break;
91
92                 *e = 0;
93                 bloom_add_data(filter, c, e - c);
94         }
95 }