Skip to content

Commit 7663cdc

Browse files
stefanbellergitster
authored andcommitted
hashmap.h: compare function has access to a data field
When using the hashmap a common need is to have access to caller provided data in the compare function. A couple of times we abuse the keydata field to pass in the data needed. This happens for example in patch-ids.c. This patch changes the function signature of the compare function to have one more void pointer available. The pointer given for each invocation of the compare function must be defined in the init function of the hashmap and is just passed through. Documentation of this new feature is deferred to a later patch. This is a rather mechanical conversion, just adding the new pass-through parameter. However while at it improve the naming of the fields of all compare functions used by hashmaps by ensuring unused parameters are prefixed with 'unused_' and naming the parameters what they are (instead of 'unused' make it 'unused_keydata'). Signed-off-by: Stefan Beller <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e0aaa1b commit 7663cdc

19 files changed

+113
-66
lines changed

attr.c

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

7878
/* attr_hashmap comparison function */
79-
static int attr_hash_entry_cmp(const struct attr_hash_entry *a,
79+
static int attr_hash_entry_cmp(void *unused_cmp_data,
80+
const struct attr_hash_entry *a,
8081
const struct attr_hash_entry *b,
81-
void *unused)
82+
void *unused_keydata)
8283
{
8384
return (a->keylen != b->keylen) || strncmp(a->key, b->key, a->keylen);
8485
}
8586

8687
/* Initialize an 'attr_hashmap' object */
8788
static void attr_hashmap_init(struct attr_hashmap *map)
8889
{
89-
hashmap_init(&map->map, (hashmap_cmp_fn) attr_hash_entry_cmp, 0);
90+
hashmap_init(&map->map, (hashmap_cmp_fn) attr_hash_entry_cmp, NULL, 0);
9091
}
9192

9293
/*

builtin/describe.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ static const char *prio_names[] = {
5454
N_("head"), N_("lightweight"), N_("annotated"),
5555
};
5656

57-
static int commit_name_cmp(const struct commit_name *cn1,
58-
const struct commit_name *cn2, const void *peeled)
57+
static int commit_name_cmp(const void *unused_cmp_data,
58+
const struct commit_name *cn1,
59+
const struct commit_name *cn2,
60+
const void *peeled)
5961
{
6062
return oidcmp(&cn1->peeled, peeled ? peeled : &cn2->peeled);
6163
}
@@ -501,7 +503,7 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
501503
return cmd_name_rev(args.argc, args.argv, prefix);
502504
}
503505

504-
hashmap_init(&names, (hashmap_cmp_fn) commit_name_cmp, 0);
506+
hashmap_init(&names, (hashmap_cmp_fn) commit_name_cmp, NULL, 0);
505507
for_each_rawref(get_name, NULL);
506508
if (!names.size && !always)
507509
die(_("No names found, cannot describe anything."));

builtin/difftool.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,10 @@ struct working_tree_entry {
130130
char path[FLEX_ARRAY];
131131
};
132132

133-
static int working_tree_entry_cmp(struct working_tree_entry *a,
134-
struct working_tree_entry *b, void *keydata)
133+
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)
135137
{
136138
return strcmp(a->path, b->path);
137139
}
@@ -146,7 +148,9 @@ struct pair_entry {
146148
const char path[FLEX_ARRAY];
147149
};
148150

149-
static int pair_cmp(struct pair_entry *a, struct pair_entry *b, void *keydata)
151+
static int pair_cmp(const void *unused_cmp_data,
152+
struct pair_entry *a, struct pair_entry *b,
153+
void *unused_keydata)
150154
{
151155
return strcmp(a->path, b->path);
152156
}
@@ -174,7 +178,9 @@ struct path_entry {
174178
char path[FLEX_ARRAY];
175179
};
176180

177-
static int path_entry_cmp(struct path_entry *a, struct path_entry *b, void *key)
181+
static int path_entry_cmp(const void *unused_cmp_data,
182+
struct path_entry *a, struct path_entry *b,
183+
void *key)
178184
{
179185
return strcmp(a->path, key ? key : b->path);
180186
}
@@ -367,9 +373,9 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
367373
wtdir_len = wtdir.len;
368374

369375
hashmap_init(&working_tree_dups,
370-
(hashmap_cmp_fn)working_tree_entry_cmp, 0);
371-
hashmap_init(&submodules, (hashmap_cmp_fn)pair_cmp, 0);
372-
hashmap_init(&symlinks2, (hashmap_cmp_fn)pair_cmp, 0);
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);
373379

374380
child.no_stdin = 1;
375381
child.git_cmd = 1;
@@ -580,9 +586,9 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
580586
* files through the symlink.
581587
*/
582588
hashmap_init(&wt_modified, (hashmap_cmp_fn)path_entry_cmp,
583-
wtindex.cache_nr);
589+
NULL, wtindex.cache_nr);
584590
hashmap_init(&tmp_modified, (hashmap_cmp_fn)path_entry_cmp,
585-
wtindex.cache_nr);
591+
NULL, wtindex.cache_nr);
586592

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

builtin/fast-export.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ struct anonymized_entry {
9393
size_t anon_len;
9494
};
9595

96-
static int anonymized_entry_cmp(const void *va, const void *vb,
97-
const void *data)
96+
static int anonymized_entry_cmp(const void *unused_cmp_data,
97+
const void *va, const void *vb,
98+
const void *unused_keydata)
9899
{
99100
const struct anonymized_entry *a = va, *b = vb;
100101
return a->orig_len != b->orig_len ||
@@ -113,7 +114,7 @@ static const void *anonymize_mem(struct hashmap *map,
113114
struct anonymized_entry key, *ret;
114115

115116
if (!map->cmpfn)
116-
hashmap_init(map, anonymized_entry_cmp, 0);
117+
hashmap_init(map, anonymized_entry_cmp, NULL, 0);
117118

118119
hashmap_entry_init(&key, memhash(orig, *len));
119120
key.orig = orig;

config.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,15 +1753,18 @@ static int configset_add_value(struct config_set *cs, const char *key, const cha
17531753
return 0;
17541754
}
17551755

1756-
static int config_set_element_cmp(const struct config_set_element *e1,
1757-
const struct config_set_element *e2, const void *unused)
1756+
static int config_set_element_cmp(const void *unused_cmp_data,
1757+
const struct config_set_element *e1,
1758+
const struct config_set_element *e2,
1759+
const void *unused_keydata)
17581760
{
17591761
return strcmp(e1->key, e2->key);
17601762
}
17611763

17621764
void git_configset_init(struct config_set *cs)
17631765
{
1764-
hashmap_init(&cs->config_hash, (hashmap_cmp_fn)config_set_element_cmp, 0);
1766+
hashmap_init(&cs->config_hash, (hashmap_cmp_fn)config_set_element_cmp,
1767+
NULL, 0);
17651768
cs->hash_initialized = 1;
17661769
cs->list.nr = 0;
17671770
cs->list.alloc = 0;

convert.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,8 @@ static int apply_multi_file_filter(const char *path, const char *src, size_t len
583583

584584
if (!subprocess_map_initialized) {
585585
subprocess_map_initialized = 1;
586-
hashmap_init(&subprocess_map, (hashmap_cmp_fn) cmd2process_cmp, 0);
586+
hashmap_init(&subprocess_map, (hashmap_cmp_fn) cmd2process_cmp,
587+
NULL, 0);
587588
entry = NULL;
588589
} else {
589590
entry = (struct cmd2process *)subprocess_find_entry(&subprocess_map, cmd);

diffcore-rename.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ static int find_exact_renames(struct diff_options *options)
341341
/* Add all sources to the hash table in reverse order, because
342342
* later on they will be retrieved in LIFO order.
343343
*/
344-
hashmap_init(&file_table, NULL, rename_src_nr);
344+
hashmap_init(&file_table, NULL, NULL, rename_src_nr);
345345
for (i = rename_src_nr-1; i >= 0; i--)
346346
insert_file_table(&file_table, i, rename_src[i].p->one);
347347

hashmap.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ static inline int entry_equals(const struct hashmap *map,
9595
const struct hashmap_entry *e1, const struct hashmap_entry *e2,
9696
const void *keydata)
9797
{
98-
return (e1 == e2) || (e1->hash == e2->hash && !map->cmpfn(e1, e2, keydata));
98+
return (e1 == e2) ||
99+
(e1->hash == e2->hash &&
100+
!map->cmpfn(map->cmpfn_data, e1, e2, keydata));
99101
}
100102

101103
static inline unsigned int bucket(const struct hashmap *map,
@@ -140,19 +142,23 @@ static inline struct hashmap_entry **find_entry_ptr(const struct hashmap *map,
140142
return e;
141143
}
142144

143-
static int always_equal(const void *unused1, const void *unused2, const void *unused3)
145+
static int always_equal(const void *unused_cmp_data,
146+
const void *unused1,
147+
const void *unused2,
148+
const void *unused_keydata)
144149
{
145150
return 0;
146151
}
147152

148153
void hashmap_init(struct hashmap *map, hashmap_cmp_fn equals_function,
149-
size_t initial_size)
154+
const void *cmpfn_data, size_t initial_size)
150155
{
151156
unsigned int size = HASHMAP_INITIAL_SIZE;
152157

153158
memset(map, 0, sizeof(*map));
154159

155160
map->cmpfn = equals_function ? equals_function : always_equal;
161+
map->cmpfn_data = cmpfn_data;
156162

157163
/* calculate initial table size and allocate the table */
158164
initial_size = (unsigned int) ((uint64_t) initial_size * 100
@@ -260,7 +266,8 @@ struct pool_entry {
260266
unsigned char data[FLEX_ARRAY];
261267
};
262268

263-
static int pool_entry_cmp(const struct pool_entry *e1,
269+
static int pool_entry_cmp(const void *unused_cmp_data,
270+
const struct pool_entry *e1,
264271
const struct pool_entry *e2,
265272
const unsigned char *keydata)
266273
{
@@ -275,7 +282,7 @@ const void *memintern(const void *data, size_t len)
275282

276283
/* initialize string pool hashmap */
277284
if (!map.tablesize)
278-
hashmap_init(&map, (hashmap_cmp_fn) pool_entry_cmp, 0);
285+
hashmap_init(&map, (hashmap_cmp_fn) pool_entry_cmp, NULL, 0);
279286

280287
/* lookup interned string in pool */
281288
hashmap_entry_init(&key, memhash(data, len));

hashmap.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ struct hashmap_entry {
3232
unsigned int hash;
3333
};
3434

35-
typedef int (*hashmap_cmp_fn)(const void *entry, const void *entry_or_key,
36-
const void *keydata);
35+
typedef int (*hashmap_cmp_fn)(const void *hashmap_cmp_fn_data,
36+
const void *entry, const void *entry_or_key,
37+
const void *keydata);
3738

3839
struct hashmap {
3940
struct hashmap_entry **table;
4041
hashmap_cmp_fn cmpfn;
42+
const void *cmpfn_data;
4143
unsigned int size, tablesize, grow_at, shrink_at;
4244
unsigned disallow_rehash : 1;
4345
};
@@ -50,8 +52,10 @@ struct hashmap_iter {
5052

5153
/* hashmap functions */
5254

53-
extern void hashmap_init(struct hashmap *map, hashmap_cmp_fn equals_function,
54-
size_t initial_size);
55+
extern void hashmap_init(struct hashmap *map,
56+
hashmap_cmp_fn equals_function,
57+
const void *equals_function_data,
58+
size_t initial_size);
5559
extern void hashmap_free(struct hashmap *map, int free_entries);
5660

5761
/* hashmap_entry functions */

name-hash.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ struct dir_entry {
1616
char name[FLEX_ARRAY];
1717
};
1818

19-
static int dir_entry_cmp(const struct dir_entry *e1,
20-
const struct dir_entry *e2, const char *name)
19+
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)
2123
{
2224
return e1->namelen != e2->namelen || strncasecmp(e1->name,
2325
name ? name : e2->name, e1->namelen);
@@ -107,8 +109,10 @@ static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)
107109
add_dir_entry(istate, ce);
108110
}
109111

110-
static int cache_entry_cmp(const struct cache_entry *ce1,
111-
const struct cache_entry *ce2, const void *remove)
112+
static int cache_entry_cmp(const void *unused_cmp_data,
113+
const struct cache_entry *ce1,
114+
const struct cache_entry *ce2,
115+
const void *remove)
112116
{
113117
/*
114118
* For remove_name_hash, find the exact entry (pointer equality); for
@@ -571,9 +575,9 @@ static void lazy_init_name_hash(struct index_state *istate)
571575
if (istate->name_hash_initialized)
572576
return;
573577
hashmap_init(&istate->name_hash, (hashmap_cmp_fn) cache_entry_cmp,
574-
istate->cache_nr);
578+
NULL, istate->cache_nr);
575579
hashmap_init(&istate->dir_hash, (hashmap_cmp_fn) dir_entry_cmp,
576-
istate->cache_nr);
580+
NULL, istate->cache_nr);
577581

578582
if (lookup_lazy_params(istate)) {
579583
hashmap_disallow_rehash(&istate->dir_hash, 1);

0 commit comments

Comments
 (0)