Sync with 2.33.8
[git/debian.git] / oidmap.c
blob286a04a53c20119fe567eec4accbaba4d9f04a97
1 #include "cache.h"
2 #include "oidmap.h"
4 static int oidmap_neq(const void *hashmap_cmp_fn_data,
5 const struct hashmap_entry *e1,
6 const struct hashmap_entry *e2,
7 const void *keydata)
9 const struct oidmap_entry *a, *b;
11 a = container_of(e1, const struct oidmap_entry, internal_entry);
12 b = container_of(e2, const struct oidmap_entry, internal_entry);
14 if (keydata)
15 return !oideq(&a->oid, (const struct object_id *) keydata);
16 return !oideq(&a->oid, &b->oid);
19 void oidmap_init(struct oidmap *map, size_t initial_size)
21 hashmap_init(&map->map, oidmap_neq, NULL, initial_size);
24 void oidmap_free(struct oidmap *map, int free_entries)
26 if (!map)
27 return;
29 /* TODO: make oidmap itself not depend on struct layouts */
30 hashmap_clear_(&map->map, free_entries ? 0 : -1);
33 void *oidmap_get(const struct oidmap *map, const struct object_id *key)
35 if (!map->map.cmpfn)
36 return NULL;
38 return hashmap_get_from_hash(&map->map, oidhash(key), key);
41 void *oidmap_remove(struct oidmap *map, const struct object_id *key)
43 struct hashmap_entry entry;
45 if (!map->map.cmpfn)
46 oidmap_init(map, 0);
48 hashmap_entry_init(&entry, oidhash(key));
49 return hashmap_remove(&map->map, &entry, key);
52 void *oidmap_put(struct oidmap *map, void *entry)
54 struct oidmap_entry *to_put = entry;
56 if (!map->map.cmpfn)
57 oidmap_init(map, 0);
59 hashmap_entry_init(&to_put->internal_entry, oidhash(&to_put->oid));
60 return hashmap_put(&map->map, &to_put->internal_entry);