Coverage Report

Created: 2026-01-10 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/git/hashmap.c
Line
Count
Source
1
/*
2
 * Generic implementation of hash-based key value mappings.
3
 */
4
#include "git-compat-util.h"
5
#include "hashmap.h"
6
7
1.76k
#define FNV32_BASE ((unsigned int) 0x811c9dc5)
8
26.4k
#define FNV32_PRIME ((unsigned int) 0x01000193)
9
10
unsigned int strhash(const char *str)
11
0
{
12
0
  unsigned int c, hash = FNV32_BASE;
13
0
  while ((c = (unsigned char) *str++))
14
0
    hash = (hash * FNV32_PRIME) ^ c;
15
0
  return hash;
16
0
}
17
18
unsigned int strihash(const char *str)
19
0
{
20
0
  unsigned int c, hash = FNV32_BASE;
21
0
  while ((c = (unsigned char) *str++)) {
22
0
    if (c >= 'a' && c <= 'z')
23
0
      c -= 'a' - 'A';
24
0
    hash = (hash * FNV32_PRIME) ^ c;
25
0
  }
26
0
  return hash;
27
0
}
28
29
unsigned int memhash(const void *buf, size_t len)
30
1.76k
{
31
1.76k
  unsigned int hash = FNV32_BASE;
32
1.76k
  unsigned char *ucbuf = (unsigned char *) buf;
33
28.2k
  while (len--) {
34
26.4k
    unsigned int c = *ucbuf++;
35
26.4k
    hash = (hash * FNV32_PRIME) ^ c;
36
26.4k
  }
37
1.76k
  return hash;
38
1.76k
}
39
40
unsigned int memihash(const void *buf, size_t len)
41
0
{
42
0
  unsigned int hash = FNV32_BASE;
43
0
  unsigned char *ucbuf = (unsigned char *) buf;
44
0
  while (len--) {
45
0
    unsigned int c = *ucbuf++;
46
0
    if (c >= 'a' && c <= 'z')
47
0
      c -= 'a' - 'A';
48
0
    hash = (hash * FNV32_PRIME) ^ c;
49
0
  }
50
0
  return hash;
51
0
}
52
53
/*
54
 * Incorporate 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
0
{
59
0
  unsigned int hash = hash_seed;
60
0
  unsigned char *ucbuf = (unsigned char *) buf;
61
0
  while (len--) {
62
0
    unsigned int c = *ucbuf++;
63
0
    if (c >= 'a' && c <= 'z')
64
0
      c -= 'a' - 'A';
65
0
    hash = (hash * FNV32_PRIME) ^ c;
66
0
  }
67
0
  return hash;
68
0
}
69
70
2
#define HASHMAP_INITIAL_SIZE 64
71
/* grow / shrink by 2^2 */
72
0
#define HASHMAP_RESIZE_BITS 2
73
/* load factor in percent */
74
2
#define HASHMAP_LOAD_FACTOR 80
75
76
static void alloc_table(struct hashmap *map, unsigned int size)
77
1
{
78
1
  map->tablesize = size;
79
1
  CALLOC_ARRAY(map->table, size);
80
81
  /* calculate resize thresholds for new size */
82
1
  map->grow_at = (unsigned int) ((uint64_t) size * HASHMAP_LOAD_FACTOR / 100);
83
1
  if (size <= HASHMAP_INITIAL_SIZE)
84
1
    map->shrink_at = 0;
85
0
  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
0
    map->shrink_at = map->grow_at / ((1 << HASHMAP_RESIZE_BITS) + 1);
92
1
}
93
94
static inline int entry_equals(const struct hashmap *map,
95
             const struct hashmap_entry *e1,
96
             const struct hashmap_entry *e2,
97
             const void *keydata)
98
1.76k
{
99
1.76k
  return (e1 == e2) ||
100
1.76k
         (e1->hash == e2->hash &&
101
1.76k
    !map->cmpfn(map->cmpfn_data, e1, e2, keydata));
102
1.76k
}
103
104
static inline unsigned int bucket(const struct hashmap *map,
105
          const struct hashmap_entry *key)
106
1.76k
{
107
1.76k
  return key->hash & (map->tablesize - 1);
108
1.76k
}
109
110
int hashmap_bucket(const struct hashmap *map, unsigned int hash)
111
0
{
112
0
  return hash & (map->tablesize - 1);
113
0
}
114
115
static void rehash(struct hashmap *map, unsigned int newsize)
116
0
{
117
  /* map->table MUST NOT be NULL when this function is called */
118
0
  unsigned int i, oldsize = map->tablesize;
119
0
  struct hashmap_entry **oldtable = map->table;
120
121
0
  alloc_table(map, newsize);
122
0
  for (i = 0; i < oldsize; i++) {
123
0
    struct hashmap_entry *e = oldtable[i];
124
0
    while (e) {
125
0
      struct hashmap_entry *next = e->next;
126
0
      unsigned int b = bucket(map, e);
127
0
      e->next = map->table[b];
128
0
      map->table[b] = e;
129
0
      e = next;
130
0
    }
131
0
  }
132
0
  free(oldtable);
133
0
}
134
135
static inline struct hashmap_entry **find_entry_ptr(const struct hashmap *map,
136
    const struct hashmap_entry *key, const void *keydata)
137
1.76k
{
138
  /* map->table MUST NOT be NULL when this function is called */
139
1.76k
  struct hashmap_entry **e = &map->table[bucket(map, key)];
140
1.76k
  while (*e && !entry_equals(map, *e, key, keydata))
141
0
    e = &(*e)->next;
142
1.76k
  return e;
143
1.76k
}
144
145
static int always_equal(const void *cmp_data UNUSED,
146
      const struct hashmap_entry *entry1 UNUSED,
147
      const struct hashmap_entry *entry2 UNUSED,
148
      const void *keydata UNUSED)
149
0
{
150
0
  return 0;
151
0
}
152
153
void hashmap_init(struct hashmap *map, hashmap_cmp_fn equals_function,
154
      const void *cmpfn_data, size_t initial_size)
155
1
{
156
1
  unsigned int size = HASHMAP_INITIAL_SIZE;
157
158
1
  memset(map, 0, sizeof(*map));
159
160
1
  map->cmpfn = equals_function ? equals_function : always_equal;
161
1
  map->cmpfn_data = cmpfn_data;
162
163
  /* calculate initial table size and allocate the table */
164
1
  initial_size = (unsigned int) ((uint64_t) initial_size * 100
165
1
      / HASHMAP_LOAD_FACTOR);
166
1
  while (initial_size > size)
167
0
    size <<= HASHMAP_RESIZE_BITS;
168
1
  alloc_table(map, size);
169
170
  /*
171
   * Keep track of the number of items in the map and
172
   * allow the map to automatically grow as necessary.
173
   */
174
1
  map->do_count_items = 1;
175
1
}
176
177
static void free_individual_entries(struct hashmap *map, ssize_t entry_offset)
178
0
{
179
0
  struct hashmap_iter iter;
180
0
  struct hashmap_entry *e;
181
182
0
  hashmap_iter_init(map, &iter);
183
0
  while ((e = hashmap_iter_next(&iter)))
184
    /*
185
     * like container_of, but using caller-calculated
186
     * offset (caller being hashmap_clear_and_free)
187
     */
188
0
    free((char *)e - entry_offset);
189
0
}
190
191
void hashmap_partial_clear_(struct hashmap *map, ssize_t entry_offset)
192
0
{
193
0
  if (!map || !map->table)
194
0
    return;
195
0
  if (entry_offset >= 0)  /* called by hashmap_clear_entries */
196
0
    free_individual_entries(map, entry_offset);
197
0
  MEMZERO_ARRAY(map->table, map->tablesize);
198
0
  map->shrink_at = 0;
199
0
  map->private_size = 0;
200
0
}
201
202
void hashmap_clear_(struct hashmap *map, ssize_t entry_offset)
203
0
{
204
0
  if (!map || !map->table)
205
0
    return;
206
0
  if (entry_offset >= 0)  /* called by hashmap_clear_and_free */
207
0
    free_individual_entries(map, entry_offset);
208
0
  FREE_AND_NULL(map->table);
209
0
  map->tablesize = 0;
210
0
  map->private_size = 0;
211
0
}
212
213
struct hashmap_entry *hashmap_get(const struct hashmap *map,
214
        const struct hashmap_entry *key,
215
        const void *keydata)
216
1.76k
{
217
1.76k
  if (!map->table)
218
0
    return NULL;
219
1.76k
  return *find_entry_ptr(map, key, keydata);
220
1.76k
}
221
222
struct hashmap_entry *hashmap_get_next(const struct hashmap *map,
223
               const struct hashmap_entry *entry)
224
0
{
225
0
  struct hashmap_entry *e = entry->next;
226
0
  for (; e; e = e->next)
227
0
    if (entry_equals(map, entry, e, NULL))
228
0
      return e;
229
0
  return NULL;
230
0
}
231
232
void hashmap_add(struct hashmap *map, struct hashmap_entry *entry)
233
1
{
234
1
  unsigned int b;
235
236
1
  if (!map->table)
237
0
    alloc_table(map, HASHMAP_INITIAL_SIZE);
238
239
1
  b = bucket(map, entry);
240
  /* add entry */
241
1
  entry->next = map->table[b];
242
1
  map->table[b] = entry;
243
244
  /* fix size and rehash if appropriate */
245
1
  if (map->do_count_items) {
246
1
    map->private_size++;
247
1
    if (map->private_size > map->grow_at)
248
0
      rehash(map, map->tablesize << HASHMAP_RESIZE_BITS);
249
1
  }
250
1
}
251
252
struct hashmap_entry *hashmap_remove(struct hashmap *map,
253
             const struct hashmap_entry *key,
254
             const void *keydata)
255
0
{
256
0
  struct hashmap_entry *old;
257
0
  struct hashmap_entry **e;
258
259
0
  if (!map->table)
260
0
    return NULL;
261
0
  e = find_entry_ptr(map, key, keydata);
262
0
  if (!*e)
263
0
    return NULL;
264
265
  /* remove existing entry */
266
0
  old = *e;
267
0
  *e = old->next;
268
0
  old->next = NULL;
269
270
  /* fix size and rehash if appropriate */
271
0
  if (map->do_count_items) {
272
0
    map->private_size--;
273
0
    if (map->private_size < map->shrink_at)
274
0
      rehash(map, map->tablesize >> HASHMAP_RESIZE_BITS);
275
0
  }
276
277
0
  return old;
278
0
}
279
280
struct hashmap_entry *hashmap_put(struct hashmap *map,
281
          struct hashmap_entry *entry)
282
0
{
283
0
  struct hashmap_entry *old = hashmap_remove(map, entry, NULL);
284
0
  hashmap_add(map, entry);
285
0
  return old;
286
0
}
287
288
void hashmap_iter_init(struct hashmap *map, struct hashmap_iter *iter)
289
0
{
290
0
  iter->map = map;
291
0
  iter->tablepos = 0;
292
0
  iter->next = NULL;
293
0
}
294
295
struct hashmap_entry *hashmap_iter_next(struct hashmap_iter *iter)
296
0
{
297
0
  struct hashmap_entry *current = iter->next;
298
0
  for (;;) {
299
0
    if (current) {
300
0
      iter->next = current->next;
301
0
      return current;
302
0
    }
303
304
0
    if (iter->tablepos >= iter->map->tablesize)
305
0
      return NULL;
306
307
0
    current = iter->map->table[iter->tablepos++];
308
0
  }
309
0
}
310
311
struct pool_entry {
312
  struct hashmap_entry ent;
313
  size_t len;
314
  unsigned char data[FLEX_ARRAY];
315
};
316
317
static int pool_entry_cmp(const void *cmp_data UNUSED,
318
        const struct hashmap_entry *eptr,
319
        const struct hashmap_entry *entry_or_key,
320
        const void *keydata)
321
1.76k
{
322
1.76k
  const struct pool_entry *e1, *e2;
323
324
1.76k
  e1 = container_of(eptr, const struct pool_entry, ent);
325
1.76k
  e2 = container_of(entry_or_key, const struct pool_entry, ent);
326
327
1.76k
  return e1->data != keydata &&
328
1.76k
         (e1->len != e2->len || memcmp(e1->data, keydata, e1->len));
329
1.76k
}
330
331
const void *memintern(const void *data, size_t len)
332
1.76k
{
333
1.76k
  static struct hashmap map;
334
1.76k
  struct pool_entry key, *e;
335
336
  /* initialize string pool hashmap */
337
1.76k
  if (!map.tablesize)
338
1
    hashmap_init(&map, pool_entry_cmp, NULL, 0);
339
340
  /* lookup interned string in pool */
341
1.76k
  hashmap_entry_init(&key.ent, memhash(data, len));
342
1.76k
  key.len = len;
343
1.76k
  e = hashmap_get_entry(&map, &key, ent, data);
344
1.76k
  if (!e) {
345
    /* not found: create it */
346
1
    FLEX_ALLOC_MEM(e, data, data, len);
347
1
    hashmap_entry_init(&e->ent, key.ent.hash);
348
1
    e->len = len;
349
1
    hashmap_add(&map, &e->ent);
350
1
  }
351
1.76k
  return e->data;
352
1.76k
}