git
/
strmap.c
179 строк · 4.3 Кб
1#include "git-compat-util.h"2#include "strmap.h"3#include "mem-pool.h"4
5int cmp_strmap_entry(const void *hashmap_cmp_fn_data UNUSED,6const struct hashmap_entry *entry1,7const struct hashmap_entry *entry2,8const void *keydata UNUSED)9{
10const struct strmap_entry *e1, *e2;11
12e1 = container_of(entry1, const struct strmap_entry, ent);13e2 = container_of(entry2, const struct strmap_entry, ent);14return strcmp(e1->key, e2->key);15}
16
17static struct strmap_entry *find_strmap_entry(struct strmap *map,18const char *str)19{
20struct strmap_entry entry;21hashmap_entry_init(&entry.ent, strhash(str));22entry.key = str;23return hashmap_get_entry(&map->map, &entry, ent, NULL);24}
25
26void strmap_init(struct strmap *map)27{
28struct strmap blank = STRMAP_INIT;29memcpy(map, &blank, sizeof(*map));30}
31
32void strmap_init_with_options(struct strmap *map,33struct mem_pool *pool,34int strdup_strings)35{
36hashmap_init(&map->map, cmp_strmap_entry, NULL, 0);37map->pool = pool;38map->strdup_strings = strdup_strings;39}
40
41static void strmap_free_entries_(struct strmap *map, int free_values)42{
43struct hashmap_iter iter;44struct strmap_entry *e;45
46if (!map)47return;48
49if (!free_values && map->pool)50/* Memory other than util is owned by and freed with the pool */51return;52
53/*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*/
60hashmap_for_each_entry(&map->map, &iter, e, ent) {61if (free_values)62free(e->value);63if (!map->pool)64free(e);65}66}
67
68void strmap_clear(struct strmap *map, int free_values)69{
70strmap_free_entries_(map, free_values);71hashmap_clear(&map->map);72}
73
74void strmap_partial_clear(struct strmap *map, int free_values)75{
76strmap_free_entries_(map, free_values);77hashmap_partial_clear(&map->map);78}
79
80static struct strmap_entry *create_entry(struct strmap *map,81const char *str,82void *data)83{
84struct strmap_entry *entry;85
86if (map->strdup_strings) {87if (!map->pool) {88FLEXPTR_ALLOC_STR(entry, key, str);89} else {90size_t len = st_add(strlen(str), 1); /* include NUL */91entry = mem_pool_alloc(map->pool,92st_add(sizeof(*entry), len));93memcpy(entry + 1, str, len);94entry->key = (void *)(entry + 1);95}96} else if (!map->pool) {97entry = xmalloc(sizeof(*entry));98} else {99entry = mem_pool_alloc(map->pool, sizeof(*entry));100}101hashmap_entry_init(&entry->ent, strhash(str));102if (!map->strdup_strings)103entry->key = str;104entry->value = data;105return entry;106}
107
108void *strmap_put(struct strmap *map, const char *str, void *data)109{
110struct strmap_entry *entry = find_strmap_entry(map, str);111
112if (entry) {113void *old = entry->value;114entry->value = data;115return old;116}117
118entry = create_entry(map, str, data);119hashmap_add(&map->map, &entry->ent);120return NULL;121}
122
123struct strmap_entry *strmap_get_entry(struct strmap *map, const char *str)124{
125return find_strmap_entry(map, str);126}
127
128void *strmap_get(struct strmap *map, const char *str)129{
130struct strmap_entry *entry = find_strmap_entry(map, str);131return entry ? entry->value : NULL;132}
133
134int strmap_contains(struct strmap *map, const char *str)135{
136return find_strmap_entry(map, str) != NULL;137}
138
139void strmap_remove(struct strmap *map, const char *str, int free_value)140{
141struct strmap_entry entry, *ret;142hashmap_entry_init(&entry.ent, strhash(str));143entry.key = str;144ret = hashmap_remove_entry(&map->map, &entry, ent, NULL);145if (!ret)146return;147if (free_value)148free(ret->value);149if (!map->pool)150free(ret);151}
152
153void strintmap_incr(struct strintmap *map, const char *str, intptr_t amt)154{
155struct strmap_entry *entry = find_strmap_entry(&map->map, str);156if (entry) {157intptr_t *whence = (intptr_t*)&entry->value;158*whence += amt;159}160else161strintmap_set(map, str, map->default_value + amt);162}
163
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*/
171struct strmap_entry *entry = find_strmap_entry(&set->map, str);172
173if (entry)174return 0;175
176entry = create_entry(&set->map, str, NULL);177hashmap_add(&set->map.map, &entry->ent);178return 1;179}
180