Commit b9d0b664 by Patrick Steinhardt

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.
parent aa245623
......@@ -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);
......
......@@ -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);
......
......@@ -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;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment