blob: ee48635708219e66bf03bad6f5e9100c766d2fb7 [file] [log] [blame]
Elijah Newrenae20bf12020-11-02 18:55:06 +00001#include "git-compat-util.h"
2#include "strmap.h"
Elijah Newrena208ec12020-11-11 20:02:18 +00003#include "mem-pool.h"
Elijah Newrenae20bf12020-11-02 18:55:06 +00004
5int cmp_strmap_entry(const void *hashmap_cmp_fn_data,
6 const struct hashmap_entry *entry1,
7 const struct hashmap_entry *entry2,
8 const void *keydata)
9{
10 const struct strmap_entry *e1, *e2;
11
12 e1 = container_of(entry1, const struct strmap_entry, ent);
13 e2 = container_of(entry2, const struct strmap_entry, ent);
14 return strcmp(e1->key, e2->key);
15}
16
17static struct strmap_entry *find_strmap_entry(struct strmap *map,
18 const char *str)
19{
20 struct strmap_entry entry;
21 hashmap_entry_init(&entry.ent, strhash(str));
22 entry.key = str;
23 return hashmap_get_entry(&map->map, &entry, ent, NULL);
24}
25
26void strmap_init(struct strmap *map)
27{
Ævar Arnfjörð Bjarmason5726a6b2021-07-01 12:51:26 +020028 struct strmap blank = STRMAP_INIT;
29 memcpy(map, &blank, sizeof(*map));
Elijah Newrenae20bf12020-11-02 18:55:06 +000030}
31
32void strmap_init_with_options(struct strmap *map,
Elijah Newrena208ec12020-11-11 20:02:18 +000033 struct mem_pool *pool,
Elijah Newrenae20bf12020-11-02 18:55:06 +000034 int strdup_strings)
35{
36 hashmap_init(&map->map, cmp_strmap_entry, NULL, 0);
Elijah Newrena208ec12020-11-11 20:02:18 +000037 map->pool = pool;
Elijah Newrenae20bf12020-11-02 18:55:06 +000038 map->strdup_strings = strdup_strings;
39}
40
41static void strmap_free_entries_(struct strmap *map, int free_values)
42{
43 struct hashmap_iter iter;
44 struct strmap_entry *e;
45
46 if (!map)
47 return;
48
Elijah Newrena208ec12020-11-11 20:02:18 +000049 if (!free_values && map->pool)
50 /* Memory other than util is owned by and freed with the pool */
51 return;
52
Elijah Newrenae20bf12020-11-02 18:55:06 +000053 /*
54 * We need to iterate over the hashmap entries and free
55 * e->key and e->value ourselves; hashmap has no API to
56 * take care of that for us. Since we're already iterating over
57 * the hashmap, though, might as well free e too and avoid the need
58 * to make some call into the hashmap API to do that.
59 */
60 hashmap_for_each_entry(&map->map, &iter, e, ent) {
61 if (free_values)
62 free(e->value);
Elijah Newren23a276a2020-11-11 20:02:19 +000063 if (!map->pool)
Elijah Newrena208ec12020-11-11 20:02:18 +000064 free(e);
Elijah Newrenae20bf12020-11-02 18:55:06 +000065 }
66}
67
68void strmap_clear(struct strmap *map, int free_values)
69{
70 strmap_free_entries_(map, free_values);
71 hashmap_clear(&map->map);
72}
73
Elijah Newren6ccdfc22020-11-05 00:22:40 +000074void strmap_partial_clear(struct strmap *map, int free_values)
75{
76 strmap_free_entries_(map, free_values);
77 hashmap_partial_clear(&map->map);
78}
79
Elijah Newren6abd2202020-11-06 00:24:53 +000080static struct strmap_entry *create_entry(struct strmap *map,
81 const char *str,
82 void *data)
83{
84 struct strmap_entry *entry;
Elijah Newren6abd2202020-11-06 00:24:53 +000085
Elijah Newren23a276a2020-11-11 20:02:19 +000086 if (map->strdup_strings) {
87 if (!map->pool) {
88 FLEXPTR_ALLOC_STR(entry, key, str);
89 } else {
90 size_t len = st_add(strlen(str), 1); /* include NUL */
91 entry = mem_pool_alloc(map->pool,
92 st_add(sizeof(*entry), len));
93 memcpy(entry + 1, str, len);
94 entry->key = (void *)(entry + 1);
95 }
96 } else if (!map->pool) {
97 entry = xmalloc(sizeof(*entry));
98 } else {
99 entry = mem_pool_alloc(map->pool, sizeof(*entry));
100 }
Elijah Newren6abd2202020-11-06 00:24:53 +0000101 hashmap_entry_init(&entry->ent, strhash(str));
Elijah Newren23a276a2020-11-11 20:02:19 +0000102 if (!map->strdup_strings)
103 entry->key = str;
Elijah Newren6abd2202020-11-06 00:24:53 +0000104 entry->value = data;
105 return entry;
106}
107
Elijah Newrenae20bf12020-11-02 18:55:06 +0000108void *strmap_put(struct strmap *map, const char *str, void *data)
109{
110 struct strmap_entry *entry = find_strmap_entry(map, str);
Elijah Newrenae20bf12020-11-02 18:55:06 +0000111
112 if (entry) {
Elijah Newren6abd2202020-11-06 00:24:53 +0000113 void *old = entry->value;
Elijah Newrenae20bf12020-11-02 18:55:06 +0000114 entry->value = data;
Elijah Newren6abd2202020-11-06 00:24:53 +0000115 return old;
Elijah Newrenae20bf12020-11-02 18:55:06 +0000116 }
Elijah Newren6abd2202020-11-06 00:24:53 +0000117
118 entry = create_entry(map, str, data);
119 hashmap_add(&map->map, &entry->ent);
120 return NULL;
Elijah Newrenae20bf12020-11-02 18:55:06 +0000121}
122
Elijah Newrenb70c82e2020-11-05 00:22:39 +0000123struct strmap_entry *strmap_get_entry(struct strmap *map, const char *str)
124{
125 return find_strmap_entry(map, str);
126}
127
Elijah Newrenae20bf12020-11-02 18:55:06 +0000128void *strmap_get(struct strmap *map, const char *str)
129{
130 struct strmap_entry *entry = find_strmap_entry(map, str);
131 return entry ? entry->value : NULL;
132}
133
134int strmap_contains(struct strmap *map, const char *str)
135{
136 return find_strmap_entry(map, str) != NULL;
137}
Elijah Newrenb70c82e2020-11-05 00:22:39 +0000138
139void strmap_remove(struct strmap *map, const char *str, int free_value)
140{
141 struct strmap_entry entry, *ret;
142 hashmap_entry_init(&entry.ent, strhash(str));
143 entry.key = str;
144 ret = hashmap_remove_entry(&map->map, &entry, ent, NULL);
145 if (!ret)
146 return;
147 if (free_value)
148 free(ret->value);
Elijah Newren23a276a2020-11-11 20:02:19 +0000149 if (!map->pool)
Elijah Newrena208ec12020-11-11 20:02:18 +0000150 free(ret);
Elijah Newrenb70c82e2020-11-05 00:22:39 +0000151}
Elijah Newren4fa1d502020-11-05 00:22:41 +0000152
153void strintmap_incr(struct strintmap *map, const char *str, intptr_t amt)
154{
155 struct strmap_entry *entry = find_strmap_entry(&map->map, str);
156 if (entry) {
157 intptr_t *whence = (intptr_t*)&entry->value;
158 *whence += amt;
159 }
160 else
161 strintmap_set(map, str, map->default_value + amt);
162}
Elijah Newren1201eb62020-11-06 00:24:54 +0000163
164int strset_add(struct strset *set, const char *str)
165{
166 /*
167 * Cannot use strmap_put() because it'll return NULL in both cases:
168 * - cannot find str: NULL means "not found"
169 * - does find str: NULL is the value associated with str
170 */
171 struct strmap_entry *entry = find_strmap_entry(&set->map, str);
172
173 if (entry)
174 return 0;
175
176 entry = create_entry(&set->map, str, NULL);
177 hashmap_add(&set->map.map, &entry->ent);
178 return 1;
179}