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 <e@80x24.org> Reviewed-by: Derrick Stolee <stolee@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Eric Wong committed Oct 6, 2019 at 23:30 UTC f0e63c41139f8982add435536d39aff6f3d4ca98
6 files changed +73 -40
diff.c
+9 -12
@@ -1035,10 +1035,8 @@ static void pmb_advance_or_null_multi_match(struct diff_options *o,
1035 {
1036 int i;
1037 char *got_match = xcalloc(1, pmb_nr);
1038 - struct hashmap_entry *ent;
1038
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) {
1040 for (i = 0; i < pmb_nr; i++) {
1041 struct moved_entry *prev = pmb[i].match;
1042 struct moved_entry *cur = (prev && prev->next_line) ?
@@ -1137,9 +1135,8 @@ static void mark_color_as_moved(struct diff_options *o,
1135
1136 for (n = 0; n < o->emitted_symbols->nr; n++) {
1137 struct hashmap *hm = NULL;
1140 - struct hashmap_entry *ent = NULL;
1138 struct moved_entry *key;
1142 - struct moved_entry *match;
1139 + struct moved_entry *match = NULL;
1140 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
1141 enum diff_symbol last_symbol = 0;
1142
@@ -1147,20 +1144,22 @@ static void mark_color_as_moved(struct diff_options *o,
1144 case DIFF_SYMBOL_PLUS:
1145 hm = del_lines;
1146 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);
1149 free(key);
1150 break;
1151 case DIFF_SYMBOL_MINUS:
1152 hm = add_lines;
1153 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);
1156 free(key);
1157 break;
1158 default:
1159 flipped_block = 0;
1160 }
1161
1163 - if (!ent) {
1162 + if (!match) {
1163 int i;
1164
1165 adjust_last_block(o, n, block_length);
@@ -1172,7 +1171,6 @@ static void mark_color_as_moved(struct diff_options *o,
1171 last_symbol = l->s;
1172 continue;
1173 }
1175 - match = container_of(ent, struct moved_entry, ent);
1174
1175 if (o->color_moved == COLOR_MOVED_PLAIN) {
1176 last_symbol = l->s;
@@ -1193,9 +1191,8 @@ static void mark_color_as_moved(struct diff_options *o,
1191 * The current line is the start of a new block.
1192 * Setup the set of potential blocks.
1193 */
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) {
1196 ALLOC_GROW(pmb, pmb_nr + 1, pmb_alloc);
1197 if (o->color_moved_ws_handling &
1198 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) {
diffcore-rename.c
+5 -9
@@ -274,23 +274,19 @@ static int find_identical_files(struct hashmap *srcs,
274 struct diff_options *options)
275 {
276 int renames = 0;
277 - struct hashmap_entry *ent;
277 struct diff_filespec *target = rename_dst[dst_index].two;
278 struct file_similarity *p, *best = NULL;
279 int i = 100, best_score = -1;
280 + unsigned int hash = hash_filespec(options->repo, target);
281
282 /*
283 * Find the best source match for specified destination.
284 */
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) {
288 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;
290
291 /* False hash collision? */
292 if (!oideq(&source->oid, &target->oid))
git-compat-util.h
+15
@@ -1322,4 +1322,19 @@ void unleak_memory(const void *ptr, size_t len);
1322 #define container_of(ptr, type, member) \
1323 ((type *) ((char *)(ptr) - offsetof(type, member)))
1324
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 +
1340 #endif
hashmap.h
+34 -6
@@ -55,17 +55,15 @@
55 *
56 * if (!strcmp("print_all_by_key", action)) {
57 * struct long2string k, *e;
58 - * struct hashmap_entry *ent;
58 * hashmap_entry_init(&k->ent, memhash(&key, sizeof(long)));
59 * k.key = key;
60 *
61 * 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) {
64 * 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))) {
67 * printf("found more: %ld %s\n", e->key, e->value);
68 * }
69 * }
@@ -387,6 +385,36 @@ static inline void *hashmap_iter_first(struct hashmap *map,
385 return hashmap_iter_next(iter);
386 }
387
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 +
418 /*
419 * Disable item counting and automatic rehashing when adding/removing items.
420 *
name-hash.c
+5 -6
@@ -702,17 +702,16 @@ void adjust_dirname_case(struct index_state *istate, char *name)
702 struct cache_entry *index_file_exists(struct index_state *istate, const char *name, int namelen, int icase)
703 {
704 struct cache_entry *ce;
705 - struct hashmap_entry *ent;
705 + unsigned int hash = memihash(name, namelen);
706
707 lazy_init_name_hash(istate);
708
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) {
713 if (same_name(ce, name, namelen, icase))
714 return ce;
715 - ent = hashmap_get_next(&istate->name_hash, ent);
715 }
716 return NULL;
717 }
t/helper/test-hashmap.c
+5 -7
@@ -194,18 +194,16 @@ int cmd__hashmap(int argc, const char **argv)
194 free(entry);
195
196 } else if (!strcmp("get", cmd) && p1) {
197 - struct hashmap_entry *e;
198 -
197 /* 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);
200
201 /* print result */
203 - if (!e)
202 + if (!entry)
203 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) {
206 puts(get_value(entry));
208 - e = hashmap_get_next(&map, e);
207 }
208
209 } else if (!strcmp("remove", cmd) && p1) {