vendor/libgit2/src/sortedcache.c in rugged-0.28.5 vs vendor/libgit2/src/sortedcache.c in rugged-0.99.0

- old
+ new

@@ -26,11 +26,11 @@ GIT_ERROR_CHECK_ALLOC(sc); 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)) { git_error_set(GIT_ERROR_OS, "failed to initialize lock"); goto fail; @@ -268,27 +268,23 @@ } /* 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; itemlen = (itemlen + 7) & ~7; - if ((item = git_pool_mallocz(&sc->pool, (uint32_t)itemlen)) == NULL) { + if ((item = git_pool_mallocz(&sc->pool, itemlen)) == NULL) { /* don't use GIT_ERROR_CHECK_ALLOC b/c of lock */ error = -1; goto done; } @@ -297,35 +293,26 @@ */ 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); + if ((error = git_vector_insert(&sc->items, item)) < 0) + git_strmap_delete(sc->map, item_key); - error = git_vector_insert(&sc->items, item); - if (error < 0) - git_strmap_delete_at(sc->map, pos); - done: if (out) *out = !error ? item : NULL; return error; } /* 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 */ size_t git_sortedcache_entrycount(const git_sortedcache *sc) { @@ -369,24 +356,23 @@ /* remove entry from cache */ 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. */ if ((item = git_vector_get(&sc->items, pos)) == NULL) { git_error_set(GIT_ERROR_INVALID, "removing item out of range"); return GIT_ENOTFOUND; } (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); return 0;