Use new HASHMAP_INIT macro to simplify hashmap initialization
Now that hashamp has lazy initialization and a HASHMAP_INIT macro, hashmaps allocated on the stack can be initialized without a call to hashmap_init() and in some cases makes the code a bit shorter. Convert some callsites over to take advantage of this. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Elijah Newren committed
Nov 11, 2020 at 20:02 UTC
b19315d8ab46ae50410dba9228a4c08ae5c73e38
6 files changed
+16
-38
attr.c
+8
-18
@@ -52,13 +52,6 @@ static inline void hashmap_unlock(struct attr_hashmap *map)
52
pthread_mutex_unlock(&map->mutex);
53
}
54
55
-/*
56
- * The global dictionary of all interned attributes. This
57
- * is a singleton object which is shared between threads.
58
- * Access to this dictionary must be surrounded with a mutex.
59
- */
60
-static struct attr_hashmap g_attr_hashmap;
61
-
55
/* The container for objects stored in "struct attr_hashmap" */
56
struct attr_hash_entry {
57
struct hashmap_entry ent;
@@ -80,11 +73,14 @@ static int attr_hash_entry_cmp(const void *unused_cmp_data,
73
return (a->keylen != b->keylen) || strncmp(a->key, b->key, a->keylen);
74
}
75
83
-/* Initialize an 'attr_hashmap' object */
84
-static void attr_hashmap_init(struct attr_hashmap *map)
85
-{
86
- hashmap_init(&map->map, attr_hash_entry_cmp, NULL, 0);
87
-}
76
+/*
77
+ * The global dictionary of all interned attributes. This
78
+ * is a singleton object which is shared between threads.
79
+ * Access to this dictionary must be surrounded with a mutex.
80
+ */
81
+static struct attr_hashmap g_attr_hashmap = {
82
+ HASHMAP_INIT(attr_hash_entry_cmp, NULL)
83
+};
84
85
/*
86
* Retrieve the 'value' stored in a hashmap given the provided 'key'.
@@ -96,9 +92,6 @@ static void *attr_hashmap_get(struct attr_hashmap *map,
92
struct attr_hash_entry k;
93
struct attr_hash_entry *e;
94
99
- if (!map->map.tablesize)
100
- attr_hashmap_init(map);
101
-
95
hashmap_entry_init(&k.ent, memhash(key, keylen));
96
k.key = key;
97
k.keylen = keylen;
@@ -114,9 +107,6 @@ static void attr_hashmap_add(struct attr_hashmap *map,
107
{
108
struct attr_hash_entry *e;
109
117
- if (!map->map.tablesize)
118
- attr_hashmap_init(map);
119
-
110
e = xmalloc(sizeof(struct attr_hash_entry));
111
hashmap_entry_init(&e->ent, memhash(key, keylen));
112
e->key = key;
bloom.c
+1
-2
@@ -229,10 +229,9 @@ struct bloom_filter *get_or_compute_bloom_filter(struct repository *r,
229
diffcore_std(&diffopt);
230
231
if (diff_queued_diff.nr <= settings->max_changed_paths) {
232
- struct hashmap pathmap;
232
+ struct hashmap pathmap = HASHMAP_INIT(pathmap_cmp, NULL);
233
struct pathmap_hash_entry *e;
234
struct hashmap_iter iter;
235
- hashmap_init(&pathmap, pathmap_cmp, NULL, 0);
235
236
for (i = 0; i < diff_queued_diff.nr; i++) {
237
const char *path = diff_queued_diff.queue[i]->two->path;
builtin/difftool.c
+4
-5
@@ -342,7 +342,10 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
342
const char *workdir, *tmp;
343
int ret = 0, i;
344
FILE *fp;
345
- struct hashmap working_tree_dups, submodules, symlinks2;
345
+ struct hashmap working_tree_dups = HASHMAP_INIT(working_tree_entry_cmp,
346
+ NULL);
347
+ struct hashmap submodules = HASHMAP_INIT(pair_cmp, NULL);
348
+ struct hashmap symlinks2 = HASHMAP_INIT(pair_cmp, NULL);
349
struct hashmap_iter iter;
350
struct pair_entry *entry;
351
struct index_state wtindex;
@@ -383,10 +386,6 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
386
rdir_len = rdir.len;
387
wtdir_len = wtdir.len;
388
386
- hashmap_init(&working_tree_dups, working_tree_entry_cmp, NULL, 0);
387
- hashmap_init(&submodules, pair_cmp, NULL, 0);
388
- hashmap_init(&symlinks2, pair_cmp, NULL, 0);
389
-
389
child.no_stdin = 1;
390
child.git_cmd = 1;
391
child.use_shell = 0;
range-diff.c
+1
-3
@@ -232,11 +232,9 @@ static int patch_util_cmp(const void *dummy, const struct patch_util *a,
232
233
static void find_exact_matches(struct string_list *a, struct string_list *b)
234
{
235
- struct hashmap map;
235
+ struct hashmap map = HASHMAP_INIT((hashmap_cmp_fn)patch_util_cmp, NULL);
236
int i;
237
238
- hashmap_init(&map, (hashmap_cmp_fn)patch_util_cmp, NULL, 0);
239
-
238
/* First, add the patches of a to a hash map */
239
for (i = 0; i < a->nr; i++) {
240
struct patch_util *util = a->items[i].util;
revision.c
+1
-8
@@ -124,11 +124,6 @@ static int path_and_oids_cmp(const void *hashmap_cmp_fn_data,
124
return strcmp(e1->path, e2->path);
125
}
126
127
-static void paths_and_oids_init(struct hashmap *map)
128
-{
129
- hashmap_init(map, path_and_oids_cmp, NULL, 0);
130
-}
131
-
127
static void paths_and_oids_clear(struct hashmap *map)
128
{
129
struct hashmap_iter iter;
@@ -213,7 +208,7 @@ void mark_trees_uninteresting_sparse(struct repository *r,
208
struct oidset *trees)
209
{
210
unsigned has_interesting = 0, has_uninteresting = 0;
216
- struct hashmap map;
211
+ struct hashmap map = HASHMAP_INIT(path_and_oids_cmp, NULL);
212
struct hashmap_iter map_iter;
213
struct path_and_oids_entry *entry;
214
struct object_id *oid;
@@ -237,8 +232,6 @@ void mark_trees_uninteresting_sparse(struct repository *r,
232
if (!has_uninteresting || !has_interesting)
233
return;
234
240
- paths_and_oids_init(&map);
241
-
235
oidset_iter_init(trees, &iter);
236
while ((oid = oidset_iter_next(&iter))) {
237
struct tree *tree = lookup_tree(r, oid);
t/helper/test-hashmap.c
+1
-2
@@ -151,12 +151,11 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
151
int cmd__hashmap(int argc, const char **argv)
152
{
153
struct strbuf line = STRBUF_INIT;
154
- struct hashmap map;
154
int icase;
155
+ struct hashmap map = HASHMAP_INIT(test_entry_cmp, &icase);
156
157
/* init hash map */
158
icase = argc > 1 && !strcmp("ignorecase", argv[1]);
159
- hashmap_init(&map, test_entry_cmp, &icase, 0);
159
160
/* process commands from stdin */
161
while (strbuf_getline(&line, stdin) != EOF) {