]> git.ipfire.org Git - thirdparty/git.git/blame - oidmap.c
hashmap: introduce hashmap_free_entries
[thirdparty/git.git] / oidmap.c
CommitLineData
9e6fabde
JT
1#include "cache.h"
2#include "oidmap.h"
3
cc00e5ce 4static int oidmap_neq(const void *hashmap_cmp_fn_data,
939af16e
EW
5 const struct hashmap_entry *e1,
6 const struct hashmap_entry *e2,
cc00e5ce 7 const void *keydata)
9e6fabde 8{
939af16e
EW
9 const struct oidmap_entry *a, *b;
10
11 a = container_of(e1, const struct oidmap_entry, internal_entry);
12 b = container_of(e2, const struct oidmap_entry, internal_entry);
13
9e6fabde 14 if (keydata)
939af16e
EW
15 return !oideq(&a->oid, (const struct object_id *) keydata);
16 return !oideq(&a->oid, &b->oid);
9e6fabde
JT
17}
18
9e6fabde
JT
19void oidmap_init(struct oidmap *map, size_t initial_size)
20{
cc00e5ce 21 hashmap_init(&map->map, oidmap_neq, NULL, initial_size);
9e6fabde
JT
22}
23
24void oidmap_free(struct oidmap *map, int free_entries)
25{
26 if (!map)
27 return;
c8e424c9
EW
28
29 /* TODO: make oidmap itself not depend on struct layouts */
30 hashmap_free_(&map->map, free_entries ? 0 : -1);
9e6fabde
JT
31}
32
33void *oidmap_get(const struct oidmap *map, const struct object_id *key)
34{
e2a5a028
BW
35 if (!map->map.cmpfn)
36 return NULL;
37
c62bff2c 38 return hashmap_get_from_hash(&map->map, oidhash(key), key);
9e6fabde
JT
39}
40
41void *oidmap_remove(struct oidmap *map, const struct object_id *key)
42{
43 struct hashmap_entry entry;
e2a5a028
BW
44
45 if (!map->map.cmpfn)
46 oidmap_init(map, 0);
47
c62bff2c 48 hashmap_entry_init(&entry, oidhash(key));
9e6fabde
JT
49 return hashmap_remove(&map->map, &entry, key);
50}
51
52void *oidmap_put(struct oidmap *map, void *entry)
53{
54 struct oidmap_entry *to_put = entry;
e2a5a028
BW
55
56 if (!map->map.cmpfn)
57 oidmap_init(map, 0);
58
c62bff2c 59 hashmap_entry_init(&to_put->internal_entry, oidhash(&to_put->oid));
26b455f2 60 return hashmap_put(&map->map, &to_put->internal_entry);
9e6fabde 61}