Skip to content

Commit d0c72a9

Browse files
authored
Merge pull request libgit2#4092 from pks-t/pks/khash-cleanups
khash cleanups
2 parents 021f494 + 8f1ff26 commit d0c72a9

33 files changed

+610
-347
lines changed

src/attr.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
#include "git2/oid.h"
88
#include <ctype.h>
99

10-
GIT__USE_STRMAP
11-
1210
const char *git_attr__true = "[internal]__TRUE__";
1311
const char *git_attr__false = "[internal]__FALSE__";
1412
const char *git_attr__unset = "[internal]__UNSET__";
@@ -209,7 +207,7 @@ int git_attr_foreach(
209207
if (git_strmap_exists(seen, assign->name))
210208
continue;
211209

212-
git_strmap_insert(seen, assign->name, assign, error);
210+
git_strmap_insert(seen, assign->name, assign, &error);
213211
if (error < 0)
214212
goto cleanup;
215213

src/attrcache.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
#include "sysdir.h"
66
#include "ignore.h"
77

8-
GIT__USE_STRMAP
9-
108
GIT_INLINE(int) attr_cache_lock(git_attr_cache *cache)
119
{
1210
GIT_UNUSED(cache); /* avoid warning if threading is off */
@@ -82,7 +80,7 @@ static int attr_cache_make_entry(
8280
&entry, git_repository_workdir(repo), path, &cache->pool);
8381

8482
if (!error) {
85-
git_strmap_insert(cache->files, entry->path, entry, error);
83+
git_strmap_insert(cache->files, entry->path, entry, &error);
8684
if (error > 0)
8785
error = 0;
8886
}
@@ -435,7 +433,7 @@ int git_attr_cache__insert_macro(git_repository *repo, git_attr_rule *macro)
435433
giterr_set(GITERR_OS, "unable to get attr cache lock");
436434
error = -1;
437435
} else {
438-
git_strmap_insert(macros, macro->match.pattern, macro, error);
436+
git_strmap_insert(macros, macro->match.pattern, macro, &error);
439437
git_mutex_unlock(&cache->lock);
440438
}
441439

src/cache.c

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
#include "object.h"
1616
#include "git2/oid.h"
1717

18-
GIT__USE_OIDMAP
19-
2018
bool git_cache__enabled = true;
2119
ssize_t git_cache__max_storage = (256 * 1024 * 1024);
2220
git_atomic_ssize git_cache__current_storage = {0};
@@ -47,13 +45,13 @@ void git_cache_dump_stats(git_cache *cache)
4745
{
4846
git_cached_obj *object;
4947

50-
if (kh_size(cache->map) == 0)
48+
if (git_cache_size(cache) == 0)
5149
return;
5250

53-
printf("Cache %p: %d items cached, %"PRIdZ" bytes\n",
54-
cache, kh_size(cache->map), cache->used_memory);
51+
printf("Cache %p: %"PRIuZ" items cached, %"PRIdZ" bytes\n",
52+
cache, git_cache_size(cache), cache->used_memory);
5553

56-
kh_foreach_value(cache->map, object, {
54+
git_oidmap_foreach_value(cache->map, object, {
5755
char oid_str[9];
5856
printf(" %s%c %s (%"PRIuZ")\n",
5957
git_object_type2string(object->type),
@@ -81,14 +79,14 @@ static void clear_cache(git_cache *cache)
8179
{
8280
git_cached_obj *evict = NULL;
8381

84-
if (kh_size(cache->map) == 0)
82+
if (git_cache_size(cache) == 0)
8583
return;
8684

87-
kh_foreach_value(cache->map, evict, {
85+
git_oidmap_foreach_value(cache->map, evict, {
8886
git_cached_obj_decref(evict);
8987
});
9088

91-
kh_clear(oid, cache->map);
89+
git_oidmap_clear(cache->map);
9290
git_atomic_ssize_add(&git_cache__current_storage, -cache->used_memory);
9391
cache->used_memory = 0;
9492
}
@@ -119,22 +117,22 @@ static void cache_evict_entries(git_cache *cache)
119117
ssize_t evicted_memory = 0;
120118

121119
/* do not infinite loop if there's not enough entries to evict */
122-
if (evict_count > kh_size(cache->map)) {
120+
if (evict_count > git_cache_size(cache)) {
123121
clear_cache(cache);
124122
return;
125123
}
126124

127125
while (evict_count > 0) {
128-
khiter_t pos = seed++ % kh_end(cache->map);
126+
khiter_t pos = seed++ % git_oidmap_end(cache->map);
129127

130-
if (kh_exist(cache->map, pos)) {
131-
git_cached_obj *evict = kh_val(cache->map, pos);
128+
if (git_oidmap_has_data(cache->map, pos)) {
129+
git_cached_obj *evict = git_oidmap_value_at(cache->map, pos);
132130

133131
evict_count--;
134132
evicted_memory += evict->size;
135133
git_cached_obj_decref(evict);
136134

137-
kh_del(oid, cache->map, pos);
135+
git_oidmap_delete_at(cache->map, pos);
138136
}
139137
}
140138

@@ -156,9 +154,9 @@ static void *cache_get(git_cache *cache, const git_oid *oid, unsigned int flags)
156154
if (!git_cache__enabled || git_rwlock_rdlock(&cache->lock) < 0)
157155
return NULL;
158156

159-
pos = kh_get(oid, cache->map, oid);
160-
if (pos != kh_end(cache->map)) {
161-
entry = kh_val(cache->map, pos);
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);
162160

163161
if (flags && entry->flags != flags) {
164162
entry = NULL;
@@ -193,24 +191,22 @@ static void *cache_store(git_cache *cache, git_cached_obj *entry)
193191
if (git_cache__current_storage.val > git_cache__max_storage)
194192
cache_evict_entries(cache);
195193

196-
pos = kh_get(oid, cache->map, &entry->oid);
194+
pos = git_oidmap_lookup_index(cache->map, &entry->oid);
197195

198196
/* not found */
199-
if (pos == kh_end(cache->map)) {
197+
if (!git_oidmap_valid_index(cache->map, pos)) {
200198
int rval;
201199

202-
pos = kh_put(oid, cache->map, &entry->oid, &rval);
200+
git_oidmap_insert(cache->map, &entry->oid, entry, &rval);
203201
if (rval >= 0) {
204-
kh_key(cache->map, pos) = &entry->oid;
205-
kh_val(cache->map, pos) = entry;
206202
git_cached_obj_incref(entry);
207203
cache->used_memory += entry->size;
208204
git_atomic_ssize_add(&git_cache__current_storage, (ssize_t)entry->size);
209205
}
210206
}
211207
/* found */
212208
else {
213-
git_cached_obj *stored_entry = kh_val(cache->map, pos);
209+
git_cached_obj *stored_entry = git_oidmap_value_at(cache->map, pos);
214210

215211
if (stored_entry->flags == entry->flags) {
216212
git_cached_obj_decref(entry);
@@ -221,8 +217,8 @@ static void *cache_store(git_cache *cache, git_cached_obj *entry)
221217
git_cached_obj_decref(stored_entry);
222218
git_cached_obj_incref(entry);
223219

224-
kh_key(cache->map, pos) = &entry->oid;
225-
kh_val(cache->map, pos) = entry;
220+
git_oidmap_set_key_at(cache->map, pos, &entry->oid);
221+
git_oidmap_set_value_at(cache->map, pos, entry);
226222
} else {
227223
/* NO OP */
228224
}

src/cache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void *git_cache_get_any(git_cache *cache, const git_oid *oid);
5353

5454
GIT_INLINE(size_t) git_cache_size(git_cache *cache)
5555
{
56-
return (size_t)kh_size(cache->map);
56+
return (size_t)git_oidmap_size(cache->map);
5757
}
5858

5959
GIT_INLINE(void) git_cached_obj_incref(void *_obj)

src/checkout.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@
3535
#include "pool.h"
3636
#include "strmap.h"
3737

38-
GIT__USE_STRMAP
39-
4038
/* See docs/checkout-internals.md for more information */
4139

4240
enum {

src/config_file.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
#include <sys/types.h>
2222
#include <regex.h>
2323

24-
GIT__USE_STRMAP
25-
2624
typedef struct cvar_t {
2725
struct cvar_t *next;
2826
git_config_entry *entry;
@@ -179,7 +177,7 @@ static int append_entry(git_strmap *values, cvar_t *var)
179177

180178
pos = git_strmap_lookup_index(values, var->entry->name);
181179
if (!git_strmap_valid_index(values, pos)) {
182-
git_strmap_insert(values, var->entry->name, var, error);
180+
git_strmap_insert(values, var->entry->name, var, &error);
183181
} else {
184182
existing = git_strmap_value_at(values, pos);
185183
while (existing->next != NULL) {

src/describe.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
#include "vector.h"
2020
#include "repository.h"
2121

22-
GIT__USE_OIDMAP
23-
2422
/* Ported from https://github.com/git/git/blob/89dde7882f71f846ccd0359756d27bebc31108de/builtin/describe.c */
2523

2624
struct commit_name {
@@ -127,7 +125,7 @@ static int add_to_known_names(
127125
if (!found) {
128126
int ret;
129127

130-
git_oidmap_insert(names, &e->peeled, e, ret);
128+
git_oidmap_insert(names, &e->peeled, e, &ret);
131129
if (ret < 0)
132130
return -1;
133131
}

src/diff_driver.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
#include "config.h"
1717
#include "repository.h"
1818

19-
GIT__USE_STRMAP
20-
2119
typedef enum {
2220
DIFF_DRIVER_AUTO = 0,
2321
DIFF_DRIVER_BINARY = 1,
@@ -217,7 +215,7 @@ static int git_diff_driver_builtin(
217215
goto done;
218216
}
219217

220-
git_strmap_insert(reg->drivers, drv->name, drv, error);
218+
git_strmap_insert(reg->drivers, drv->name, drv, &error);
221219
if (error > 0)
222220
error = 0;
223221

@@ -331,7 +329,7 @@ static int git_diff_driver_load(
331329
goto done;
332330

333331
/* store driver in registry */
334-
git_strmap_insert(reg->drivers, drv->name, drv, error);
332+
git_strmap_insert(reg->drivers, drv->name, drv, &error);
335333
if (error < 0)
336334
goto done;
337335
error = 0;

src/fileops.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
#include "win32/findfile.h"
1414
#endif
1515

16-
GIT__USE_STRMAP
17-
1816
int git_futils_mkpath2file(const char *file_path, const mode_t mode)
1917
{
2018
return git_futils_mkdir(
@@ -607,7 +605,7 @@ int git_futils_mkdir_relative(
607605

608606
memcpy(cache_path, make_path.ptr, make_path.size + 1);
609607

610-
git_strmap_insert(opts->dir_map, cache_path, cache_path, error);
608+
git_strmap_insert(opts->dir_map, cache_path, cache_path, &error);
611609
if (error < 0)
612610
goto done;
613611
}

0 commit comments

Comments
 (0)