]> git.ipfire.org Git - thirdparty/git.git/blame - hashmap.c
hashmap: use *_entry APIs to wrap container_of
[thirdparty/git.git] / hashmap.c
CommitLineData
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
10unsigned 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
18unsigned 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
29unsigned 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
40unsigned 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
JH
53/*
54 * Incoporate another chunk of data into a memihash
55 * computation.
56 */
57unsigned 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
76static 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
94static inline int entry_equals(const struct hashmap *map,
95 const struct hashmap_entry *e1, const struct hashmap_entry *e2,
96 const void *keydata)
97{
7663cdc8
SB
98 return (e1 == e2) ||
99 (e1->hash == e2->hash &&
100 !map->cmpfn(map->cmpfn_data, e1, e2, keydata));
6a364ced
KB
101}
102
103static inline unsigned int bucket(const struct hashmap *map,
104 const struct hashmap_entry *key)
105{
106 return key->hash & (map->tablesize - 1);
107}
108
0607e100
JH
109int hashmap_bucket(const struct hashmap *map, unsigned int hash)
110{
111 return hash & (map->tablesize - 1);
112}
113
6a364ced
KB
114static void rehash(struct hashmap *map, unsigned int newsize)
115{
116 unsigned int i, oldsize = map->tablesize;
117 struct hashmap_entry **oldtable = map->table;
118
119 alloc_table(map, newsize);
120 for (i = 0; i < oldsize; i++) {
121 struct hashmap_entry *e = oldtable[i];
122 while (e) {
123 struct hashmap_entry *next = e->next;
124 unsigned int b = bucket(map, e);
125 e->next = map->table[b];
126 map->table[b] = e;
127 e = next;
128 }
129 }
130 free(oldtable);
131}
132
133static inline struct hashmap_entry **find_entry_ptr(const struct hashmap *map,
134 const struct hashmap_entry *key, const void *keydata)
135{
136 struct hashmap_entry **e = &map->table[bucket(map, key)];
137 while (*e && !entry_equals(map, *e, key, keydata))
138 e = &(*e)->next;
139 return e;
140}
141
7663cdc8
SB
142static int always_equal(const void *unused_cmp_data,
143 const void *unused1,
144 const void *unused2,
145 const void *unused_keydata)
6a364ced
KB
146{
147 return 0;
148}
149
150void hashmap_init(struct hashmap *map, hashmap_cmp_fn equals_function,
7663cdc8 151 const void *cmpfn_data, size_t initial_size)
6a364ced
KB
152{
153 unsigned int size = HASHMAP_INITIAL_SIZE;
0607e100
JH
154
155 memset(map, 0, sizeof(*map));
156
6a364ced 157 map->cmpfn = equals_function ? equals_function : always_equal;
7663cdc8 158 map->cmpfn_data = cmpfn_data;
6a364ced
KB
159
160 /* calculate initial table size and allocate the table */
161 initial_size = (unsigned int) ((uint64_t) initial_size * 100
162 / HASHMAP_LOAD_FACTOR);
163 while (initial_size > size)
164 size <<= HASHMAP_RESIZE_BITS;
165 alloc_table(map, size);
8b604d19
JH
166
167 /*
168 * Keep track of the number of items in the map and
169 * allow the map to automatically grow as necessary.
170 */
171 map->do_count_items = 1;
6a364ced
KB
172}
173
174void hashmap_free(struct hashmap *map, int free_entries)
175{
176 if (!map || !map->table)
177 return;
178 if (free_entries) {
179 struct hashmap_iter iter;
180 struct hashmap_entry *e;
181 hashmap_iter_init(map, &iter);
182 while ((e = hashmap_iter_next(&iter)))
183 free(e);
184 }
185 free(map->table);
186 memset(map, 0, sizeof(*map));
187}
188
b6c52416
EW
189void *hashmap_get(const struct hashmap *map, const struct hashmap_entry *key,
190 const void *keydata)
6a364ced
KB
191{
192 return *find_entry_ptr(map, key, keydata);
193}
194
6bcbdfb2 195struct hashmap_entry *hashmap_get_next(const struct hashmap *map,
f6eb6bdc 196 const struct hashmap_entry *entry)
6a364ced 197{
f6eb6bdc 198 struct hashmap_entry *e = entry->next;
6a364ced
KB
199 for (; e; e = e->next)
200 if (entry_equals(map, entry, e, NULL))
201 return e;
202 return NULL;
203}
204
b94e5c1d 205void hashmap_add(struct hashmap *map, struct hashmap_entry *entry)
6a364ced
KB
206{
207 unsigned int b = bucket(map, entry);
208
209 /* add entry */
b94e5c1d 210 entry->next = map->table[b];
6a364ced
KB
211 map->table[b] = entry;
212
213 /* fix size and rehash if appropriate */
8b604d19
JH
214 if (map->do_count_items) {
215 map->private_size++;
216 if (map->private_size > map->grow_at)
217 rehash(map, map->tablesize << HASHMAP_RESIZE_BITS);
218 }
6a364ced
KB
219}
220
28ee7941
EW
221void *hashmap_remove(struct hashmap *map, const struct hashmap_entry *key,
222 const void *keydata)
6a364ced
KB
223{
224 struct hashmap_entry *old;
225 struct hashmap_entry **e = find_entry_ptr(map, key, keydata);
226 if (!*e)
227 return NULL;
228
229 /* remove existing entry */
230 old = *e;
231 *e = old->next;
232 old->next = NULL;
233
234 /* fix size and rehash if appropriate */
8b604d19
JH
235 if (map->do_count_items) {
236 map->private_size--;
237 if (map->private_size < map->shrink_at)
238 rehash(map, map->tablesize >> HASHMAP_RESIZE_BITS);
239 }
240
6a364ced
KB
241 return old;
242}
243
26b455f2 244void *hashmap_put(struct hashmap *map, struct hashmap_entry *entry)
6a364ced
KB
245{
246 struct hashmap_entry *old = hashmap_remove(map, entry, NULL);
247 hashmap_add(map, entry);
248 return old;
249}
250
251void hashmap_iter_init(struct hashmap *map, struct hashmap_iter *iter)
252{
253 iter->map = map;
254 iter->tablepos = 0;
255 iter->next = NULL;
256}
257
258void *hashmap_iter_next(struct hashmap_iter *iter)
259{
260 struct hashmap_entry *current = iter->next;
261 for (;;) {
262 if (current) {
263 iter->next = current->next;
264 return current;
265 }
266
267 if (iter->tablepos >= iter->map->tablesize)
268 return NULL;
269
270 current = iter->map->table[iter->tablepos++];
271 }
272}
7b64d42d
KB
273
274struct pool_entry {
275 struct hashmap_entry ent;
276 size_t len;
277 unsigned char data[FLEX_ARRAY];
278};
279
7663cdc8
SB
280static int pool_entry_cmp(const void *unused_cmp_data,
281 const struct pool_entry *e1,
7b64d42d
KB
282 const struct pool_entry *e2,
283 const unsigned char *keydata)
284{
285 return e1->data != keydata &&
286 (e1->len != e2->len || memcmp(e1->data, keydata, e1->len));
287}
288
289const void *memintern(const void *data, size_t len)
290{
291 static struct hashmap map;
292 struct pool_entry key, *e;
293
294 /* initialize string pool hashmap */
295 if (!map.tablesize)
7663cdc8 296 hashmap_init(&map, (hashmap_cmp_fn) pool_entry_cmp, NULL, 0);
7b64d42d
KB
297
298 /* lookup interned string in pool */
d22245a2 299 hashmap_entry_init(&key.ent, memhash(data, len));
7b64d42d 300 key.len = len;
b6c52416 301 e = hashmap_get(&map, &key.ent, data);
7b64d42d
KB
302 if (!e) {
303 /* not found: create it */
96ffc06f 304 FLEX_ALLOC_MEM(e, data, data, len);
d22245a2 305 hashmap_entry_init(&e->ent, key.ent.hash);
7b64d42d 306 e->len = len;
b94e5c1d 307 hashmap_add(&map, &e->ent);
7b64d42d
KB
308 }
309 return e->data;
310}