Coverage Report

Created: 2026-01-15 06:28

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
11.1k
#define FNV32_BASE ((unsigned int) 0x811c9dc5)
8
67.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
11.1k
{
31
11.1k
  unsigned int hash = FNV32_BASE;
32
11.1k
  unsigned char *ucbuf = (unsigned char *) buf;
33
78.6k
  while (len--) {
34
67.4k
    unsigned int c = *ucbuf++;
35
67.4k
    hash = (hash * FNV32_PRIME) ^ c;
36
67.4k
  }
37
11.1k
  return hash;
38
11.1k
}
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
250
#define HASHMAP_INITIAL_SIZE 64
71
/* grow / shrink by 2^2 */
72
4
#define HASHMAP_RESIZE_BITS 2
73
/* load factor in percent */
74
249
#define HASHMAP_LOAD_FACTOR 80
75
76
static void alloc_table(struct hashmap *map, unsigned int size)
77
126
{
78
126
  map->tablesize = size;
79
126
  CALLOC_ARRAY(map->table, size);
80
81
  /* calculate resize thresholds for new size */
82
126
  map->grow_at = (unsigned int) ((uint64_t) size * HASHMAP_LOAD_FACTOR / 100);
83
126
  if (size <= HASHMAP_INITIAL_SIZE)
84
124
    map->shrink_at = 0;
85
2
  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
2
    map->shrink_at = map->grow_at / ((1 << HASHMAP_RESIZE_BITS) + 1);
92
126
}
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
15.9k
{
99
15.9k
  return (e1 == e2) ||
100
15.9k
         (e1->hash == e2->hash &&
101
10.9k
    !map->cmpfn(map->cmpfn_data, e1, e2, keydata));
102
15.9k
}
103
104
static inline unsigned int bucket(const struct hashmap *map,
105
          const struct hashmap_entry *key)
106
11.4k
{
107
11.4k
  return key->hash & (map->tablesize - 1);
108
11.4k
}
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
2
{
117
  /* map->table MUST NOT be NULL when this function is called */
118
2
  unsigned int i, oldsize = map->tablesize;
119
2
  struct hashmap_entry **oldtable = map->table;
120
121
2
  alloc_table(map, newsize);
122
322
  for (i = 0; i < oldsize; i++) {
123
320
    struct hashmap_entry *e = oldtable[i];
124
577
    while (e) {
125
257
      struct hashmap_entry *next = e->next;
126
257
      unsigned int b = bucket(map, e);
127
257
      e->next = map->table[b];
128
257
      map->table[b] = e;
129
257
      e = next;
130
257
    }
131
320
  }
132
2
  free(oldtable);
133
2
}
134
135
static inline struct hashmap_entry **find_entry_ptr(const struct hashmap *map,
136
    const struct hashmap_entry *key, const void *keydata)
137
10.7k
{
138
  /* map->table MUST NOT be NULL when this function is called */
139
10.7k
  struct hashmap_entry **e = &map->table[bucket(map, key)];
140
16.3k
  while (*e && !entry_equals(map, *e, key, keydata))
141
5.55k
    e = &(*e)->next;
142
10.7k
  return e;
143
10.7k
}
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
123
{
156
123
  unsigned int size = HASHMAP_INITIAL_SIZE;
157
158
123
  memset(map, 0, sizeof(*map));
159
160
123
  map->cmpfn = equals_function ? equals_function : always_equal;
161
123
  map->cmpfn_data = cmpfn_data;
162
163
  /* calculate initial table size and allocate the table */
164
123
  initial_size = (unsigned int) ((uint64_t) initial_size * 100
165
123
      / HASHMAP_LOAD_FACTOR);
166
123
  while (initial_size > size)
167
0
    size <<= HASHMAP_RESIZE_BITS;
168
123
  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
123
  map->do_count_items = 1;
175
123
}
176
177
static void free_individual_entries(struct hashmap *map, ssize_t entry_offset)
178
61
{
179
61
  struct hashmap_iter iter;
180
61
  struct hashmap_entry *e;
181
182
61
  hashmap_iter_init(map, &iter);
183
61
  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
61
}
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
244
{
204
244
  if (!map || !map->table)
205
122
    return;
206
122
  if (entry_offset >= 0)  /* called by hashmap_clear_and_free */
207
61
    free_individual_entries(map, entry_offset);
208
122
  FREE_AND_NULL(map->table);
209
122
  map->tablesize = 0;
210
122
  map->private_size = 0;
211
122
}
212
213
struct hashmap_entry *hashmap_get(const struct hashmap *map,
214
        const struct hashmap_entry *key,
215
        const void *keydata)
216
10.7k
{
217
10.7k
  if (!map->table)
218
1
    return NULL;
219
10.7k
  return *find_entry_ptr(map, key, keydata);
220
10.7k
}
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
369
{
234
369
  unsigned int b;
235
236
369
  if (!map->table)
237
1
    alloc_table(map, HASHMAP_INITIAL_SIZE);
238
239
369
  b = bucket(map, entry);
240
  /* add entry */
241
369
  entry->next = map->table[b];
242
369
  map->table[b] = entry;
243
244
  /* fix size and rehash if appropriate */
245
369
  if (map->do_count_items) {
246
369
    map->private_size++;
247
369
    if (map->private_size > map->grow_at)
248
2
      rehash(map, map->tablesize << HASHMAP_RESIZE_BITS);
249
369
  }
250
369
}
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
366
{
290
366
  iter->map = map;
291
366
  iter->tablepos = 0;
292
366
  iter->next = NULL;
293
366
}
294
295
struct hashmap_entry *hashmap_iter_next(struct hashmap_iter *iter)
296
366
{
297
366
  struct hashmap_entry *current = iter->next;
298
8.17k
  for (;;) {
299
8.17k
    if (current) {
300
0
      iter->next = current->next;
301
0
      return current;
302
0
    }
303
304
8.17k
    if (iter->tablepos >= iter->map->tablesize)
305
366
      return NULL;
306
307
7.80k
    current = iter->map->table[iter->tablepos++];
308
7.80k
  }
309
366
}
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.72k
{
322
1.72k
  const struct pool_entry *e1, *e2;
323
324
1.72k
  e1 = container_of(eptr, const struct pool_entry, ent);
325
1.72k
  e2 = container_of(entry_or_key, const struct pool_entry, ent);
326
327
1.72k
  return e1->data != keydata &&
328
1.72k
         (e1->len != e2->len || memcmp(e1->data, keydata, e1->len));
329
1.72k
}
330
331
const void *memintern(const void *data, size_t len)
332
1.73k
{
333
1.73k
  static struct hashmap map;
334
1.73k
  struct pool_entry key, *e;
335
336
  /* initialize string pool hashmap */
337
1.73k
  if (!map.tablesize)
338
1
    hashmap_init(&map, pool_entry_cmp, NULL, 0);
339
340
  /* lookup interned string in pool */
341
1.73k
  hashmap_entry_init(&key.ent, memhash(data, len));
342
1.73k
  key.len = len;
343
1.73k
  e = hashmap_get_entry(&map, &key, ent, data);
344
1.73k
  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.73k
  return e->data;
352
1.73k
}