Skip to content

Commit f0e63c4

Browse files
Eric Wonggitster
authored andcommitted
hashmap: use *_entry APIs to wrap container_of
Using `container_of' can be verbose and choosing names for intermediate "struct hashmap_entry" pointers is a hard problem. So introduce "*_entry" APIs inspired by similar linked-list APIs in the Linux kernel. Unfortunately, `__typeof__' is not portable C, so we need an extra parameter to specify the type. Signed-off-by: Eric Wong <[email protected]> Reviewed-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6bcbdfb commit f0e63c4

File tree

6 files changed

+73
-40
lines changed

6 files changed

+73
-40
lines changed

diff.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,10 +1035,8 @@ static void pmb_advance_or_null_multi_match(struct diff_options *o,
10351035
{
10361036
int i;
10371037
char *got_match = xcalloc(1, pmb_nr);
1038-
struct hashmap_entry *ent;
10391038

1040-
for (ent = &match->ent; ent; ent = hashmap_get_next(hm, ent)) {
1041-
match = container_of(ent, struct moved_entry, ent);
1039+
hashmap_for_each_entry_from(hm, match, struct moved_entry, ent) {
10421040
for (i = 0; i < pmb_nr; i++) {
10431041
struct moved_entry *prev = pmb[i].match;
10441042
struct moved_entry *cur = (prev && prev->next_line) ?
@@ -1137,30 +1135,31 @@ static void mark_color_as_moved(struct diff_options *o,
11371135

11381136
for (n = 0; n < o->emitted_symbols->nr; n++) {
11391137
struct hashmap *hm = NULL;
1140-
struct hashmap_entry *ent = NULL;
11411138
struct moved_entry *key;
1142-
struct moved_entry *match;
1139+
struct moved_entry *match = NULL;
11431140
struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
11441141
enum diff_symbol last_symbol = 0;
11451142

11461143
switch (l->s) {
11471144
case DIFF_SYMBOL_PLUS:
11481145
hm = del_lines;
11491146
key = prepare_entry(o, n);
1150-
ent = hashmap_get(hm, &key->ent, NULL);
1147+
match = hashmap_get_entry(hm, key, NULL,
1148+
struct moved_entry, ent);
11511149
free(key);
11521150
break;
11531151
case DIFF_SYMBOL_MINUS:
11541152
hm = add_lines;
11551153
key = prepare_entry(o, n);
1156-
ent = hashmap_get(hm, &key->ent, NULL);
1154+
match = hashmap_get_entry(hm, key, NULL,
1155+
struct moved_entry, ent);
11571156
free(key);
11581157
break;
11591158
default:
11601159
flipped_block = 0;
11611160
}
11621161

1163-
if (!ent) {
1162+
if (!match) {
11641163
int i;
11651164

11661165
adjust_last_block(o, n, block_length);
@@ -1172,7 +1171,6 @@ static void mark_color_as_moved(struct diff_options *o,
11721171
last_symbol = l->s;
11731172
continue;
11741173
}
1175-
match = container_of(ent, struct moved_entry, ent);
11761174

11771175
if (o->color_moved == COLOR_MOVED_PLAIN) {
11781176
last_symbol = l->s;
@@ -1193,9 +1191,8 @@ static void mark_color_as_moved(struct diff_options *o,
11931191
* The current line is the start of a new block.
11941192
* Setup the set of potential blocks.
11951193
*/
1196-
for (; ent; ent = hashmap_get_next(hm, ent)) {
1197-
match = container_of(ent, struct moved_entry,
1198-
ent);
1194+
hashmap_for_each_entry_from(hm, match,
1195+
struct moved_entry, ent) {
11991196
ALLOC_GROW(pmb, pmb_nr + 1, pmb_alloc);
12001197
if (o->color_moved_ws_handling &
12011198
COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) {

diffcore-rename.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -274,23 +274,19 @@ static int find_identical_files(struct hashmap *srcs,
274274
struct diff_options *options)
275275
{
276276
int renames = 0;
277-
struct hashmap_entry *ent;
278277
struct diff_filespec *target = rename_dst[dst_index].two;
279278
struct file_similarity *p, *best = NULL;
280279
int i = 100, best_score = -1;
280+
unsigned int hash = hash_filespec(options->repo, target);
281281

282282
/*
283283
* Find the best source match for specified destination.
284284
*/
285-
ent = hashmap_get_from_hash(srcs,
286-
hash_filespec(options->repo, target),
287-
NULL);
288-
for (; ent; ent = hashmap_get_next(srcs, ent)) {
285+
p = hashmap_get_entry_from_hash(srcs, hash, NULL,
286+
struct file_similarity, entry);
287+
hashmap_for_each_entry_from(srcs, p, struct file_similarity, entry) {
289288
int score;
290-
struct diff_filespec *source;
291-
292-
p = container_of(ent, struct file_similarity, entry);
293-
source = p->filespec;
289+
struct diff_filespec *source = p->filespec;
294290

295291
/* False hash collision? */
296292
if (!oideq(&source->oid, &target->oid))

git-compat-util.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,4 +1322,19 @@ void unleak_memory(const void *ptr, size_t len);
13221322
#define container_of(ptr, type, member) \
13231323
((type *) ((char *)(ptr) - offsetof(type, member)))
13241324

1325+
/*
1326+
* helper function for `container_of_or_null' to avoid multiple
1327+
* evaluation of @ptr
1328+
*/
1329+
static inline void *container_of_or_null_offset(void *ptr, size_t offset)
1330+
{
1331+
return ptr ? (char *)ptr - offset : NULL;
1332+
}
1333+
1334+
/*
1335+
* like `container_of', but allows returned value to be NULL
1336+
*/
1337+
#define container_of_or_null(ptr, type, member) \
1338+
(type *)container_of_or_null_offset(ptr, offsetof(type, member))
1339+
13251340
#endif

hashmap.h

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,15 @@
5555
*
5656
* if (!strcmp("print_all_by_key", action)) {
5757
* struct long2string k, *e;
58-
* struct hashmap_entry *ent;
5958
* hashmap_entry_init(&k->ent, memhash(&key, sizeof(long)));
6059
* k.key = key;
6160
*
6261
* flags &= ~COMPARE_VALUE;
63-
* ent = hashmap_get(&map, &k, NULL);
64-
* if (ent) {
65-
* e = container_of(ent, struct long2string, ent);
62+
* e = hashmap_get_entry(&map, &k, NULL, struct long2string, ent);
63+
* if (e) {
6664
* printf("first: %ld %s\n", e->key, e->value);
67-
* while ((ent = hashmap_get_next(&map, ent))) {
68-
* e = container_of(ent, struct long2string, ent);
65+
* while ((e = hashmap_get_next_entry(&map, e,
66+
* struct long2string, ent))) {
6967
* printf("found more: %ld %s\n", e->key, e->value);
7068
* }
7169
* }
@@ -387,6 +385,36 @@ static inline void *hashmap_iter_first(struct hashmap *map,
387385
return hashmap_iter_next(iter);
388386
}
389387

388+
/*
389+
* returns a @pointer of @type matching @keyvar, or NULL if nothing found.
390+
* @keyvar is a pointer of @type
391+
* @member is the name of the "struct hashmap_entry" field in @type
392+
*/
393+
#define hashmap_get_entry(map, keyvar, keydata, type, member) \
394+
container_of_or_null(hashmap_get(map, &(keyvar)->member, keydata), \
395+
type, member)
396+
397+
#define hashmap_get_entry_from_hash(map, hash, keydata, type, member) \
398+
container_of_or_null(hashmap_get_from_hash(map, hash, keydata), \
399+
type, member)
400+
/*
401+
* returns the next equal @type pointer to @var, or NULL if not found.
402+
* @var is a pointer of @type
403+
* @member is the name of the "struct hashmap_entry" field in @type
404+
*/
405+
#define hashmap_get_next_entry(map, var, type, member) \
406+
container_of_or_null(hashmap_get_next(map, &(var)->member), \
407+
type, member)
408+
409+
/*
410+
* iterate @map starting from @var, where @var is a pointer of @type
411+
* and @member is the name of the "struct hashmap_entry" field in @type
412+
*/
413+
#define hashmap_for_each_entry_from(map, var, type, member) \
414+
for (; \
415+
var; \
416+
var = hashmap_get_next_entry(map, var, type, member))
417+
390418
/*
391419
* Disable item counting and automatic rehashing when adding/removing items.
392420
*

name-hash.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -702,17 +702,16 @@ void adjust_dirname_case(struct index_state *istate, char *name)
702702
struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int icase)
703703
{
704704
struct cache_entry *ce;
705-
struct hashmap_entry *ent;
705+
unsigned int hash = memihash(name, namelen);
706706

707707
lazy_init_name_hash(istate);
708708

709-
ent = hashmap_get_from_hash(&istate->name_hash,
710-
memihash(name, namelen), NULL);
711-
while (ent) {
712-
ce = container_of(ent, struct cache_entry, ent);
709+
ce = hashmap_get_entry_from_hash(&istate->name_hash, hash, NULL,
710+
struct cache_entry, ent);
711+
hashmap_for_each_entry_from(&istate->name_hash, ce,
712+
struct cache_entry, ent) {
713713
if (same_name(ce, name, namelen, icase))
714714
return ce;
715-
ent = hashmap_get_next(&istate->name_hash, ent);
716715
}
717716
return NULL;
718717
}

t/helper/test-hashmap.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,18 +194,16 @@ int cmd__hashmap(int argc, const char **argv)
194194
free(entry);
195195

196196
} else if (!strcmp("get", cmd) && p1) {
197-
struct hashmap_entry *e;
198-
199197
/* lookup entry in hashmap */
200-
e = hashmap_get_from_hash(&map, hash, p1);
198+
entry = hashmap_get_entry_from_hash(&map, hash, p1,
199+
struct test_entry, ent);
201200

202201
/* print result */
203-
if (!e)
202+
if (!entry)
204203
puts("NULL");
205-
while (e) {
206-
entry = container_of(e, struct test_entry, ent);
204+
hashmap_for_each_entry_from(&map, entry,
205+
struct test_entry, ent) {
207206
puts(get_value(entry));
208-
e = hashmap_get_next(&map, e);
209207
}
210208

211209
} else if (!strcmp("remove", cmd) && p1) {

0 commit comments

Comments
 (0)