diff options
author | Patrick Steinhardt <ps@pks.im> | 2019-01-23 10:42:46 +0100 |
---|---|---|
committer | Patrick Steinhardt <ps@pks.im> | 2019-02-15 13:16:48 +0100 |
commit | 351eeff3b2a666b8ead5302c1130629597438df6 (patch) | |
tree | 5b494d3e5912949980fbbbb4945da4c2eb630aa1 | |
parent | bda0839734bad8351e1dbc9c7beb8ae1f00d831e (diff) | |
download | libgit2-351eeff3b2a666b8ead5302c1130629597438df6.tar.gz |
maps: use uniform lifecycle management functions
Currently, the lifecycle functions for maps (allocation, deallocation, resize)
are not named in a uniform way and do not have a uniform function signature.
Rename the functions to fix that, and stick to libgit2's naming scheme of saying
`git_foo_new`. This results in the following new interface for allocation:
- `int git_<t>map_new(git_<t>map **out)` to allocate a new map, returning an
error code if we ran out of memory
- `void git_<t>map_free(git_<t>map *map)` to free a map
- `void git_<t>map_clear(git<t>map *map)` to remove all entries from a map
This commit also fixes all existing callers.
-rw-r--r-- | src/apply.c | 2 | ||||
-rw-r--r-- | src/attr.c | 2 | ||||
-rw-r--r-- | src/attrcache.c | 4 | ||||
-rw-r--r-- | src/cache.c | 7 | ||||
-rw-r--r-- | src/checkout.c | 10 | ||||
-rw-r--r-- | src/config_entries.c | 2 | ||||
-rw-r--r-- | src/describe.c | 4 | ||||
-rw-r--r-- | src/diff_driver.c | 2 | ||||
-rw-r--r-- | src/idxmap.c | 56 | ||||
-rw-r--r-- | src/idxmap.h | 68 | ||||
-rw-r--r-- | src/index.c | 14 | ||||
-rw-r--r-- | src/indexer.c | 9 | ||||
-rw-r--r-- | src/merge.c | 4 | ||||
-rw-r--r-- | src/mwindow.c | 2 | ||||
-rw-r--r-- | src/odb_mempack.c | 3 | ||||
-rw-r--r-- | src/offmap.c | 8 | ||||
-rw-r--r-- | src/offmap.h | 29 | ||||
-rw-r--r-- | src/oidmap.c | 7 | ||||
-rw-r--r-- | src/oidmap.h | 29 | ||||
-rw-r--r-- | src/pack-objects.c | 6 | ||||
-rw-r--r-- | src/pack.c | 4 | ||||
-rw-r--r-- | src/pack.h | 4 | ||||
-rw-r--r-- | src/repository.c | 2 | ||||
-rw-r--r-- | src/revwalk.c | 4 | ||||
-rw-r--r-- | src/sortedcache.c | 2 | ||||
-rw-r--r-- | src/strmap.c | 8 | ||||
-rw-r--r-- | src/strmap.h | 29 | ||||
-rw-r--r-- | src/submodule.c | 4 | ||||
-rw-r--r-- | src/transaction.c | 2 | ||||
-rw-r--r-- | src/tree.c | 2 | ||||
-rw-r--r-- | tests/core/oidmap.c | 6 | ||||
-rw-r--r-- | tests/core/strmap.c | 2 |
32 files changed, 237 insertions, 100 deletions
diff --git a/src/apply.c b/src/apply.c index d72aa8374..e47b6d492 100644 --- a/src/apply.c +++ b/src/apply.c @@ -577,7 +577,7 @@ static int apply_deltas( size_t i; int error = 0; - if (git_strmap_alloc(&removed_paths) < 0) + if (git_strmap_new(&removed_paths) < 0) return -1; for (i = 0; i < git_diff_num_deltas(diff); i++) { diff --git a/src/attr.c b/src/attr.c index 1f2643345..21ef1ba24 100644 --- a/src/attr.c +++ b/src/attr.c @@ -215,7 +215,7 @@ int git_attr_foreach( return -1; if ((error = collect_attr_files(repo, NULL, flags, pathname, &files)) < 0 || - (error = git_strmap_alloc(&seen)) < 0) + (error = git_strmap_new(&seen)) < 0) goto cleanup; git_vector_foreach(&files, i, file) { diff --git a/src/attrcache.c b/src/attrcache.c index cb8a4a4e9..6d241e4a5 100644 --- a/src/attrcache.c +++ b/src/attrcache.c @@ -400,8 +400,8 @@ int git_attr_cache__init(git_repository *repo) /* allocate hashtable for attribute and ignore file contents, * hashtable for attribute macros, and string pool */ - if ((ret = git_strmap_alloc(&cache->files)) < 0 || - (ret = git_strmap_alloc(&cache->macros)) < 0) + if ((ret = git_strmap_new(&cache->files)) < 0 || + (ret = git_strmap_new(&cache->macros)) < 0) goto cancel; git_pool_init(&cache->pool, 1); diff --git a/src/cache.c b/src/cache.c index 66107eaa9..424aaae84 100644 --- a/src/cache.c +++ b/src/cache.c @@ -65,12 +65,15 @@ void git_cache_dump_stats(git_cache *cache) int git_cache_init(git_cache *cache) { memset(cache, 0, sizeof(*cache)); - cache->map = git_oidmap_alloc(); - GIT_ERROR_CHECK_ALLOC(cache->map); + + if ((git_oidmap_new(&cache->map)) < 0) + return -1; + if (git_rwlock_init(&cache->lock)) { git_error_set(GIT_ERROR_OS, "failed to initialize cache rwlock"); return -1; } + return 0; } diff --git a/src/checkout.c b/src/checkout.c index fe020894a..35862bd0f 100644 --- a/src/checkout.c +++ b/src/checkout.c @@ -2518,11 +2518,11 @@ static int checkout_data_init( git_pool_init(&data->pool, 1); if ((error = git_vector_init(&data->removes, 0, git__strcmp_cb)) < 0 || - (error = git_vector_init(&data->remove_conflicts, 0, NULL)) < 0 || - (error = git_vector_init(&data->update_conflicts, 0, NULL)) < 0 || - (error = git_buf_puts(&data->target_path, data->opts.target_directory)) < 0 || - (error = git_path_to_dir(&data->target_path)) < 0 || - (error = git_strmap_alloc(&data->mkdir_map)) < 0) + (error = git_vector_init(&data->remove_conflicts, 0, NULL)) < 0 || + (error = git_vector_init(&data->update_conflicts, 0, NULL)) < 0 || + (error = git_buf_puts(&data->target_path, data->opts.target_directory)) < 0 || + (error = git_path_to_dir(&data->target_path)) < 0 || + (error = git_strmap_new(&data->mkdir_map)) < 0) goto cleanup; data->target_len = git_buf_len(&data->target_path); diff --git a/src/config_entries.c b/src/config_entries.c index f6277bcc7..c4551fb67 100644 --- a/src/config_entries.c +++ b/src/config_entries.c @@ -59,7 +59,7 @@ int git_config_entries_new(git_config_entries **out) GIT_ERROR_CHECK_ALLOC(entries); GIT_REFCOUNT_INC(entries); - if ((error = git_strmap_alloc(&entries->map)) < 0) + if ((error = git_strmap_new(&entries->map)) < 0) git__free(entries); else *out = entries; diff --git a/src/describe.c b/src/describe.c index 893ca646e..2cfa75f40 100644 --- a/src/describe.c +++ b/src/describe.c @@ -681,8 +681,8 @@ int git_describe_commit( "git_describe_options"); data.opts = &normalized; - data.names = git_oidmap_alloc(); - GIT_ERROR_CHECK_ALLOC(data.names); + if ((error = git_oidmap_new(&data.names)) < 0) + return error; /** TODO: contains to be implemented */ diff --git a/src/diff_driver.c b/src/diff_driver.c index 05246dfa3..9bc266e02 100644 --- a/src/diff_driver.c +++ b/src/diff_driver.c @@ -63,7 +63,7 @@ git_diff_driver_registry *git_diff_driver_registry_new(void) if (!reg) return NULL; - if (git_strmap_alloc(®->drivers) < 0) { + if (git_strmap_new(®->drivers) < 0) { git_diff_driver_registry_free(reg); return NULL; } diff --git a/src/idxmap.c b/src/idxmap.c index 3a5fc105a..8ed9849c9 100644 --- a/src/idxmap.c +++ b/src/idxmap.c @@ -32,26 +32,42 @@ static kh_inline khint_t idxentry_hash(const git_index_entry *e) __KHASH_IMPL(idx, static kh_inline, const git_index_entry *, git_index_entry *, 1, idxentry_hash, idxentry_equal) __KHASH_IMPL(idxicase, static kh_inline, const git_index_entry *, git_index_entry *, 1, idxentry_hash, idxentry_icase_equal) -int git_idxmap_alloc(git_idxmap **map) +int git_idxmap_new(git_idxmap **out) { - if ((*map = kh_init(idx)) == NULL) { - git_error_set_oom(); - return -1; - } + *out = kh_init(idx); + GIT_ERROR_CHECK_ALLOC(*out); return 0; } -int git_idxmap_icase_alloc(git_idxmap_icase **map) +int git_idxmap_icase_new(git_idxmap_icase **out) { - if ((*map = kh_init(idxicase)) == NULL) { - git_error_set_oom(); - return -1; - } + *out = kh_init(idxicase); + GIT_ERROR_CHECK_ALLOC(*out); return 0; } +void git_idxmap_free(git_idxmap *map) +{ + kh_destroy(idx, map); +} + +void git_idxmap_icase_free(git_idxmap_icase *map) +{ + kh_destroy(idxicase, map); +} + +void git_idxmap_clear(git_idxmap *map) +{ + kh_clear(idx, map); +} + +void git_idxmap_icase_clear(git_idxmap_icase *map) +{ + kh_clear(idxicase, map); +} + void git_idxmap_insert(git_idxmap *map, const git_index_entry *key, void *value, int *rval) { khiter_t idx = kh_put(idx, map, key, rval); @@ -109,26 +125,6 @@ void git_idxmap_icase_resize(git_idxmap_icase *map, size_t size) kh_resize(idxicase, map, size); } -void git_idxmap_free(git_idxmap *map) -{ - kh_destroy(idx, map); -} - -void git_idxmap_icase_free(git_idxmap_icase *map) -{ - kh_destroy(idxicase, map); -} - -void git_idxmap_clear(git_idxmap *map) -{ - kh_clear(idx, map); -} - -void git_idxmap_icase_clear(git_idxmap_icase *map) -{ - kh_clear(idxicase, map); -} - void git_idxmap_delete_at(git_idxmap *map, size_t idx) { kh_del(idx, map, idx); diff --git a/src/idxmap.h b/src/idxmap.h index 215a24521..442f5f509 100644 --- a/src/idxmap.h +++ b/src/idxmap.h @@ -11,11 +11,71 @@ #include "git2/index.h" +/** A map with `git_index_entry`s as key. */ typedef struct kh_idx_s git_idxmap; +/** A map with case-insensitive `git_index_entry`s as key */ typedef struct kh_idxicase_s git_idxmap_icase; -int git_idxmap_alloc(git_idxmap **map); -int git_idxmap_icase_alloc(git_idxmap_icase **map); +/** + * Allocate a new index entry map. + * + * @param out Pointer to the map that shall be allocated. + * @return 0 on success, an error code if allocation has failed. + */ +int git_idxmap_new(git_idxmap **out); + +/** + * Allocate a new case-insensitive index entry map. + * + * @param out Pointer to the map that shall be allocated. + * @return 0 on success, an error code if allocation has failed. + */ +int git_idxmap_icase_new(git_idxmap_icase **out); + +/** + * Free memory associated with the map. + * + * Note that this function will _not_ free values added to this + * map. + * + * @param map Pointer to the map that is to be free'd. May be + * `NULL`. + */ +void git_idxmap_free(git_idxmap *map); + +/** + * Free memory associated with the map. + * + * Note that this function will _not_ free values added to this + * map. + * + * @param map Pointer to the map that is to be free'd. May be + * `NULL`. + */ +void git_idxmap_icase_free(git_idxmap_icase *map); + +/** + * Clear all entries from the map. + * + * This function will remove all entries from the associated map. + * Memory associated with it will not be released, though. + * + * @param map Pointer to the map that shall be cleared. May be + * `NULL`. + */ +void git_idxmap_clear(git_idxmap *map); + +/** + * Clear all entries from the map. + * + * This function will remove all entries from the associated map. + * Memory associated with it will not be released, though. + * + * @param map Pointer to the map that shall be cleared. May be + * `NULL`. + */ +void git_idxmap_icase_clear(git_idxmap_icase *map); + void git_idxmap_insert(git_idxmap *map, const git_index_entry *key, void *value, int *rval); void git_idxmap_icase_insert(git_idxmap_icase *map, const git_index_entry *key, void *value, int *rval); @@ -27,10 +87,6 @@ int git_idxmap_has_data(git_idxmap *map, size_t idx); void git_idxmap_resize(git_idxmap *map, size_t size); void git_idxmap_icase_resize(git_idxmap_icase *map, size_t size); -void git_idxmap_free(git_idxmap *map); -void git_idxmap_icase_free(git_idxmap_icase *map); -void git_idxmap_clear(git_idxmap *map); -void git_idxmap_icase_clear(git_idxmap_icase *map); void git_idxmap_delete_at(git_idxmap *map, size_t idx); void git_idxmap_icase_delete_at(git_idxmap_icase *map, size_t idx); diff --git a/src/index.c b/src/index.c index 7f865c2c7..3f58eec36 100644 --- a/src/index.c +++ b/src/index.c @@ -423,10 +423,10 @@ int git_index_open(git_index **index_out, const char *index_path) } if (git_vector_init(&index->entries, 32, git_index_entry_cmp) < 0 || - git_idxmap_alloc(&index->entries_map) < 0 || - git_vector_init(&index->names, 8, conflict_name_cmp) < 0 || - git_vector_init(&index->reuc, 8, reuc_cmp) < 0 || - git_vector_init(&index->deleted, 8, git_index_entry_cmp) < 0) + git_idxmap_new(&index->entries_map) < 0 || + git_vector_init(&index->names, 8, conflict_name_cmp) < 0 || + git_vector_init(&index->reuc, 8, reuc_cmp) < 0 || + git_vector_init(&index->deleted, 8, git_index_entry_cmp) < 0) goto fail; index->entries_cmp_path = git__strcmp_cb; @@ -3106,7 +3106,7 @@ int git_index_read_tree(git_index *index, const git_tree *tree) size_t i; git_index_entry *e; - if (git_idxmap_alloc(&entries_map) < 0) + if (git_idxmap_new(&entries_map) < 0) return -1; git_vector_set_cmp(&entries, index->entries._cmp); /* match sort */ @@ -3180,8 +3180,8 @@ static int git_index_read_iterator( assert((new_iterator->flags & GIT_ITERATOR_DONT_IGNORE_CASE)); if ((error = git_vector_init(&new_entries, new_length_hint, index->entries._cmp)) < 0 || - (error = git_vector_init(&remove_entries, index->entries.length, NULL)) < 0 || - (error = git_idxmap_alloc(&new_entries_map)) < 0) + (error = git_vector_init(&remove_entries, index->entries.length, NULL)) < 0 || + (error = git_idxmap_new(&new_entries_map)) < 0) goto done; if (index->ignore_case && new_length_hint) diff --git a/src/indexer.c b/src/indexer.c index 27b763790..f898d206a 100644 --- a/src/indexer.c +++ b/src/indexer.c @@ -147,8 +147,9 @@ int git_indexer_new( git_hash_ctx_init(&idx->hash_ctx); git_hash_ctx_init(&idx->trailer); git_buf_init(&idx->entry_data, 0); - idx->expected_oids = git_oidmap_alloc(); - GIT_ERROR_CHECK_ALLOC(idx->expected_oids); + + if ((error = git_oidmap_new(&idx->expected_oids)) < 0) + goto cleanup; idx->do_verify = opts.verify; @@ -781,8 +782,8 @@ int git_indexer_append(git_indexer *idx, const void *data, size_t size, git_tran return -1; } - idx->pack->idx_cache = git_oidmap_alloc(); - GIT_ERROR_CHECK_ALLOC(idx->pack->idx_cache); + if (git_oidmap_new(&idx->pack->idx_cache) < 0) + return -1; idx->pack->has_cache = 1; if (git_vector_init(&idx->objects, total_objects, objects_cmp) < 0) diff --git a/src/merge.c b/src/merge.c index 14b76fa2e..22df380b8 100644 --- a/src/merge.c +++ b/src/merge.c @@ -1168,8 +1168,8 @@ static int merge_diff_mark_similarity_exact( git_oidmap *ours_deletes_by_oid = NULL, *theirs_deletes_by_oid = NULL; int error = 0; - if (!(ours_deletes_by_oid = git_oidmap_alloc()) || - !(theirs_deletes_by_oid = git_oidmap_alloc())) { + if (git_oidmap_new(&ours_deletes_by_oid) < 0 || + git_oidmap_new(&theirs_deletes_by_oid) < 0) { error = -1; goto done; } diff --git a/src/mwindow.c b/src/mwindow.c index ffbee7d14..07d3b15ed 100644 --- a/src/mwindow.c +++ b/src/mwindow.c @@ -44,7 +44,7 @@ int git_mwindow_global_init(void) assert(!git__pack_cache); git__on_shutdown(git_mwindow_files_free); - return git_strmap_alloc(&git__pack_cache); + return git_strmap_new(&git__pack_cache); } int git_mwindow_get_pack(struct git_pack_file **out, const char *path) diff --git a/src/odb_mempack.c b/src/odb_mempack.c index fb29dddd1..c68e5932e 100644 --- a/src/odb_mempack.c +++ b/src/odb_mempack.c @@ -171,7 +171,8 @@ int git_mempack_new(git_odb_backend **out) db = git__calloc(1, sizeof(struct memory_packer_db)); GIT_ERROR_CHECK_ALLOC(db); - db->objects = git_oidmap_alloc(); + if (git_oidmap_new(&db->objects) < 0) + return -1; db->parent.version = GIT_ODB_BACKEND_VERSION; db->parent.read = &impl__read; diff --git a/src/offmap.c b/src/offmap.c index d0fa9f031..d4388cab6 100644 --- a/src/offmap.c +++ b/src/offmap.c @@ -18,9 +18,13 @@ __KHASH_TYPE(off, git_off_t, void *) __KHASH_IMPL(off, static kh_inline, git_off_t, void *, 1, kh_int64_hash_func, kh_int64_hash_equal) -git_offmap *git_offmap_alloc(void) + +int git_offmap_new(git_offmap **out) { - return kh_init(off); + *out = kh_init(off); + GIT_ERROR_CHECK_ALLOC(*out); + + return 0; } void git_offmap_free(git_offmap *map) diff --git a/src/offmap.h b/src/offmap.h index c68809389..1280fd337 100644 --- a/src/offmap.h +++ b/src/offmap.h @@ -11,10 +11,37 @@ #include "git2/types.h" +/** A map with `git_off_t`s as key. */ typedef struct kh_off_s git_offmap; -git_offmap *git_offmap_alloc(void); +/** + * Allocate a new `git_off_t` map. + * + * @param out Pointer to the map that shall be allocated. + * @return 0 on success, an error code if allocation has failed. + */ +int git_offmap_new(git_offmap **out); + +/** + * Free memory associated with the map. + * + * Note that this function will _not_ free values added to this + * map. + * + * @param map Pointer to the map that is to be free'd. May be + * `NULL`. + */ void git_offmap_free(git_offmap *map); + +/** + * Clear all entries from the map. + * + * This function will remove all entries from the associated map. + * Memory associated with it will not be released, though. + * + * @param map Pointer to the map that shall be cleared. May be + * `NULL`. + */ void git_offmap_clear(git_offmap *map); size_t git_offmap_num_entries(git_offmap *map); diff --git a/src/oidmap.c b/src/oidmap.c index c42e5c25a..6b2180766 100644 --- a/src/oidmap.c +++ b/src/oidmap.c @@ -25,9 +25,12 @@ GIT_INLINE(khint_t) git_oidmap_hash(const git_oid *oid) __KHASH_IMPL(oid, static kh_inline, const git_oid *, void *, 1, git_oidmap_hash, git_oid_equal) -git_oidmap *git_oidmap_alloc() +int git_oidmap_new(git_oidmap **out) { - return kh_init(oid); + *out = kh_init(oid); + GIT_ERROR_CHECK_ALLOC(*out); + + return 0; } void git_oidmap_free(git_oidmap *map) diff --git a/src/oidmap.h b/src/oidmap.h index a417907bd..733357f61 100644 --- a/src/oidmap.h +++ b/src/oidmap.h @@ -11,10 +11,37 @@ #include "git2/oid.h" +/** A map with `git_oid`s as key. */ typedef struct kh_oid_s git_oidmap; -git_oidmap *git_oidmap_alloc(void); +/** + * Allocate a new OID map. + * + * @param out Pointer to the map that shall be allocated. + * @return 0 on success, an error code if allocation has failed. + */ +int git_oidmap_new(git_oidmap **out); + +/** + * Free memory associated with the map. + * + * Note that this function will _not_ free values added to this + * map. + * + * @param map Pointer to the map that is to be free'd. May be + * `NULL`. + */ void git_oidmap_free(git_oidmap *map); + +/** + * Clear all entries from the map. + * + * This function will remove all entries from the associated map. + * Memory associated with it will not be released, though. + * + * @param map Pointer to the map that shall be cleared. May be + * `NULL`. + */ void git_oidmap_clear(git_oidmap *map); size_t git_oidmap_size(git_oidmap *map); diff --git a/src/pack-objects.c b/src/pack-objects.c index e1528646a..4ccc84d4b 100644 --- a/src/pack-objects.c +++ b/src/pack-objects.c @@ -141,12 +141,10 @@ int git_packbuilder_new(git_packbuilder **out, git_repository *repo) pb = git__calloc(1, sizeof(*pb)); GIT_ERROR_CHECK_ALLOC(pb); - pb->object_ix = git_oidmap_alloc(); - if (!pb->object_ix) + if (git_oidmap_new(&pb->object_ix) < 0) goto on_error; - pb->walk_objects = git_oidmap_alloc(); - if (!pb->walk_objects) + if (git_oidmap_new(&pb->walk_objects) < 0) goto on_error; git_pool_init(&pb->object_pool, sizeof(struct walk_object)); diff --git a/src/pack.c b/src/pack.c index cdd7e9e60..88af6afd4 100644 --- a/src/pack.c +++ b/src/pack.c @@ -92,8 +92,8 @@ static void cache_free(git_pack_cache *cache) static int cache_init(git_pack_cache *cache) { - cache->entries = git_offmap_alloc(); - GIT_ERROR_CHECK_ALLOC(cache->entries); + if (git_offmap_new(&cache->entries) < 0) + return -1; cache->memory_limit = GIT_PACK_CACHE_MEMORY_LIMIT; diff --git a/src/pack.h b/src/pack.h index f21ad9fa3..483c4e8d7 100644 --- a/src/pack.h +++ b/src/pack.h @@ -17,6 +17,7 @@ #include "map.h" #include "mwindow.h" #include "odb.h" +#include "offmap.h" #include "oidmap.h" #include "array.h" @@ -71,9 +72,6 @@ struct pack_chain_elem { typedef git_array_t(struct pack_chain_elem) git_dependency_chain; -#include "offmap.h" -#include "oidmap.h" - #define GIT_PACK_CACHE_MEMORY_LIMIT 16 * 1024 * 1024 #define GIT_PACK_CACHE_SIZE_LIMIT 1024 * 1024 /* don't bother caching anything over 1MB */ diff --git a/src/repository.c b/src/repository.c index 2bfa57736..c06276df6 100644 --- a/src/repository.c +++ b/src/repository.c @@ -2916,7 +2916,7 @@ int git_repository_submodule_cache_all(git_repository *repo) assert(repo); - if ((error = git_strmap_alloc(&repo->submodule_cache))) + if ((error = git_strmap_new(&repo->submodule_cache))) return error; error = git_submodule__map(repo, repo->submodule_cache); diff --git a/src/revwalk.c b/src/revwalk.c index 1e3a5f2ff..b28c341b1 100644 --- a/src/revwalk.c +++ b/src/revwalk.c @@ -626,8 +626,8 @@ int git_revwalk_new(git_revwalk **revwalk_out, git_repository *repo) git_revwalk *walk = git__calloc(1, sizeof(git_revwalk)); GIT_ERROR_CHECK_ALLOC(walk); - walk->commits = git_oidmap_alloc(); - GIT_ERROR_CHECK_ALLOC(walk->commits); + if (git_oidmap_new(&walk->commits) < 0) + return -1; if (git_pqueue_init(&walk->iterator_time, 0, 8, git_commit_list_time_cmp) < 0) return -1; diff --git a/src/sortedcache.c b/src/sortedcache.c index 02a685e10..3967bce15 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)) { diff --git a/src/strmap.c b/src/strmap.c index 20b14acf3..98b939fea 100644 --- a/src/strmap.c +++ b/src/strmap.c @@ -18,12 +18,10 @@ __KHASH_TYPE(str, const char *, void *) __KHASH_IMPL(str, static kh_inline, const char *, void *, 1, kh_str_hash_func, kh_str_hash_equal) -int git_strmap_alloc(git_strmap **map) +int git_strmap_new(git_strmap **out) { - if ((*map = kh_init(str)) == NULL) { - git_error_set_oom(); - return -1; - } + *out = kh_init(str); + GIT_ERROR_CHECK_ALLOC(*out); return 0; } diff --git a/src/strmap.h b/src/strmap.h index 2649acbfb..5a43c6637 100644 --- a/src/strmap.h +++ b/src/strmap.h @@ -9,10 +9,37 @@ #include "common.h" +/** A map with C strings as key. */ typedef struct kh_str_s git_strmap; -int git_strmap_alloc(git_strmap **map); +/** + * Allocate a new string map. + * + * @param out Pointer to the map that shall be allocated. + * @return 0 on success, an error code if allocation has failed. + */ +int git_strmap_new(git_strmap **out); + +/** + * Free memory associated with the map. + * + * Note that this function will _not_ free keys or values added + * to this map. + * + * @param map Pointer to the map that is to be free'd. May be + * `NULL`. + */ void git_strmap_free(git_strmap *map); + +/** + * Clear all entries from the map. + * + * This function will remove all entries from the associated map. + * Memory associated with it will not be released, though. + * + * @param map Pointer to the map that shall be cleared. May be + * `NULL`. + */ void git_strmap_clear(git_strmap *map); size_t git_strmap_num_entries(git_strmap *map); diff --git a/src/submodule.c b/src/submodule.c index e3ec88554..e11b544a2 100644 --- a/src/submodule.c +++ b/src/submodule.c @@ -202,7 +202,7 @@ static int load_submodule_names(git_strmap **out, git_repository *repo, git_conf *out = NULL; - if ((error = git_strmap_alloc(&names)) < 0) + if ((error = git_strmap_new(&names)) < 0) goto out; if ((error = git_config_iterator_glob_new(&iter, cfg, key)) < 0) @@ -618,7 +618,7 @@ int git_submodule_foreach( return -1; } - if ((error = git_strmap_alloc(&submodules)) < 0) + if ((error = git_strmap_new(&submodules)) < 0) return error; if ((error = git_submodule__map(repo, submodules)) < 0) diff --git a/src/transaction.c b/src/transaction.c index d8e38d803..a09b2eb46 100644 --- a/src/transaction.c +++ b/src/transaction.c @@ -84,7 +84,7 @@ int git_transaction_new(git_transaction **out, git_repository *repo) goto on_error; } - if ((error = git_strmap_alloc(&tx->locks)) < 0) { + if ((error = git_strmap_new(&tx->locks)) < 0) { error = -1; goto on_error; } diff --git a/src/tree.c b/src/tree.c index 466180451..9134451c6 100644 --- a/src/tree.c +++ b/src/tree.c @@ -688,7 +688,7 @@ int git_treebuilder_new( bld->repo = repo; - if (git_strmap_alloc(&bld->map) < 0) { + if (git_strmap_new(&bld->map) < 0) { git__free(bld); return -1; } diff --git a/tests/core/oidmap.c b/tests/core/oidmap.c index b5f6f99e1..519b5875e 100644 --- a/tests/core/oidmap.c +++ b/tests/core/oidmap.c @@ -24,8 +24,7 @@ void test_core_oidmap__basic(void) } } - map = git_oidmap_alloc(); - cl_assert(map != NULL); + cl_git_pass(git_oidmap_new(&map)); for (i = 0; i < NITEMS; ++i) { size_t pos; @@ -78,8 +77,7 @@ void test_core_oidmap__hash_collision(void) items[i].oid.id[11] = (unsigned char)(i >> 24); } - map = git_oidmap_alloc(); - cl_assert(map != NULL); + cl_git_pass(git_oidmap_new(&map)); for (i = 0; i < NITEMS; ++i) { size_t pos; diff --git a/tests/core/strmap.c b/tests/core/strmap.c index 64f516452..e19ceaedf 100644 --- a/tests/core/strmap.c +++ b/tests/core/strmap.c @@ -5,7 +5,7 @@ git_strmap *g_table; void test_core_strmap__initialize(void) { - cl_git_pass(git_strmap_alloc(&g_table)); + cl_git_pass(git_strmap_new(&g_table)); cl_assert(g_table != NULL); } |