Commit aa245623 by Patrick Steinhardt

offmap: introduce high-level getter for values

The current way of looking up an entry from a map is tightly coupled with the
map implementation, as one first has to look up the index of the key and then
retrieve the associated value by using the index. As a caller, you usually do
not care about any indices at all, though, so this is more complicated than
really necessary. Furthermore, it invites for errors to happen if the correct
error checking sequence is not being followed.

Introduce a new high-level function `git_offmap_get` that takes a map and a key
and returns a pointer to the associated value if such a key exists. Otherwise,
a `NULL` pointer is returned. Adjust all callers that can trivially be
converted.
parent 2e0a3048
......@@ -42,6 +42,15 @@ size_t git_offmap_size(git_offmap *map)
return kh_size(map);
}
void *git_offmap_get(git_offmap *map, const git_off_t key)
{
size_t idx = git_offmap_lookup_index(map, key);
if (!git_offmap_valid_index(map, idx) ||
!git_offmap_has_data(map, idx))
return NULL;
return kh_val(map, idx);
}
size_t git_offmap_lookup_index(git_offmap *map, const git_off_t key)
{
return kh_get(off, map, key);
......
......@@ -52,6 +52,15 @@ void git_offmap_clear(git_offmap *map);
*/
size_t git_offmap_size(git_offmap *map);
/**
* Return value associated with the given key.
*
* @param map map to search key in
* @param key key to search for
* @return value associated with the given key or NULL if the key was not found
*/
void *git_offmap_get(git_offmap *map, const git_off_t key);
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);
......
......@@ -111,15 +111,12 @@ static int cache_init(git_pack_cache *cache)
static git_pack_cache_entry *cache_get(git_pack_cache *cache, git_off_t offset)
{
git_pack_cache_entry *entry = NULL;
size_t k;
git_pack_cache_entry *entry;
if (git_mutex_lock(&cache->lock) < 0)
return NULL;
k = git_offmap_lookup_index(cache->entries, offset);
if (git_offmap_valid_index(cache->entries, k)) { /* found it */
entry = git_offmap_value_at(cache->entries, k);
if ((entry = git_offmap_get(cache->entries, offset)) != NULL) {
git_atomic_inc(&entry->refcount);
entry->last_usage = cache->use_ctr++;
}
......
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