hashmap_get_next returns "struct hashmap_entry *"

This is a step towards removing the requirement for hashmap_entry being the first field of a struct. 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 6bcbdfb277bdc81b5ad6996b3fb005382a35c2ee
6 files changed +39 -23
diff.c
+12 -7
@@ -1035,8 +1035,10 @@ 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;
1039
1039 - for (; match; match = hashmap_get_next(hm, &match->ent)) {
1040 + for (ent = &match->ent; ent; ent = hashmap_get_next(hm, ent)) {
1041 + match = container_of(ent, struct moved_entry, ent);
1042 for (i = 0; i < pmb_nr; i++) {
1043 struct moved_entry *prev = pmb[i].match;
1044 struct moved_entry *cur = (prev && prev->next_line) ?
@@ -1135,8 +1137,9 @@ static void mark_color_as_moved(struct diff_options *o,
1137
1138 for (n = 0; n < o->emitted_symbols->nr; n++) {
1139 struct hashmap *hm = NULL;
1140 + struct hashmap_entry *ent = NULL;
1141 struct moved_entry *key;
1139 - struct moved_entry *match = NULL;
1142 + struct moved_entry *match;
1143 struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
1144 enum diff_symbol last_symbol = 0;
1145
@@ -1144,20 +1147,20 @@ static void mark_color_as_moved(struct diff_options *o,
1147 case DIFF_SYMBOL_PLUS:
1148 hm = del_lines;
1149 key = prepare_entry(o, n);
1147 - match = hashmap_get(hm, &key->ent, NULL);
1150 + ent = hashmap_get(hm, &key->ent, NULL);
1151 free(key);
1152 break;
1153 case DIFF_SYMBOL_MINUS:
1154 hm = add_lines;
1155 key = prepare_entry(o, n);
1153 - match = hashmap_get(hm, &key->ent, NULL);
1156 + ent = hashmap_get(hm, &key->ent, NULL);
1157 free(key);
1158 break;
1159 default:
1160 flipped_block = 0;
1161 }
1162
1160 - if (!match) {
1163 + if (!ent) {
1164 int i;
1165
1166 adjust_last_block(o, n, block_length);
@@ -1169,6 +1172,7 @@ static void mark_color_as_moved(struct diff_options *o,
1172 last_symbol = l->s;
1173 continue;
1174 }
1175 + match = container_of(ent, struct moved_entry, ent);
1176
1177 if (o->color_moved == COLOR_MOVED_PLAIN) {
1178 last_symbol = l->s;
@@ -1189,8 +1193,9 @@ static void mark_color_as_moved(struct diff_options *o,
1193 * The current line is the start of a new block.
1194 * Setup the set of potential blocks.
1195 */
1192 - for (; match; match = hashmap_get_next(hm,
1193 - &match->ent)) {
1196 + for (; ent; ent = hashmap_get_next(hm, ent)) {
1197 + match = container_of(ent, struct moved_entry,
1198 + ent);
1199 ALLOC_GROW(pmb, pmb_nr + 1, pmb_alloc);
1200 if (o->color_moved_ws_handling &
1201 COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) {
diffcore-rename.c
+7 -4
@@ -274,7 +274,7 @@ static int find_identical_files(struct hashmap *srcs,
274 struct diff_options *options)
275 {
276 int renames = 0;
277 -
277 + struct hashmap_entry *ent;
278 struct diff_filespec *target = rename_dst[dst_index].two;
279 struct file_similarity *p, *best = NULL;
280 int i = 100, best_score = -1;
@@ -282,12 +282,15 @@ static int find_identical_files(struct hashmap *srcs,
282 /*
283 * Find the best source match for specified destination.
284 */
285 - p = hashmap_get_from_hash(srcs,
285 + ent = hashmap_get_from_hash(srcs,
286 hash_filespec(options->repo, target),
287 NULL);
288 - for (; p; p = hashmap_get_next(srcs, &p->entry)) {
288 + for (; ent; ent = hashmap_get_next(srcs, ent)) {
289 int score;
290 - struct diff_filespec *source = p->filespec;
290 + struct diff_filespec *source;
291 +
292 + p = container_of(ent, struct file_similarity, entry);
293 + source = p->filespec;
294
295 /* False hash collision? */
296 if (!oideq(&source->oid, &target->oid))
hashmap.c
+1 -1
@@ -192,7 +192,7 @@ void *hashmap_get(const struct hashmap *map, const struct hashmap_entry *key,
192 return *find_entry_ptr(map, key, keydata);
193 }
194
195 -void *hashmap_get_next(const struct hashmap *map,
195 +struct hashmap_entry *hashmap_get_next(const struct hashmap *map,
196 const struct hashmap_entry *entry)
197 {
198 struct hashmap_entry *e = entry->next;
hashmap.h
+8 -4
@@ -55,15 +55,19 @@
55 *
56 * if (!strcmp("print_all_by_key", action)) {
57 * struct long2string k, *e;
58 + * struct hashmap_entry *ent;
59 * hashmap_entry_init(&k->ent, memhash(&key, sizeof(long)));
60 * k.key = key;
61 *
62 * flags &= ~COMPARE_VALUE;
62 - * e = hashmap_get(&map, &k, NULL);
63 - * if (e) {
63 + * ent = hashmap_get(&map, &k, NULL);
64 + * if (ent) {
65 + * e = container_of(ent, struct long2string, ent);
66 * printf("first: %ld %s\n", e->key, e->value);
65 - * while ((e = hashmap_get_next(&map, e)))
67 + * while ((ent = hashmap_get_next(&map, ent))) {
68 + * e = container_of(ent, struct long2string, ent);
69 * printf("found more: %ld %s\n", e->key, e->value);
70 + * }
71 * }
72 * }
73 *
@@ -320,7 +324,7 @@ static inline void *hashmap_get_from_hash(const struct hashmap *map,
324 * `entry` is the hashmap_entry to start the search from, obtained via a previous
325 * call to `hashmap_get` or `hashmap_get_next`.
326 */
323 -void *hashmap_get_next(const struct hashmap *map,
327 +struct hashmap_entry *hashmap_get_next(const struct hashmap *map,
328 const struct hashmap_entry *entry);
329
330 /*
name-hash.c
+5 -3
@@ -702,15 +702,17 @@ 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;
706
707 lazy_init_name_hash(istate);
708
708 - ce = hashmap_get_from_hash(&istate->name_hash,
709 + ent = hashmap_get_from_hash(&istate->name_hash,
710 memihash(name, namelen), NULL);
710 - while (ce) {
711 + while (ent) {
712 + ce = container_of(ent, struct cache_entry, ent);
713 if (same_name(ce, name, namelen, icase))
714 return ce;
713 - ce = hashmap_get_next(&istate->name_hash, &ce->ent);
715 + ent = hashmap_get_next(&istate->name_hash, ent);
716 }
717 return NULL;
718 }
t/helper/test-hashmap.c
+6 -4
@@ -194,16 +194,18 @@ 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
199 /* lookup entry in hashmap */
199 - entry = hashmap_get_from_hash(&map, hash, p1);
200 + e = hashmap_get_from_hash(&map, hash, p1);
201
202 /* print result */
202 - if (!entry)
203 + if (!e)
204 puts("NULL");
204 - while (entry) {
205 + while (e) {
206 + entry = container_of(e, struct test_entry, ent);
207 puts(get_value(entry));
206 - entry = hashmap_get_next(&map, &entry->ent);
208 + e = hashmap_get_next(&map, e);
209 }
210
211 } else if (!strcmp("remove", cmd) && p1) {