Skip to content

Commit a01aec2

Browse files
committed
Merge branch 'ew/hashmap' into pu
* ew/hashmap: hashmap_get_next returns "struct hashmap_entry *" introduce container_of macro hashmap_put takes "struct hashmap_entry *" hashmap_remove takes "const struct hashmap_entry *" hashmap_get takes "const struct hashmap_entry *" hashmap_add takes "struct hashmap_entry *" hashmap_get_next takes "const struct hashmap_entry *" hashmap_entry: detect improper initialization hashmap_entry_init takes "struct hashmap_entry *" packfile: use hashmap_entry in delta_base_cache_entry diff: use hashmap_entry_init on moved_entry.ent
2 parents a9a7307 + af3b684 commit a01aec2

27 files changed

+210
-163
lines changed

attr.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,10 +98,10 @@ static void *attr_hashmap_get(struct attr_hashmap *map,
9898
if (!map->map.tablesize)
9999
attr_hashmap_init(map);
100100

101-
hashmap_entry_init(&k, memhash(key, keylen));
101+
hashmap_entry_init(&k.ent, memhash(key, keylen));
102102
k.key = key;
103103
k.keylen = keylen;
104-
e = hashmap_get(&map->map, &k, NULL);
104+
e = hashmap_get(&map->map, &k.ent, NULL);
105105

106106
return e ? e->value : NULL;
107107
}
@@ -117,12 +117,12 @@ static void attr_hashmap_add(struct attr_hashmap *map,
117117
attr_hashmap_init(map);
118118

119119
e = xmalloc(sizeof(struct attr_hash_entry));
120-
hashmap_entry_init(e, memhash(key, keylen));
120+
hashmap_entry_init(&e->ent, memhash(key, keylen));
121121
e->key = key;
122122
e->keylen = keylen;
123123
e->value = value;
124124

125-
hashmap_add(&map->map, e);
125+
hashmap_add(&map->map, &e->ent);
126126
}
127127

128128
struct all_attrs_item {

blame.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -417,14 +417,14 @@ static void get_fingerprint(struct fingerprint *result,
417417
/* Ignore whitespace pairs */
418418
if (hash == 0)
419419
continue;
420-
hashmap_entry_init(entry, hash);
420+
hashmap_entry_init(&entry->entry, hash);
421421

422-
found_entry = hashmap_get(&result->map, entry, NULL);
422+
found_entry = hashmap_get(&result->map, &entry->entry, NULL);
423423
if (found_entry) {
424424
found_entry->count += 1;
425425
} else {
426426
entry->count = 1;
427-
hashmap_add(&result->map, entry);
427+
hashmap_add(&result->map, &entry->entry);
428428
++entry;
429429
}
430430
}
@@ -452,7 +452,7 @@ static int fingerprint_similarity(struct fingerprint *a, struct fingerprint *b)
452452
hashmap_iter_init(&b->map, &iter);
453453

454454
while ((entry_b = hashmap_iter_next(&iter))) {
455-
if ((entry_a = hashmap_get(&a->map, entry_b, NULL))) {
455+
if ((entry_a = hashmap_get(&a->map, &entry_b->entry, NULL))) {
456456
intersection += entry_a->count < entry_b->count ?
457457
entry_a->count : entry_b->count;
458458
}
@@ -471,9 +471,9 @@ static void fingerprint_subtract(struct fingerprint *a, struct fingerprint *b)
471471
hashmap_iter_init(&b->map, &iter);
472472

473473
while ((entry_b = hashmap_iter_next(&iter))) {
474-
if ((entry_a = hashmap_get(&a->map, entry_b, NULL))) {
474+
if ((entry_a = hashmap_get(&a->map, &entry_b->entry, NULL))) {
475475
if (entry_a->count <= entry_b->count)
476-
hashmap_remove(&a->map, entry_b, NULL);
476+
hashmap_remove(&a->map, &entry_b->entry, NULL);
477477
else
478478
entry_a->count -= entry_b->count;
479479
}

builtin/describe.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ static void add_to_known_names(const char *path,
123123
if (!e) {
124124
e = xmalloc(sizeof(struct commit_name));
125125
oidcpy(&e->peeled, peeled);
126-
hashmap_entry_init(e, oidhash(peeled));
127-
hashmap_add(&names, e);
126+
hashmap_entry_init(&e->entry, oidhash(peeled));
127+
hashmap_add(&names, &e->entry);
128128
e->path = NULL;
129129
}
130130
e->tag = tag;

builtin/difftool.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,14 @@ static void add_left_or_right(struct hashmap *map, const char *path,
161161
struct pair_entry *e, *existing;
162162

163163
FLEX_ALLOC_STR(e, path, path);
164-
hashmap_entry_init(e, strhash(path));
165-
existing = hashmap_get(map, e, NULL);
164+
hashmap_entry_init(&e->entry, strhash(path));
165+
existing = hashmap_get(map, &e->entry, NULL);
166166
if (existing) {
167167
free(e);
168168
e = existing;
169169
} else {
170170
e->left[0] = e->right[0] = '\0';
171-
hashmap_add(map, e);
171+
hashmap_add(map, &e->entry);
172172
}
173173
strlcpy(is_right ? e->right : e->left, content, PATH_MAX);
174174
}
@@ -234,8 +234,8 @@ static void changed_files(struct hashmap *result, const char *index_path,
234234
while (!strbuf_getline_nul(&buf, fp)) {
235235
struct path_entry *entry;
236236
FLEX_ALLOC_STR(entry, path, buf.buf);
237-
hashmap_entry_init(entry, strhash(buf.buf));
238-
hashmap_add(result, entry);
237+
hashmap_entry_init(&entry->entry, strhash(buf.buf));
238+
hashmap_add(result, &entry->entry);
239239
}
240240
fclose(fp);
241241
if (finish_command(&diff_files))
@@ -461,12 +461,13 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
461461

462462
/* Avoid duplicate working_tree entries */
463463
FLEX_ALLOC_STR(entry, path, dst_path);
464-
hashmap_entry_init(entry, strhash(dst_path));
465-
if (hashmap_get(&working_tree_dups, entry, NULL)) {
464+
hashmap_entry_init(&entry->entry, strhash(dst_path));
465+
if (hashmap_get(&working_tree_dups, &entry->entry,
466+
NULL)) {
466467
free(entry);
467468
continue;
468469
}
469-
hashmap_add(&working_tree_dups, entry);
470+
hashmap_add(&working_tree_dups, &entry->entry);
470471

471472
if (!use_wt_file(workdir, dst_path, &roid)) {
472473
if (checkout_path(rmode, &roid, dst_path,

builtin/fast-export.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,23 +144,24 @@ static const void *anonymize_mem(struct hashmap *map,
144144
const void *orig, size_t *len)
145145
{
146146
struct anonymized_entry key, *ret;
147+
unsigned int hash = memhash(orig, *len);
147148

148149
if (!map->cmpfn)
149150
hashmap_init(map, anonymized_entry_cmp, NULL, 0);
150151

151-
hashmap_entry_init(&key, memhash(orig, *len));
152+
hashmap_entry_init(&key.hash, hash);
152153
key.orig = orig;
153154
key.orig_len = *len;
154-
ret = hashmap_get(map, &key, NULL);
155+
ret = hashmap_get(map, &key.hash, NULL);
155156

156157
if (!ret) {
157158
ret = xmalloc(sizeof(*ret));
158-
hashmap_entry_init(&ret->hash, key.hash.hash);
159+
hashmap_entry_init(&ret->hash, hash);
159160
ret->orig = xstrdup(orig);
160161
ret->orig_len = *len;
161162
ret->anon = generate(orig, len);
162163
ret->anon_len = *len;
163-
hashmap_put(map, ret);
164+
hashmap_put(map, &ret->hash);
164165
}
165166

166167
*len = ret->anon_len;

builtin/fetch.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,9 @@ static struct refname_hash_entry *refname_hash_add(struct hashmap *map,
282282
size_t len = strlen(refname);
283283

284284
FLEX_ALLOC_MEM(ent, refname, refname, len);
285-
hashmap_entry_init(ent, strhash(refname));
285+
hashmap_entry_init(&ent->ent, strhash(refname));
286286
oidcpy(&ent->oid, oid);
287-
hashmap_add(map, ent);
287+
hashmap_add(map, &ent->ent);
288288
return ent;
289289
}
290290

config.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,9 +1856,9 @@ static struct config_set_element *configset_find_element(struct config_set *cs,
18561856
if (git_config_parse_key(key, &normalized_key, NULL))
18571857
return NULL;
18581858

1859-
hashmap_entry_init(&k, strhash(normalized_key));
1859+
hashmap_entry_init(&k.ent, strhash(normalized_key));
18601860
k.key = normalized_key;
1861-
found_entry = hashmap_get(&cs->config_hash, &k, NULL);
1861+
found_entry = hashmap_get(&cs->config_hash, &k.ent, NULL);
18621862
free(normalized_key);
18631863
return found_entry;
18641864
}
@@ -1877,10 +1877,10 @@ static int configset_add_value(struct config_set *cs, const char *key, const cha
18771877
*/
18781878
if (!e) {
18791879
e = xmalloc(sizeof(*e));
1880-
hashmap_entry_init(e, strhash(key));
1880+
hashmap_entry_init(&e->ent, strhash(key));
18811881
e->key = xstrdup(key);
18821882
string_list_init(&e->value_list, 1);
1883-
hashmap_add(&cs->config_hash, e);
1883+
hashmap_add(&cs->config_hash, &e->ent);
18841884
}
18851885
si = string_list_append_nodup(&e->value_list, xstrdup_or_null(value));
18861886

diff.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -964,8 +964,9 @@ static struct moved_entry *prepare_entry(struct diff_options *o,
964964
struct moved_entry *ret = xmalloc(sizeof(*ret));
965965
struct emitted_diff_symbol *l = &o->emitted_symbols->buf[line_no];
966966
unsigned flags = o->color_moved_ws_handling & XDF_WHITESPACE_FLAGS;
967+
unsigned int hash = xdiff_hash_string(l->line, l->len, flags);
967968

968-
ret->ent.hash = xdiff_hash_string(l->line, l->len, flags);
969+
hashmap_entry_init(&ret->ent, hash);
969970
ret->es = l;
970971
ret->next_line = NULL;
971972

@@ -1002,7 +1003,7 @@ static void add_lines_to_move_detection(struct diff_options *o,
10021003
if (prev_line && prev_line->es->s == o->emitted_symbols->buf[n].s)
10031004
prev_line->next_line = key;
10041005

1005-
hashmap_add(hm, key);
1006+
hashmap_add(hm, &key->ent);
10061007
prev_line = key;
10071008
}
10081009
}
@@ -1034,8 +1035,10 @@ static void pmb_advance_or_null_multi_match(struct diff_options *o,
10341035
{
10351036
int i;
10361037
char *got_match = xcalloc(1, pmb_nr);
1038+
struct hashmap_entry *ent = &match->ent;
10371039

1038-
for (; match; match = hashmap_get_next(hm, match)) {
1040+
for (; ent; ent = hashmap_get_next(hm, ent)) {
1041+
match = container_of(ent, struct moved_entry, ent);
10391042
for (i = 0; i < pmb_nr; i++) {
10401043
struct moved_entry *prev = pmb[i].match;
10411044
struct moved_entry *cur = (prev && prev->next_line) ?
@@ -1134,29 +1137,30 @@ static void mark_color_as_moved(struct diff_options *o,
11341137

11351138
for (n = 0; n < o->emitted_symbols->nr; n++) {
11361139
struct hashmap *hm = NULL;
1140+
struct hashmap_entry *ent = NULL;
11371141
struct moved_entry *key;
1138-
struct moved_entry *match = NULL;
1142+
struct moved_entry *match;
11391143
struct emitted_diff_symbol *l = &o->emitted_symbols->buf[n];
11401144
enum diff_symbol last_symbol = 0;
11411145

11421146
switch (l->s) {
11431147
case DIFF_SYMBOL_PLUS:
11441148
hm = del_lines;
11451149
key = prepare_entry(o, n);
1146-
match = hashmap_get(hm, key, NULL);
1150+
ent = hashmap_get(hm, &key->ent, NULL);
11471151
free(key);
11481152
break;
11491153
case DIFF_SYMBOL_MINUS:
11501154
hm = add_lines;
11511155
key = prepare_entry(o, n);
1152-
match = hashmap_get(hm, key, NULL);
1156+
ent = hashmap_get(hm, &key->ent, NULL);
11531157
free(key);
11541158
break;
11551159
default:
11561160
flipped_block = 0;
11571161
}
11581162

1159-
if (!match) {
1163+
if (!ent) {
11601164
int i;
11611165

11621166
adjust_last_block(o, n, block_length);
@@ -1168,6 +1172,7 @@ static void mark_color_as_moved(struct diff_options *o,
11681172
last_symbol = l->s;
11691173
continue;
11701174
}
1175+
match = container_of(ent, struct moved_entry, ent);
11711176

11721177
if (o->color_moved == COLOR_MOVED_PLAIN) {
11731178
last_symbol = l->s;
@@ -1188,7 +1193,9 @@ static void mark_color_as_moved(struct diff_options *o,
11881193
* The current line is the start of a new block.
11891194
* Setup the set of potential blocks.
11901195
*/
1191-
for (; match; match = hashmap_get_next(hm, match)) {
1196+
for (; ent; ent = hashmap_get_next(hm, ent)) {
1197+
match = container_of(ent, struct moved_entry,
1198+
ent);
11921199
ALLOC_GROW(pmb, pmb_nr + 1, pmb_alloc);
11931200
if (o->color_moved_ws_handling &
11941201
COLOR_MOVED_WS_ALLOW_INDENTATION_CHANGE) {

diffcore-rename.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -274,20 +274,23 @@ static int find_identical_files(struct hashmap *srcs,
274274
struct diff_options *options)
275275
{
276276
int renames = 0;
277-
277+
struct hashmap_entry *ent;
278278
struct diff_filespec *target = rename_dst[dst_index].two;
279279
struct file_similarity *p, *best = NULL;
280280
int i = 100, best_score = -1;
281281

282282
/*
283283
* Find the best source match for specified destination.
284284
*/
285-
p = hashmap_get_from_hash(srcs,
285+
ent = hashmap_get_from_hash(srcs,
286286
hash_filespec(options->repo, target),
287287
NULL);
288-
for (; p; p = hashmap_get_next(srcs, p)) {
288+
for (; ent; ent = hashmap_get_next(srcs, ent)) {
289289
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;
291294

292295
/* False hash collision? */
293296
if (!oideq(&source->oid, &target->oid))
@@ -329,8 +332,8 @@ static void insert_file_table(struct repository *r,
329332
entry->index = index;
330333
entry->filespec = filespec;
331334

332-
hashmap_entry_init(entry, hash_filespec(r, filespec));
333-
hashmap_add(table, entry);
335+
hashmap_entry_init(&entry->entry, hash_filespec(r, filespec));
336+
hashmap_add(table, &entry->entry);
334337
}
335338

336339
/*

git-compat-util.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,4 +1309,14 @@ void unleak_memory(const void *ptr, size_t len);
13091309
*/
13101310
#include "banned.h"
13111311

1312+
/*
1313+
* container_of - Get the address of an object containing a field.
1314+
*
1315+
* @ptr: pointer to the field.
1316+
* @type: type of the object.
1317+
* @member: name of the field within the object.
1318+
*/
1319+
#define container_of(ptr, type, member) \
1320+
((type *) ((char *)(ptr) - offsetof(type, member)))
1321+
13121322
#endif

0 commit comments

Comments
 (0)