Skip to content

Commit b19315d

Browse files
newrengitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 23a276a commit b19315d

File tree

6 files changed

+16
-38
lines changed

6 files changed

+16
-38
lines changed

attr.c

+8-18
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,6 @@ static inline void hashmap_unlock(struct attr_hashmap *map)
5252
pthread_mutex_unlock(&map->mutex);
5353
}
5454

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-
6255
/* The container for objects stored in "struct attr_hashmap" */
6356
struct attr_hash_entry {
6457
struct hashmap_entry ent;
@@ -80,11 +73,14 @@ static int attr_hash_entry_cmp(const void *unused_cmp_data,
8073
return (a->keylen != b->keylen) || strncmp(a->key, b->key, a->keylen);
8174
}
8275

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+
};
8884

8985
/*
9086
* Retrieve the 'value' stored in a hashmap given the provided 'key'.
@@ -96,9 +92,6 @@ static void *attr_hashmap_get(struct attr_hashmap *map,
9692
struct attr_hash_entry k;
9793
struct attr_hash_entry *e;
9894

99-
if (!map->map.tablesize)
100-
attr_hashmap_init(map);
101-
10295
hashmap_entry_init(&k.ent, memhash(key, keylen));
10396
k.key = key;
10497
k.keylen = keylen;
@@ -114,9 +107,6 @@ static void attr_hashmap_add(struct attr_hashmap *map,
114107
{
115108
struct attr_hash_entry *e;
116109

117-
if (!map->map.tablesize)
118-
attr_hashmap_init(map);
119-
120110
e = xmalloc(sizeof(struct attr_hash_entry));
121111
hashmap_entry_init(&e->ent, memhash(key, keylen));
122112
e->key = key;

bloom.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,9 @@ struct bloom_filter *get_or_compute_bloom_filter(struct repository *r,
229229
diffcore_std(&diffopt);
230230

231231
if (diff_queued_diff.nr <= settings->max_changed_paths) {
232-
struct hashmap pathmap;
232+
struct hashmap pathmap = HASHMAP_INIT(pathmap_cmp, NULL);
233233
struct pathmap_hash_entry *e;
234234
struct hashmap_iter iter;
235-
hashmap_init(&pathmap, pathmap_cmp, NULL, 0);
236235

237236
for (i = 0; i < diff_queued_diff.nr; i++) {
238237
const char *path = diff_queued_diff.queue[i]->two->path;

builtin/difftool.c

+4-5
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,10 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
342342
const char *workdir, *tmp;
343343
int ret = 0, i;
344344
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);
346349
struct hashmap_iter iter;
347350
struct pair_entry *entry;
348351
struct index_state wtindex;
@@ -383,10 +386,6 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
383386
rdir_len = rdir.len;
384387
wtdir_len = wtdir.len;
385388

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-
390389
child.no_stdin = 1;
391390
child.git_cmd = 1;
392391
child.use_shell = 0;

range-diff.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,9 @@ static int patch_util_cmp(const void *dummy, const struct patch_util *a,
232232

233233
static void find_exact_matches(struct string_list *a, struct string_list *b)
234234
{
235-
struct hashmap map;
235+
struct hashmap map = HASHMAP_INIT((hashmap_cmp_fn)patch_util_cmp, NULL);
236236
int i;
237237

238-
hashmap_init(&map, (hashmap_cmp_fn)patch_util_cmp, NULL, 0);
239-
240238
/* First, add the patches of a to a hash map */
241239
for (i = 0; i < a->nr; i++) {
242240
struct patch_util *util = a->items[i].util;

revision.c

+1-8
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,6 @@ static int path_and_oids_cmp(const void *hashmap_cmp_fn_data,
124124
return strcmp(e1->path, e2->path);
125125
}
126126

127-
static void paths_and_oids_init(struct hashmap *map)
128-
{
129-
hashmap_init(map, path_and_oids_cmp, NULL, 0);
130-
}
131-
132127
static void paths_and_oids_clear(struct hashmap *map)
133128
{
134129
struct hashmap_iter iter;
@@ -213,7 +208,7 @@ void mark_trees_uninteresting_sparse(struct repository *r,
213208
struct oidset *trees)
214209
{
215210
unsigned has_interesting = 0, has_uninteresting = 0;
216-
struct hashmap map;
211+
struct hashmap map = HASHMAP_INIT(path_and_oids_cmp, NULL);
217212
struct hashmap_iter map_iter;
218213
struct path_and_oids_entry *entry;
219214
struct object_id *oid;
@@ -237,8 +232,6 @@ void mark_trees_uninteresting_sparse(struct repository *r,
237232
if (!has_uninteresting || !has_interesting)
238233
return;
239234

240-
paths_and_oids_init(&map);
241-
242235
oidset_iter_init(trees, &iter);
243236
while ((oid = oidset_iter_next(&iter))) {
244237
struct tree *tree = lookup_tree(r, oid);

t/helper/test-hashmap.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,11 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
151151
int cmd__hashmap(int argc, const char **argv)
152152
{
153153
struct strbuf line = STRBUF_INIT;
154-
struct hashmap map;
155154
int icase;
155+
struct hashmap map = HASHMAP_INIT(test_entry_cmp, &icase);
156156

157157
/* init hash map */
158158
icase = argc > 1 && !strcmp("ignorecase", argv[1]);
159-
hashmap_init(&map, test_entry_cmp, &icase, 0);
160159

161160
/* process commands from stdin */
162161
while (strbuf_getline(&line, stdin) != EOF) {

0 commit comments

Comments
 (0)