vendor/libgit2/src/sortedcache.c in rugged-0.27.10 vs vendor/libgit2/src/sortedcache.c in rugged-0.27.10.1

- old
+ new

@@ -18,23 +18,23 @@ git_sortedcache *sc; size_t pathlen, alloclen; pathlen = path ? strlen(path) : 0; - GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_sortedcache), pathlen); - GIT_ERROR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1); + GITERR_CHECK_ALLOC_ADD(&alloclen, sizeof(git_sortedcache), pathlen); + GITERR_CHECK_ALLOC_ADD(&alloclen, alloclen, 1); sc = git__calloc(1, alloclen); - GIT_ERROR_CHECK_ALLOC(sc); + GITERR_CHECK_ALLOC(sc); git_pool_init(&sc->pool, 1); if (git_vector_init(&sc->items, 4, item_cmp) < 0 || - git_strmap_new(&sc->map) < 0) + git_strmap_alloc(&sc->map) < 0) goto fail; if (git_rwlock_init(&sc->lock)) { - git_error_set(GIT_ERROR_OS, "failed to initialize lock"); + giterr_set(GITERR_OS, "failed to initialize lock"); goto fail; } sc->item_path_offset = item_path_offset; sc->free_item = free_item; @@ -165,11 +165,11 @@ int git_sortedcache_wlock(git_sortedcache *sc) { GIT_UNUSED(sc); /* prevent warning when compiled w/o threads */ if (git_rwlock_wrlock(&sc->lock) < 0) { - git_error_set(GIT_ERROR_OS, "unable to acquire write lock on cache"); + giterr_set(GITERR_OS, "unable to acquire write lock on cache"); return -1; } return 0; } @@ -184,11 +184,11 @@ int git_sortedcache_rlock(git_sortedcache *sc) { GIT_UNUSED(sc); /* prevent warning when compiled w/o threads */ if (git_rwlock_rdlock(&sc->lock) < 0) { - git_error_set(GIT_ERROR_OS, "unable to acquire read lock on cache"); + giterr_set(GITERR_OS, "unable to acquire read lock on cache"); return -1; } return 0; } @@ -217,18 +217,18 @@ error = fd; goto unlock; } if (p_fstat(fd, &st) < 0) { - git_error_set(GIT_ERROR_OS, "failed to stat file"); + giterr_set(GITERR_OS, "failed to stat file"); error = -1; (void)p_close(fd); goto unlock; } if (!git__is_sizet(st.st_size)) { - git_error_set(GIT_ERROR_INVALID, "unable to load file larger than size_t"); + giterr_set(GITERR_INVALID, "unable to load file larger than size_t"); error = -1; (void)p_close(fd); goto unlock; } @@ -268,24 +268,28 @@ } /* find and/or insert item, returning pointer to item data */ int git_sortedcache_upsert(void **out, git_sortedcache *sc, const char *key) { - size_t keylen, itemlen; int error = 0; - char *item_key; + khiter_t pos; void *item; + size_t keylen, itemlen; + char *item_key; - if ((item = git_strmap_get(sc->map, key)) != NULL) + pos = git_strmap_lookup_index(sc->map, key); + if (git_strmap_valid_index(sc->map, pos)) { + item = git_strmap_value_at(sc->map, pos); goto done; + } keylen = strlen(key); itemlen = sc->item_path_offset + keylen + 1; itemlen = (itemlen + 7) & ~7; - if ((item = git_pool_mallocz(&sc->pool, itemlen)) == NULL) { - /* don't use GIT_ERROR_CHECK_ALLOC b/c of lock */ + if ((item = git_pool_mallocz(&sc->pool, (uint32_t)itemlen)) == NULL) { + /* don't use GITERR_CHECK_ALLOC b/c of lock */ error = -1; goto done; } /* one strange thing is that even if the vector or hash table insert @@ -293,26 +297,35 @@ */ item_key = ((char *)item) + sc->item_path_offset; memcpy(item_key, key, keylen); - if ((error = git_strmap_set(sc->map, item_key, item)) < 0) + pos = git_strmap_put(sc->map, item_key, &error); + if (error < 0) goto done; - if ((error = git_vector_insert(&sc->items, item)) < 0) - git_strmap_delete(sc->map, item_key); + 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); + done: if (out) *out = !error ? item : NULL; return error; } /* lookup item by key */ void *git_sortedcache_lookup(const git_sortedcache *sc, const char *key) { - return git_strmap_get(sc->map, key); + khiter_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; } /* find out how many items are in the cache */ size_t git_sortedcache_entrycount(const git_sortedcache *sc) { @@ -356,23 +369,24 @@ /* remove entry from cache */ int git_sortedcache_remove(git_sortedcache *sc, size_t pos) { char *item; + khiter_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"); + giterr_set(GITERR_INVALID, "removing item out of range"); return GIT_ENOTFOUND; } (void)git_vector_remove(&sc->items, pos); - git_strmap_delete(sc->map, item + sc->item_path_offset); + mappos = git_strmap_lookup_index(sc->map, item + sc->item_path_offset); + git_strmap_delete_at(sc->map, mappos); if (sc->free_item) sc->free_item(sc->free_item_payload, item); return 0;