diff options
Diffstat (limited to 'src')
| -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; |
