Skip to content

Commit 939af16

Browse files
Eric Wonggitster
authored andcommitted
hashmap_cmp_fn takes hashmap_entry params
Another step in eliminating the requirement of hashmap_entry being the first member of a struct. Signed-off-by: Eric Wong <[email protected]> Reviewed-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f23a465 commit 939af16

23 files changed

+204
-115
lines changed

attr.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,14 @@ struct attr_hash_entry {
7070

7171
/* attr_hashmap comparison function */
7272
static int attr_hash_entry_cmp(const void *unused_cmp_data,
73-
const void *entry,
74-
const void *entry_or_key,
73+
const struct hashmap_entry *eptr,
74+
const struct hashmap_entry *entry_or_key,
7575
const void *unused_keydata)
7676
{
77-
const struct attr_hash_entry *a = entry;
78-
const struct attr_hash_entry *b = entry_or_key;
77+
const struct attr_hash_entry *a, *b;
78+
79+
a = container_of(eptr, const struct attr_hash_entry, ent);
80+
b = container_of(entry_or_key, const struct attr_hash_entry, ent);
7981
return (a->keylen != b->keylen) || strncmp(a->key, b->key, a->keylen);
8082
}
8183

builtin/describe.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,14 @@ static const char *prio_names[] = {
6464
};
6565

6666
static int commit_name_neq(const void *unused_cmp_data,
67-
const void *entry,
68-
const void *entry_or_key,
67+
const struct hashmap_entry *eptr,
68+
const struct hashmap_entry *entry_or_key,
6969
const void *peeled)
7070
{
71-
const struct commit_name *cn1 = entry;
72-
const struct commit_name *cn2 = entry_or_key;
71+
const struct commit_name *cn1, *cn2;
72+
73+
cn1 = container_of(eptr, const struct commit_name, entry);
74+
cn2 = container_of(entry_or_key, const struct commit_name, entry);
7375

7476
return !oideq(&cn1->peeled, peeled ? peeled : &cn2->peeled);
7577
}

builtin/difftool.c

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,15 @@ struct working_tree_entry {
125125
};
126126

127127
static int working_tree_entry_cmp(const void *unused_cmp_data,
128-
const void *entry,
129-
const void *entry_or_key,
128+
const struct hashmap_entry *eptr,
129+
const struct hashmap_entry *entry_or_key,
130130
const void *unused_keydata)
131131
{
132-
const struct working_tree_entry *a = entry;
133-
const struct working_tree_entry *b = entry_or_key;
132+
const struct working_tree_entry *a, *b;
133+
134+
a = container_of(eptr, const struct working_tree_entry, entry);
135+
b = container_of(entry_or_key, const struct working_tree_entry, entry);
136+
134137
return strcmp(a->path, b->path);
135138
}
136139

@@ -145,12 +148,14 @@ struct pair_entry {
145148
};
146149

147150
static int pair_cmp(const void *unused_cmp_data,
148-
const void *entry,
149-
const void *entry_or_key,
151+
const struct hashmap_entry *eptr,
152+
const struct hashmap_entry *entry_or_key,
150153
const void *unused_keydata)
151154
{
152-
const struct pair_entry *a = entry;
153-
const struct pair_entry *b = entry_or_key;
155+
const struct pair_entry *a, *b;
156+
157+
a = container_of(eptr, const struct pair_entry, entry);
158+
b = container_of(entry_or_key, const struct pair_entry, entry);
154159

155160
return strcmp(a->path, b->path);
156161
}
@@ -179,12 +184,14 @@ struct path_entry {
179184
};
180185

181186
static int path_entry_cmp(const void *unused_cmp_data,
182-
const void *entry,
183-
const void *entry_or_key,
187+
const struct hashmap_entry *eptr,
188+
const struct hashmap_entry *entry_or_key,
184189
const void *key)
185190
{
186-
const struct path_entry *a = entry;
187-
const struct path_entry *b = entry_or_key;
191+
const struct path_entry *a, *b;
192+
193+
a = container_of(eptr, const struct path_entry, entry);
194+
b = container_of(entry_or_key, const struct path_entry, entry);
188195

189196
return strcmp(a->path, key ? key : b->path);
190197
}

builtin/fast-export.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,15 @@ struct anonymized_entry {
126126
};
127127

128128
static int anonymized_entry_cmp(const void *unused_cmp_data,
129-
const void *va, const void *vb,
129+
const struct hashmap_entry *eptr,
130+
const struct hashmap_entry *entry_or_key,
130131
const void *unused_keydata)
131132
{
132-
const struct anonymized_entry *a = va, *b = vb;
133+
const struct anonymized_entry *a, *b;
134+
135+
a = container_of(eptr, const struct anonymized_entry, hash);
136+
b = container_of(entry_or_key, const struct anonymized_entry, hash);
137+
133138
return a->orig_len != b->orig_len ||
134139
memcmp(a->orig, b->orig, a->orig_len);
135140
}

builtin/fetch.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,13 +258,14 @@ struct refname_hash_entry {
258258
};
259259

260260
static int refname_hash_entry_cmp(const void *hashmap_cmp_fn_data,
261-
const void *e1_,
262-
const void *e2_,
261+
const struct hashmap_entry *eptr,
262+
const struct hashmap_entry *entry_or_key,
263263
const void *keydata)
264264
{
265-
const struct refname_hash_entry *e1 = e1_;
266-
const struct refname_hash_entry *e2 = e2_;
265+
const struct refname_hash_entry *e1, *e2;
267266

267+
e1 = container_of(eptr, const struct refname_hash_entry, ent);
268+
e2 = container_of(entry_or_key, const struct refname_hash_entry, ent);
268269
return strcmp(e1->refname, keydata ? keydata : e2->refname);
269270
}
270271

config.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1914,12 +1914,14 @@ static int configset_add_value(struct config_set *cs, const char *key, const cha
19141914
}
19151915

19161916
static int config_set_element_cmp(const void *unused_cmp_data,
1917-
const void *entry,
1918-
const void *entry_or_key,
1917+
const struct hashmap_entry *eptr,
1918+
const struct hashmap_entry *entry_or_key,
19191919
const void *unused_keydata)
19201920
{
1921-
const struct config_set_element *e1 = entry;
1922-
const struct config_set_element *e2 = entry_or_key;
1921+
const struct config_set_element *e1, *e2;
1922+
1923+
e1 = container_of(eptr, const struct config_set_element, ent);
1924+
e2 = container_of(entry_or_key, const struct config_set_element, ent);
19231925

19241926
return strcmp(e1->key, e2->key);
19251927
}

diff.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -933,16 +933,18 @@ static int cmp_in_block_with_wsd(const struct diff_options *o,
933933
}
934934

935935
static int moved_entry_cmp(const void *hashmap_cmp_fn_data,
936-
const void *entry,
937-
const void *entry_or_key,
936+
const struct hashmap_entry *eptr,
937+
const struct hashmap_entry *entry_or_key,
938938
const void *keydata)
939939
{
940940
const struct diff_options *diffopt = hashmap_cmp_fn_data;
941-
const struct moved_entry *a = entry;
942-
const struct moved_entry *b = entry_or_key;
941+
const struct moved_entry *a, *b;
943942
unsigned flags = diffopt->color_moved_ws_handling
944943
& XDF_WHITESPACE_FLAGS;
945944

945+
a = container_of(eptr, const struct moved_entry, ent);
946+
b = container_of(entry_or_key, const struct moved_entry, ent);
947+
946948
if (diffopt->color_moved_ws_handling &
947949
COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE)
948950
/*
@@ -1019,7 +1021,7 @@ static void pmb_advance_or_null(struct diff_options *o,
10191021
struct moved_entry *prev = pmb[i].match;
10201022
struct moved_entry *cur = (prev && prev->next_line) ?
10211023
prev->next_line : NULL;
1022-
if (cur && !hm->cmpfn(o, cur, match, NULL)) {
1024+
if (cur && !hm->cmpfn(o, &cur->ent, &match->ent, NULL)) {
10231025
pmb[i].match = cur;
10241026
} else {
10251027
pmb[i].match = NULL;

hashmap.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ static inline struct hashmap_entry **find_entry_ptr(const struct hashmap *map,
140140
}
141141

142142
static int always_equal(const void *unused_cmp_data,
143-
const void *unused1,
144-
const void *unused2,
143+
const struct hashmap_entry *unused1,
144+
const struct hashmap_entry *unused2,
145145
const void *unused_keydata)
146146
{
147147
return 0;
@@ -279,10 +279,15 @@ struct pool_entry {
279279
};
280280

281281
static int pool_entry_cmp(const void *unused_cmp_data,
282-
const struct pool_entry *e1,
283-
const struct pool_entry *e2,
284-
const unsigned char *keydata)
282+
const struct hashmap_entry *eptr,
283+
const struct hashmap_entry *entry_or_key,
284+
const void *keydata)
285285
{
286+
const struct pool_entry *e1, *e2;
287+
288+
e1 = container_of(eptr, const struct pool_entry, ent);
289+
e2 = container_of(entry_or_key, const struct pool_entry, ent);
290+
286291
return e1->data != keydata &&
287292
(e1->len != e2->len || memcmp(e1->data, keydata, e1->len));
288293
}
@@ -294,7 +299,7 @@ const void *memintern(const void *data, size_t len)
294299

295300
/* initialize string pool hashmap */
296301
if (!map.tablesize)
297-
hashmap_init(&map, (hashmap_cmp_fn) pool_entry_cmp, NULL, 0);
302+
hashmap_init(&map, pool_entry_cmp, NULL, 0);
298303

299304
/* lookup interned string in pool */
300305
hashmap_entry_init(&key.ent, memhash(data, len));

hashmap.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,16 @@
2121
* #define COMPARE_VALUE 1
2222
*
2323
* static int long2string_cmp(const void *hashmap_cmp_fn_data,
24-
* const struct long2string *e1,
25-
* const struct long2string *e2,
24+
* const struct hashmap_entry *eptr,
25+
* const struct hashmap_entry *entry_or_key,
2626
* const void *keydata)
2727
* {
2828
* const char *string = keydata;
2929
* unsigned flags = *(unsigned *)hashmap_cmp_fn_data;
30+
* const struct long2string *e1, *e2;
31+
*
32+
* e1 = container_of(eptr, const struct long2string, ent);
33+
* e2 = container_of(entry_or_key, const struct long2string, ent);
3034
*
3135
* if (flags & COMPARE_VALUE)
3236
* return e1->key != e2->key ||
@@ -41,7 +45,7 @@
4145
* char value[255], action[32];
4246
* unsigned flags = 0;
4347
*
44-
* hashmap_init(&map, (hashmap_cmp_fn) long2string_cmp, &flags, 0);
48+
* hashmap_init(&map, long2string_cmp, &flags, 0);
4549
*
4650
* while (scanf("%s %ld %s", action, &key, value)) {
4751
*
@@ -172,7 +176,8 @@ struct hashmap_entry {
172176
* The `hashmap_cmp_fn_data` entry is the pointer given in the init function.
173177
*/
174178
typedef int (*hashmap_cmp_fn)(const void *hashmap_cmp_fn_data,
175-
const void *entry, const void *entry_or_key,
179+
const struct hashmap_entry *entry,
180+
const struct hashmap_entry *entry_or_key,
176181
const void *keydata);
177182

178183
/*

merge-recursive.c

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,16 @@ struct path_hashmap_entry {
3535
};
3636

3737
static int path_hashmap_cmp(const void *cmp_data,
38-
const void *entry,
39-
const void *entry_or_key,
38+
const struct hashmap_entry *eptr,
39+
const struct hashmap_entry *entry_or_key,
4040
const void *keydata)
4141
{
42-
const struct path_hashmap_entry *a = entry;
43-
const struct path_hashmap_entry *b = entry_or_key;
42+
const struct path_hashmap_entry *a, *b;
4443
const char *key = keydata;
4544

45+
a = container_of(eptr, const struct path_hashmap_entry, e);
46+
b = container_of(entry_or_key, const struct path_hashmap_entry, e);
47+
4648
if (ignore_case)
4749
return strcasecmp(a->path, key ? key : b->path);
4850
else
@@ -68,12 +70,14 @@ static struct dir_rename_entry *dir_rename_find_entry(struct hashmap *hashmap,
6870
}
6971

7072
static int dir_rename_cmp(const void *unused_cmp_data,
71-
const void *entry,
72-
const void *entry_or_key,
73+
const struct hashmap_entry *eptr,
74+
const struct hashmap_entry *entry_or_key,
7375
const void *unused_keydata)
7476
{
75-
const struct dir_rename_entry *e1 = entry;
76-
const struct dir_rename_entry *e2 = entry_or_key;
77+
const struct dir_rename_entry *e1, *e2;
78+
79+
e1 = container_of(eptr, const struct dir_rename_entry, ent);
80+
e2 = container_of(entry_or_key, const struct dir_rename_entry, ent);
7781

7882
return strcmp(e1->dir, e2->dir);
7983
}
@@ -104,17 +108,22 @@ static struct collision_entry *collision_find_entry(struct hashmap *hashmap,
104108
struct collision_entry, ent);
105109
}
106110

107-
static int collision_cmp(void *unused_cmp_data,
108-
const struct collision_entry *e1,
109-
const struct collision_entry *e2,
111+
static int collision_cmp(const void *unused_cmp_data,
112+
const struct hashmap_entry *eptr,
113+
const struct hashmap_entry *entry_or_key,
110114
const void *unused_keydata)
111115
{
116+
const struct collision_entry *e1, *e2;
117+
118+
e1 = container_of(eptr, const struct collision_entry, ent);
119+
e2 = container_of(entry_or_key, const struct collision_entry, ent);
120+
112121
return strcmp(e1->target_file, e2->target_file);
113122
}
114123

115124
static void collision_init(struct hashmap *map)
116125
{
117-
hashmap_init(map, (hashmap_cmp_fn) collision_cmp, NULL, 0);
126+
hashmap_init(map, collision_cmp, NULL, 0);
118127
}
119128

120129
static void flush_output(struct merge_options *opt)

name-hash.c

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

1919
static int dir_entry_cmp(const void *unused_cmp_data,
20-
const void *entry,
21-
const void *entry_or_key,
20+
const struct hashmap_entry *eptr,
21+
const struct hashmap_entry *entry_or_key,
2222
const void *keydata)
2323
{
24-
const struct dir_entry *e1 = entry;
25-
const struct dir_entry *e2 = entry_or_key;
24+
const struct dir_entry *e1, *e2;
2625
const char *name = keydata;
2726

27+
e1 = container_of(eptr, const struct dir_entry, ent);
28+
e2 = container_of(entry_or_key, const struct dir_entry, ent);
29+
2830
return e1->namelen != e2->namelen || strncasecmp(e1->name,
2931
name ? name : e2->name, e1->namelen);
3032
}
@@ -115,12 +117,15 @@ static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)
115117
}
116118

117119
static int cache_entry_cmp(const void *unused_cmp_data,
118-
const void *entry,
119-
const void *entry_or_key,
120+
const struct hashmap_entry *eptr,
121+
const struct hashmap_entry *entry_or_key,
120122
const void *remove)
121123
{
122-
const struct cache_entry *ce1 = entry;
123-
const struct cache_entry *ce2 = entry_or_key;
124+
const struct cache_entry *ce1, *ce2;
125+
126+
ce1 = container_of(eptr, const struct cache_entry, ent);
127+
ce2 = container_of(entry_or_key, const struct cache_entry, ent);
128+
124129
/*
125130
* For remove_name_hash, find the exact entry (pointer equality); for
126131
* index_file_exists, find all entries with matching hash code and

oidmap.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22
#include "oidmap.h"
33

44
static int oidmap_neq(const void *hashmap_cmp_fn_data,
5-
const void *entry, const void *entry_or_key,
5+
const struct hashmap_entry *e1,
6+
const struct hashmap_entry *e2,
67
const void *keydata)
78
{
8-
const struct oidmap_entry *entry_ = entry;
9+
const struct oidmap_entry *a, *b;
10+
11+
a = container_of(e1, const struct oidmap_entry, internal_entry);
12+
b = container_of(e2, const struct oidmap_entry, internal_entry);
13+
914
if (keydata)
10-
return !oideq(&entry_->oid, (const struct object_id *) keydata);
11-
return !oideq(&entry_->oid,
12-
&((const struct oidmap_entry *) entry_or_key)->oid);
15+
return !oideq(&a->oid, (const struct object_id *) keydata);
16+
return !oideq(&a->oid, &b->oid);
1317
}
1418

1519
void oidmap_init(struct oidmap *map, size_t initial_size)

0 commit comments

Comments
 (0)