Skip to content

Commit b9d0b66

Browse files
committed
offmap: introduce high-level setter for key/value pairs
Currently, there is only one caller that adds entries into an offset map, and this caller first uses `git_offmap_put` to add a key and then set the value at the returned index by using `git_offmap_set_value_at`. This is just too tighlty coupled with implementation details of the map as it exposes the index of inserted entries, which we really do not care about at all. Introduce a new function `git_offmap_set`, which takes as parameters the map, key and value and directly returns an error code. Convert the caller to make use of it instead.
1 parent aa24562 commit b9d0b66

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

src/offmap.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@ void *git_offmap_get(git_offmap *map, const git_off_t key)
5151
return kh_val(map, idx);
5252
}
5353

54+
int git_offmap_set(git_offmap *map, const git_off_t key, void *value)
55+
{
56+
size_t idx;
57+
int rval;
58+
59+
idx = kh_put(off, map, key, &rval);
60+
if (rval < 0)
61+
return -1;
62+
63+
if (rval == 0)
64+
kh_key(map, idx) = key;
65+
66+
kh_val(map, idx) = value;
67+
68+
return 0;
69+
}
70+
71+
5472
size_t git_offmap_lookup_index(git_offmap *map, const git_off_t key)
5573
{
5674
return kh_get(off, map, key);

src/offmap.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,21 @@ size_t git_offmap_size(git_offmap *map);
6161
*/
6262
void *git_offmap_get(git_offmap *map, const git_off_t key);
6363

64+
/**
65+
* Set the entry for key to value.
66+
*
67+
* If the map has no corresponding entry for the given key, a new
68+
* entry will be created with the given value. If an entry exists
69+
* already, its value will be updated to match the given value.
70+
*
71+
* @param map map to create new entry in
72+
* @param key key to set
73+
* @param value value to associate the key with; may be NULL
74+
* @return zero if the key was successfully set, a negative error
75+
* code otherwise
76+
*/
77+
int git_offmap_set(git_offmap *map, const git_off_t key, void *value);
78+
6479
size_t git_offmap_lookup_index(git_offmap *map, const git_off_t key);
6580
int git_offmap_valid_index(git_offmap *map, size_t idx);
6681

src/pack.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,7 @@ static int cache_add(
147147
git_off_t offset)
148148
{
149149
git_pack_cache_entry *entry;
150-
int error, exists = 0;
151-
size_t k;
150+
int exists;
152151

153152
if (base->len > GIT_PACK_CACHE_SIZE_LIMIT)
154153
return -1;
@@ -166,9 +165,7 @@ static int cache_add(
166165
while (cache->memory_used + base->len > cache->memory_limit)
167166
free_lowest_entry(cache);
168167

169-
k = git_offmap_put(cache->entries, offset, &error);
170-
assert(error != 0);
171-
git_offmap_set_value_at(cache->entries, k, entry);
168+
git_offmap_set(cache->entries, offset, entry);
172169
cache->memory_used += entry->raw.len;
173170

174171
*cached_out = entry;

0 commit comments

Comments
 (0)