Skip to content

Commit 55c965f

Browse files
committed
Merge branch 'sb/hashmap-cleanup'
Many uses of comparision callback function the hashmap API uses cast the callback function type when registering it to hashmap_init(), which defeats the compile time type checking when the callback interface changes (e.g. gaining more parameters). The callback implementations have been updated to take "void *" pointers and cast them to the type they expect instead. * sb/hashmap-cleanup: t/helper/test-hashmap: use custom data instead of duplicate cmp functions name-hash.c: drop hashmap_cmp_fn cast submodule-config.c: drop hashmap_cmp_fn cast remote.c: drop hashmap_cmp_fn cast patch-ids.c: drop hashmap_cmp_fn cast convert/sub-process: drop cast to hashmap_cmp_fn config.c: drop hashmap_cmp_fn cast builtin/describe: drop hashmap_cmp_fn cast builtin/difftool.c: drop hashmap_cmp_fn cast attr.c: drop hashmap_cmp_fn cast
2 parents 3ab01ac + 6815d11 commit 55c965f

File tree

12 files changed

+107
-75
lines changed

12 files changed

+107
-75
lines changed

attr.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,20 @@ struct attr_hash_entry {
7676
};
7777

7878
/* attr_hashmap comparison function */
79-
static int attr_hash_entry_cmp(void *unused_cmp_data,
80-
const struct attr_hash_entry *a,
81-
const struct attr_hash_entry *b,
82-
void *unused_keydata)
79+
static int attr_hash_entry_cmp(const void *unused_cmp_data,
80+
const void *entry,
81+
const void *entry_or_key,
82+
const void *unused_keydata)
8383
{
84+
const struct attr_hash_entry *a = entry;
85+
const struct attr_hash_entry *b = entry_or_key;
8486
return (a->keylen != b->keylen) || strncmp(a->key, b->key, a->keylen);
8587
}
8688

8789
/* Initialize an 'attr_hashmap' object */
8890
static void attr_hashmap_init(struct attr_hashmap *map)
8991
{
90-
hashmap_init(&map->map, (hashmap_cmp_fn) attr_hash_entry_cmp, NULL, 0);
92+
hashmap_init(&map->map, attr_hash_entry_cmp, NULL, 0);
9193
}
9294

9395
/*

builtin/describe.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,13 @@ static const char *prio_names[] = {
5555
};
5656

5757
static int commit_name_cmp(const void *unused_cmp_data,
58-
const struct commit_name *cn1,
59-
const struct commit_name *cn2,
58+
const void *entry,
59+
const void *entry_or_key,
6060
const void *peeled)
6161
{
62+
const struct commit_name *cn1 = entry;
63+
const struct commit_name *cn2 = entry_or_key;
64+
6265
return oidcmp(&cn1->peeled, peeled ? peeled : &cn2->peeled);
6366
}
6467

@@ -503,7 +506,7 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
503506
return cmd_name_rev(args.argc, args.argv, prefix);
504507
}
505508

506-
hashmap_init(&names, (hashmap_cmp_fn) commit_name_cmp, NULL, 0);
509+
hashmap_init(&names, commit_name_cmp, NULL, 0);
507510
for_each_rawref(get_name, NULL);
508511
if (!names.size && !always)
509512
die(_("No names found, cannot describe anything."));

builtin/difftool.c

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -131,10 +131,12 @@ struct working_tree_entry {
131131
};
132132

133133
static int working_tree_entry_cmp(const void *unused_cmp_data,
134-
struct working_tree_entry *a,
135-
struct working_tree_entry *b,
136-
void *unused_keydata)
134+
const void *entry,
135+
const void *entry_or_key,
136+
const void *unused_keydata)
137137
{
138+
const struct working_tree_entry *a = entry;
139+
const struct working_tree_entry *b = entry_or_key;
138140
return strcmp(a->path, b->path);
139141
}
140142

@@ -149,9 +151,13 @@ struct pair_entry {
149151
};
150152

151153
static int pair_cmp(const void *unused_cmp_data,
152-
struct pair_entry *a, struct pair_entry *b,
153-
void *unused_keydata)
154+
const void *entry,
155+
const void *entry_or_key,
156+
const void *unused_keydata)
154157
{
158+
const struct pair_entry *a = entry;
159+
const struct pair_entry *b = entry_or_key;
160+
155161
return strcmp(a->path, b->path);
156162
}
157163

@@ -179,9 +185,13 @@ struct path_entry {
179185
};
180186

181187
static int path_entry_cmp(const void *unused_cmp_data,
182-
struct path_entry *a, struct path_entry *b,
183-
void *key)
188+
const void *entry,
189+
const void *entry_or_key,
190+
const void *key)
184191
{
192+
const struct path_entry *a = entry;
193+
const struct path_entry *b = entry_or_key;
194+
185195
return strcmp(a->path, key ? key : b->path);
186196
}
187197

@@ -372,10 +382,9 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
372382
rdir_len = rdir.len;
373383
wtdir_len = wtdir.len;
374384

375-
hashmap_init(&working_tree_dups,
376-
(hashmap_cmp_fn)working_tree_entry_cmp, NULL, 0);
377-
hashmap_init(&submodules, (hashmap_cmp_fn)pair_cmp, NULL, 0);
378-
hashmap_init(&symlinks2, (hashmap_cmp_fn)pair_cmp, NULL, 0);
385+
hashmap_init(&working_tree_dups, working_tree_entry_cmp, NULL, 0);
386+
hashmap_init(&submodules, pair_cmp, NULL, 0);
387+
hashmap_init(&symlinks2, pair_cmp, NULL, 0);
379388

380389
child.no_stdin = 1;
381390
child.git_cmd = 1;
@@ -585,10 +594,8 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
585594
* in the common case of --symlinks and the difftool updating
586595
* files through the symlink.
587596
*/
588-
hashmap_init(&wt_modified, (hashmap_cmp_fn)path_entry_cmp,
589-
NULL, wtindex.cache_nr);
590-
hashmap_init(&tmp_modified, (hashmap_cmp_fn)path_entry_cmp,
591-
NULL, wtindex.cache_nr);
597+
hashmap_init(&wt_modified, path_entry_cmp, NULL, wtindex.cache_nr);
598+
hashmap_init(&tmp_modified, path_entry_cmp, NULL, wtindex.cache_nr);
592599

593600
for (i = 0; i < wtindex.cache_nr; i++) {
594601
struct hashmap_entry dummy;

config.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,17 +1719,19 @@ static int configset_add_value(struct config_set *cs, const char *key, const cha
17191719
}
17201720

17211721
static int config_set_element_cmp(const void *unused_cmp_data,
1722-
const struct config_set_element *e1,
1723-
const struct config_set_element *e2,
1722+
const void *entry,
1723+
const void *entry_or_key,
17241724
const void *unused_keydata)
17251725
{
1726+
const struct config_set_element *e1 = entry;
1727+
const struct config_set_element *e2 = entry_or_key;
1728+
17261729
return strcmp(e1->key, e2->key);
17271730
}
17281731

17291732
void git_configset_init(struct config_set *cs)
17301733
{
1731-
hashmap_init(&cs->config_hash, (hashmap_cmp_fn)config_set_element_cmp,
1732-
NULL, 0);
1734+
hashmap_init(&cs->config_hash, config_set_element_cmp, NULL, 0);
17331735
cs->hash_initialized = 1;
17341736
cs->list.nr = 0;
17351737
cs->list.alloc = 0;

convert.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -625,8 +625,7 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t len
625625

626626
if (!subprocess_map_initialized) {
627627
subprocess_map_initialized = 1;
628-
hashmap_init(&subprocess_map, (hashmap_cmp_fn) cmd2process_cmp,
629-
NULL, 0);
628+
hashmap_init(&subprocess_map, cmd2process_cmp, NULL, 0);
630629
entry = NULL;
631630
} else {
632631
entry = (struct cmd2process *)subprocess_find_entry(&subprocess_map, cmd);

name-hash.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@ struct dir_entry {
1717
};
1818

1919
static int dir_entry_cmp(const void *unused_cmp_data,
20-
const struct dir_entry *e1,
21-
const struct dir_entry *e2,
22-
const char *name)
20+
const void *entry,
21+
const void *entry_or_key,
22+
const void *keydata)
2323
{
24+
const struct dir_entry *e1 = entry;
25+
const struct dir_entry *e2 = entry_or_key;
26+
const char *name = keydata;
27+
2428
return e1->namelen != e2->namelen || strncasecmp(e1->name,
2529
name ? name : e2->name, e1->namelen);
2630
}
@@ -110,10 +114,12 @@ static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)
110114
}
111115

112116
static int cache_entry_cmp(const void *unused_cmp_data,
113-
const struct cache_entry *ce1,
114-
const struct cache_entry *ce2,
117+
const void *entry,
118+
const void *entry_or_key,
115119
const void *remove)
116120
{
121+
const struct cache_entry *ce1 = entry;
122+
const struct cache_entry *ce2 = entry_or_key;
117123
/*
118124
* For remove_name_hash, find the exact entry (pointer equality); for
119125
* index_file_exists, find all entries with matching hash code and
@@ -574,10 +580,8 @@ static void lazy_init_name_hash(struct index_state *istate)
574580
{
575581
if (istate->name_hash_initialized)
576582
return;
577-
hashmap_init(&istate->name_hash, (hashmap_cmp_fn) cache_entry_cmp,
578-
NULL, istate->cache_nr);
579-
hashmap_init(&istate->dir_hash, (hashmap_cmp_fn) dir_entry_cmp,
580-
NULL, istate->cache_nr);
583+
hashmap_init(&istate->name_hash, cache_entry_cmp, NULL, istate->cache_nr);
584+
hashmap_init(&istate->dir_hash, dir_entry_cmp, NULL, istate->cache_nr);
581585

582586
if (lookup_lazy_params(istate)) {
583587
hashmap_disallow_rehash(&istate->dir_hash, 1);

patch-ids.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,16 @@ int commit_patch_id(struct commit *commit, struct diff_options *options,
3535
* the side of safety. The actual value being negative does not have
3636
* any significance; only that it is non-zero matters.
3737
*/
38-
static int patch_id_cmp(struct diff_options *opt,
39-
struct patch_id *a,
40-
struct patch_id *b,
38+
static int patch_id_cmp(const void *cmpfn_data,
39+
const void *entry,
40+
const void *entry_or_key,
4141
const void *unused_keydata)
4242
{
43+
/* NEEDSWORK: const correctness? */
44+
struct diff_options *opt = (void *)cmpfn_data;
45+
struct patch_id *a = (void *)entry;
46+
struct patch_id *b = (void *)entry_or_key;
47+
4348
if (is_null_oid(&a->patch_id) &&
4449
commit_patch_id(a->commit, opt, &a->patch_id, 0))
4550
return error("Could not get patch ID for %s",
@@ -58,8 +63,7 @@ int init_patch_ids(struct patch_ids *ids)
5863
ids->diffopts.detect_rename = 0;
5964
DIFF_OPT_SET(&ids->diffopts, RECURSIVE);
6065
diff_setup_done(&ids->diffopts);
61-
hashmap_init(&ids->patches, (hashmap_cmp_fn)patch_id_cmp,
62-
&ids->diffopts, 256);
66+
hashmap_init(&ids->patches, patch_id_cmp, &ids->diffopts, 256);
6367
return 0;
6468
}
6569

remote.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,14 @@ struct remotes_hash_key {
134134
};
135135

136136
static int remotes_hash_cmp(const void *unused_cmp_data,
137-
const struct remote *a,
138-
const struct remote *b,
139-
const struct remotes_hash_key *key)
137+
const void *entry,
138+
const void *entry_or_key,
139+
const void *keydata)
140140
{
141+
const struct remote *a = entry;
142+
const struct remote *b = entry_or_key;
143+
const struct remotes_hash_key *key = keydata;
144+
141145
if (key)
142146
return strncmp(a->name, key->str, key->len) || a->name[key->len];
143147
else
@@ -147,7 +151,7 @@ static int remotes_hash_cmp(const void *unused_cmp_data,
147151
static inline void init_remotes_hash(void)
148152
{
149153
if (!remotes_hash.cmpfn)
150-
hashmap_init(&remotes_hash, (hashmap_cmp_fn)remotes_hash_cmp, NULL, 0);
154+
hashmap_init(&remotes_hash, remotes_hash_cmp, NULL, 0);
151155
}
152156

153157
static struct remote *make_remote(const char *name, int len)

sub-process.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
#include "pkt-line.h"
77

88
int cmd2process_cmp(const void *unused_cmp_data,
9-
const struct subprocess_entry *e1,
10-
const struct subprocess_entry *e2,
9+
const void *entry,
10+
const void *entry_or_key,
1111
const void *unused_keydata)
1212
{
13+
const struct subprocess_entry *e1 = entry;
14+
const struct subprocess_entry *e2 = entry_or_key;
15+
1316
return strcmp(e1->cmd, e2->cmd);
1417
}
1518

sub-process.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ struct subprocess_entry {
2121
/* subprocess functions */
2222

2323
extern int cmd2process_cmp(const void *unused_cmp_data,
24-
const struct subprocess_entry *e1,
25-
const struct subprocess_entry *e2,
24+
const void *e1,
25+
const void *e2,
2626
const void *unused_keydata);
2727

2828
typedef int(*subprocess_start_fn)(struct subprocess_entry *entry);

0 commit comments

Comments
 (0)