Skip to content

Commit d22245a

Browse files
Eric Wonggitster
authored andcommitted
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 <[email protected]> Reviewed-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d0a48a0 commit d22245a

23 files changed

+55
-52
lines changed

attr.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ static void *attr_hashmap_get(struct attr_hashmap *map,
9898
if (!map->map.tablesize)
9999
attr_hashmap_init(map);
100100

101-
hashmap_entry_init(&k, memhash(key, keylen));
101+
hashmap_entry_init(&k.ent, memhash(key, keylen));
102102
k.key = key;
103103
k.keylen = keylen;
104104
e = hashmap_get(&map->map, &k, NULL);
@@ -117,7 +117,7 @@ static void attr_hashmap_add(struct attr_hashmap *map,
117117
attr_hashmap_init(map);
118118

119119
e = xmalloc(sizeof(struct attr_hash_entry));
120-
hashmap_entry_init(e, memhash(key, keylen));
120+
hashmap_entry_init(&e->ent, memhash(key, keylen));
121121
e->key = key;
122122
e->keylen = keylen;
123123
e->value = value;

blame.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ static void get_fingerprint(struct fingerprint *result,
417417
/* Ignore whitespace pairs */
418418
if (hash == 0)
419419
continue;
420-
hashmap_entry_init(entry, hash);
420+
hashmap_entry_init(&entry->entry, hash);
421421

422422
found_entry = hashmap_get(&result->map, entry, NULL);
423423
if (found_entry) {

builtin/describe.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ static void add_to_known_names(const char *path,
123123
if (!e) {
124124
e = xmalloc(sizeof(struct commit_name));
125125
oidcpy(&e->peeled, peeled);
126-
hashmap_entry_init(e, oidhash(peeled));
126+
hashmap_entry_init(&e->entry, oidhash(peeled));
127127
hashmap_add(&names, e);
128128
e->path = NULL;
129129
}

builtin/difftool.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ static void add_left_or_right(struct hashmap *map, const char *path,
161161
struct pair_entry *e, *existing;
162162

163163
FLEX_ALLOC_STR(e, path, path);
164-
hashmap_entry_init(e, strhash(path));
164+
hashmap_entry_init(&e->entry, strhash(path));
165165
existing = hashmap_get(map, e, NULL);
166166
if (existing) {
167167
free(e);
@@ -234,7 +234,7 @@ static void changed_files(struct hashmap *result, const char *index_path,
234234
while (!strbuf_getline_nul(&buf, fp)) {
235235
struct path_entry *entry;
236236
FLEX_ALLOC_STR(entry, path, buf.buf);
237-
hashmap_entry_init(entry, strhash(buf.buf));
237+
hashmap_entry_init(&entry->entry, strhash(buf.buf));
238238
hashmap_add(result, entry);
239239
}
240240
fclose(fp);
@@ -461,7 +461,7 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
461461

462462
/* Avoid duplicate working_tree entries */
463463
FLEX_ALLOC_STR(entry, path, dst_path);
464-
hashmap_entry_init(entry, strhash(dst_path));
464+
hashmap_entry_init(&entry->entry, strhash(dst_path));
465465
if (hashmap_get(&working_tree_dups, entry, NULL)) {
466466
free(entry);
467467
continue;

builtin/fast-export.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ static const void *anonymize_mem(struct hashmap *map,
148148
if (!map->cmpfn)
149149
hashmap_init(map, anonymized_entry_cmp, NULL, 0);
150150

151-
hashmap_entry_init(&key, memhash(orig, *len));
151+
hashmap_entry_init(&key.hash, memhash(orig, *len));
152152
key.orig = orig;
153153
key.orig_len = *len;
154154
ret = hashmap_get(map, &key, NULL);

builtin/fetch.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ static struct refname_hash_entry *refname_hash_add(struct hashmap *map,
276276
size_t len = strlen(refname);
277277

278278
FLEX_ALLOC_MEM(ent, refname, refname, len);
279-
hashmap_entry_init(ent, strhash(refname));
279+
hashmap_entry_init(&ent->ent, strhash(refname));
280280
oidcpy(&ent->oid, oid);
281281
hashmap_add(map, ent);
282282
return ent;

config.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1861,7 +1861,7 @@ static struct config_set_element *configset_find_element(struct config_set *cs,
18611861
if (git_config_parse_key(key, &normalized_key, NULL))
18621862
return NULL;
18631863

1864-
hashmap_entry_init(&k, strhash(normalized_key));
1864+
hashmap_entry_init(&k.ent, strhash(normalized_key));
18651865
k.key = normalized_key;
18661866
found_entry = hashmap_get(&cs->config_hash, &k, NULL);
18671867
free(normalized_key);
@@ -1882,7 +1882,7 @@ static int configset_add_value(struct config_set *cs, const char *key, const cha
18821882
*/
18831883
if (!e) {
18841884
e = xmalloc(sizeof(*e));
1885-
hashmap_entry_init(e, strhash(key));
1885+
hashmap_entry_init(&e->ent, strhash(key));
18861886
e->key = xstrdup(key);
18871887
string_list_init(&e->value_list, 1);
18881888
hashmap_add(&cs->config_hash, e);

diffcore-rename.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ static void insert_file_table(struct repository *r,
329329
entry->index = index;
330330
entry->filespec = filespec;
331331

332-
hashmap_entry_init(entry, hash_filespec(r, filespec));
332+
hashmap_entry_init(&entry->entry, hash_filespec(r, filespec));
333333
hashmap_add(table, entry);
334334
}
335335

hashmap.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,13 @@ const void *memintern(const void *data, size_t len)
293293
hashmap_init(&map, (hashmap_cmp_fn) pool_entry_cmp, NULL, 0);
294294

295295
/* lookup interned string in pool */
296-
hashmap_entry_init(&key, memhash(data, len));
296+
hashmap_entry_init(&key.ent, memhash(data, len));
297297
key.len = len;
298298
e = hashmap_get(&map, &key, data);
299299
if (!e) {
300300
/* not found: create it */
301301
FLEX_ALLOC_MEM(e, data, data, len);
302-
hashmap_entry_init(e, key.ent.hash);
302+
hashmap_entry_init(&e->ent, key.ent.hash);
303303
e->len = len;
304304
hashmap_add(&map, e);
305305
}

hashmap.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@
4848
* if (!strcmp("add", action)) {
4949
* struct long2string *e;
5050
* 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)));
5252
* e->key = key;
5353
* hashmap_add(&map, e);
5454
* }
5555
*
5656
* if (!strcmp("print_all_by_key", action)) {
5757
* struct long2string k, *e;
58-
* hashmap_entry_init(&k, memhash(&key, sizeof(long)));
58+
* hashmap_entry_init(&k->ent, memhash(&key, sizeof(long)));
5959
* k.key = key;
6060
*
6161
* flags &= ~COMPARE_VALUE;
@@ -70,7 +70,7 @@
7070
* if (!strcmp("has_exact_match", action)) {
7171
* struct long2string *e;
7272
* 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)));
7474
* e->key = key;
7575
*
7676
* flags |= COMPARE_VALUE;
@@ -80,7 +80,7 @@
8080
*
8181
* if (!strcmp("has_exact_match_no_heap_alloc", action)) {
8282
* struct long2string k;
83-
* hashmap_entry_init(&k, memhash(&key, sizeof(long)));
83+
* hashmap_entry_init(&k->ent, memhash(&key, sizeof(long)));
8484
* k.key = key;
8585
*
8686
* flags |= COMPARE_VALUE;
@@ -244,9 +244,9 @@ void hashmap_free(struct hashmap *map, int free_entries);
244244
* your structure was allocated with xmalloc(), you can just free(3) it,
245245
* and if it is on stack, you can just let it go out of scope).
246246
*/
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)
248249
{
249-
struct hashmap_entry *e = entry;
250250
e->hash = hash;
251251
e->next = NULL;
252252
}

0 commit comments

Comments
 (0)