Skip to content

Commit cf6124d

Browse files
committed
offmap: convert to use functions instead of macros
1 parent 0d71690 commit cf6124d

File tree

2 files changed

+97
-23
lines changed

2 files changed

+97
-23
lines changed

src/offmap.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (C) the libgit2 contributors. All rights reserved.
3+
*
4+
* This file is part of libgit2, distributed under the GNU GPL v2 with
5+
* a Linking Exception. For full terms see the included COPYING file.
6+
*/
7+
8+
#include "offmap.h"
9+
10+
GIT__USE_OFFMAP
11+
12+
git_offmap *git_offmap_alloc(void)
13+
{
14+
return kh_init(off);
15+
}
16+
17+
void git_offmap__free(git_offmap *map)
18+
{
19+
kh_destroy(off, map);
20+
}
21+
22+
void git_offmap_clear(git_offmap *map)
23+
{
24+
kh_clear(off, map);
25+
}
26+
27+
size_t git_offmap_num_entries(git_offmap *map)
28+
{
29+
return kh_size(map);
30+
}
31+
32+
size_t git_offmap_lookup_index(git_offmap *map, const git_off_t key)
33+
{
34+
return kh_get(off, map, key);
35+
}
36+
37+
int git_offmap_valid_index(git_offmap *map, size_t idx)
38+
{
39+
return idx != kh_end(map);
40+
}
41+
42+
int git_offmap_exists(git_offmap *map, const git_off_t key)
43+
{
44+
return kh_get(off, map, key) != kh_end(map);
45+
}
46+
47+
void *git_offmap_value_at(git_offmap *map, size_t idx)
48+
{
49+
return kh_val(map, idx);
50+
}
51+
52+
void git_offmap_set_value_at(git_offmap *map, size_t idx, void *value)
53+
{
54+
kh_val(map, idx) = value;
55+
}
56+
57+
void git_offmap_delete_at(git_offmap *map, size_t idx)
58+
{
59+
kh_del(off, map, idx);
60+
}
61+
62+
int git_offmap_put(git_offmap *map, const git_off_t key, int *err)
63+
{
64+
return kh_put(off, map, key, err);
65+
}
66+
67+
void git_offmap_insert(git_offmap *map, const git_off_t key, void *value, int *rval)
68+
{
69+
khiter_t idx = kh_put(off, map, key, rval);
70+
71+
if ((*rval) >= 0) {
72+
if ((*rval) == 0)
73+
kh_key(map, idx) = key;
74+
kh_val(map, idx) = value;
75+
}
76+
}
77+
78+
void git_offmap_delete(git_offmap *map, const git_off_t key)
79+
{
80+
khiter_t idx = git_offmap_lookup_index(map, key);
81+
if (git_offmap_valid_index(map, idx))
82+
git_offmap_delete_at(map, idx);
83+
}

src/offmap.h

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,34 +23,25 @@ typedef khash_t(off) git_offmap;
2323
#define GIT__USE_OFFMAP \
2424
__KHASH_IMPL(off, static kh_inline, git_off_t, void *, 1, kh_int64_hash_func, kh_int64_hash_equal)
2525

26-
#define git_offmap_alloc() kh_init(off)
27-
#define git_offmap_free(h) kh_destroy(off, h), h = NULL
28-
#define git_offmap_clear(h) kh_clear(off, h)
26+
git_offmap *git_offmap_alloc(void);
27+
#define git_offmap_free(h) git_offmap__free(h); (h) = NULL
28+
void git_offmap__free(git_offmap *map);
29+
void git_offmap_clear(git_offmap *map);
2930

30-
#define git_offmap_num_entries(h) kh_size(h)
31+
size_t git_offmap_num_entries(git_offmap *map);
3132

32-
#define git_offmap_lookup_index(h, k) kh_get(off, h, k)
33-
#define git_offmap_valid_index(h, idx) (idx != kh_end(h))
33+
size_t git_offmap_lookup_index(git_offmap *map, const git_off_t key);
34+
int git_offmap_valid_index(git_offmap *map, size_t idx);
3435

35-
#define git_offmap_exists(h, k) (kh_get(off, h, k) != kh_end(h))
36+
int git_offmap_exists(git_offmap *map, const git_off_t key);
3637

37-
#define git_offmap_value_at(h, idx) kh_val(h, idx)
38-
#define git_offmap_set_value_at(h, idx, v) kh_val(h, idx) = v
39-
#define git_offmap_delete_at(h, idx) kh_del(off, h, idx)
38+
void *git_offmap_value_at(git_offmap *map, size_t idx);
39+
void git_offmap_set_value_at(git_offmap *map, size_t idx, void *value);
40+
void git_offmap_delete_at(git_offmap *map, size_t idx);
4041

41-
#define git_offmap_put(h, k, err) kh_put(off, h, k, err)
42-
43-
#define git_offmap_insert(h, key, val, rval) do { \
44-
khiter_t __pos = kh_put(off, h, key, rval); \
45-
if ((*rval) >= 0) { \
46-
if ((*rval) == 0) kh_key(h, __pos) = key; \
47-
kh_val(h, __pos) = val; \
48-
} } while (0)
49-
50-
#define git_offmap_delete(h, key) do { \
51-
khiter_t __pos = git_offmap_lookup_index(h, key); \
52-
if (git_offmap_valid_index(h, __pos)) \
53-
git_offmap_delete_at(h, __pos); } while (0)
42+
int git_offmap_put(git_offmap *map, const git_off_t key, int *err);
43+
void git_offmap_insert(git_offmap *map, const git_off_t key, void *value, int *rval);
44+
void git_offmap_delete(git_offmap *map, const git_off_t key);
5445

5546
#define git_offmap_foreach kh_foreach
5647
#define git_offmap_foreach_value kh_foreach_value

0 commit comments

Comments
 (0)