Skip to content

Commit 6d37ca2

Browse files
committed
Merge branch 'en/strmap' into en/merge-ort-impl
* en/strmap: shortlog: use strset from strmap.h Use new HASHMAP_INIT macro to simplify hashmap initialization strmap: take advantage of FLEXPTR_ALLOC_STR when relevant strmap: enable allocations to come from a mem_pool strmap: add a strset sub-type strmap: split create_entry() out of strmap_put() strmap: add functions facilitating use as a string->int map strmap: enable faster clearing and reusing of strmaps strmap: add more utility functions strmap: new utility functions hashmap: provide deallocation function names hashmap: introduce a new hashmap_partial_clear() hashmap: allow re-use after hashmap_free() hashmap: adjust spacing to fix argument alignment hashmap: add usage documentation explaining hashmap_free[_entries]()
2 parents 14c4586 + 449a900 commit 6d37ca2

27 files changed

+621
-170
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,6 +1003,7 @@ LIB_OBJS += stable-qsort.o
10031003
LIB_OBJS += strbuf.o
10041004
LIB_OBJS += streaming.o
10051005
LIB_OBJS += string-list.o
1006+
LIB_OBJS += strmap.o
10061007
LIB_OBJS += strvec.o
10071008
LIB_OBJS += sub-process.o
10081009
LIB_OBJS += submodule-config.o

add-interactive.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ static int get_modified_files(struct repository *r,
557557
if (ps)
558558
clear_pathspec(&rev.prune_data);
559559
}
560-
hashmap_free_entries(&s.file_map, struct pathname_entry, ent);
560+
hashmap_clear_and_free(&s.file_map, struct pathname_entry, ent);
561561
if (unmerged_count)
562562
*unmerged_count = s.unmerged_count;
563563
if (binary_count)

attr.c

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,6 @@ static inline void hashmap_unlock(struct attr_hashmap *map)
5252
pthread_mutex_unlock(&map->mutex);
5353
}
5454

55-
/*
56-
* The global dictionary of all interned attributes. This
57-
* is a singleton object which is shared between threads.
58-
* Access to this dictionary must be surrounded with a mutex.
59-
*/
60-
static struct attr_hashmap g_attr_hashmap;
61-
6255
/* The container for objects stored in "struct attr_hashmap" */
6356
struct attr_hash_entry {
6457
struct hashmap_entry ent;
@@ -80,11 +73,14 @@ static int attr_hash_entry_cmp(const void *unused_cmp_data,
8073
return (a->keylen != b->keylen) || strncmp(a->key, b->key, a->keylen);
8174
}
8275

83-
/* Initialize an 'attr_hashmap' object */
84-
static void attr_hashmap_init(struct attr_hashmap *map)
85-
{
86-
hashmap_init(&map->map, attr_hash_entry_cmp, NULL, 0);
87-
}
76+
/*
77+
* The global dictionary of all interned attributes. This
78+
* is a singleton object which is shared between threads.
79+
* Access to this dictionary must be surrounded with a mutex.
80+
*/
81+
static struct attr_hashmap g_attr_hashmap = {
82+
HASHMAP_INIT(attr_hash_entry_cmp, NULL)
83+
};
8884

8985
/*
9086
* Retrieve the 'value' stored in a hashmap given the provided 'key'.
@@ -96,9 +92,6 @@ static void *attr_hashmap_get(struct attr_hashmap *map,
9692
struct attr_hash_entry k;
9793
struct attr_hash_entry *e;
9894

99-
if (!map->map.tablesize)
100-
attr_hashmap_init(map);
101-
10295
hashmap_entry_init(&k.ent, memhash(key, keylen));
10396
k.key = key;
10497
k.keylen = keylen;
@@ -114,9 +107,6 @@ static void attr_hashmap_add(struct attr_hashmap *map,
114107
{
115108
struct attr_hash_entry *e;
116109

117-
if (!map->map.tablesize)
118-
attr_hashmap_init(map);
119-
120110
e = xmalloc(sizeof(struct attr_hash_entry));
121111
hashmap_entry_init(&e->ent, memhash(key, keylen));
122112
e->key = key;

blame.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ static void get_fingerprint(struct fingerprint *result,
435435

436436
static void free_fingerprint(struct fingerprint *f)
437437
{
438-
hashmap_free(&f->map);
438+
hashmap_clear(&f->map);
439439
free(f->entries);
440440
}
441441

bloom.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,9 @@ struct bloom_filter *get_or_compute_bloom_filter(struct repository *r,
229229
diffcore_std(&diffopt);
230230

231231
if (diff_queued_diff.nr <= settings->max_changed_paths) {
232-
struct hashmap pathmap;
232+
struct hashmap pathmap = HASHMAP_INIT(pathmap_cmp, NULL);
233233
struct pathmap_hash_entry *e;
234234
struct hashmap_iter iter;
235-
hashmap_init(&pathmap, pathmap_cmp, NULL, 0);
236235

237236
for (i = 0; i < diff_queued_diff.nr; i++) {
238237
const char *path = diff_queued_diff.queue[i]->two->path;
@@ -287,7 +286,7 @@ struct bloom_filter *get_or_compute_bloom_filter(struct repository *r,
287286
}
288287

289288
cleanup:
290-
hashmap_free_entries(&pathmap, struct pathmap_hash_entry, entry);
289+
hashmap_clear_and_free(&pathmap, struct pathmap_hash_entry, entry);
291290
} else {
292291
for (i = 0; i < diff_queued_diff.nr; i++)
293292
diff_free_filepair(diff_queued_diff.queue[i]);

builtin/difftool.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,10 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
342342
const char *workdir, *tmp;
343343
int ret = 0, i;
344344
FILE *fp;
345-
struct hashmap working_tree_dups, submodules, symlinks2;
345+
struct hashmap working_tree_dups = HASHMAP_INIT(working_tree_entry_cmp,
346+
NULL);
347+
struct hashmap submodules = HASHMAP_INIT(pair_cmp, NULL);
348+
struct hashmap symlinks2 = HASHMAP_INIT(pair_cmp, NULL);
346349
struct hashmap_iter iter;
347350
struct pair_entry *entry;
348351
struct index_state wtindex;
@@ -383,10 +386,6 @@ static int run_dir_diff(const char *extcmd, int symlinks, const char *prefix,
383386
rdir_len = rdir.len;
384387
wtdir_len = wtdir.len;
385388

386-
hashmap_init(&working_tree_dups, working_tree_entry_cmp, NULL, 0);
387-
hashmap_init(&submodules, pair_cmp, NULL, 0);
388-
hashmap_init(&symlinks2, pair_cmp, NULL, 0);
389-
390389
child.no_stdin = 1;
391390
child.git_cmd = 1;
392391
child.use_shell = 0;

builtin/fetch.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ static void find_non_local_tags(const struct ref *refs,
393393
item = refname_hash_add(&remote_refs, ref->name, &ref->old_oid);
394394
string_list_insert(&remote_refs_list, ref->name);
395395
}
396-
hashmap_free_entries(&existing_refs, struct refname_hash_entry, ent);
396+
hashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent);
397397

398398
/*
399399
* We may have a final lightweight tag that needs to be
@@ -428,7 +428,7 @@ static void find_non_local_tags(const struct ref *refs,
428428
**tail = rm;
429429
*tail = &rm->next;
430430
}
431-
hashmap_free_entries(&remote_refs, struct refname_hash_entry, ent);
431+
hashmap_clear_and_free(&remote_refs, struct refname_hash_entry, ent);
432432
string_list_clear(&remote_refs_list, 0);
433433
oidset_clear(&fetch_oids);
434434
}
@@ -573,7 +573,7 @@ static struct ref *get_ref_map(struct remote *remote,
573573
}
574574
}
575575
if (existing_refs_populated)
576-
hashmap_free_entries(&existing_refs, struct refname_hash_entry, ent);
576+
hashmap_clear_and_free(&existing_refs, struct refname_hash_entry, ent);
577577

578578
return ref_map;
579579
}

builtin/shortlog.c

Lines changed: 4 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "shortlog.h"
1111
#include "parse-options.h"
1212
#include "trailer.h"
13+
#include "strmap.h"
1314

1415
static char const * const shortlog_usage[] = {
1516
N_("git shortlog [<options>] [<revision-range>] [[--] <path>...]"),
@@ -169,60 +170,6 @@ static void read_from_stdin(struct shortlog *log)
169170
strbuf_release(&oneline);
170171
}
171172

172-
struct strset_item {
173-
struct hashmap_entry ent;
174-
char value[FLEX_ARRAY];
175-
};
176-
177-
struct strset {
178-
struct hashmap map;
179-
};
180-
181-
#define STRSET_INIT { { NULL } }
182-
183-
static int strset_item_hashcmp(const void *hash_data,
184-
const struct hashmap_entry *entry,
185-
const struct hashmap_entry *entry_or_key,
186-
const void *keydata)
187-
{
188-
const struct strset_item *a, *b;
189-
190-
a = container_of(entry, const struct strset_item, ent);
191-
if (keydata)
192-
return strcmp(a->value, keydata);
193-
194-
b = container_of(entry_or_key, const struct strset_item, ent);
195-
return strcmp(a->value, b->value);
196-
}
197-
198-
/*
199-
* Adds "str" to the set if it was not already present; returns true if it was
200-
* already there.
201-
*/
202-
static int strset_check_and_add(struct strset *ss, const char *str)
203-
{
204-
unsigned int hash = strhash(str);
205-
struct strset_item *item;
206-
207-
if (!ss->map.table)
208-
hashmap_init(&ss->map, strset_item_hashcmp, NULL, 0);
209-
210-
if (hashmap_get_from_hash(&ss->map, hash, str))
211-
return 1;
212-
213-
FLEX_ALLOC_STR(item, value, str);
214-
hashmap_entry_init(&item->ent, hash);
215-
hashmap_add(&ss->map, &item->ent);
216-
return 0;
217-
}
218-
219-
static void strset_clear(struct strset *ss)
220-
{
221-
if (!ss->map.table)
222-
return;
223-
hashmap_free_entries(&ss->map, struct strset_item, ent);
224-
}
225-
226173
static void insert_records_from_trailers(struct shortlog *log,
227174
struct strset *dups,
228175
struct commit *commit,
@@ -253,7 +200,7 @@ static void insert_records_from_trailers(struct shortlog *log,
253200
if (!parse_ident(log, &ident, value))
254201
value = ident.buf;
255202

256-
if (strset_check_and_add(dups, value))
203+
if (!strset_add(dups, value))
257204
continue;
258205
insert_one_record(log, value, oneline);
259206
}
@@ -291,7 +238,7 @@ void shortlog_add_commit(struct shortlog *log, struct commit *commit)
291238
log->email ? "%aN <%aE>" : "%aN",
292239
&ident, &ctx);
293240
if (!HAS_MULTI_BITS(log->groups) ||
294-
!strset_check_and_add(&dups, ident.buf))
241+
strset_add(&dups, ident.buf))
295242
insert_one_record(log, ident.buf, oneline_str);
296243
}
297244
if (log->groups & SHORTLOG_GROUP_COMMITTER) {
@@ -300,7 +247,7 @@ void shortlog_add_commit(struct shortlog *log, struct commit *commit)
300247
log->email ? "%cN <%cE>" : "%cN",
301248
&ident, &ctx);
302249
if (!HAS_MULTI_BITS(log->groups) ||
303-
!strset_check_and_add(&dups, ident.buf))
250+
strset_add(&dups, ident.buf))
304251
insert_one_record(log, ident.buf, oneline_str);
305252
}
306253
if (log->groups & SHORTLOG_GROUP_TRAILER) {

config.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1963,7 +1963,7 @@ void git_configset_clear(struct config_set *cs)
19631963
free(entry->key);
19641964
string_list_clear(&entry->value_list, 1);
19651965
}
1966-
hashmap_free_entries(&cs->config_hash, struct config_set_element, ent);
1966+
hashmap_clear_and_free(&cs->config_hash, struct config_set_element, ent);
19671967
cs->hash_initialized = 0;
19681968
free(cs->list.items);
19691969
cs->list.nr = 0;

diff.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6289,9 +6289,9 @@ static void diff_flush_patch_all_file_pairs(struct diff_options *o)
62896289
if (o->color_moved == COLOR_MOVED_ZEBRA_DIM)
62906290
dim_moved_lines(o);
62916291

6292-
hashmap_free_entries(&add_lines, struct moved_entry,
6292+
hashmap_clear_and_free(&add_lines, struct moved_entry,
62936293
ent);
6294-
hashmap_free_entries(&del_lines, struct moved_entry,
6294+
hashmap_clear_and_free(&del_lines, struct moved_entry,
62956295
ent);
62966296
}
62976297

diffcore-rename.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ static int find_exact_renames(struct diff_options *options)
407407
renames += find_identical_files(&file_table, i, options);
408408

409409
/* Free the hash data structure and entries */
410-
hashmap_free_entries(&file_table, struct file_similarity, entry);
410+
hashmap_clear_and_free(&file_table, struct file_similarity, entry);
411411

412412
return renames;
413413
}

dir.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -817,8 +817,8 @@ static void add_pattern_to_hashsets(struct pattern_list *pl, struct path_pattern
817817

818818
clear_hashmaps:
819819
warning(_("disabling cone pattern matching"));
820-
hashmap_free_entries(&pl->parent_hashmap, struct pattern_entry, ent);
821-
hashmap_free_entries(&pl->recursive_hashmap, struct pattern_entry, ent);
820+
hashmap_clear_and_free(&pl->parent_hashmap, struct pattern_entry, ent);
821+
hashmap_clear_and_free(&pl->recursive_hashmap, struct pattern_entry, ent);
822822
pl->use_cone_patterns = 0;
823823
}
824824

@@ -921,8 +921,8 @@ void clear_pattern_list(struct pattern_list *pl)
921921
free(pl->patterns[i]);
922922
free(pl->patterns);
923923
free(pl->filebuf);
924-
hashmap_free_entries(&pl->recursive_hashmap, struct pattern_entry, ent);
925-
hashmap_free_entries(&pl->parent_hashmap, struct pattern_entry, ent);
924+
hashmap_clear_and_free(&pl->recursive_hashmap, struct pattern_entry, ent);
925+
hashmap_clear_and_free(&pl->parent_hashmap, struct pattern_entry, ent);
926926

927927
memset(pl, 0, sizeof(*pl));
928928
}

0 commit comments

Comments
 (0)