Merge pull request #1413 from arrbee/more-iterator-refactor Further tree_iterator refactoring
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 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
diff --git a/src/checkout.c b/src/checkout.c
index 68ebbe3..e52649a 100644
--- a/src/checkout.c
+++ b/src/checkout.c
@@ -610,7 +610,7 @@ static int checkout_get_actions(
if (act & CHECKOUT_ACTION__CONFLICT)
counts[CHECKOUT_ACTION__CONFLICT]++;
}
-
+
error = checkout_remaining_wd_items(data, workdir, wditem, &pathspec);
if (error < 0)
goto fail;
diff --git a/src/diff_output.c b/src/diff_output.c
index 43262b1..c0aff68 100644
--- a/src/diff_output.c
+++ b/src/diff_output.c
@@ -330,6 +330,33 @@ static int get_workdir_sm_content(
return 0;
}
+static int get_filtered(
+ git_map *map, git_file fd, git_diff_file *file, git_vector *filters)
+{
+ int error;
+ git_buf raw = GIT_BUF_INIT, filtered = GIT_BUF_INIT;
+
+ if ((error = git_futils_readbuffer_fd(&raw, fd, (size_t)file->size)) < 0)
+ return error;
+
+ if (!filters->length)
+ git_buf_swap(&filtered, &raw);
+ else
+ error = git_filters_apply(&filtered, &raw, filters);
+
+ if (!error) {
+ map->len = git_buf_len(&filtered);
+ map->data = git_buf_detach(&filtered);
+
+ file->flags |= GIT_DIFF_FLAG__FREE_DATA;
+ }
+
+ git_buf_free(&raw);
+ git_buf_free(&filtered);
+
+ return error;
+}
+
static int get_workdir_content(
diff_context *ctxt,
git_diff_delta *delta,
@@ -381,8 +408,8 @@ static int get_workdir_content(
goto cleanup;
}
- if (!file->size)
- file->size = git_futils_filesize(fd);
+ if (!file->size && !(file->size = git_futils_filesize(fd)))
+ goto close_and_cleanup;
if ((error = diff_delta_is_binary_by_size(ctxt, delta, file)) < 0 ||
(delta->flags & GIT_DIFF_FLAG_BINARY) != 0)
@@ -394,26 +421,12 @@ static int get_workdir_content(
goto close_and_cleanup;
if (error == 0) { /* note: git_filters_load returns filter count */
- if (!file->size)
- goto close_and_cleanup;
-
error = git_futils_mmap_ro(map, fd, 0, (size_t)file->size);
- file->flags |= GIT_DIFF_FLAG__UNMAP_DATA;
- } else {
- git_buf raw = GIT_BUF_INIT, filtered = GIT_BUF_INIT;
-
- if (!(error = git_futils_readbuffer_fd(&raw, fd, (size_t)file->size)) &&
- !(error = git_filters_apply(&filtered, &raw, &filters)))
- {
- map->len = git_buf_len(&filtered);
- map->data = git_buf_detach(&filtered);
-
- file->flags |= GIT_DIFF_FLAG__FREE_DATA;
- }
-
- git_buf_free(&raw);
- git_buf_free(&filtered);
+ if (!error)
+ file->flags |= GIT_DIFF_FLAG__UNMAP_DATA;
}
+ if (error != 0)
+ error = get_filtered(map, fd, file, &filters);
close_and_cleanup:
git_filters_free(&filters);
diff --git a/src/errors.c b/src/errors.c
index c5f0b3b..e2629f6 100644
--- a/src/errors.c
+++ b/src/errors.c
@@ -103,6 +103,7 @@ int giterr_set_regex(const regex_t *regex, int error_code)
void giterr_clear(void)
{
+ set_error(0, NULL);
GIT_GLOBAL->last_error = NULL;
errno = 0;
diff --git a/src/iterator.c b/src/iterator.c
index 1ac6a49..b15bced 100644
--- a/src/iterator.c
+++ b/src/iterator.c
@@ -148,8 +148,7 @@ int git_iterator_for_nothing(
const char *start,
const char *end)
{
- empty_iterator *i = git__calloc(1, sizeof(empty_iterator));
- GITERR_CHECK_ALLOC(i);
+ empty_iterator *i;
#define empty_iterator__current empty_iterator__noop
#define empty_iterator__advance empty_iterator__noop
@@ -165,15 +164,16 @@ int git_iterator_for_nothing(
}
-typedef struct {
- size_t parent_entry_index; /* index in parent entries array */
- size_t parent_tree_index; /* index in parent entry tree */
- git_tree *tree; /* this tree if this is tree (only valid while current) */
-} tree_iterator_entry;
+typedef struct tree_iterator_entry tree_iterator_entry;
+struct tree_iterator_entry {
+ tree_iterator_entry *parent;
+ const git_tree_entry *te;
+ git_tree *tree;
+};
typedef struct tree_iterator_frame tree_iterator_frame;
struct tree_iterator_frame {
- tree_iterator_frame *parent, *child;
+ tree_iterator_frame *up, *down;
size_t n_entries; /* items in this frame */
size_t current; /* start of currently active range in frame */
@@ -182,13 +182,14 @@ struct tree_iterator_frame {
const char *start;
size_t startlen;
- tree_iterator_entry entries[GIT_FLEX_ARRAY];
+ tree_iterator_entry *entries[GIT_FLEX_ARRAY];
};
typedef struct {
git_iterator base;
git_iterator_callbacks cb;
- tree_iterator_frame *head, *top;
+ tree_iterator_frame *head, *root;
+ git_pool pool;
git_index_entry entry;
git_buf path;
int path_ambiguities;
@@ -196,30 +197,6 @@ typedef struct {
int (*strncomp)(const char *a, const char *b, size_t sz);
} tree_iterator;
-static const git_tree_entry *tree_iterator__tree_entry(
- tree_iterator_frame *tf, const tree_iterator_entry *entry)
-{
- git_tree *tree = tf->parent->entries[entry->parent_entry_index].tree;
- if (!tree)
- return NULL;
- return git_tree_entry_byindex(tree, entry->parent_tree_index);
-}
-
-static const git_tree_entry *tree_iterator__tree_entry_by_index(
- tree_iterator_frame *tf, size_t i)
-{
- git_tree *tree;
-
- if (i >= tf->n_entries)
- return NULL;
-
- tree = tf->parent->entries[tf->entries[i].parent_entry_index].tree;
- if (!tree)
- return NULL;
-
- return git_tree_entry_byindex(tree, tf->entries[i].parent_tree_index);
-}
-
static char *tree_iterator__current_filename(
tree_iterator *ti, const git_tree_entry *te)
{
@@ -238,90 +215,76 @@ static char *tree_iterator__current_filename(
static void tree_iterator__rewrite_filename(tree_iterator *ti)
{
- tree_iterator_frame *scan = ti->head;
- size_t current = scan->current;
+ tree_iterator_entry *scan = ti->head->entries[ti->head->current];
ssize_t strpos = ti->path.size;
const git_tree_entry *te;
if (strpos && ti->path.ptr[strpos - 1] == '/')
strpos--;
- while (scan && scan->parent) {
- tree_iterator_entry *entry = &scan->entries[current];
-
- if (!(te = tree_iterator__tree_entry(scan, entry)))
- break;
-
+ for (; scan && (te = scan->te); scan = scan->parent) {
strpos -= te->filename_len;
memcpy(&ti->path.ptr[strpos], te->filename, te->filename_len);
strpos -= 1; /* separator */
-
- current = entry->parent_entry_index;
- scan = scan->parent;
}
}
-static int tree_iterator__tree_entry_cmp(
+static int tree_iterator__te_cmp(
const git_tree_entry *a,
const git_tree_entry *b,
- int (*strncomp)(const char *, const char *, size_t))
+ int (*compare)(const char *, const char *, size_t))
{
- size_t common = min(a->filename_len, b->filename_len);
- int cmp = strncomp(a->filename, b->filename, common);
-
- if (!cmp) {
- char a_next = a->filename[common], b_next = b->filename[common];
-
- if (!a_next && a->attr == GIT_FILEMODE_TREE)
- a_next = '/';
- if (!b_next && b->attr == GIT_FILEMODE_TREE)
- b_next = '/';
-
- cmp = (int)a_next - (int)b_next;
- }
-
- return cmp;
+ return git_path_cmp(
+ a->filename, a->filename_len, a->attr == GIT_FILEMODE_TREE,
+ b->filename, b->filename_len, b->attr == GIT_FILEMODE_TREE,
+ compare);
}
-static int tree_iterator__entry_cmp(const void *a, const void *b, void *p)
+static int tree_iterator__ci_cmp(const void *a, const void *b, void *p)
{
const tree_iterator_entry *ae = a, *be = b;
- const git_tree_entry *ate = tree_iterator__tree_entry(p, ae);
- const git_tree_entry *bte = tree_iterator__tree_entry(p, be);
- int cmp = tree_iterator__tree_entry_cmp(ate, bte, git__strncasecmp);
+ int cmp = tree_iterator__te_cmp(ae->te, be->te, git__strncasecmp);
- /* stabilize sort order among equivalent names */
if (!cmp) {
- cmp = (ae->parent_entry_index < be->parent_entry_index) ? -1 :
- (ae->parent_entry_index > be->parent_entry_index) ? 1 : 0;
- if (!cmp)
- cmp = (ae->parent_tree_index < be->parent_tree_index) ? -1 :
- (ae->parent_tree_index > be->parent_tree_index) ? 1 : 0;
+ /* stabilize sort order among equivalent names */
+ if (!ae->parent->te || !be->parent->te)
+ cmp = tree_iterator__te_cmp(ae->te, be->te, git__strncmp);
+ else
+ cmp = tree_iterator__ci_cmp(ae->parent, be->parent, p);
}
return cmp;
}
+static int tree_iterator__search_cmp(const void *key, const void *val, void *p)
+{
+ const tree_iterator_frame *tf = key;
+ const git_tree_entry *te = ((tree_iterator_entry *)val)->te;
+
+ return git_path_cmp(
+ tf->start, tf->startlen, false,
+ te->filename, te->filename_len, te->attr == GIT_FILEMODE_TREE,
+ ((tree_iterator *)p)->strncomp);
+}
+
static int tree_iterator__set_next(tree_iterator *ti, tree_iterator_frame *tf)
{
- /* find next and load trees for current range */
- int error = 0;
+ int error;
const git_tree_entry *te, *last = NULL;
tf->next = tf->current;
- while (tf->next < tf->n_entries) {
- if (!(te = tree_iterator__tree_entry_by_index(tf, tf->next)) ||
- (last && tree_iterator__tree_entry_cmp(last, te, ti->strncomp)))
+ for (; tf->next < tf->n_entries; tf->next++, last = te) {
+ te = tf->entries[tf->next]->te;
+
+ if (last && tree_iterator__te_cmp(last, te, ti->strncomp))
break;
+ /* load trees for items in [current,next) range */
if (git_tree_entry__is_tree(te) &&
(error = git_tree_lookup(
- &tf->entries[tf->next].tree, ti->base.repo, &te->oid)) < 0)
- break;
-
- tf->next++;
- last = te;
+ &tf->entries[tf->next]->tree, ti->base.repo, &te->oid)) < 0)
+ return error;
}
if (tf->next > tf->current + 1)
@@ -330,129 +293,139 @@ static int tree_iterator__set_next(tree_iterator *ti, tree_iterator_frame *tf)
if (last && !tree_iterator__current_filename(ti, last))
return -1;
- return error;
+ return 0;
}
GIT_INLINE(bool) tree_iterator__at_tree(tree_iterator *ti)
{
return (ti->head->current < ti->head->n_entries &&
- ti->head->entries[ti->head->current].tree != NULL);
+ ti->head->entries[ti->head->current]->tree != NULL);
}
static int tree_iterator__push_frame(tree_iterator *ti)
{
int error = 0;
- tree_iterator_frame *tf = ti->head, *new_tf = NULL;
- size_t i, n_entries = 0, sz = sizeof(tree_iterator_frame);
- const git_tree_entry *te;
+ tree_iterator_frame *head = ti->head, *tf = NULL;
+ size_t i, n_entries = 0;
- /* if current item in head is not a tree, do nothing */
- if (tf->current >= tf->n_entries || !tf->entries[tf->current].tree)
+ if (head->current >= head->n_entries || !head->entries[head->current]->tree)
return 0;
- /* build frame - sum tree entries from parent range */
- for (i = tf->current; i < tf->next; ++i)
- n_entries += git_tree_entrycount(tf->entries[i].tree);
- sz += n_entries * sizeof(tree_iterator_entry);
- new_tf = git__calloc(sz, sizeof(char));
- GITERR_CHECK_ALLOC(new_tf);
+ for (i = head->current; i < head->next; ++i)
+ n_entries += git_tree_entrycount(head->entries[i]->tree);
+
+ tf = git__calloc(sizeof(tree_iterator_frame) +
+ n_entries * sizeof(tree_iterator_entry *), 1);
+ GITERR_CHECK_ALLOC(tf);
+
+ tf->n_entries = n_entries;
- /* populate frame and entries */
- new_tf->parent = tf;
- new_tf->n_entries = n_entries;
+ tf->up = head;
+ head->down = tf;
+ ti->head = tf;
- for (i = tf->current, n_entries = 0; i < tf->next; ++i) {
- git_tree *tree = tf->entries[i].tree;
+ for (i = head->current, n_entries = 0; i < head->next; ++i) {
+ git_tree *tree = head->entries[i]->tree;
size_t j, max_j = git_tree_entrycount(tree);
for (j = 0; j < max_j; ++j) {
- new_tf->entries[n_entries].parent_entry_index = i;
- new_tf->entries[n_entries].parent_tree_index = j;
- n_entries++;
+ tree_iterator_entry *entry = git_pool_malloc(&ti->pool, 1);
+ GITERR_CHECK_ALLOC(entry);
+
+ entry->parent = head->entries[i];
+ entry->te = git_tree_entry_byindex(tree, j);
+ entry->tree = NULL;
+
+ tf->entries[n_entries++] = entry;
}
}
/* if ignore_case, sort entries case insensitively */
if (iterator__ignore_case(ti))
- git__qsort_r(
- new_tf->entries, new_tf->n_entries, sizeof(tree_iterator_entry),
- tree_iterator__entry_cmp, new_tf);
-
- /* pick new_tf->current based on "start" (or start at zero) */
- if (tf->startlen > 0) {
- /* find first item >= start */
- for (i = 0; i < new_tf->n_entries; ++i) {
- if (!(te = tree_iterator__tree_entry_by_index(new_tf, i)))
- break;
- sz = min(tf->startlen, te->filename_len);
- if (ti->strncomp(tf->start, te->filename, sz) <= 0 &&
- (tf->startlen <= te->filename_len ||
- tf->start[te->filename_len] == '/'))
- break;
- }
- new_tf->current = i;
+ git__tsort_r(
+ (void **)tf->entries, tf->n_entries, tree_iterator__ci_cmp, tf);
+
+ /* pick tf->current based on "start" (or start at zero) */
+ if (head->startlen > 0) {
+ git__bsearch_r((void **)tf->entries, tf->n_entries, head,
+ tree_iterator__search_cmp, ti, &tf->current);
+
+ while (tf->current &&
+ !tree_iterator__search_cmp(head, tf->entries[tf->current-1], ti))
+ tf->current--;
- if ((new_tf->start = strchr(tf->start, '/')) != NULL) {
- new_tf->start++;
- new_tf->startlen = strlen(new_tf->start);
+ if ((tf->start = strchr(head->start, '/')) != NULL) {
+ tf->start++;
+ tf->startlen = strlen(tf->start);
}
}
ti->path_has_filename = false;
- /* find next and load trees for current range */
- if ((error = tree_iterator__set_next(ti, new_tf)) < 0)
+ if ((error = tree_iterator__set_next(ti, tf)) < 0)
return error;
- tf->child = new_tf;
- ti->head = new_tf;
-
+ /* autoexpand as needed */
if (!iterator__include_trees(ti) && tree_iterator__at_tree(ti))
return tree_iterator__push_frame(ti);
return 0;
}
-GIT_INLINE(void) tree_iterator__free_tree(tree_iterator_entry *entry)
-{
- if (entry->tree) {
- git_tree_free(entry->tree);
- entry->tree = NULL;
- }
-}
-
static bool tree_iterator__move_to_next(
tree_iterator *ti, tree_iterator_frame *tf)
{
if (tf->next > tf->current + 1)
ti->path_ambiguities--;
+ if (!tf->up) { /* at root */
+ tf->current = tf->next;
+ return false;
+ }
+
for (; tf->current < tf->next; tf->current++) {
- if (tf->parent)
- tree_iterator__free_tree(&tf->entries[tf->current]);
+ git_tree_free(tf->entries[tf->current]->tree);
+ tf->entries[tf->current]->tree = NULL;
}
return (tf->current < tf->n_entries);
}
-static bool tree_iterator__pop_frame(tree_iterator *ti)
+static bool tree_iterator__pop_frame(tree_iterator *ti, bool final)
{
tree_iterator_frame *tf = ti->head;
- if (!tf->parent)
+ if (!tf->up)
return false;
+ ti->head = tf->up;
+ ti->head->down = NULL;
+
tree_iterator__move_to_next(ti, tf);
- ti->head = tf->parent;
- ti->head->child = NULL;
- git__free(tf);
+ if (!final) { /* if final, don't bother to clean up */
+ git_pool_free_array(&ti->pool, tf->n_entries, (void **)tf->entries);
+ git_buf_rtruncate_at_char(&ti->path, '/');
+ }
- git_buf_rtruncate_at_char(&ti->path, '/');
+ git__free(tf);
return true;
}
+static int tree_iterator__pop_all(tree_iterator *ti, bool to_end, bool final)
+{
+ while (tree_iterator__pop_frame(ti, final)) /* pop to root */;
+
+ if (!final) {
+ ti->head->current = to_end ? ti->head->n_entries : 0;
+ ti->path_ambiguities = 0;
+ git_buf_clear(&ti->path);
+ }
+
+ return 0;
+}
+
static int tree_iterator__current(
const git_index_entry **entry, git_iterator *self)
{
@@ -462,24 +435,21 @@ static int tree_iterator__current(
iterator__clear_entry(entry);
- if (!(te = tree_iterator__tree_entry_by_index(tf, tf->current)))
+ if (tf->current >= tf->n_entries)
return 0;
+ te = tf->entries[tf->current]->te;
ti->entry.mode = te->attr;
git_oid_cpy(&ti->entry.oid, &te->oid);
ti->entry.path = tree_iterator__current_filename(ti, te);
- if (ti->entry.path == NULL)
- return -1;
+ GITERR_CHECK_ALLOC(ti->entry.path);
if (ti->path_ambiguities > 0)
tree_iterator__rewrite_filename(ti);
- if (iterator__past_end(ti, ti->entry.path)) {
- while (tree_iterator__pop_frame(ti)) /* pop to top */;
- ti->head->current = ti->head->n_entries;
- return 0;
- }
+ if (iterator__past_end(ti, ti->entry.path))
+ return tree_iterator__pop_all(ti, true, false);
if (entry)
*entry = &ti->entry;
@@ -524,7 +494,8 @@ static int tree_iterator__advance(
}
/* scan forward and up, advancing in frame or popping frame when done */
- while (!tree_iterator__move_to_next(ti, tf) && tree_iterator__pop_frame(ti))
+ while (!tree_iterator__move_to_next(ti, tf) &&
+ tree_iterator__pop_frame(ti, false))
tf = ti->head;
/* find next and load trees */
@@ -549,15 +520,12 @@ static int tree_iterator__reset(
{
tree_iterator *ti = (tree_iterator *)self;
- while (tree_iterator__pop_frame(ti)) /* pop to top */;
- ti->top->current = 0;
+ tree_iterator__pop_all(ti, false, false);
if (iterator__reset_range(self, start, end) < 0)
return -1;
- git_buf_clear(&ti->path);
- ti->path_ambiguities = 0;
- return tree_iterator__push_frame(ti); /* re-expand top tree */
+ return tree_iterator__push_frame(ti); /* re-expand root tree */
}
static int tree_iterator__at_end(git_iterator *self)
@@ -570,30 +538,29 @@ static void tree_iterator__free(git_iterator *self)
{
tree_iterator *ti = (tree_iterator *)self;
- while (tree_iterator__pop_frame(ti)) /* pop to top */;
-
- if (ti->head) {
- tree_iterator__free_tree(&ti->head->entries[0]);
- git__free(ti->head);
- }
- ti->head = ti->top = NULL;
+ tree_iterator__pop_all(ti, true, false);
+ git_tree_free(ti->head->entries[0]->tree);
+ git__free(ti->head);
+ git_pool_clear(&ti->pool);
git_buf_free(&ti->path);
}
-static int tree_iterator__create_top_frame(tree_iterator *ti, git_tree *tree)
+static int tree_iterator__create_root_frame(tree_iterator *ti, git_tree *tree)
{
size_t sz = sizeof(tree_iterator_frame) + sizeof(tree_iterator_entry);
- tree_iterator_frame *top = git__calloc(sz, sizeof(char));
- GITERR_CHECK_ALLOC(top);
+ tree_iterator_frame *root = git__calloc(sz, sizeof(char));
+ GITERR_CHECK_ALLOC(root);
- top->n_entries = 1;
- top->next = 1;
- top->start = ti->base.start;
- top->startlen = top->start ? strlen(top->start) : 0;
- top->entries[0].tree = tree;
+ root->n_entries = 1;
+ root->next = 1;
+ root->start = ti->base.start;
+ root->startlen = root->start ? strlen(root->start) : 0;
+ root->entries[0] = git_pool_mallocz(&ti->pool, 1);
+ GITERR_CHECK_ALLOC(root->entries[0]);
+ root->entries[0]->tree = tree;
- ti->head = ti->top = top;
+ ti->head = ti->root = root;
return 0;
}
@@ -620,8 +587,9 @@ int git_iterator_for_tree(
goto fail;
ti->strncomp = iterator__ignore_case(ti) ? git__strncasecmp : git__strncmp;
- if ((error = tree_iterator__create_top_frame(ti, tree)) < 0 ||
- (error = tree_iterator__push_frame(ti)) < 0) /* expand top right now */
+ if ((error = git_pool_init(&ti->pool, sizeof(tree_iterator_entry),0)) < 0 ||
+ (error = tree_iterator__create_root_frame(ti, tree)) < 0 ||
+ (error = tree_iterator__push_frame(ti)) < 0) /* expand root now */
goto fail;
*iter = (git_iterator *)ti;
@@ -878,7 +846,6 @@ typedef struct {
git_iterator base;
git_iterator_callbacks cb;
workdir_iterator_frame *stack;
- int (*entrycmp)(const void *pfx, const void *item);
git_ignores ignores;
git_index_entry entry;
git_buf path;
@@ -940,16 +907,11 @@ static void workdir_iterator__free_frame(workdir_iterator_frame *wf)
static int workdir_iterator__update_entry(workdir_iterator *wi);
-static int workdir_iterator__entry_cmp_case(const void *pfx, const void *item)
-{
- const git_path_with_stat *ps = item;
- return git__prefixcmp((const char *)pfx, ps->path);
-}
-
-static int workdir_iterator__entry_cmp_icase(const void *pfx, const void *item)
+static int workdir_iterator__entry_cmp(const void *i, const void *item)
{
+ const workdir_iterator *wi = (const workdir_iterator *)i;
const git_path_with_stat *ps = item;
- return git__prefixcmp_icase((const char *)pfx, ps->path);
+ return wi->base.prefixcomp(wi->base.start, ps->path);
}
static void workdir_iterator__seek_frame_start(
@@ -960,7 +922,7 @@ static void workdir_iterator__seek_frame_start(
if (wi->base.start)
git_vector_bsearch2(
- &wf->index, &wf->entries, wi->entrycmp, wi->base.start);
+ &wf->index, &wf->entries, workdir_iterator__entry_cmp, wi);
else
wf->index = 0;
@@ -1236,10 +1198,7 @@ int git_iterator_for_workdir(
git__free(wi);
return -1;
}
-
wi->root_len = wi->path.size;
- wi->entrycmp = iterator__ignore_case(wi) ?
- workdir_iterator__entry_cmp_icase : workdir_iterator__entry_cmp_case;
if ((error = workdir_iterator__expand_dir(wi)) < 0) {
if (error != GIT_ENOTFOUND)
@@ -1306,7 +1265,8 @@ int git_iterator_current_tree_entry(
*tree_entry = NULL;
else {
tree_iterator_frame *tf = ((tree_iterator *)iter)->head;
- *tree_entry = tree_iterator__tree_entry_by_index(tf, tf->current);
+ *tree_entry = (tf->current < tf->n_entries) ?
+ tf->entries[tf->current]->te : NULL;
}
return 0;
@@ -1327,12 +1287,10 @@ int git_iterator_current_parent_tree(
if (iter->type != GIT_ITERATOR_TYPE_TREE)
return 0;
- tf = ti->top;
-
- while (*scan) {
- /* get entry of this parent that child is currently on */
- if (!(tf = tf->child) ||
- !(te = tree_iterator__tree_entry_by_index(tf, tf->current)) ||
+ for (tf = ti->root; *scan; ) {
+ if (!(tf = tf->down) ||
+ tf->current >= tf->n_entries ||
+ !(te = tf->entries[tf->current]->te) ||
ti->strncomp(scan, te->filename, te->filename_len) != 0)
return 0;
@@ -1341,7 +1299,7 @@ int git_iterator_current_parent_tree(
scan++;
}
- *tree_ptr = tf->entries[tf->current].tree;
+ *tree_ptr = tf->entries[tf->current]->tree;
return 0;
}
diff --git a/src/path.c b/src/path.c
index 263cf9e..5767fae 100644
--- a/src/path.c
+++ b/src/path.c
@@ -679,37 +679,14 @@ int git_path_apply_relative(git_buf *target, const char *relpath)
int git_path_cmp(
const char *name1, size_t len1, int isdir1,
- const char *name2, size_t len2, int isdir2)
+ const char *name2, size_t len2, int isdir2,
+ int (*compare)(const char *, const char *, size_t))
{
unsigned char c1, c2;
size_t len = len1 < len2 ? len1 : len2;
int cmp;
- cmp = memcmp(name1, name2, len);
- if (cmp)
- return cmp;
-
- c1 = name1[len];
- c2 = name2[len];
-
- if (c1 == '\0' && isdir1)
- c1 = '/';
-
- if (c2 == '\0' && isdir2)
- c2 = '/';
-
- return (c1 < c2) ? -1 : (c1 > c2) ? 1 : 0;
-}
-
-int git_path_icmp(
- const char *name1, size_t len1, int isdir1,
- const char *name2, size_t len2, int isdir2)
-{
- unsigned char c1, c2;
- size_t len = len1 < len2 ? len1 : len2;
- int cmp;
-
- cmp = strncasecmp(name1, name2, len);
+ cmp = compare(name1, name2, len);
if (cmp)
return cmp;
diff --git a/src/path.h b/src/path.h
index feefd65..ead4fa3 100644
--- a/src/path.h
+++ b/src/path.h
@@ -265,12 +265,8 @@ extern int git_path_direach(
*/
extern int git_path_cmp(
const char *name1, size_t len1, int isdir1,
- const char *name2, size_t len2, int isdir2);
-
-/** Path sort function that is case insensitive */
-extern int git_path_icmp(
- const char *name1, size_t len1, int isdir1,
- const char *name2, size_t len2, int isdir2);
+ const char *name2, size_t len2, int isdir2,
+ int (*compare)(const char *, const char *, size_t));
/**
* Invoke callback up path directory by directory until the ceiling is
diff --git a/src/pool.c b/src/pool.c
index 64b5c6b..33ea74f 100644
--- a/src/pool.c
+++ b/src/pool.c
@@ -10,6 +10,11 @@ struct git_pool_page {
char data[GIT_FLEX_ARRAY];
};
+typedef struct git_pool_freelist_item git_pool_freelist_item;
+struct git_pool_freelist_item {
+ git_pool_freelist_item *next;
+};
+
#define GIT_POOL_MIN_USABLE 4
#define GIT_POOL_MIN_PAGESZ 2 * sizeof(void*)
@@ -150,7 +155,7 @@ void *git_pool_malloc(git_pool *pool, uint32_t items)
pool->has_multi_item_alloc = 1;
else if (pool->free_list != NULL) {
ptr = pool->free_list;
- pool->free_list = *((void **)pool->free_list);
+ pool->free_list = ((git_pool_freelist_item *)pool->free_list)->next;
return ptr;
}
@@ -235,10 +240,31 @@ char *git_pool_strcat(git_pool *pool, const char *a, const char *b)
void git_pool_free(git_pool *pool, void *ptr)
{
- assert(pool && ptr && pool->item_size >= sizeof(void*));
+ git_pool_freelist_item *item = ptr;
+
+ assert(pool && pool->item_size >= sizeof(void*));
+
+ if (item) {
+ item->next = pool->free_list;
+ pool->free_list = item;
+ }
+}
+
+void git_pool_free_array(git_pool *pool, size_t count, void **ptrs)
+{
+ git_pool_freelist_item **items = (git_pool_freelist_item **)ptrs;
+ size_t i;
+
+ assert(pool && ptrs && pool->item_size >= sizeof(void*));
+
+ if (!count)
+ return;
+
+ for (i = count - 1; i > 0; --i)
+ items[i]->next = items[i - 1];
- *((void **)ptr) = pool->free_list;
- pool->free_list = ptr;
+ items[i]->next = pool->free_list;
+ pool->free_list = items[count - 1];
}
uint32_t git_pool__open_pages(git_pool *pool)
diff --git a/src/pool.h b/src/pool.h
index 2b262a5..5ac9b76 100644
--- a/src/pool.h
+++ b/src/pool.h
@@ -126,6 +126,13 @@ extern char *git_pool_strcat(git_pool *pool, const char *a, const char *b);
*/
extern void git_pool_free(git_pool *pool, void *ptr);
+/**
+ * Push an array of pool allocated blocks efficiently onto the free list.
+ *
+ * This has the same constraints as `git_pool_free()` above.
+ */
+extern void git_pool_free_array(git_pool *pool, size_t count, void **ptrs);
+
/*
* Misc utilities
*/
diff --git a/src/tree.c b/src/tree.c
index 11123a1..17b3c37 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -55,23 +55,28 @@ static int valid_entry_name(const char *filename)
strcmp(filename, DOT_GIT) != 0));
}
-int git_tree_entry_cmp(const git_tree_entry *e1, const git_tree_entry *e2)
+static int entry_sort_cmp(const void *a, const void *b)
{
+ const git_tree_entry *e1 = (const git_tree_entry *)a;
+ const git_tree_entry *e2 = (const git_tree_entry *)b;
+
return git_path_cmp(
e1->filename, e1->filename_len, git_tree_entry__is_tree(e1),
- e2->filename, e2->filename_len, git_tree_entry__is_tree(e2));
+ e2->filename, e2->filename_len, git_tree_entry__is_tree(e2),
+ git__strncmp);
}
-int git_tree_entry_icmp(const git_tree_entry *e1, const git_tree_entry *e2)
+int git_tree_entry_cmp(const git_tree_entry *e1, const git_tree_entry *e2)
{
- return git_path_icmp(
- e1->filename, e1->filename_len, git_tree_entry__is_tree(e1),
- e2->filename, e2->filename_len, git_tree_entry__is_tree(e2));
+ return entry_sort_cmp(e1, e2);
}
-static int entry_sort_cmp(const void *a, const void *b)
+int git_tree_entry_icmp(const git_tree_entry *e1, const git_tree_entry *e2)
{
- return git_tree_entry_cmp((const git_tree_entry *)a, (const git_tree_entry *)b);
+ return git_path_cmp(
+ e1->filename, e1->filename_len, git_tree_entry__is_tree(e1),
+ e2->filename, e2->filename_len, git_tree_entry__is_tree(e2),
+ git__strncasecmp);
}
static git_tree_entry *alloc_entry(const char *filename)
diff --git a/tests-clar/core/pool.c b/tests-clar/core/pool.c
index 5ed9736..c42bb6d 100644
--- a/tests-clar/core/pool.c
+++ b/tests-clar/core/pool.c
@@ -83,3 +83,53 @@ void test_core_pool__2(void)
git_pool_clear(&p);
}
+
+void test_core_pool__free_list(void)
+{
+ int i;
+ git_pool p;
+ void *ptr, *ptrs[50];
+
+ cl_git_pass(git_pool_init(&p, 100, 100));
+
+ for (i = 0; i < 10; ++i) {
+ ptr = git_pool_malloc(&p, 1);
+ cl_assert(ptr != NULL);
+ }
+ cl_assert_equal_i(10, (int)p.items);
+
+ for (i = 0; i < 50; ++i) {
+ ptrs[i] = git_pool_malloc(&p, 1);
+ cl_assert(ptrs[i] != NULL);
+ }
+ cl_assert_equal_i(60, (int)p.items);
+
+ git_pool_free(&p, ptr);
+ cl_assert_equal_i(60, (int)p.items);
+
+ git_pool_free_array(&p, 50, ptrs);
+ cl_assert_equal_i(60, (int)p.items);
+
+ for (i = 0; i < 50; ++i) {
+ ptrs[i] = git_pool_malloc(&p, 1);
+ cl_assert(ptrs[i] != NULL);
+ }
+ cl_assert_equal_i(60, (int)p.items);
+
+ for (i = 0; i < 111; ++i) {
+ ptr = git_pool_malloc(&p, 1);
+ cl_assert(ptr != NULL);
+ }
+ cl_assert_equal_i(170, (int)p.items);
+
+ git_pool_free_array(&p, 50, ptrs);
+ cl_assert_equal_i(170, (int)p.items);
+
+ for (i = 0; i < 50; ++i) {
+ ptrs[i] = git_pool_malloc(&p, 1);
+ cl_assert(ptrs[i] != NULL);
+ }
+ cl_assert_equal_i(170, (int)p.items);
+
+ git_pool_clear(&p);
+}
diff --git a/tests-clar/repo/iterator.c b/tests-clar/repo/iterator.c
index 9e1f098..00c46d6 100644
--- a/tests-clar/repo/iterator.c
+++ b/tests-clar/repo/iterator.c
@@ -337,6 +337,8 @@ void test_repo_iterator__tree_icase(void)
&i, head, flag | GIT_ITERATOR_DONT_AUTOEXPAND, "k", "k/Z"));
expect_iterator_items(i, 1, NULL, 6, NULL);
git_iterator_free(i);
+
+ git_tree_free(head);
}
void test_repo_iterator__tree_more(void)