diff options
Diffstat (limited to 'src/sortedcache.c')
-rw-r--r-- | src/sortedcache.c | 36 |
1 files changed, 11 insertions, 25 deletions
diff --git a/src/sortedcache.c b/src/sortedcache.c index 02a685e10..15ba6fdd4 100644 --- a/src/sortedcache.c +++ b/src/sortedcache.c @@ -28,7 +28,7 @@ int git_sortedcache_new( git_pool_init(&sc->pool, 1); if (git_vector_init(&sc->items, 4, item_cmp) < 0 || - git_strmap_alloc(&sc->map) < 0) + git_strmap_new(&sc->map) < 0) goto fail; if (git_rwlock_init(&sc->lock)) { @@ -270,17 +270,13 @@ int git_sortedcache_clear(git_sortedcache *sc, bool wlock) /* find and/or insert item, returning pointer to item data */ int git_sortedcache_upsert(void **out, git_sortedcache *sc, const char *key) { - size_t pos; - int error = 0; - void *item; size_t keylen, itemlen; + int error = 0; char *item_key; + void *item; - pos = git_strmap_lookup_index(sc->map, key); - if (git_strmap_valid_index(sc->map, pos)) { - item = git_strmap_value_at(sc->map, pos); + if ((item = git_strmap_get(sc->map, key)) != NULL) goto done; - } keylen = strlen(key); itemlen = sc->item_path_offset + keylen + 1; @@ -299,17 +295,11 @@ int git_sortedcache_upsert(void **out, git_sortedcache *sc, const char *key) item_key = ((char *)item) + sc->item_path_offset; memcpy(item_key, key, keylen); - pos = git_strmap_put(sc->map, item_key, &error); - if (error < 0) + if ((error = git_strmap_set(sc->map, item_key, item)) < 0) goto done; - if (!error) - git_strmap_set_key_at(sc->map, pos, item_key); - git_strmap_set_value_at(sc->map, pos, item); - - error = git_vector_insert(&sc->items, item); - if (error < 0) - git_strmap_delete_at(sc->map, pos); + if ((error = git_vector_insert(&sc->items, item)) < 0) + git_strmap_delete(sc->map, item_key); done: if (out) @@ -320,10 +310,7 @@ done: /* lookup item by key */ void *git_sortedcache_lookup(const git_sortedcache *sc, const char *key) { - size_t pos = git_strmap_lookup_index(sc->map, key); - if (git_strmap_valid_index(sc->map, pos)) - return git_strmap_value_at(sc->map, pos); - return NULL; + return git_strmap_get(sc->map, key); } /* find out how many items are in the cache */ @@ -371,9 +358,9 @@ int git_sortedcache_lookup_index( int git_sortedcache_remove(git_sortedcache *sc, size_t pos) { char *item; - size_t mappos; - /* because of pool allocation, this can't actually remove the item, + /* + * Because of pool allocation, this can't actually remove the item, * but we can remove it from the items vector and the hash table. */ @@ -384,8 +371,7 @@ int git_sortedcache_remove(git_sortedcache *sc, size_t pos) (void)git_vector_remove(&sc->items, pos); - mappos = git_strmap_lookup_index(sc->map, item + sc->item_path_offset); - git_strmap_delete_at(sc->map, mappos); + git_strmap_delete(sc->map, item + sc->item_path_offset); if (sc->free_item) sc->free_item(sc->free_item_payload, item); |