Skip to content

Commit 4069f92

Browse files
authored
Merge pull request libgit2#4901 from pks-t/pks/uniform-map-api
High-level map APIs
2 parents 75dd7f2 + df42f36 commit 4069f92

34 files changed

+1122
-861
lines changed

src/apply.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,6 @@ static int apply_one(
439439
git_filemode_t pre_filemode;
440440
git_index_entry pre_entry, post_entry;
441441
bool skip_preimage = false;
442-
size_t pos;
443442
int error;
444443

445444
if ((error = git_patch_from_diff(&patch, diff, i)) < 0)
@@ -464,8 +463,7 @@ static int apply_one(
464463
*/
465464
if (delta->status != GIT_DELTA_RENAMED &&
466465
delta->status != GIT_DELTA_ADDED) {
467-
pos = git_strmap_lookup_index(removed_paths, delta->old_file.path);
468-
if (git_strmap_valid_index(removed_paths, pos)) {
466+
if (git_strmap_exists(removed_paths, delta->old_file.path)) {
469467
error = apply_err("path '%s' has been renamed or deleted", delta->old_file.path);
470468
goto done;
471469
}
@@ -549,7 +547,7 @@ static int apply_one(
549547

550548
if (delta->status == GIT_DELTA_RENAMED ||
551549
delta->status == GIT_DELTA_DELETED)
552-
git_strmap_insert(removed_paths, delta->old_file.path, (char *)delta->old_file.path, &error);
550+
error = git_strmap_set(removed_paths, delta->old_file.path, (char *) delta->old_file.path);
553551

554552
if (delta->status == GIT_DELTA_RENAMED ||
555553
delta->status == GIT_DELTA_ADDED)
@@ -577,7 +575,7 @@ static int apply_deltas(
577575
size_t i;
578576
int error = 0;
579577

580-
if (git_strmap_alloc(&removed_paths) < 0)
578+
if (git_strmap_new(&removed_paths) < 0)
581579
return -1;
582580

583581
for (i = 0; i < git_diff_num_deltas(diff); i++) {

src/attr.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ int git_attr_foreach(
215215
return -1;
216216

217217
if ((error = collect_attr_files(repo, NULL, flags, pathname, &files)) < 0 ||
218-
(error = git_strmap_alloc(&seen)) < 0)
218+
(error = git_strmap_new(&seen)) < 0)
219219
goto cleanup;
220220

221221
git_vector_foreach(&files, i, file) {
@@ -227,8 +227,7 @@ int git_attr_foreach(
227227
if (git_strmap_exists(seen, assign->name))
228228
continue;
229229

230-
git_strmap_insert(seen, assign->name, assign, &error);
231-
if (error < 0)
230+
if ((error = git_strmap_set(seen, assign->name, assign)) < 0)
232231
goto cleanup;
233232

234233
error = callback(assign->name, assign->value, payload);

src/attrcache.c

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,7 @@ GIT_INLINE(void) attr_cache_unlock(git_attr_cache *cache)
3333
GIT_INLINE(git_attr_file_entry *) attr_cache_lookup_entry(
3434
git_attr_cache *cache, const char *path)
3535
{
36-
size_t pos = git_strmap_lookup_index(cache->files, path);
37-
38-
if (git_strmap_valid_index(cache->files, pos))
39-
return git_strmap_value_at(cache->files, pos);
40-
else
41-
return NULL;
36+
return git_strmap_get(cache->files, path);
4237
}
4338

4439
int git_attr_cache__alloc_file_entry(
@@ -80,18 +75,16 @@ int git_attr_cache__alloc_file_entry(
8075
static int attr_cache_make_entry(
8176
git_attr_file_entry **out, git_repository *repo, const char *path)
8277
{
83-
int error = 0;
8478
git_attr_cache *cache = git_repository_attr_cache(repo);
8579
git_attr_file_entry *entry = NULL;
80+
int error;
8681

87-
error = git_attr_cache__alloc_file_entry(
88-
&entry, git_repository_workdir(repo), path, &cache->pool);
82+
if ((error = git_attr_cache__alloc_file_entry(&entry, git_repository_workdir(repo),
83+
path, &cache->pool)) < 0)
84+
return error;
8985

90-
if (!error) {
91-
git_strmap_insert(cache->files, entry->path, entry, &error);
92-
if (error > 0)
93-
error = 0;
94-
}
86+
if ((error = git_strmap_set(cache->files, entry->path, entry)) < 0)
87+
return error;
9588

9689
*out = entry;
9790
return error;
@@ -265,19 +258,15 @@ bool git_attr_cache__is_cached(
265258
const char *filename)
266259
{
267260
git_attr_cache *cache = git_repository_attr_cache(repo);
268-
git_strmap *files;
269-
size_t pos;
270261
git_attr_file_entry *entry;
262+
git_strmap *files;
271263

272264
if (!cache || !(files = cache->files))
273265
return false;
274266

275-
pos = git_strmap_lookup_index(files, filename);
276-
if (!git_strmap_valid_index(files, pos))
267+
if ((entry = git_strmap_get(files, filename)) == NULL)
277268
return false;
278269

279-
entry = git_strmap_value_at(files, pos);
280-
281270
return entry && (entry->file[source] != NULL);
282271
}
283272

@@ -400,8 +389,8 @@ int git_attr_cache__init(git_repository *repo)
400389
/* allocate hashtable for attribute and ignore file contents,
401390
* hashtable for attribute macros, and string pool
402391
*/
403-
if ((ret = git_strmap_alloc(&cache->files)) < 0 ||
404-
(ret = git_strmap_alloc(&cache->macros)) < 0)
392+
if ((ret = git_strmap_new(&cache->files)) < 0 ||
393+
(ret = git_strmap_new(&cache->macros)) < 0)
405394
goto cancel;
406395

407396
git_pool_init(&cache->pool, 1);
@@ -446,24 +435,17 @@ int git_attr_cache__insert_macro(git_repository *repo, git_attr_rule *macro)
446435
git_error_set(GIT_ERROR_OS, "unable to get attr cache lock");
447436
error = -1;
448437
} else {
449-
git_strmap_insert(macros, macro->match.pattern, macro, &error);
438+
error = git_strmap_set(macros, macro->match.pattern, macro);
450439
git_mutex_unlock(&cache->lock);
451440
}
452441

453-
return (error < 0) ? -1 : 0;
442+
return error;
454443
}
455444

456445
git_attr_rule *git_attr_cache__lookup_macro(
457446
git_repository *repo, const char *name)
458447
{
459448
git_strmap *macros = git_repository_attr_cache(repo)->macros;
460-
size_t pos;
461449

462-
pos = git_strmap_lookup_index(macros, name);
463-
464-
if (!git_strmap_valid_index(macros, pos))
465-
return NULL;
466-
467-
return (git_attr_rule *)git_strmap_value_at(macros, pos);
450+
return git_strmap_get(macros, name);
468451
}
469-

src/cache.c

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,15 @@ void git_cache_dump_stats(git_cache *cache)
6565
int git_cache_init(git_cache *cache)
6666
{
6767
memset(cache, 0, sizeof(*cache));
68-
cache->map = git_oidmap_alloc();
69-
GIT_ERROR_CHECK_ALLOC(cache->map);
68+
69+
if ((git_oidmap_new(&cache->map)) < 0)
70+
return -1;
71+
7072
if (git_rwlock_init(&cache->lock)) {
7173
git_error_set(GIT_ERROR_OS, "failed to initialize cache rwlock");
7274
return -1;
7375
}
76+
7477
return 0;
7578
}
7679

@@ -112,8 +115,7 @@ void git_cache_dispose(git_cache *cache)
112115
/* Called with lock */
113116
static void cache_evict_entries(git_cache *cache)
114117
{
115-
uint32_t seed = rand();
116-
size_t evict_count = 8;
118+
size_t evict_count = 8, i;
117119
ssize_t evicted_memory = 0;
118120

119121
/* do not infinite loop if there's not enough entries to evict */
@@ -122,18 +124,19 @@ static void cache_evict_entries(git_cache *cache)
122124
return;
123125
}
124126

127+
i = 0;
125128
while (evict_count > 0) {
126-
size_t pos = seed++ % git_oidmap_end(cache->map);
129+
git_cached_obj *evict;
130+
const git_oid *key;
127131

128-
if (git_oidmap_has_data(cache->map, pos)) {
129-
git_cached_obj *evict = git_oidmap_value_at(cache->map, pos);
132+
if (git_oidmap_iterate((void **) &evict, cache->map, &i, &key) == GIT_ITEROVER)
133+
break;
130134

131-
evict_count--;
132-
evicted_memory += evict->size;
133-
git_cached_obj_decref(evict);
135+
evict_count--;
136+
evicted_memory += evict->size;
137+
git_cached_obj_decref(evict);
134138

135-
git_oidmap_delete_at(cache->map, pos);
136-
}
139+
git_oidmap_delete(cache->map, key);
137140
}
138141

139142
cache->used_memory -= evicted_memory;
@@ -148,16 +151,12 @@ static bool cache_should_store(git_object_t object_type, size_t object_size)
148151

149152
static void *cache_get(git_cache *cache, const git_oid *oid, unsigned int flags)
150153
{
151-
size_t pos;
152-
git_cached_obj *entry = NULL;
154+
git_cached_obj *entry;
153155

154156
if (!git_cache__enabled || git_rwlock_rdlock(&cache->lock) < 0)
155157
return NULL;
156158

157-
pos = git_oidmap_lookup_index(cache->map, oid);
158-
if (git_oidmap_valid_index(cache->map, pos)) {
159-
entry = git_oidmap_value_at(cache->map, pos);
160-
159+
if ((entry = git_oidmap_get(cache->map, oid)) != NULL) {
161160
if (flags && entry->flags != flags) {
162161
entry = NULL;
163162
} else {
@@ -172,7 +171,7 @@ static void *cache_get(git_cache *cache, const git_oid *oid, unsigned int flags)
172171

173172
static void *cache_store(git_cache *cache, git_cached_obj *entry)
174173
{
175-
size_t pos;
174+
git_cached_obj *stored_entry;
176175

177176
git_cached_obj_incref(entry);
178177

@@ -191,34 +190,26 @@ static void *cache_store(git_cache *cache, git_cached_obj *entry)
191190
if (git_cache__current_storage.val > git_cache__max_storage)
192191
cache_evict_entries(cache);
193192

194-
pos = git_oidmap_lookup_index(cache->map, &entry->oid);
195-
196193
/* not found */
197-
if (!git_oidmap_valid_index(cache->map, pos)) {
198-
int rval;
199-
200-
git_oidmap_insert(cache->map, &entry->oid, entry, &rval);
201-
if (rval >= 0) {
194+
if ((stored_entry = git_oidmap_get(cache->map, &entry->oid)) == NULL) {
195+
if (git_oidmap_set(cache->map, &entry->oid, entry) == 0) {
202196
git_cached_obj_incref(entry);
203197
cache->used_memory += entry->size;
204198
git_atomic_ssize_add(&git_cache__current_storage, (ssize_t)entry->size);
205199
}
206200
}
207201
/* found */
208202
else {
209-
git_cached_obj *stored_entry = git_oidmap_value_at(cache->map, pos);
210-
211203
if (stored_entry->flags == entry->flags) {
212204
git_cached_obj_decref(entry);
213205
git_cached_obj_incref(stored_entry);
214206
entry = stored_entry;
215207
} else if (stored_entry->flags == GIT_CACHE_STORE_RAW &&
216-
entry->flags == GIT_CACHE_STORE_PARSED) {
208+
entry->flags == GIT_CACHE_STORE_PARSED) {
217209
git_cached_obj_decref(stored_entry);
218210
git_cached_obj_incref(entry);
219211

220-
git_oidmap_set_key_at(cache->map, pos, &entry->oid);
221-
git_oidmap_set_value_at(cache->map, pos, entry);
212+
git_oidmap_set(cache->map, &entry->oid, entry);
222213
} else {
223214
/* NO OP */
224215
}

src/checkout.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2518,11 +2518,11 @@ static int checkout_data_init(
25182518
git_pool_init(&data->pool, 1);
25192519

25202520
if ((error = git_vector_init(&data->removes, 0, git__strcmp_cb)) < 0 ||
2521-
(error = git_vector_init(&data->remove_conflicts, 0, NULL)) < 0 ||
2522-
(error = git_vector_init(&data->update_conflicts, 0, NULL)) < 0 ||
2523-
(error = git_buf_puts(&data->target_path, data->opts.target_directory)) < 0 ||
2524-
(error = git_path_to_dir(&data->target_path)) < 0 ||
2525-
(error = git_strmap_alloc(&data->mkdir_map)) < 0)
2521+
(error = git_vector_init(&data->remove_conflicts, 0, NULL)) < 0 ||
2522+
(error = git_vector_init(&data->update_conflicts, 0, NULL)) < 0 ||
2523+
(error = git_buf_puts(&data->target_path, data->opts.target_directory)) < 0 ||
2524+
(error = git_path_to_dir(&data->target_path)) < 0 ||
2525+
(error = git_strmap_new(&data->mkdir_map)) < 0)
25262526
goto cleanup;
25272527

25282528
data->target_len = git_buf_len(&data->target_path);

src/config_entries.c

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ int git_config_entries_new(git_config_entries **out)
5959
GIT_ERROR_CHECK_ALLOC(entries);
6060
GIT_REFCOUNT_INC(entries);
6161

62-
if ((error = git_strmap_alloc(&entries->map)) < 0)
62+
if ((error = git_strmap_new(&entries->map)) < 0)
6363
git__free(entries);
6464
else
6565
*out = entries;
@@ -133,14 +133,12 @@ int git_config_entries_append(git_config_entries *entries, git_config_entry *ent
133133
{
134134
config_entry_list *existing, *var;
135135
int error = 0;
136-
size_t pos;
137136

138137
var = git__calloc(1, sizeof(config_entry_list));
139138
GIT_ERROR_CHECK_ALLOC(var);
140139
var->entry = entry;
141140

142-
pos = git_strmap_lookup_index(entries->map, entry->name);
143-
if (!git_strmap_valid_index(entries->map, pos)) {
141+
if ((existing = git_strmap_get(entries->map, entry->name)) == NULL) {
144142
/*
145143
* We only ever inspect `last` from the first config
146144
* entry in a multivar. In case where this new entry is
@@ -152,12 +150,8 @@ int git_config_entries_append(git_config_entries *entries, git_config_entry *ent
152150
*/
153151
var->last = var;
154152

155-
git_strmap_insert(entries->map, entry->name, var, &error);
156-
157-
if (error > 0)
158-
error = 0;
153+
error = git_strmap_set(entries->map, entry->name, var);
159154
} else {
160-
existing = git_strmap_value_at(entries->map, pos);
161155
config_entry_list_append(&existing, var);
162156
}
163157

@@ -171,15 +165,12 @@ int git_config_entries_append(git_config_entries *entries, git_config_entry *ent
171165

172166
int config_entry_get(config_entry_list **out, git_config_entries *entries, const char *key)
173167
{
174-
size_t pos;
175-
176-
pos = git_strmap_lookup_index(entries->map, key);
168+
config_entry_list *list;
177169

178-
/* no error message; the config system will write one */
179-
if (!git_strmap_valid_index(entries->map, pos))
170+
if ((list = git_strmap_get(entries->map, key)) == NULL)
180171
return GIT_ENOTFOUND;
181172

182-
*out = git_strmap_value_at(entries->map, pos);
173+
*out = list;
183174

184175
return 0;
185176
}

src/describe.c

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,7 @@ struct commit_name {
3636

3737
static void *oidmap_value_bykey(git_oidmap *map, const git_oid *key)
3838
{
39-
size_t pos = git_oidmap_lookup_index(map, key);
40-
41-
if (!git_oidmap_valid_index(map, pos))
42-
return NULL;
43-
44-
return git_oidmap_value_at(map, pos);
39+
return git_oidmap_get(map, key);
4540
}
4641

4742
static struct commit_name *find_commit_name(
@@ -124,13 +119,8 @@ static int add_to_known_names(
124119
e->path = git__strdup(path);
125120
git_oid_cpy(&e->peeled, peeled);
126121

127-
if (!found) {
128-
int ret;
129-
130-
git_oidmap_insert(names, &e->peeled, e, &ret);
131-
if (ret < 0)
132-
return -1;
133-
}
122+
if (!found && git_oidmap_set(names, &e->peeled, e) < 0)
123+
return -1;
134124
}
135125
else
136126
git_tag_free(tag);
@@ -681,8 +671,8 @@ int git_describe_commit(
681671
"git_describe_options");
682672
data.opts = &normalized;
683673

684-
data.names = git_oidmap_alloc();
685-
GIT_ERROR_CHECK_ALLOC(data.names);
674+
if ((error = git_oidmap_new(&data.names)) < 0)
675+
return error;
686676

687677
/** TODO: contains to be implemented */
688678

0 commit comments

Comments
 (0)