diff options
author | Patrick Steinhardt <ps@pks.im> | 2018-12-17 09:10:53 +0100 |
---|---|---|
committer | Patrick Steinhardt <ps@pks.im> | 2019-02-15 13:16:48 +0100 |
commit | b9d0b664071fed96e690a70ee4472facf554eb70 (patch) | |
tree | 2507c85e58c49d378a028c7b571d98fe3809f4f3 | |
parent | aa2456239b4644c43d3cc9e002ed718e5078e7cc (diff) | |
download | libgit2-b9d0b664071fed96e690a70ee4472facf554eb70.tar.gz |
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.
-rw-r--r-- | src/offmap.c | 18 | ||||
-rw-r--r-- | src/offmap.h | 15 | ||||
-rw-r--r-- | src/pack.c | 7 |
3 files changed, 35 insertions, 5 deletions
diff --git a/src/offmap.c b/src/offmap.c index 561b63cf2..8a3bf4908 100644 --- a/src/offmap.c +++ b/src/offmap.c @@ -51,6 +51,24 @@ void *git_offmap_get(git_offmap *map, const git_off_t key) return kh_val(map, idx); } +int git_offmap_set(git_offmap *map, const git_off_t key, void *value) +{ + size_t idx; + int rval; + + idx = kh_put(off, map, key, &rval); + if (rval < 0) + return -1; + + if (rval == 0) + kh_key(map, idx) = key; + + kh_val(map, idx) = value; + + return 0; +} + + size_t git_offmap_lookup_index(git_offmap *map, const git_off_t key) { return kh_get(off, map, key); diff --git a/src/offmap.h b/src/offmap.h index 5491ba76b..bf2ef7fbb 100644 --- a/src/offmap.h +++ b/src/offmap.h @@ -61,6 +61,21 @@ size_t git_offmap_size(git_offmap *map); */ void *git_offmap_get(git_offmap *map, const git_off_t key); +/** + * Set the entry for key to value. + * + * If the map has no corresponding entry for the given key, a new + * entry will be created with the given value. If an entry exists + * already, its value will be updated to match the given value. + * + * @param map map to create new entry in + * @param key key to set + * @param value value to associate the key with; may be NULL + * @return zero if the key was successfully set, a negative error + * code otherwise + */ +int git_offmap_set(git_offmap *map, const git_off_t key, void *value); + size_t git_offmap_lookup_index(git_offmap *map, const git_off_t key); int git_offmap_valid_index(git_offmap *map, size_t idx); diff --git a/src/pack.c b/src/pack.c index ab78d3f61..c9d5602cd 100644 --- a/src/pack.c +++ b/src/pack.c @@ -147,8 +147,7 @@ static int cache_add( git_off_t offset) { git_pack_cache_entry *entry; - int error, exists = 0; - size_t k; + int exists; if (base->len > GIT_PACK_CACHE_SIZE_LIMIT) return -1; @@ -166,9 +165,7 @@ static int cache_add( while (cache->memory_used + base->len > cache->memory_limit) free_lowest_entry(cache); - k = git_offmap_put(cache->entries, offset, &error); - assert(error != 0); - git_offmap_set_value_at(cache->entries, k, entry); + git_offmap_set(cache->entries, offset, entry); cache->memory_used += entry->raw.len; *cached_out = entry; |