Coverage Report

Created: 2024-09-16 06:10

/src/git/oidmap.c
Line
Count
Source (jump to first uncovered line)
1
#include "git-compat-util.h"
2
#include "hash.h"
3
#include "oidmap.h"
4
5
static int oidmap_neq(const void *hashmap_cmp_fn_data UNUSED,
6
          const struct hashmap_entry *e1,
7
          const struct hashmap_entry *e2,
8
          const void *keydata)
9
0
{
10
0
  const struct oidmap_entry *a, *b;
11
12
0
  a = container_of(e1, const struct oidmap_entry, internal_entry);
13
0
  b = container_of(e2, const struct oidmap_entry, internal_entry);
14
15
0
  if (keydata)
16
0
    return !oideq(&a->oid, (const struct object_id *) keydata);
17
0
  return !oideq(&a->oid, &b->oid);
18
0
}
19
20
void oidmap_init(struct oidmap *map, size_t initial_size)
21
0
{
22
0
  hashmap_init(&map->map, oidmap_neq, NULL, initial_size);
23
0
}
24
25
void oidmap_free(struct oidmap *map, int free_entries)
26
0
{
27
0
  if (!map)
28
0
    return;
29
30
  /* TODO: make oidmap itself not depend on struct layouts */
31
0
  hashmap_clear_(&map->map, free_entries ? 0 : -1);
32
0
}
33
34
void *oidmap_get(const struct oidmap *map, const struct object_id *key)
35
0
{
36
0
  if (!map->map.cmpfn)
37
0
    return NULL;
38
39
0
  return hashmap_get_from_hash(&map->map, oidhash(key), key);
40
0
}
41
42
void *oidmap_remove(struct oidmap *map, const struct object_id *key)
43
0
{
44
0
  struct hashmap_entry entry;
45
46
0
  if (!map->map.cmpfn)
47
0
    oidmap_init(map, 0);
48
49
0
  hashmap_entry_init(&entry, oidhash(key));
50
0
  return hashmap_remove(&map->map, &entry, key);
51
0
}
52
53
void *oidmap_put(struct oidmap *map, void *entry)
54
0
{
55
0
  struct oidmap_entry *to_put = entry;
56
57
0
  if (!map->map.cmpfn)
58
0
    oidmap_init(map, 0);
59
60
0
  hashmap_entry_init(&to_put->internal_entry, oidhash(&to_put->oid));
61
0
  return hashmap_put(&map->map, &to_put->internal_entry);
62
0
}