]>
Commit | Line | Data |
---|---|---|
6a364ced KB |
1 | /* |
2 | * Generic implementation of hash-based key value mappings. | |
3 | */ | |
4 | #include "cache.h" | |
5 | #include "hashmap.h" | |
6 | ||
7 | #define FNV32_BASE ((unsigned int) 0x811c9dc5) | |
8 | #define FNV32_PRIME ((unsigned int) 0x01000193) | |
9 | ||
10 | unsigned int strhash(const char *str) | |
11 | { | |
12 | unsigned int c, hash = FNV32_BASE; | |
13 | while ((c = (unsigned char) *str++)) | |
14 | hash = (hash * FNV32_PRIME) ^ c; | |
15 | return hash; | |
16 | } | |
17 | ||
18 | unsigned int strihash(const char *str) | |
19 | { | |
20 | unsigned int c, hash = FNV32_BASE; | |
21 | while ((c = (unsigned char) *str++)) { | |
22 | if (c >= 'a' && c <= 'z') | |
23 | c -= 'a' - 'A'; | |
24 | hash = (hash * FNV32_PRIME) ^ c; | |
25 | } | |
26 | return hash; | |
27 | } | |
28 | ||
29 | unsigned int memhash(const void *buf, size_t len) | |
30 | { | |
31 | unsigned int hash = FNV32_BASE; | |
32 | unsigned char *ucbuf = (unsigned char *) buf; | |
33 | while (len--) { | |
34 | unsigned int c = *ucbuf++; | |
35 | hash = (hash * FNV32_PRIME) ^ c; | |
36 | } | |
37 | return hash; | |
38 | } | |
39 | ||
40 | unsigned int memihash(const void *buf, size_t len) | |
41 | { | |
42 | unsigned int hash = FNV32_BASE; | |
43 | unsigned char *ucbuf = (unsigned char *) buf; | |
44 | while (len--) { | |
45 | unsigned int c = *ucbuf++; | |
46 | if (c >= 'a' && c <= 'z') | |
47 | c -= 'a' - 'A'; | |
48 | hash = (hash * FNV32_PRIME) ^ c; | |
49 | } | |
50 | return hash; | |
51 | } | |
52 | ||
f75619bd | 53 | /* |
15beaaa3 | 54 | * Incorporate another chunk of data into a memihash |
f75619bd JH |
55 | * computation. |
56 | */ | |
57 | unsigned int memihash_cont(unsigned int hash_seed, const void *buf, size_t len) | |
58 | { | |
59 | unsigned int hash = hash_seed; | |
60 | unsigned char *ucbuf = (unsigned char *) buf; | |
61 | while (len--) { | |
62 | unsigned int c = *ucbuf++; | |
63 | if (c >= 'a' && c <= 'z') | |
64 | c -= 'a' - 'A'; | |
65 | hash = (hash * FNV32_PRIME) ^ c; | |
66 | } | |
67 | return hash; | |
68 | } | |
69 | ||
6a364ced KB |
70 | #define HASHMAP_INITIAL_SIZE 64 |
71 | /* grow / shrink by 2^2 */ | |
72 | #define HASHMAP_RESIZE_BITS 2 | |
73 | /* load factor in percent */ | |
74 | #define HASHMAP_LOAD_FACTOR 80 | |
75 | ||
76 | static void alloc_table(struct hashmap *map, unsigned int size) | |
77 | { | |
78 | map->tablesize = size; | |
79 | map->table = xcalloc(size, sizeof(struct hashmap_entry *)); | |
80 | ||
81 | /* calculate resize thresholds for new size */ | |
82 | map->grow_at = (unsigned int) ((uint64_t) size * HASHMAP_LOAD_FACTOR / 100); | |
83 | if (size <= HASHMAP_INITIAL_SIZE) | |
84 | map->shrink_at = 0; | |
85 | else | |
86 | /* | |
87 | * The shrink-threshold must be slightly smaller than | |
88 | * (grow-threshold / resize-factor) to prevent erratic resizing, | |
89 | * thus we divide by (resize-factor + 1). | |
90 | */ | |
91 | map->shrink_at = map->grow_at / ((1 << HASHMAP_RESIZE_BITS) + 1); | |
92 | } | |
93 | ||
94 | static inline int entry_equals(const struct hashmap *map, | |
97a39a4a EN |
95 | const struct hashmap_entry *e1, |
96 | const struct hashmap_entry *e2, | |
97 | const void *keydata) | |
6a364ced | 98 | { |
7663cdc8 SB |
99 | return (e1 == e2) || |
100 | (e1->hash == e2->hash && | |
101 | !map->cmpfn(map->cmpfn_data, e1, e2, keydata)); | |
6a364ced KB |
102 | } |
103 | ||
104 | static inline unsigned int bucket(const struct hashmap *map, | |
97a39a4a | 105 | const struct hashmap_entry *key) |
6a364ced KB |
106 | { |
107 | return key->hash & (map->tablesize - 1); | |
108 | } | |
109 | ||
0607e100 JH |
110 | int hashmap_bucket(const struct hashmap *map, unsigned int hash) |
111 | { | |
112 | return hash & (map->tablesize - 1); | |
113 | } | |
114 | ||
6a364ced KB |
115 | static void rehash(struct hashmap *map, unsigned int newsize) |
116 | { | |
117 | unsigned int i, oldsize = map->tablesize; | |
118 | struct hashmap_entry **oldtable = map->table; | |
119 | ||
120 | alloc_table(map, newsize); | |
121 | for (i = 0; i < oldsize; i++) { | |
122 | struct hashmap_entry *e = oldtable[i]; | |
123 | while (e) { | |
124 | struct hashmap_entry *next = e->next; | |
125 | unsigned int b = bucket(map, e); | |
126 | e->next = map->table[b]; | |
127 | map->table[b] = e; | |
128 | e = next; | |
129 | } | |
130 | } | |
131 | free(oldtable); | |
132 | } | |
133 | ||
134 | static inline struct hashmap_entry **find_entry_ptr(const struct hashmap *map, | |
135 | const struct hashmap_entry *key, const void *keydata) | |
136 | { | |
137 | struct hashmap_entry **e = &map->table[bucket(map, key)]; | |
138 | while (*e && !entry_equals(map, *e, key, keydata)) | |
139 | e = &(*e)->next; | |
140 | return e; | |
141 | } | |
142 | ||
7663cdc8 | 143 | static int always_equal(const void *unused_cmp_data, |
939af16e EW |
144 | const struct hashmap_entry *unused1, |
145 | const struct hashmap_entry *unused2, | |
7663cdc8 | 146 | const void *unused_keydata) |
6a364ced KB |
147 | { |
148 | return 0; | |
149 | } | |
150 | ||
151 | void hashmap_init(struct hashmap *map, hashmap_cmp_fn equals_function, | |
97a39a4a | 152 | const void *cmpfn_data, size_t initial_size) |
6a364ced KB |
153 | { |
154 | unsigned int size = HASHMAP_INITIAL_SIZE; | |
0607e100 JH |
155 | |
156 | memset(map, 0, sizeof(*map)); | |
157 | ||
6a364ced | 158 | map->cmpfn = equals_function ? equals_function : always_equal; |
7663cdc8 | 159 | map->cmpfn_data = cmpfn_data; |
6a364ced KB |
160 | |
161 | /* calculate initial table size and allocate the table */ | |
162 | initial_size = (unsigned int) ((uint64_t) initial_size * 100 | |
163 | / HASHMAP_LOAD_FACTOR); | |
164 | while (initial_size > size) | |
165 | size <<= HASHMAP_RESIZE_BITS; | |
166 | alloc_table(map, size); | |
8b604d19 JH |
167 | |
168 | /* | |
169 | * Keep track of the number of items in the map and | |
170 | * allow the map to automatically grow as necessary. | |
171 | */ | |
172 | map->do_count_items = 1; | |
6a364ced KB |
173 | } |
174 | ||
c8e424c9 | 175 | void hashmap_free_(struct hashmap *map, ssize_t entry_offset) |
6a364ced KB |
176 | { |
177 | if (!map || !map->table) | |
178 | return; | |
c8e424c9 | 179 | if (entry_offset >= 0) { /* called by hashmap_free_entries */ |
6a364ced KB |
180 | struct hashmap_iter iter; |
181 | struct hashmap_entry *e; | |
c8e424c9 | 182 | |
6a364ced KB |
183 | hashmap_iter_init(map, &iter); |
184 | while ((e = hashmap_iter_next(&iter))) | |
c8e424c9 EW |
185 | /* |
186 | * like container_of, but using caller-calculated | |
187 | * offset (caller being hashmap_free_entries) | |
188 | */ | |
189 | free((char *)e - entry_offset); | |
6a364ced KB |
190 | } |
191 | free(map->table); | |
192 | memset(map, 0, sizeof(*map)); | |
193 | } | |
194 | ||
f23a4651 EW |
195 | struct hashmap_entry *hashmap_get(const struct hashmap *map, |
196 | const struct hashmap_entry *key, | |
197 | const void *keydata) | |
6a364ced KB |
198 | { |
199 | return *find_entry_ptr(map, key, keydata); | |
200 | } | |
201 | ||
6bcbdfb2 | 202 | struct hashmap_entry *hashmap_get_next(const struct hashmap *map, |
97a39a4a | 203 | const struct hashmap_entry *entry) |
6a364ced | 204 | { |
f6eb6bdc | 205 | struct hashmap_entry *e = entry->next; |
6a364ced KB |
206 | for (; e; e = e->next) |
207 | if (entry_equals(map, entry, e, NULL)) | |
208 | return e; | |
209 | return NULL; | |
210 | } | |
211 | ||
b94e5c1d | 212 | void hashmap_add(struct hashmap *map, struct hashmap_entry *entry) |
6a364ced KB |
213 | { |
214 | unsigned int b = bucket(map, entry); | |
215 | ||
216 | /* add entry */ | |
b94e5c1d | 217 | entry->next = map->table[b]; |
6a364ced KB |
218 | map->table[b] = entry; |
219 | ||
220 | /* fix size and rehash if appropriate */ | |
8b604d19 JH |
221 | if (map->do_count_items) { |
222 | map->private_size++; | |
223 | if (map->private_size > map->grow_at) | |
224 | rehash(map, map->tablesize << HASHMAP_RESIZE_BITS); | |
225 | } | |
6a364ced KB |
226 | } |
227 | ||
8a973d0b | 228 | struct hashmap_entry *hashmap_remove(struct hashmap *map, |
97a39a4a EN |
229 | const struct hashmap_entry *key, |
230 | const void *keydata) | |
6a364ced KB |
231 | { |
232 | struct hashmap_entry *old; | |
233 | struct hashmap_entry **e = find_entry_ptr(map, key, keydata); | |
234 | if (!*e) | |
235 | return NULL; | |
236 | ||
237 | /* remove existing entry */ | |
238 | old = *e; | |
239 | *e = old->next; | |
240 | old->next = NULL; | |
241 | ||
242 | /* fix size and rehash if appropriate */ | |
8b604d19 JH |
243 | if (map->do_count_items) { |
244 | map->private_size--; | |
245 | if (map->private_size < map->shrink_at) | |
246 | rehash(map, map->tablesize >> HASHMAP_RESIZE_BITS); | |
247 | } | |
248 | ||
6a364ced KB |
249 | return old; |
250 | } | |
251 | ||
8a973d0b | 252 | struct hashmap_entry *hashmap_put(struct hashmap *map, |
97a39a4a | 253 | struct hashmap_entry *entry) |
6a364ced KB |
254 | { |
255 | struct hashmap_entry *old = hashmap_remove(map, entry, NULL); | |
256 | hashmap_add(map, entry); | |
257 | return old; | |
258 | } | |
259 | ||
260 | void hashmap_iter_init(struct hashmap *map, struct hashmap_iter *iter) | |
261 | { | |
262 | iter->map = map; | |
263 | iter->tablepos = 0; | |
264 | iter->next = NULL; | |
265 | } | |
266 | ||
87571c3f | 267 | struct hashmap_entry *hashmap_iter_next(struct hashmap_iter *iter) |
6a364ced KB |
268 | { |
269 | struct hashmap_entry *current = iter->next; | |
270 | for (;;) { | |
271 | if (current) { | |
272 | iter->next = current->next; | |
273 | return current; | |
274 | } | |
275 | ||
276 | if (iter->tablepos >= iter->map->tablesize) | |
277 | return NULL; | |
278 | ||
279 | current = iter->map->table[iter->tablepos++]; | |
280 | } | |
281 | } | |
7b64d42d KB |
282 | |
283 | struct pool_entry { | |
284 | struct hashmap_entry ent; | |
285 | size_t len; | |
286 | unsigned char data[FLEX_ARRAY]; | |
287 | }; | |
288 | ||
7663cdc8 | 289 | static int pool_entry_cmp(const void *unused_cmp_data, |
939af16e EW |
290 | const struct hashmap_entry *eptr, |
291 | const struct hashmap_entry *entry_or_key, | |
292 | const void *keydata) | |
7b64d42d | 293 | { |
939af16e EW |
294 | const struct pool_entry *e1, *e2; |
295 | ||
296 | e1 = container_of(eptr, const struct pool_entry, ent); | |
297 | e2 = container_of(entry_or_key, const struct pool_entry, ent); | |
298 | ||
7b64d42d KB |
299 | return e1->data != keydata && |
300 | (e1->len != e2->len || memcmp(e1->data, keydata, e1->len)); | |
301 | } | |
302 | ||
303 | const void *memintern(const void *data, size_t len) | |
304 | { | |
305 | static struct hashmap map; | |
306 | struct pool_entry key, *e; | |
307 | ||
308 | /* initialize string pool hashmap */ | |
309 | if (!map.tablesize) | |
939af16e | 310 | hashmap_init(&map, pool_entry_cmp, NULL, 0); |
7b64d42d KB |
311 | |
312 | /* lookup interned string in pool */ | |
d22245a2 | 313 | hashmap_entry_init(&key.ent, memhash(data, len)); |
7b64d42d | 314 | key.len = len; |
404ab78e | 315 | e = hashmap_get_entry(&map, &key, ent, data); |
7b64d42d KB |
316 | if (!e) { |
317 | /* not found: create it */ | |
96ffc06f | 318 | FLEX_ALLOC_MEM(e, data, data, len); |
d22245a2 | 319 | hashmap_entry_init(&e->ent, key.ent.hash); |
7b64d42d | 320 | e->len = len; |
b94e5c1d | 321 | hashmap_add(&map, &e->ent); |
7b64d42d KB |
322 | } |
323 | return e->data; | |
324 | } |