Elijah Newren | ae20bf1 | 2020-11-02 18:55:06 +0000 | [diff] [blame] | 1 | #include "git-compat-util.h" |
| 2 | #include "strmap.h" |
Elijah Newren | a208ec1 | 2020-11-11 20:02:18 +0000 | [diff] [blame] | 3 | #include "mem-pool.h" |
Elijah Newren | ae20bf1 | 2020-11-02 18:55:06 +0000 | [diff] [blame] | 4 | |
| 5 | int 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 | |
| 17 | static 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 | |
| 26 | void strmap_init(struct strmap *map) |
| 27 | { |
Elijah Newren | a208ec1 | 2020-11-11 20:02:18 +0000 | [diff] [blame] | 28 | strmap_init_with_options(map, NULL, 1); |
Elijah Newren | ae20bf1 | 2020-11-02 18:55:06 +0000 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | void strmap_init_with_options(struct strmap *map, |
Elijah Newren | a208ec1 | 2020-11-11 20:02:18 +0000 | [diff] [blame] | 32 | struct mem_pool *pool, |
Elijah Newren | ae20bf1 | 2020-11-02 18:55:06 +0000 | [diff] [blame] | 33 | int strdup_strings) |
| 34 | { |
| 35 | hashmap_init(&map->map, cmp_strmap_entry, NULL, 0); |
Elijah Newren | a208ec1 | 2020-11-11 20:02:18 +0000 | [diff] [blame] | 36 | map->pool = pool; |
Elijah Newren | ae20bf1 | 2020-11-02 18:55:06 +0000 | [diff] [blame] | 37 | map->strdup_strings = strdup_strings; |
| 38 | } |
| 39 | |
| 40 | static 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 | |
Elijah Newren | a208ec1 | 2020-11-11 20:02:18 +0000 | [diff] [blame] | 48 | if (!free_values && map->pool) |
| 49 | /* Memory other than util is owned by and freed with the pool */ |
| 50 | return; |
| 51 | |
Elijah Newren | ae20bf1 | 2020-11-02 18:55:06 +0000 | [diff] [blame] | 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); |
Elijah Newren | 23a276a | 2020-11-11 20:02:19 +0000 | [diff] [blame] | 62 | if (!map->pool) |
Elijah Newren | a208ec1 | 2020-11-11 20:02:18 +0000 | [diff] [blame] | 63 | free(e); |
Elijah Newren | ae20bf1 | 2020-11-02 18:55:06 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
| 67 | void strmap_clear(struct strmap *map, int free_values) |
| 68 | { |
| 69 | strmap_free_entries_(map, free_values); |
| 70 | hashmap_clear(&map->map); |
| 71 | } |
| 72 | |
Elijah Newren | 6ccdfc2 | 2020-11-05 00:22:40 +0000 | [diff] [blame] | 73 | void strmap_partial_clear(struct strmap *map, int free_values) |
| 74 | { |
| 75 | strmap_free_entries_(map, free_values); |
| 76 | hashmap_partial_clear(&map->map); |
| 77 | } |
| 78 | |
Elijah Newren | 6abd220 | 2020-11-06 00:24:53 +0000 | [diff] [blame] | 79 | static struct strmap_entry *create_entry(struct strmap *map, |
| 80 | const char *str, |
| 81 | void *data) |
| 82 | { |
| 83 | struct strmap_entry *entry; |
Elijah Newren | 6abd220 | 2020-11-06 00:24:53 +0000 | [diff] [blame] | 84 | |
Elijah Newren | 23a276a | 2020-11-11 20:02:19 +0000 | [diff] [blame] | 85 | if (map->strdup_strings) { |
| 86 | if (!map->pool) { |
| 87 | FLEXPTR_ALLOC_STR(entry, key, str); |
| 88 | } else { |
| 89 | size_t len = st_add(strlen(str), 1); /* include NUL */ |
| 90 | entry = mem_pool_alloc(map->pool, |
| 91 | st_add(sizeof(*entry), len)); |
| 92 | memcpy(entry + 1, str, len); |
| 93 | entry->key = (void *)(entry + 1); |
| 94 | } |
| 95 | } else if (!map->pool) { |
| 96 | entry = xmalloc(sizeof(*entry)); |
| 97 | } else { |
| 98 | entry = mem_pool_alloc(map->pool, sizeof(*entry)); |
| 99 | } |
Elijah Newren | 6abd220 | 2020-11-06 00:24:53 +0000 | [diff] [blame] | 100 | hashmap_entry_init(&entry->ent, strhash(str)); |
Elijah Newren | 23a276a | 2020-11-11 20:02:19 +0000 | [diff] [blame] | 101 | if (!map->strdup_strings) |
| 102 | entry->key = str; |
Elijah Newren | 6abd220 | 2020-11-06 00:24:53 +0000 | [diff] [blame] | 103 | entry->value = data; |
| 104 | return entry; |
| 105 | } |
| 106 | |
Elijah Newren | ae20bf1 | 2020-11-02 18:55:06 +0000 | [diff] [blame] | 107 | void *strmap_put(struct strmap *map, const char *str, void *data) |
| 108 | { |
| 109 | struct strmap_entry *entry = find_strmap_entry(map, str); |
Elijah Newren | ae20bf1 | 2020-11-02 18:55:06 +0000 | [diff] [blame] | 110 | |
| 111 | if (entry) { |
Elijah Newren | 6abd220 | 2020-11-06 00:24:53 +0000 | [diff] [blame] | 112 | void *old = entry->value; |
Elijah Newren | ae20bf1 | 2020-11-02 18:55:06 +0000 | [diff] [blame] | 113 | entry->value = data; |
Elijah Newren | 6abd220 | 2020-11-06 00:24:53 +0000 | [diff] [blame] | 114 | return old; |
Elijah Newren | ae20bf1 | 2020-11-02 18:55:06 +0000 | [diff] [blame] | 115 | } |
Elijah Newren | 6abd220 | 2020-11-06 00:24:53 +0000 | [diff] [blame] | 116 | |
| 117 | entry = create_entry(map, str, data); |
| 118 | hashmap_add(&map->map, &entry->ent); |
| 119 | return NULL; |
Elijah Newren | ae20bf1 | 2020-11-02 18:55:06 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Elijah Newren | b70c82e | 2020-11-05 00:22:39 +0000 | [diff] [blame] | 122 | struct strmap_entry *strmap_get_entry(struct strmap *map, const char *str) |
| 123 | { |
| 124 | return find_strmap_entry(map, str); |
| 125 | } |
| 126 | |
Elijah Newren | ae20bf1 | 2020-11-02 18:55:06 +0000 | [diff] [blame] | 127 | void *strmap_get(struct strmap *map, const char *str) |
| 128 | { |
| 129 | struct strmap_entry *entry = find_strmap_entry(map, str); |
| 130 | return entry ? entry->value : NULL; |
| 131 | } |
| 132 | |
| 133 | int strmap_contains(struct strmap *map, const char *str) |
| 134 | { |
| 135 | return find_strmap_entry(map, str) != NULL; |
| 136 | } |
Elijah Newren | b70c82e | 2020-11-05 00:22:39 +0000 | [diff] [blame] | 137 | |
| 138 | void strmap_remove(struct strmap *map, const char *str, int free_value) |
| 139 | { |
| 140 | struct strmap_entry entry, *ret; |
| 141 | hashmap_entry_init(&entry.ent, strhash(str)); |
| 142 | entry.key = str; |
| 143 | ret = hashmap_remove_entry(&map->map, &entry, ent, NULL); |
| 144 | if (!ret) |
| 145 | return; |
| 146 | if (free_value) |
| 147 | free(ret->value); |
Elijah Newren | 23a276a | 2020-11-11 20:02:19 +0000 | [diff] [blame] | 148 | if (!map->pool) |
Elijah Newren | a208ec1 | 2020-11-11 20:02:18 +0000 | [diff] [blame] | 149 | free(ret); |
Elijah Newren | b70c82e | 2020-11-05 00:22:39 +0000 | [diff] [blame] | 150 | } |
Elijah Newren | 4fa1d50 | 2020-11-05 00:22:41 +0000 | [diff] [blame] | 151 | |
| 152 | void strintmap_incr(struct strintmap *map, const char *str, intptr_t amt) |
| 153 | { |
| 154 | struct strmap_entry *entry = find_strmap_entry(&map->map, str); |
| 155 | if (entry) { |
| 156 | intptr_t *whence = (intptr_t*)&entry->value; |
| 157 | *whence += amt; |
| 158 | } |
| 159 | else |
| 160 | strintmap_set(map, str, map->default_value + amt); |
| 161 | } |
Elijah Newren | 1201eb6 | 2020-11-06 00:24:54 +0000 | [diff] [blame] | 162 | |
| 163 | int strset_add(struct strset *set, const char *str) |
| 164 | { |
| 165 | /* |
| 166 | * Cannot use strmap_put() because it'll return NULL in both cases: |
| 167 | * - cannot find str: NULL means "not found" |
| 168 | * - does find str: NULL is the value associated with str |
| 169 | */ |
| 170 | struct strmap_entry *entry = find_strmap_entry(&set->map, str); |
| 171 | |
| 172 | if (entry) |
| 173 | return 0; |
| 174 | |
| 175 | entry = create_entry(&set->map, str, NULL); |
| 176 | hashmap_add(&set->map.map, &entry->ent); |
| 177 | return 1; |
| 178 | } |