8
#include "notes.h"
9
#include "gpg-interface.h"
10
#include "mergesort.h"
11
+#include "commit-slab.h"
12
13
static struct commit_extra_header *read_commit_extra_header_lines(const char *buf, size_t len, const char **);
14
502
return item;
503
}
504
504
-struct commit_slab_piece {
505
- int buf;
506
-};
507
-
508
-struct commit_slab {
509
- int piece_size;
510
- int piece_count;
511
- struct commit_slab_piece **piece;
512
-};
513
-
514
-static void slab_init(struct commit_slab *s)
515
-{
516
- /* allocate ~512kB at once, allowing for malloc overhead */
517
- int size = (512*1024-32) / sizeof(struct commit_slab_piece);
518
-
519
- s->piece_size = size;
520
- s->piece_count = 0;
521
- s->piece = NULL;
522
-}
523
-
524
-static void slab_clear(struct commit_slab *s)
525
-{
526
- int i;
527
-
528
- for (i = 0; i < s->piece_count; i++)
529
- free(s->piece[i]);
530
- s->piece_count = 0;
531
- free(s->piece);
532
- s->piece = NULL;
533
-}
534
-
535
-static inline struct commit_slab_piece *slab_at(struct commit_slab *s,
536
- const struct commit *c)
537
-{
538
- int nth_piece, nth_slot;
539
-
540
- nth_piece = c->index / s->piece_size;
541
- nth_slot = c->index % s->piece_size;
542
-
543
- if (s->piece_count <= nth_piece) {
544
- int i;
505
+/*
506
+ * Topological sort support
507
+ */
508
546
- s->piece = xrealloc(s->piece, (nth_piece + 1) * sizeof(s->piece));
547
- for (i = s->piece_count; i <= nth_piece; i++)
548
- s->piece[i] = NULL;
549
- s->piece_count = nth_piece + 1;
550
- }
551
- if (!s->piece[nth_piece])
552
- s->piece[nth_piece] = xcalloc(s->piece_size, sizeof(**s->piece));
553
- return &s->piece[nth_piece][nth_slot];
554
-}
509
+/* count number of children that have not been emitted */
510
+define_commit_slab(indegree_slab, int);
511
512
/*
513
* Performs an in-place topological sort on the list supplied.
517
struct commit_list *next, *orig = *list;
518
struct commit_list *work, **insert;
519
struct commit_list **pptr;
564
- struct commit_slab indegree;
520
+ struct indegree_slab indegree;
521
522
if (!orig)
523
return;
524
*list = NULL;
525
570
- slab_init(&indegree);
526
+ init_indegree_slab(&indegree);
527
528
/* Mark them and clear the indegree */
529
for (next = orig; next; next = next->next) {
530
struct commit *commit = next->item;
575
- slab_at(&indegree, commit)->buf = 1;
531
+ *(indegree_slab_at(&indegree, commit)) = 1;
532
}
533
534
/* update the indegree */
536
struct commit_list * parents = next->item->parents;
537
while (parents) {
538
struct commit *parent = parents->item;
583
- int *pi = &slab_at(&indegree, parent)->buf;
539
+ int *pi = indegree_slab_at(&indegree, parent);
540
541
if (*pi)
542
(*pi)++;
556
for (next = orig; next; next = next->next) {
557
struct commit *commit = next->item;
558
603
- if (slab_at(&indegree, commit)->buf == 1)
559
+ if (*(indegree_slab_at(&indegree, commit)) == 1)
560
insert = &commit_list_insert(commit, insert)->next;
561
}
562
577
commit = work_item->item;
578
for (parents = commit->parents; parents ; parents = parents->next) {
579
struct commit *parent = parents->item;
624
- int *pi = &slab_at(&indegree, parent)->buf;
580
+ int *pi = indegree_slab_at(&indegree, parent);
581
582
if (!*pi)
583
continue;
598
* work_item is a commit all of whose children
599
* have already been emitted. we can emit it now.
600
*/
645
- slab_at(&indegree, commit)->buf = 0;
601
+ *(indegree_slab_at(&indegree, commit)) = 0;
602
*pptr = work_item;
603
pptr = &work_item->next;
604
}
605
650
- slab_clear(&indegree);
606
+ clear_indegree_slab(&indegree);
607
}
608
609
/* merge-base stuff */