hashmap: remove type arg from hashmap_{get,put,remove}_entry
Since these macros already take a `keyvar' pointer of a known type, we can rely on OFFSETOF_VAR to get the correct offset without relying on non-portable `__typeof__' and `offsetof'. Argument order is also rearranged, so `keyvar' and `member' are sequential as they are used as: `keyvar->member' 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
404ab78e39fc74c4eb604b6003642ed264f687a6
17 files changed
+56
-55
attr.c
+1
-1
@@ -103,7 +103,7 @@ static void *attr_hashmap_get(struct attr_hashmap *map,
103
hashmap_entry_init(&k.ent, memhash(key, keylen));
104
k.key = key;
105
k.keylen = keylen;
106
- e = hashmap_get_entry(&map->map, &k, NULL, struct attr_hash_entry, ent);
106
+ e = hashmap_get_entry(&map->map, &k, ent, NULL);
107
108
return e ? e->value : NULL;
109
}
blame.c
+4
-6
@@ -419,8 +419,8 @@ static void get_fingerprint(struct fingerprint *result,
419
continue;
420
hashmap_entry_init(&entry->entry, hash);
421
422
- found_entry = hashmap_get_entry(&result->map, entry, NULL,
423
- struct fingerprint_entry, entry);
422
+ found_entry = hashmap_get_entry(&result->map, entry,
423
+ /* member name */ entry, NULL);
424
if (found_entry) {
425
found_entry->count += 1;
426
} else {
@@ -452,8 +452,7 @@ static int fingerprint_similarity(struct fingerprint *a, struct fingerprint *b)
452
453
hashmap_for_each_entry(&b->map, &iter, entry_b,
454
entry /* member name */) {
455
- entry_a = hashmap_get_entry(&a->map, entry_b, NULL,
456
- struct fingerprint_entry, entry);
455
+ entry_a = hashmap_get_entry(&a->map, entry_b, entry, NULL);
456
if (entry_a) {
457
intersection += entry_a->count < entry_b->count ?
458
entry_a->count : entry_b->count;
@@ -474,8 +473,7 @@ static void fingerprint_subtract(struct fingerprint *a, struct fingerprint *b)
473
474
hashmap_for_each_entry(&b->map, &iter, entry_b,
475
entry /* member name */) {
477
- entry_a = hashmap_get_entry(&a->map, entry_b, NULL,
478
- struct fingerprint_entry, entry);
476
+ entry_a = hashmap_get_entry(&a->map, entry_b, entry, NULL);
477
if (entry_a) {
478
if (entry_a->count <= entry_b->count)
479
hashmap_remove(&a->map, &entry_b->entry, NULL);
builtin/difftool.c
+1
-1
@@ -167,7 +167,7 @@ static void add_left_or_right(struct hashmap *map, const char *path,
167
168
FLEX_ALLOC_STR(e, path, path);
169
hashmap_entry_init(&e->entry, strhash(path));
170
- existing = hashmap_get_entry(map, e, NULL, struct pair_entry, entry);
170
+ existing = hashmap_get_entry(map, e, entry, NULL);
171
if (existing) {
172
free(e);
173
e = existing;
builtin/fast-export.c
+1
-1
@@ -156,7 +156,7 @@ static const void *anonymize_mem(struct hashmap *map,
156
hashmap_entry_init(&key.hash, memhash(orig, *len));
157
key.orig = orig;
158
key.orig_len = *len;
159
- ret = hashmap_get_entry(map, &key, NULL, struct anonymized_entry, hash);
159
+ ret = hashmap_get_entry(map, &key, hash, NULL);
160
161
if (!ret) {
162
ret = xmalloc(sizeof(*ret));
config.c
+1
-2
@@ -1863,8 +1863,7 @@ static struct config_set_element *configset_find_element(struct config_set *cs,
1863
1864
hashmap_entry_init(&k.ent, strhash(normalized_key));
1865
k.key = normalized_key;
1866
- found_entry = hashmap_get_entry(&cs->config_hash, &k, NULL,
1867
- struct config_set_element, ent);
1866
+ found_entry = hashmap_get_entry(&cs->config_hash, &k, ent, NULL);
1867
free(normalized_key);
1868
return found_entry;
1869
}
diff.c
+2
-4
@@ -1146,15 +1146,13 @@ static void mark_color_as_moved(struct diff_options *o,
1146
case DIFF_SYMBOL_PLUS:
1147
hm = del_lines;
1148
key = prepare_entry(o, n);
1149
- match = hashmap_get_entry(hm, key, NULL,
1150
- struct moved_entry, ent);
1149
+ match = hashmap_get_entry(hm, key, ent, NULL);
1150
free(key);
1151
break;
1152
case DIFF_SYMBOL_MINUS:
1153
hm = add_lines;
1154
key = prepare_entry(o, n);
1156
- match = hashmap_get_entry(hm, key, NULL,
1157
- struct moved_entry, ent);
1155
+ match = hashmap_get_entry(hm, key, ent, NULL);
1156
free(key);
1157
break;
1158
default:
hashmap.c
+1
-1
@@ -311,7 +311,7 @@ const void *memintern(const void *data, size_t len)
311
/* lookup interned string in pool */
312
hashmap_entry_init(&key.ent, memhash(data, len));
313
key.len = len;
314
- e = hashmap_get_entry(&map, &key, data, struct pool_entry, ent);
314
+ e = hashmap_get_entry(&map, &key, ent, data);
315
if (!e) {
316
/* not found: create it */
317
FLEX_ALLOC_MEM(e, data, data, len);
hashmap.h
+33
-12
@@ -63,7 +63,7 @@
63
* k.key = key;
64
*
65
* flags &= ~COMPARE_VALUE;
66
- * e = hashmap_get_entry(&map, &k, NULL, struct long2string, ent);
66
+ * e = hashmap_get_entry(&map, &k, ent, NULL);
67
* if (e) {
68
* printf("first: %ld %s\n", e->key, e->value);
69
* while ((e = hashmap_get_next_entry(&map, e,
@@ -359,8 +359,17 @@ void hashmap_add(struct hashmap *map, struct hashmap_entry *entry);
359
struct hashmap_entry *hashmap_put(struct hashmap *map,
360
struct hashmap_entry *entry);
361
362
-#define hashmap_put_entry(map, keyvar, type, member) \
363
- container_of_or_null(hashmap_put(map, &(keyvar)->member), type, member)
362
+/*
363
+ * Adds or replaces a hashmap entry contained within @keyvar,
364
+ * where @keyvar is a pointer to a struct containing a
365
+ * "struct hashmap_entry" @member.
366
+ *
367
+ * Returns the replaced pointer which is of the same type as @keyvar,
368
+ * or NULL if not found.
369
+ */
370
+#define hashmap_put_entry(map, keyvar, member) \
371
+ container_of_or_null_offset(hashmap_put(map, &(keyvar)->member), \
372
+ OFFSETOF_VAR(keyvar, member))
373
374
/*
375
* Removes a hashmap entry matching the specified key. If the hashmap contains
@@ -373,9 +382,20 @@ struct hashmap_entry *hashmap_remove(struct hashmap *map,
382
const struct hashmap_entry *key,
383
const void *keydata);
384
376
-#define hashmap_remove_entry(map, keyvar, keydata, type, member) \
377
- container_of_or_null(hashmap_remove(map, &(keyvar)->member, keydata), \
378
- type, member)
385
+/*
386
+ * Removes a hashmap entry contained within @keyvar,
387
+ * where @keyvar is a pointer to a struct containing a
388
+ * "struct hashmap_entry" @member.
389
+ *
390
+ * See `hashmap_get` for an explanation of @keydata
391
+ *
392
+ * Returns the replaced pointer which is of the same type as @keyvar,
393
+ * or NULL if not found.
394
+ */
395
+#define hashmap_remove_entry(map, keyvar, member, keydata) \
396
+ container_of_or_null_offset( \
397
+ hashmap_remove(map, &(keyvar)->member, keydata), \
398
+ OFFSETOF_VAR(keyvar, member))
399
400
/*
401
* Returns the `bucket` an entry is stored in.
@@ -436,13 +456,14 @@ static inline struct hashmap_entry *hashmap_iter_first(struct hashmap *map,
456
OFFSETOF_VAR(var, member)))
457
458
/*
439
- * returns a @pointer of @type matching @keyvar, or NULL if nothing found.
440
- * @keyvar is a pointer of @type
441
- * @member is the name of the "struct hashmap_entry" field in @type
459
+ * returns a pointer of type matching @keyvar, or NULL if nothing found.
460
+ * @keyvar is a pointer to a struct containing a
461
+ * "struct hashmap_entry" @member.
462
*/
443
-#define hashmap_get_entry(map, keyvar, keydata, type, member) \
444
- container_of_or_null(hashmap_get(map, &(keyvar)->member, keydata), \
445
- type, member)
463
+#define hashmap_get_entry(map, keyvar, member, keydata) \
464
+ container_of_or_null_offset( \
465
+ hashmap_get(map, &(keyvar)->member, keydata), \
466
+ OFFSETOF_VAR(keyvar, member))
467
468
#define hashmap_get_entry_from_hash(map, hash, keydata, type, member) \
469
container_of_or_null(hashmap_get_from_hash(map, hash, keydata), \
merge-recursive.c
+2
-4
@@ -65,8 +65,7 @@ static struct dir_rename_entry *dir_rename_find_entry(struct hashmap *hashmap,
65
return NULL;
66
hashmap_entry_init(&key.ent, strhash(dir));
67
key.dir = dir;
68
- return hashmap_get_entry(hashmap, &key, NULL,
69
- struct dir_rename_entry, ent);
68
+ return hashmap_get_entry(hashmap, &key, ent, NULL);
69
}
70
71
static int dir_rename_cmp(const void *unused_cmp_data,
@@ -104,8 +103,7 @@ static struct collision_entry *collision_find_entry(struct hashmap *hashmap,
103
104
hashmap_entry_init(&key.ent, strhash(target_file));
105
key.target_file = target_file;
107
- return hashmap_get_entry(hashmap, &key, NULL,
108
- struct collision_entry, ent);
106
+ return hashmap_get_entry(hashmap, &key, ent, NULL);
107
}
108
109
static int collision_cmp(const void *unused_cmp_data,
name-hash.c
+1
-2
@@ -37,8 +37,7 @@ static struct dir_entry *find_dir_entry__hash(struct index_state *istate,
37
struct dir_entry key;
38
hashmap_entry_init(&key.ent, hash);
39
key.namelen = namelen;
40
- return hashmap_get_entry(&istate->dir_hash, &key, name,
41
- struct dir_entry, ent);
40
+ return hashmap_get_entry(&istate->dir_hash, &key, ent, name);
41
}
42
43
static struct dir_entry *find_dir_entry(struct index_state *istate,
patch-ids.c
+1
-2
@@ -101,8 +101,7 @@ struct patch_id *has_commit_patch_id(struct commit *commit,
101
if (init_patch_id_entry(&patch, commit, ids))
102
return NULL;
103
104
- return hashmap_get_entry(&ids->patches, &patch, NULL,
105
- struct patch_id, ent);
104
+ return hashmap_get_entry(&ids->patches, &patch, ent, NULL);
105
}
106
107
struct patch_id *add_commit_patch_id(struct commit *commit,
range-diff.c
+1
-3
@@ -229,9 +229,7 @@ static void find_exact_matches(struct string_list *a, struct string_list *b)
229
util->patch = b->items[i].string;
230
util->diff = util->patch + util->diff_offset;
231
hashmap_entry_init(&util->e, strhash(util->diff));
232
- other = hashmap_remove_entry(&map, util, NULL,
233
- struct patch_util,
234
- e /* member name */);
232
+ other = hashmap_remove_entry(&map, util, e, NULL);
233
if (other) {
234
if (other->matching >= 0)
235
BUG("already assigned!");
remote.c
+1
-2
@@ -162,8 +162,7 @@ static struct remote *make_remote(const char *name, int len)
162
remotes[remotes_nr++] = ret;
163
164
hashmap_entry_init(&ret->ent, lookup_entry.hash);
165
- replaced = hashmap_put_entry(&remotes_hash, ret, struct remote,
166
- ent /* member name */);
165
+ replaced = hashmap_put_entry(&remotes_hash, ret, ent);
166
assert(replaced == NULL); /* no previous entry overwritten */
167
return ret;
168
}
revision.c
+1
-2
@@ -151,8 +151,7 @@ static void paths_and_oids_insert(struct hashmap *map,
151
key.path = (char *)path;
152
oidset_init(&key.trees, 0);
153
154
- entry = hashmap_get_entry(map, &key, NULL,
155
- struct path_and_oids_entry, ent);
154
+ entry = hashmap_get_entry(map, &key, ent, NULL);
155
if (!entry) {
156
entry = xcalloc(1, sizeof(struct path_and_oids_entry));
157
hashmap_entry_init(&entry->ent, hash);
sub-process.c
+1
-2
@@ -24,8 +24,7 @@ struct subprocess_entry *subprocess_find_entry(struct hashmap *hashmap, const ch
24
25
hashmap_entry_init(&key.ent, strhash(cmd));
26
key.cmd = cmd;
27
- return hashmap_get_entry(hashmap, &key, NULL,
28
- struct subprocess_entry, ent);
27
+ return hashmap_get_entry(hashmap, &key, ent, NULL);
28
}
29
30
int subprocess_read_status(int fd, struct strbuf *status)
submodule-config.c
+3
-7
@@ -141,9 +141,7 @@ static void cache_remove_path(struct submodule_cache *cache,
141
struct submodule_entry *removed;
142
hashmap_entry_init(&e.ent, hash);
143
e.config = submodule;
144
- removed = hashmap_remove_entry(&cache->for_path, &e, NULL,
145
- struct submodule_entry,
146
- ent /* member name */);
144
+ removed = hashmap_remove_entry(&cache->for_path, &e, ent, NULL);
145
free(removed);
146
}
147
@@ -172,8 +170,7 @@ static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
170
hashmap_entry_init(&key.ent, hash);
171
key.config = &key_config;
172
175
- entry = hashmap_get_entry(&cache->for_path, &key, NULL,
176
- struct submodule_entry, ent);
173
+ entry = hashmap_get_entry(&cache->for_path, &key, ent, NULL);
174
if (entry)
175
return entry->config;
176
return NULL;
@@ -193,8 +190,7 @@ static struct submodule *cache_lookup_name(struct submodule_cache *cache,
190
hashmap_entry_init(&key.ent, hash);
191
key.config = &key_config;
192
196
- entry = hashmap_get_entry(&cache->for_name, &key, NULL,
197
- struct submodule_entry, ent);
193
+ entry = hashmap_get_entry(&cache->for_name, &key, ent, NULL);
194
if (entry)
195
return entry->config;
196
return NULL;
t/helper/test-hashmap.c
+1
-3
@@ -189,9 +189,7 @@ int cmd__hashmap(int argc, const char **argv)
189
entry = alloc_test_entry(hash, p1, p2);
190
191
/* add / replace entry */
192
- entry = hashmap_put_entry(&map, entry,
193
- struct test_entry,
194
- ent /* member name */);
192
+ entry = hashmap_put_entry(&map, entry, ent);
193
194
/* print and free replaced entry, if any */
195
puts(entry ? get_value(entry) : "NULL");