]> git.ipfire.org Git - thirdparty/git.git/blob - hashmap.c
hashmap: allow memihash computation to be continued
[thirdparty/git.git] / hashmap.c
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
53 /*
54 * Incoporate another chunk of data into a memihash
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
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,
95 const struct hashmap_entry *e1, const struct hashmap_entry *e2,
96 const void *keydata)
97 {
98 return (e1 == e2) || (e1->hash == e2->hash && !map->cmpfn(e1, e2, keydata));
99 }
100
101 static inline unsigned int bucket(const struct hashmap *map,
102 const struct hashmap_entry *key)
103 {
104 return key->hash & (map->tablesize - 1);
105 }
106
107 static void rehash(struct hashmap *map, unsigned int newsize)
108 {
109 unsigned int i, oldsize = map->tablesize;
110 struct hashmap_entry **oldtable = map->table;
111
112 alloc_table(map, newsize);
113 for (i = 0; i < oldsize; i++) {
114 struct hashmap_entry *e = oldtable[i];
115 while (e) {
116 struct hashmap_entry *next = e->next;
117 unsigned int b = bucket(map, e);
118 e->next = map->table[b];
119 map->table[b] = e;
120 e = next;
121 }
122 }
123 free(oldtable);
124 }
125
126 static inline struct hashmap_entry **find_entry_ptr(const struct hashmap *map,
127 const struct hashmap_entry *key, const void *keydata)
128 {
129 struct hashmap_entry **e = &map->table[bucket(map, key)];
130 while (*e && !entry_equals(map, *e, key, keydata))
131 e = &(*e)->next;
132 return e;
133 }
134
135 static int always_equal(const void *unused1, const void *unused2, const void *unused3)
136 {
137 return 0;
138 }
139
140 void hashmap_init(struct hashmap *map, hashmap_cmp_fn equals_function,
141 size_t initial_size)
142 {
143 unsigned int size = HASHMAP_INITIAL_SIZE;
144 map->size = 0;
145 map->cmpfn = equals_function ? equals_function : always_equal;
146
147 /* calculate initial table size and allocate the table */
148 initial_size = (unsigned int) ((uint64_t) initial_size * 100
149 / HASHMAP_LOAD_FACTOR);
150 while (initial_size > size)
151 size <<= HASHMAP_RESIZE_BITS;
152 alloc_table(map, size);
153 }
154
155 void hashmap_free(struct hashmap *map, int free_entries)
156 {
157 if (!map || !map->table)
158 return;
159 if (free_entries) {
160 struct hashmap_iter iter;
161 struct hashmap_entry *e;
162 hashmap_iter_init(map, &iter);
163 while ((e = hashmap_iter_next(&iter)))
164 free(e);
165 }
166 free(map->table);
167 memset(map, 0, sizeof(*map));
168 }
169
170 void *hashmap_get(const struct hashmap *map, const void *key, const void *keydata)
171 {
172 return *find_entry_ptr(map, key, keydata);
173 }
174
175 void *hashmap_get_next(const struct hashmap *map, const void *entry)
176 {
177 struct hashmap_entry *e = ((struct hashmap_entry *) entry)->next;
178 for (; e; e = e->next)
179 if (entry_equals(map, entry, e, NULL))
180 return e;
181 return NULL;
182 }
183
184 void hashmap_add(struct hashmap *map, void *entry)
185 {
186 unsigned int b = bucket(map, entry);
187
188 /* add entry */
189 ((struct hashmap_entry *) entry)->next = map->table[b];
190 map->table[b] = entry;
191
192 /* fix size and rehash if appropriate */
193 map->size++;
194 if (map->size > map->grow_at)
195 rehash(map, map->tablesize << HASHMAP_RESIZE_BITS);
196 }
197
198 void *hashmap_remove(struct hashmap *map, const void *key, const void *keydata)
199 {
200 struct hashmap_entry *old;
201 struct hashmap_entry **e = find_entry_ptr(map, key, keydata);
202 if (!*e)
203 return NULL;
204
205 /* remove existing entry */
206 old = *e;
207 *e = old->next;
208 old->next = NULL;
209
210 /* fix size and rehash if appropriate */
211 map->size--;
212 if (map->size < map->shrink_at)
213 rehash(map, map->tablesize >> HASHMAP_RESIZE_BITS);
214 return old;
215 }
216
217 void *hashmap_put(struct hashmap *map, void *entry)
218 {
219 struct hashmap_entry *old = hashmap_remove(map, entry, NULL);
220 hashmap_add(map, entry);
221 return old;
222 }
223
224 void hashmap_iter_init(struct hashmap *map, struct hashmap_iter *iter)
225 {
226 iter->map = map;
227 iter->tablepos = 0;
228 iter->next = NULL;
229 }
230
231 void *hashmap_iter_next(struct hashmap_iter *iter)
232 {
233 struct hashmap_entry *current = iter->next;
234 for (;;) {
235 if (current) {
236 iter->next = current->next;
237 return current;
238 }
239
240 if (iter->tablepos >= iter->map->tablesize)
241 return NULL;
242
243 current = iter->map->table[iter->tablepos++];
244 }
245 }
246
247 struct pool_entry {
248 struct hashmap_entry ent;
249 size_t len;
250 unsigned char data[FLEX_ARRAY];
251 };
252
253 static int pool_entry_cmp(const struct pool_entry *e1,
254 const struct pool_entry *e2,
255 const unsigned char *keydata)
256 {
257 return e1->data != keydata &&
258 (e1->len != e2->len || memcmp(e1->data, keydata, e1->len));
259 }
260
261 const void *memintern(const void *data, size_t len)
262 {
263 static struct hashmap map;
264 struct pool_entry key, *e;
265
266 /* initialize string pool hashmap */
267 if (!map.tablesize)
268 hashmap_init(&map, (hashmap_cmp_fn) pool_entry_cmp, 0);
269
270 /* lookup interned string in pool */
271 hashmap_entry_init(&key, memhash(data, len));
272 key.len = len;
273 e = hashmap_get(&map, &key, data);
274 if (!e) {
275 /* not found: create it */
276 FLEX_ALLOC_MEM(e, data, data, len);
277 hashmap_entry_init(e, key.ent.hash);
278 e->len = len;
279 hashmap_add(&map, e);
280 }
281 return e->data;
282 }