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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
diff --git a/src/offmap.c b/src/offmap.c
index 561b63c..8a3bf49 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 5491ba7..bf2ef7f 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 ab78d3f..c9d5602 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;