hashmap_entry_init takes "struct hashmap_entry *"

C compilers do type checking to make life easier for us. So rely on that and update all hashmap_entry_init callers to take "struct hashmap_entry *" to avoid future bugs while improving safety and readability. Signed-off-by: Eric Wong <e@80x24.org> Reviewed-by: Derrick Stolee <stolee@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Wong committed Oct 6, 2019 at 23:30 UTC d22245a2e360d2e708ca37169be8eb5a5899b98d
23 files changed +55 -52
attr.c
+2 -2
@@ -98,7 +98,7 @@ static void *attr_hashmap_get(struct attr_hashmap *map,
98 if (!map->map.tablesize)
99 attr_hashmap_init(map);
100
101 - hashmap_entry_init(&k, memhash(key, keylen));
101 + hashmap_entry_init(&k.ent, memhash(key, keylen));
102 k.key = key;
103 k.keylen = keylen;
104 e = hashmap_get(&map->map, &k, NULL);
@@ -117,7 +117,7 @@ static void attr_hashmap_add(struct attr_hashmap *map,
117 attr_hashmap_init(map);
118
119 e = xmalloc(sizeof(struct attr_hash_entry));
120 - hashmap_entry_init(e, memhash(key, keylen));
120 + hashmap_entry_init(&e->ent, memhash(key, keylen));
121 e->key = key;
122 e->keylen = keylen;
123 e->value = value;
blame.c
+1 -1
@@ -417,7 +417,7 @@ static void get_fingerprint(struct fingerprint *result,
417 /* Ignore whitespace pairs */
418 if (hash == 0)
419 continue;
420 - hashmap_entry_init(entry, hash);
420 + hashmap_entry_init(&entry->entry, hash);
421
422 found_entry = hashmap_get(&result->map, entry, NULL);
423 if (found_entry) {
builtin/describe.c
+1 -1
@@ -123,7 +123,7 @@ static void add_to_known_names(const char *path,
123 if (!e) {
124 e = xmalloc(sizeof(struct commit_name));
125 oidcpy(&e->peeled, peeled);
126 - hashmap_entry_init(e, oidhash(peeled));
126 + hashmap_entry_init(&e->entry, oidhash(peeled));
127 hashmap_add(&names, e);
128 e->path = NULL;
129 }
builtin/difftool.c
+3 -3
@@ -161,7 +161,7 @@ static void add_left_or_right(struct hashmap *map, const char *path,
161 struct pair_entry *e, *existing;
162
163 FLEX_ALLOC_STR(e, path, path);
164 - hashmap_entry_init(e, strhash(path));
164 + hashmap_entry_init(&e->entry, strhash(path));
165 existing = hashmap_get(map, e, NULL);
166 if (existing) {
167 free(e);
@@ -234,7 +234,7 @@ static void changed_files(struct hashmap *result, const char *index_path,
234 while (!strbuf_getline_nul(&buf, fp)) {
235 struct path_entry *entry;
236 FLEX_ALLOC_STR(entry, path, buf.buf);
237 - hashmap_entry_init(entry, strhash(buf.buf));
237 + hashmap_entry_init(&entry->entry, strhash(buf.buf));
238 hashmap_add(result, entry);
239 }
240 fclose(fp);
@@ -461,7 +461,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
461
462 /* Avoid duplicate working_tree entries */
463 FLEX_ALLOC_STR(entry, path, dst_path);
464 - hashmap_entry_init(entry, strhash(dst_path));
464 + hashmap_entry_init(&entry->entry, strhash(dst_path));
465 if (hashmap_get(&working_tree_dups, entry, NULL)) {
466 free(entry);
467 continue;
builtin/fast-export.c
+1 -1
@@ -148,7 +148,7 @@ static const void *anonymize_mem(struct hashmap *map,
148 if (!map->cmpfn)
149 hashmap_init(map, anonymized_entry_cmp, NULL, 0);
150
151 - hashmap_entry_init(&key, memhash(orig, *len));
151 + hashmap_entry_init(&key.hash, memhash(orig, *len));
152 key.orig = orig;
153 key.orig_len = *len;
154 ret = hashmap_get(map, &key, NULL);
builtin/fetch.c
+1 -1
@@ -276,7 +276,7 @@ static struct refname_hash_entry *refname_hash_add(struct hashmap *map,
276 size_t len = strlen(refname);
277
278 FLEX_ALLOC_MEM(ent, refname, refname, len);
279 - hashmap_entry_init(ent, strhash(refname));
279 + hashmap_entry_init(&ent->ent, strhash(refname));
280 oidcpy(&ent->oid, oid);
281 hashmap_add(map, ent);
282 return ent;
config.c
+2 -2
@@ -1861,7 +1861,7 @@ static struct config_set_element *configset_find_element(struct config_set *cs,
1861 if (git_config_parse_key(key, &normalized_key, NULL))
1862 return NULL;
1863
1864 - hashmap_entry_init(&k, strhash(normalized_key));
1864 + hashmap_entry_init(&k.ent, strhash(normalized_key));
1865 k.key = normalized_key;
1866 found_entry = hashmap_get(&cs->config_hash, &k, NULL);
1867 free(normalized_key);
@@ -1882,7 +1882,7 @@ static int configset_add_value(struct config_set *cs, const char *key, const cha
1882 */
1883 if (!e) {
1884 e = xmalloc(sizeof(*e));
1885 - hashmap_entry_init(e, strhash(key));
1885 + hashmap_entry_init(&e->ent, strhash(key));
1886 e->key = xstrdup(key);
1887 string_list_init(&e->value_list, 1);
1888 hashmap_add(&cs->config_hash, e);
diffcore-rename.c
+1 -1
@@ -329,7 +329,7 @@ static void insert_file_table(struct repository *r,
329 entry->index = index;
330 entry->filespec = filespec;
331
332 - hashmap_entry_init(entry, hash_filespec(r, filespec));
332 + hashmap_entry_init(&entry->entry, hash_filespec(r, filespec));
333 hashmap_add(table, entry);
334 }
335
hashmap.c
+2 -2
@@ -293,13 +293,13 @@ const void *memintern(const void *data, size_t len)
293 hashmap_init(&map, (hashmap_cmp_fn) pool_entry_cmp, NULL, 0);
294
295 /* lookup interned string in pool */
296 - hashmap_entry_init(&key, memhash(data, len));
296 + hashmap_entry_init(&key.ent, memhash(data, len));
297 key.len = len;
298 e = hashmap_get(&map, &key, data);
299 if (!e) {
300 /* not found: create it */
301 FLEX_ALLOC_MEM(e, data, data, len);
302 - hashmap_entry_init(e, key.ent.hash);
302 + hashmap_entry_init(&e->ent, key.ent.hash);
303 e->len = len;
304 hashmap_add(&map, e);
305 }
hashmap.h
+6 -6
@@ -48,14 +48,14 @@
48 * if (!strcmp("add", action)) {
49 * struct long2string *e;
50 * FLEX_ALLOC_STR(e, value, value);
51 - * hashmap_entry_init(e, memhash(&key, sizeof(long)));
51 + * hashmap_entry_init(&e->ent, memhash(&key, sizeof(long)));
52 * e->key = key;
53 * hashmap_add(&map, e);
54 * }
55 *
56 * if (!strcmp("print_all_by_key", action)) {
57 * struct long2string k, *e;
58 - * hashmap_entry_init(&k, memhash(&key, sizeof(long)));
58 + * hashmap_entry_init(&k->ent, memhash(&key, sizeof(long)));
59 * k.key = key;
60 *
61 * flags &= ~COMPARE_VALUE;
@@ -70,7 +70,7 @@
70 * if (!strcmp("has_exact_match", action)) {
71 * struct long2string *e;
72 * FLEX_ALLOC_STR(e, value, value);
73 - * hashmap_entry_init(e, memhash(&key, sizeof(long)));
73 + * hashmap_entry_init(&e->ent, memhash(&key, sizeof(long)));
74 * e->key = key;
75 *
76 * flags |= COMPARE_VALUE;
@@ -80,7 +80,7 @@
80 *
81 * if (!strcmp("has_exact_match_no_heap_alloc", action)) {
82 * struct long2string k;
83 - * hashmap_entry_init(&k, memhash(&key, sizeof(long)));
83 + * hashmap_entry_init(&k->ent, memhash(&key, sizeof(long)));
84 * k.key = key;
85 *
86 * flags |= COMPARE_VALUE;
@@ -244,9 +244,9 @@ void hashmap_free(struct hashmap *map, int free_entries);
244 * your structure was allocated with xmalloc(), you can just free(3) it,
245 * and if it is on stack, you can just let it go out of scope).
246 */
247 -static inline void hashmap_entry_init(void *entry, unsigned int hash)
247 +static inline void hashmap_entry_init(struct hashmap_entry *e,
248 + unsigned int hash)
249 {
249 - struct hashmap_entry *e = entry;
250 e->hash = hash;
251 e->next = NULL;
252 }
merge-recursive.c
+7 -6
@@ -61,7 +61,7 @@ static struct dir_rename_entry *dir_rename_find_entry(struct hashmap *hashmap,
61
62 if (dir == NULL)
63 return NULL;
64 - hashmap_entry_init(&key, strhash(dir));
64 + hashmap_entry_init(&key.ent, strhash(dir));
65 key.dir = dir;
66 return hashmap_get(hashmap, &key, NULL);
67 }
@@ -85,7 +85,7 @@ static void dir_rename_init(struct hashmap *map)
85 static void dir_rename_entry_init(struct dir_rename_entry *entry,
86 char *directory)
87 {
88 - hashmap_entry_init(entry, strhash(directory));
88 + hashmap_entry_init(&entry->ent, strhash(directory));
89 entry->dir = directory;
90 entry->non_unique_new_dir = 0;
91 strbuf_init(&entry->new_dir, 0);
@@ -97,7 +97,7 @@ static struct collision_entry *collision_find_entry(struct hashmap *hashmap,
97 {
98 struct collision_entry key;
99
100 - hashmap_entry_init(&key, strhash(target_file));
100 + hashmap_entry_init(&key.ent, strhash(target_file));
101 key.target_file = target_file;
102 return hashmap_get(hashmap, &key, NULL);
103 }
@@ -454,7 +454,7 @@ static int save_files_dirs(const struct object_id *oid,
454 strbuf_addstr(base, path);
455
456 FLEX_ALLOC_MEM(entry, path, base->buf, base->len);
457 - hashmap_entry_init(entry, path_hash(entry->path));
457 + hashmap_entry_init(&entry->e, path_hash(entry->path));
458 hashmap_add(&opt->current_file_dir_set, entry);
459
460 strbuf_setlen(base, baselen);
@@ -731,7 +731,7 @@ static char *unique_path(struct merge_options *opt, const char *path, const char
731 }
732
733 FLEX_ALLOC_MEM(entry, path, newpath.buf, newpath.len);
734 - hashmap_entry_init(entry, path_hash(entry->path));
734 + hashmap_entry_init(&entry->e, path_hash(entry->path));
735 hashmap_add(&opt->current_file_dir_set, entry);
736 return strbuf_detach(&newpath, NULL);
737 }
@@ -2358,7 +2358,8 @@ static void compute_collisions(struct hashmap *collisions,
2358 if (!collision_ent) {
2359 collision_ent = xcalloc(1,
2360 sizeof(struct collision_entry));
2361 - hashmap_entry_init(collision_ent, strhash(new_path));
2361 + hashmap_entry_init(&collision_ent->ent,
2362 + strhash(new_path));
2363 hashmap_put(collisions, collision_ent);
2364 collision_ent->target_file = new_path;
2365 } else {
name-hash.c
+5 -5
@@ -33,7 +33,7 @@ static struct dir_entry *find_dir_entry__hash(struct index_state *istate,
33 const char *name, unsigned int namelen, unsigned int hash)
34 {
35 struct dir_entry key;
36 - hashmap_entry_init(&key, hash);
36 + hashmap_entry_init(&key.ent, hash);
37 key.namelen = namelen;
38 return hashmap_get(&istate->dir_hash, &key, name);
39 }
@@ -68,7 +68,7 @@ static struct dir_entry *hash_dir_entry(struct index_state *istate,
68 if (!dir) {
69 /* not found, create it and add to hash table */
70 FLEX_ALLOC_MEM(dir, name, ce->name, namelen);
71 - hashmap_entry_init(dir, memihash(ce->name, namelen));
71 + hashmap_entry_init(&dir->ent, memihash(ce->name, namelen));
72 dir->namelen = namelen;
73 hashmap_add(&istate->dir_hash, dir);
74
@@ -106,7 +106,7 @@ static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)
106 if (ce->ce_flags & CE_HASHED)
107 return;
108 ce->ce_flags |= CE_HASHED;
109 - hashmap_entry_init(ce, memihash(ce->name, ce_namelen(ce)));
109 + hashmap_entry_init(&ce->ent, memihash(ce->name, ce_namelen(ce)));
110 hashmap_add(&istate->name_hash, ce);
111
112 if (ignore_case)
@@ -280,7 +280,7 @@ static struct dir_entry *hash_dir_entry_with_parent_and_prefix(
280 dir = find_dir_entry__hash(istate, prefix->buf, prefix->len, hash);
281 if (!dir) {
282 FLEX_ALLOC_MEM(dir, name, prefix->buf, prefix->len);
283 - hashmap_entry_init(dir, hash);
283 + hashmap_entry_init(&dir->ent, hash);
284 dir->namelen = prefix->len;
285 dir->parent = parent;
286 hashmap_add(&istate->dir_hash, dir);
@@ -472,7 +472,7 @@ static void *lazy_name_thread_proc(void *_data)
472 for (k = 0; k < d->istate->cache_nr; k++) {
473 struct cache_entry *ce_k = d->istate->cache[k];
474 ce_k->ce_flags |= CE_HASHED;
475 - hashmap_entry_init(ce_k, d->lazy_entries[k].hash_name);
475 + hashmap_entry_init(&ce_k->ent, d->lazy_entries[k].hash_name);
476 hashmap_add(&d->istate->name_hash, ce_k);
477 }
478
packfile.c
+1 -1
@@ -1487,7 +1487,7 @@ static void add_delta_base_cache(struct packed_git *p, off_t base_offset,
1487
1488 if (!delta_base_cache.cmpfn)
1489 hashmap_init(&delta_base_cache, delta_base_cache_hash_cmp, NULL, 0);
1490 - hashmap_entry_init(ent, pack_entry_hash(p, base_offset));
1490 + hashmap_entry_init(&ent->ent, pack_entry_hash(p, base_offset));
1491 hashmap_add(&delta_base_cache, ent);
1492 }
1493
patch-ids.c
+1 -1
@@ -83,7 +83,7 @@ static int init_patch_id_entry(struct patch_id *patch,
83 if (commit_patch_id(commit, &ids->diffopts, &header_only_patch_id, 1, 0))
84 return -1;
85
86 - hashmap_entry_init(patch, oidhash(&header_only_patch_id));
86 + hashmap_entry_init(&patch->ent, oidhash(&header_only_patch_id));
87 return 0;
88 }
89
range-diff.c
+2 -2
@@ -217,7 +217,7 @@ static void find_exact_matches(struct string_list *a, struct string_list *b)
217 util->i = i;
218 util->patch = a->items[i].string;
219 util->diff = util->patch + util->diff_offset;
220 - hashmap_entry_init(util, strhash(util->diff));
220 + hashmap_entry_init(&util->e, strhash(util->diff));
221 hashmap_add(&map, util);
222 }
223
@@ -228,7 +228,7 @@ static void find_exact_matches(struct string_list *a, struct string_list *b)
228 util->i = i;
229 util->patch = b->items[i].string;
230 util->diff = util->patch + util->diff_offset;
231 - hashmap_entry_init(util, strhash(util->diff));
231 + hashmap_entry_init(&util->e, strhash(util->diff));
232 other = hashmap_remove(&map, util, NULL);
233 if (other) {
234 if (other->matching >= 0)
ref-filter.c
+2 -1
@@ -1565,7 +1565,8 @@ static void populate_worktree_map(struct hashmap *map, struct worktree **worktre
1565 struct ref_to_worktree_entry *entry;
1566 entry = xmalloc(sizeof(*entry));
1567 entry->wt = worktrees[i];
1568 - hashmap_entry_init(entry, strhash(worktrees[i]->head_ref));
1568 + hashmap_entry_init(&entry->ent,
1569 + strhash(worktrees[i]->head_ref));
1570
1571 hashmap_add(map, entry);
1572 }
refs.c
+1 -1
@@ -1796,7 +1796,7 @@ static struct ref_store_hash_entry *alloc_ref_store_hash_entry(
1796 struct ref_store_hash_entry *entry;
1797
1798 FLEX_ALLOC_STR(entry, name, name);
1799 - hashmap_entry_init(entry, strhash(name));
1799 + hashmap_entry_init(&entry->ent, strhash(name));
1800 entry->refs = refs;
1801 return entry;
1802 }
remote.c
+1 -1
@@ -158,7 +158,7 @@ static struct remote *make_remote(const char *name, int len)
158 ALLOC_GROW(remotes, remotes_nr + 1, remotes_alloc);
159 remotes[remotes_nr++] = ret;
160
161 - hashmap_entry_init(ret, lookup_entry.hash);
161 + hashmap_entry_init(&ret->ent, lookup_entry.hash);
162 replaced = hashmap_put(&remotes_hash, ret);
163 assert(replaced == NULL); /* no previous entry overwritten */
164 return ret;
revision.c
+2 -2
@@ -141,7 +141,7 @@ static void paths_and_oids_insert(struct hashmap *map,
141 struct path_and_oids_entry key;
142 struct path_and_oids_entry *entry;
143
144 - hashmap_entry_init(&key, hash);
144 + hashmap_entry_init(&key.ent, hash);
145
146 /* use a shallow copy for the lookup */
147 key.path = (char *)path;
@@ -149,7 +149,7 @@ static void paths_and_oids_insert(struct hashmap *map,
149
150 if (!(entry = (struct path_and_oids_entry *)hashmap_get(map, &key, NULL))) {
151 entry = xcalloc(1, sizeof(struct path_and_oids_entry));
152 - hashmap_entry_init(entry, hash);
152 + hashmap_entry_init(&entry->ent, hash);
153 entry->path = xstrdup(key.path);
154 oidset_init(&entry->trees, 16);
155 hashmap_put(map, entry);
sequencer.c
+3 -2
@@ -4538,7 +4538,7 @@ static const char *label_oid(struct object_id *oid, const char *label,
4538 }
4539
4540 FLEX_ALLOC_STR(labels_entry, label, label);
4541 - hashmap_entry_init(labels_entry, strihash(label));
4541 + hashmap_entry_init(&labels_entry->entry, strihash(label));
4542 hashmap_add(&state->labels, labels_entry);
4543
4544 FLEX_ALLOC_STR(string_entry, string, label);
@@ -5252,7 +5252,8 @@ int todo_list_rearrange_squash(struct todo_list *todo_list)
5252 strhash(subject), subject)) {
5253 FLEX_ALLOC_MEM(entry, subject, subject, subject_len);
5254 entry->i = i;
5255 - hashmap_entry_init(entry, strhash(entry->subject));
5255 + hashmap_entry_init(&entry->entry,
5256 + strhash(entry->subject));
5257 hashmap_put(&subject2item, entry);
5258 }
5259 }
sub-process.c
+2 -2
@@ -20,7 +20,7 @@ struct subprocess_entry *subprocess_find_entry(struct hashmap *hashmap, const ch
20 {
21 struct subprocess_entry key;
22
23 - hashmap_entry_init(&key, strhash(cmd));
23 + hashmap_entry_init(&key.ent, strhash(cmd));
24 key.cmd = cmd;
25 return hashmap_get(hashmap, &key, NULL);
26 }
@@ -96,7 +96,7 @@ int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, co
96 return err;
97 }
98
99 - hashmap_entry_init(entry, strhash(cmd));
99 + hashmap_entry_init(&entry->ent, strhash(cmd));
100
101 err = startfn(entry);
102 if (err) {
submodule-config.c
+5 -5
@@ -123,7 +123,7 @@ static void cache_put_path(struct submodule_cache *cache,
123 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
124 submodule->path);
125 struct submodule_entry *e = xmalloc(sizeof(*e));
126 - hashmap_entry_init(e, hash);
126 + hashmap_entry_init(&e->ent, hash);
127 e->config = submodule;
128 hashmap_put(&cache->for_path, e);
129 }
@@ -135,7 +135,7 @@ static void cache_remove_path(struct submodule_cache *cache,
135 submodule->path);
136 struct submodule_entry e;
137 struct submodule_entry *removed;
138 - hashmap_entry_init(&e, hash);
138 + hashmap_entry_init(&e.ent, hash);
139 e.config = submodule;
140 removed = hashmap_remove(&cache->for_path, &e, NULL);
141 free(removed);
@@ -147,7 +147,7 @@ static void cache_add(struct submodule_cache *cache,
147 unsigned int hash = hash_oid_string(&submodule->gitmodules_oid,
148 submodule->name);
149 struct submodule_entry *e = xmalloc(sizeof(*e));
150 - hashmap_entry_init(e, hash);
150 + hashmap_entry_init(&e->ent, hash);
151 e->config = submodule;
152 hashmap_add(&cache->for_name, e);
153 }
@@ -163,7 +163,7 @@ static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
163 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
164 key_config.path = path;
165
166 - hashmap_entry_init(&key, hash);
166 + hashmap_entry_init(&key.ent, hash);
167 key.config = &key_config;
168
169 entry = hashmap_get(&cache->for_path, &key, NULL);
@@ -183,7 +183,7 @@ static struct submodule *cache_lookup_name(struct submodule_cache *cache,
183 oidcpy(&key_config.gitmodules_oid, gitmodules_oid);
184 key_config.name = name;
185
186 - hashmap_entry_init(&key, hash);
186 + hashmap_entry_init(&key.ent, hash);
187 key.config = &key_config;
188
189 entry = hashmap_get(&cache->for_name, &key, NULL);
t/helper/test-hashmap.c
+3 -3
@@ -37,7 +37,7 @@ static struct test_entry *alloc_test_entry(unsigned int hash,
37 size_t klen = strlen(key);
38 size_t vlen = strlen(value);
39 struct test_entry *entry = xmalloc(st_add4(sizeof(*entry), klen, vlen, 2));
40 - hashmap_entry_init(entry, hash);
40 + hashmap_entry_init(&entry->ent, hash);
41 memcpy(entry->key, key, klen + 1);
42 memcpy(entry->key + klen + 1, value, vlen + 1);
43 return entry;
@@ -103,7 +103,7 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
103
104 /* add entries */
105 for (i = 0; i < TEST_SIZE; i++) {
106 - hashmap_entry_init(entries[i], hashes[i]);
106 + hashmap_entry_init(&entries[i]->ent, hashes[i]);
107 hashmap_add(&map, entries[i]);
108 }
109
@@ -116,7 +116,7 @@ static void perf_hashmap(unsigned int method, unsigned int rounds)
116 /* fill the map (sparsely if specified) */
117 j = (method & TEST_SPARSE) ? TEST_SIZE / 10 : TEST_SIZE;
118 for (i = 0; i < j; i++) {
119 - hashmap_entry_init(entries[i], hashes[i]);
119 + hashmap_entry_init(&entries[i]->ent, hashes[i]);
120 hashmap_add(&map, entries[i]);
121 }
122