]> git.ipfire.org Git - thirdparty/git.git/blame - strmap.c
strmap: enable allocations to come from a mem_pool
[thirdparty/git.git] / strmap.c
CommitLineData
ae20bf1a
EN
1#include "git-compat-util.h"
2#include "strmap.h"
a208ec1f 3#include "mem-pool.h"
ae20bf1a
EN
4
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{
a208ec1f 28 strmap_init_with_options(map, NULL, 1);
ae20bf1a
EN
29}
30
31void strmap_init_with_options(struct strmap *map,
a208ec1f 32 struct mem_pool *pool,
ae20bf1a
EN
33 int strdup_strings)
34{
35 hashmap_init(&map->map, cmp_strmap_entry, NULL, 0);
a208ec1f 36 map->pool = pool;
ae20bf1a
EN
37 map->strdup_strings = strdup_strings;
38}
39
40static void strmap_free_entries_(struct strmap *map, int free_values)
41{
42 struct hashmap_iter iter;
43 struct strmap_entry *e;
44
45 if (!map)
46 return;
47
a208ec1f
EN
48 if (!free_values && map->pool)
49 /* Memory other than util is owned by and freed with the pool */
50 return;
51
ae20bf1a
EN
52 /*
53 * We need to iterate over the hashmap entries and free
54 * e->key and e->value ourselves; hashmap has no API to
55 * take care of that for us. Since we're already iterating over
56 * the hashmap, though, might as well free e too and avoid the need
57 * to make some call into the hashmap API to do that.
58 */
59 hashmap_for_each_entry(&map->map, &iter, e, ent) {
60 if (free_values)
61 free(e->value);
a208ec1f
EN
62 if (!map->pool) {
63 if (map->strdup_strings)
64 free((char*)e->key);
65 free(e);
66 }
ae20bf1a
EN
67 }
68}
69
70void strmap_clear(struct strmap *map, int free_values)
71{
72 strmap_free_entries_(map, free_values);
73 hashmap_clear(&map->map);
74}
75
6ccdfc2a
EN
76void strmap_partial_clear(struct strmap *map, int free_values)
77{
78 strmap_free_entries_(map, free_values);
79 hashmap_partial_clear(&map->map);
80}
81
6abd2206
EN
82static struct strmap_entry *create_entry(struct strmap *map,
83 const char *str,
84 void *data)
85{
86 struct strmap_entry *entry;
87 const char *key = str;
88
a208ec1f
EN
89 entry = map->pool ? mem_pool_alloc(map->pool, sizeof(*entry))
90 : xmalloc(sizeof(*entry));
6abd2206
EN
91 hashmap_entry_init(&entry->ent, strhash(str));
92
93 if (map->strdup_strings)
a208ec1f
EN
94 key = map->pool ? mem_pool_strdup(map->pool, str)
95 : xstrdup(str);
6abd2206
EN
96 entry->key = key;
97 entry->value = data;
98 return entry;
99}
100
ae20bf1a
EN
101void *strmap_put(struct strmap *map, const char *str, void *data)
102{
103 struct strmap_entry *entry = find_strmap_entry(map, str);
ae20bf1a
EN
104
105 if (entry) {
6abd2206 106 void *old = entry->value;
ae20bf1a 107 entry->value = data;
6abd2206 108 return old;
ae20bf1a 109 }
6abd2206
EN
110
111 entry = create_entry(map, str, data);
112 hashmap_add(&map->map, &entry->ent);
113 return NULL;
ae20bf1a
EN
114}
115
b70c82e6
EN
116struct strmap_entry *strmap_get_entry(struct strmap *map, const char *str)
117{
118 return find_strmap_entry(map, str);
119}
120
ae20bf1a
EN
121void *strmap_get(struct strmap *map, const char *str)
122{
123 struct strmap_entry *entry = find_strmap_entry(map, str);
124 return entry ? entry->value : NULL;
125}
126
127int strmap_contains(struct strmap *map, const char *str)
128{
129 return find_strmap_entry(map, str) != NULL;
130}
b70c82e6
EN
131
132void strmap_remove(struct strmap *map, const char *str, int free_value)
133{
134 struct strmap_entry entry, *ret;
135 hashmap_entry_init(&entry.ent, strhash(str));
136 entry.key = str;
137 ret = hashmap_remove_entry(&map->map, &entry, ent, NULL);
138 if (!ret)
139 return;
140 if (free_value)
141 free(ret->value);
a208ec1f
EN
142 if (!map->pool) {
143 if (map->strdup_strings)
144 free((char*)ret->key);
145 free(ret);
146 }
b70c82e6 147}
4fa1d501
EN
148
149void strintmap_incr(struct strintmap *map, const char *str, intptr_t amt)
150{
151 struct strmap_entry *entry = find_strmap_entry(&map->map, str);
152 if (entry) {
153 intptr_t *whence = (intptr_t*)&entry->value;
154 *whence += amt;
155 }
156 else
157 strintmap_set(map, str, map->default_value + amt);
158}
1201eb62
EN
159
160int strset_add(struct strset *set, const char *str)
161{
162 /*
163 * Cannot use strmap_put() because it'll return NULL in both cases:
164 * - cannot find str: NULL means "not found"
165 * - does find str: NULL is the value associated with str
166 */
167 struct strmap_entry *entry = find_strmap_entry(&set->map, str);
168
169 if (entry)
170 return 0;
171
172 entry = create_entry(&set->map, str, NULL);
173 hashmap_add(&set->map.map, &entry->ent);
174 return 1;
175}