Skip to content

Commit 404ab78

Browse files
Eric Wonggitster
Eric Wong
authored andcommitted
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 <[email protected]> Reviewed-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 23dee69 commit 404ab78

17 files changed

+56
-55
lines changed

Diff for: attr.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static void *attr_hashmap_get(struct attr_hashmap *map,
103103
hashmap_entry_init(&k.ent, memhash(key, keylen));
104104
k.key = key;
105105
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);
107107

108108
return e ? e->value : NULL;
109109
}

Diff for: blame.c

+4-6
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,8 @@ static void get_fingerprint(struct fingerprint *result,
419419
continue;
420420
hashmap_entry_init(&entry->entry, hash);
421421

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);
424424
if (found_entry) {
425425
found_entry->count += 1;
426426
} else {
@@ -452,8 +452,7 @@ static int fingerprint_similarity(struct fingerprint *a, struct fingerprint *b)
452452

453453
hashmap_for_each_entry(&b->map, &iter, entry_b,
454454
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);
457456
if (entry_a) {
458457
intersection += entry_a->count < entry_b->count ?
459458
entry_a->count : entry_b->count;
@@ -474,8 +473,7 @@ static void fingerprint_subtract(struct fingerprint *a, struct fingerprint *b)
474473

475474
hashmap_for_each_entry(&b->map, &iter, entry_b,
476475
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);
479477
if (entry_a) {
480478
if (entry_a->count <= entry_b->count)
481479
hashmap_remove(&a->map, &entry_b->entry, NULL);

Diff for: builtin/difftool.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ static void add_left_or_right(struct hashmap *map, const char *path,
167167

168168
FLEX_ALLOC_STR(e, path, path);
169169
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);
171171
if (existing) {
172172
free(e);
173173
e = existing;

Diff for: builtin/fast-export.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ static const void *anonymize_mem(struct hashmap *map,
156156
hashmap_entry_init(&key.hash, memhash(orig, *len));
157157
key.orig = orig;
158158
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);
160160

161161
if (!ret) {
162162
ret = xmalloc(sizeof(*ret));

Diff for: config.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -1863,8 +1863,7 @@ static struct config_set_element *configset_find_element(struct config_set *cs,
18631863

18641864
hashmap_entry_init(&k.ent, strhash(normalized_key));
18651865
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);
18681867
free(normalized_key);
18691868
return found_entry;
18701869
}

Diff for: diff.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -1146,15 +1146,13 @@ static void mark_color_as_moved(struct diff_options *o,
11461146
case DIFF_SYMBOL_PLUS:
11471147
hm = del_lines;
11481148
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);
11511150
free(key);
11521151
break;
11531152
case DIFF_SYMBOL_MINUS:
11541153
hm = add_lines;
11551154
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);
11581156
free(key);
11591157
break;
11601158
default:

Diff for: hashmap.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ const void *memintern(const void *data, size_t len)
311311
/* lookup interned string in pool */
312312
hashmap_entry_init(&key.ent, memhash(data, len));
313313
key.len = len;
314-
e = hashmap_get_entry(&map, &key, data, struct pool_entry, ent);
314+
e = hashmap_get_entry(&map, &key, ent, data);
315315
if (!e) {
316316
/* not found: create it */
317317
FLEX_ALLOC_MEM(e, data, data, len);

Diff for: hashmap.h

+33-12
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
* k.key = key;
6464
*
6565
* flags &= ~COMPARE_VALUE;
66-
* e = hashmap_get_entry(&map, &k, NULL, struct long2string, ent);
66+
* e = hashmap_get_entry(&map, &k, ent, NULL);
6767
* if (e) {
6868
* printf("first: %ld %s\n", e->key, e->value);
6969
* while ((e = hashmap_get_next_entry(&map, e,
@@ -359,8 +359,17 @@ void hashmap_add(struct hashmap *map, struct hashmap_entry *entry);
359359
struct hashmap_entry *hashmap_put(struct hashmap *map,
360360
struct hashmap_entry *entry);
361361

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))
364373

365374
/*
366375
* Removes a hashmap entry matching the specified key. If the hashmap contains
@@ -373,9 +382,20 @@ struct hashmap_entry *hashmap_remove(struct hashmap *map,
373382
const struct hashmap_entry *key,
374383
const void *keydata);
375384

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))
379399

380400
/*
381401
* Returns the `bucket` an entry is stored in.
@@ -436,13 +456,14 @@ static inline struct hashmap_entry *hashmap_iter_first(struct hashmap *map,
436456
OFFSETOF_VAR(var, member)))
437457

438458
/*
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.
442462
*/
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))
446467

447468
#define hashmap_get_entry_from_hash(map, hash, keydata, type, member) \
448469
container_of_or_null(hashmap_get_from_hash(map, hash, keydata), \

Diff for: merge-recursive.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@ static struct dir_rename_entry *dir_rename_find_entry(struct hashmap *hashmap,
6565
return NULL;
6666
hashmap_entry_init(&key.ent, strhash(dir));
6767
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);
7069
}
7170

7271
static int dir_rename_cmp(const void *unused_cmp_data,
@@ -104,8 +103,7 @@ static struct collision_entry *collision_find_entry(struct hashmap *hashmap,
104103

105104
hashmap_entry_init(&key.ent, strhash(target_file));
106105
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);
109107
}
110108

111109
static int collision_cmp(const void *unused_cmp_data,

Diff for: name-hash.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ static struct dir_entry *find_dir_entry__hash(struct index_state *istate,
3737
struct dir_entry key;
3838
hashmap_entry_init(&key.ent, hash);
3939
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);
4241
}
4342

4443
static struct dir_entry *find_dir_entry(struct index_state *istate,

Diff for: patch-ids.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ struct patch_id *has_commit_patch_id(struct commit *commit,
101101
if (init_patch_id_entry(&patch, commit, ids))
102102
return NULL;
103103

104-
return hashmap_get_entry(&ids->patches, &patch, NULL,
105-
struct patch_id, ent);
104+
return hashmap_get_entry(&ids->patches, &patch, ent, NULL);
106105
}
107106

108107
struct patch_id *add_commit_patch_id(struct commit *commit,

Diff for: range-diff.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,7 @@ static void find_exact_matches(struct string_list *a, struct string_list *b)
229229
util->patch = b->items[i].string;
230230
util->diff = util->patch + util->diff_offset;
231231
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);
235233
if (other) {
236234
if (other->matching >= 0)
237235
BUG("already assigned!");

Diff for: remote.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,7 @@ static struct remote *make_remote(const char *name, int len)
162162
remotes[remotes_nr++] = ret;
163163

164164
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);
167166
assert(replaced == NULL); /* no previous entry overwritten */
168167
return ret;
169168
}

Diff for: revision.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ static void paths_and_oids_insert(struct hashmap *map,
151151
key.path = (char *)path;
152152
oidset_init(&key.trees, 0);
153153

154-
entry = hashmap_get_entry(map, &key, NULL,
155-
struct path_and_oids_entry, ent);
154+
entry = hashmap_get_entry(map, &key, ent, NULL);
156155
if (!entry) {
157156
entry = xcalloc(1, sizeof(struct path_and_oids_entry));
158157
hashmap_entry_init(&entry->ent, hash);

Diff for: sub-process.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ struct subprocess_entry *subprocess_find_entry(struct hashmap *hashmap, const ch
2424

2525
hashmap_entry_init(&key.ent, strhash(cmd));
2626
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);
2928
}
3029

3130
int subprocess_read_status(int fd, struct strbuf *status)

Diff for: submodule-config.c

+3-7
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,7 @@ static void cache_remove_path(struct submodule_cache *cache,
141141
struct submodule_entry *removed;
142142
hashmap_entry_init(&e.ent, hash);
143143
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);
147145
free(removed);
148146
}
149147

@@ -172,8 +170,7 @@ static const struct submodule *cache_lookup_path(struct submodule_cache *cache,
172170
hashmap_entry_init(&key.ent, hash);
173171
key.config = &key_config;
174172

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);
177174
if (entry)
178175
return entry->config;
179176
return NULL;
@@ -193,8 +190,7 @@ static struct submodule *cache_lookup_name(struct submodule_cache *cache,
193190
hashmap_entry_init(&key.ent, hash);
194191
key.config = &key_config;
195192

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);
198194
if (entry)
199195
return entry->config;
200196
return NULL;

Diff for: t/helper/test-hashmap.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,7 @@ int cmd__hashmap(int argc, const char **argv)
189189
entry = alloc_test_entry(hash, p1, p2);
190190

191191
/* 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);
195193

196194
/* print and free replaced entry, if any */
197195
puts(entry ? get_value(entry) : "NULL");

0 commit comments

Comments
 (0)