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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
diff --git a/src/apply.c b/src/apply.c
index d72aa83..e47b6d4 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 1f26433..21ef1ba 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 cb8a4a4..6d241e4 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 66107ea..424aaae 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 fe02089..35862bd 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 f6277bc..c4551fb 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 893ca64..2cfa75f 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 05246df..9bc266e 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 3a5fc10..8ed9849 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 215a245..442f5f5 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 7f865c2..3f58eec 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 27b7637..f898d20 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 14b76fa..22df380 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 ffbee7d..07d3b15 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 fb29ddd..c68e593 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 d0fa9f0..d4388ca 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 c688093..1280fd3 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 c42e5c2..6b21807 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 a417907..733357f 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 e152864..4ccc84d 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 cdd7e9e..88af6af 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 f21ad9f..483c4e8 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 2bfa577..c06276d 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 1e3a5f2..b28c341 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 02a685e..3967bce 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 20b14ac..98b939f 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 2649acb..5a43c66 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 e3ec885..e11b544 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 d8e38d8..a09b2eb 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 4661804..9134451 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 b5f6f99..519b587 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 64f5164..e19ceae 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);
}