27
uint32_t commit_pos;
28
};
29
30
-struct bitmap_writer {
31
- struct ewah_bitmap *commits;
32
- struct ewah_bitmap *trees;
33
- struct ewah_bitmap *blobs;
34
- struct ewah_bitmap *tags;
35
-
36
- kh_oid_map_t *bitmaps;
37
- struct packing_data *to_pack;
38
-
39
- struct bitmapped_commit *selected;
40
- unsigned int selected_nr, selected_alloc;
41
-
42
- struct progress *progress;
43
- int show_progress;
44
- unsigned char pack_checksum[GIT_MAX_RAWSZ];
45
-};
46
-
47
-static struct bitmap_writer writer;
30
+void bitmap_writer_init(struct bitmap_writer *writer)
31
+{
32
+ memset(writer, 0, sizeof(struct bitmap_writer));
33
+}
34
49
-void bitmap_writer_show_progress(int show)
35
+void bitmap_writer_show_progress(struct bitmap_writer *writer, int show)
36
{
51
- writer.show_progress = show;
37
+ writer->show_progress = show;
38
}
39
40
/**
41
* Build the initial type index for the packfile or multi-pack-index
42
*/
57
-void bitmap_writer_build_type_index(struct packing_data *to_pack,
43
+void bitmap_writer_build_type_index(struct bitmap_writer *writer,
44
+ struct packing_data *to_pack,
45
struct pack_idx_entry **index,
46
uint32_t index_nr)
47
{
48
uint32_t i;
49
63
- writer.commits = ewah_new();
64
- writer.trees = ewah_new();
65
- writer.blobs = ewah_new();
66
- writer.tags = ewah_new();
50
+ writer->commits = ewah_new();
51
+ writer->trees = ewah_new();
52
+ writer->blobs = ewah_new();
53
+ writer->tags = ewah_new();
54
ALLOC_ARRAY(to_pack->in_pack_pos, to_pack->nr_objects);
55
56
for (i = 0; i < index_nr; ++i) {
75
76
switch (real_type) {
77
case OBJ_COMMIT:
91
- ewah_set(writer.commits, i);
78
+ ewah_set(writer->commits, i);
79
break;
80
81
case OBJ_TREE:
95
- ewah_set(writer.trees, i);
82
+ ewah_set(writer->trees, i);
83
break;
84
85
case OBJ_BLOB:
99
- ewah_set(writer.blobs, i);
86
+ ewah_set(writer->blobs, i);
87
break;
88
89
case OBJ_TAG:
103
- ewah_set(writer.tags, i);
90
+ ewah_set(writer->tags, i);
91
break;
92
93
default:
102
* Compute the actual bitmaps
103
*/
104
118
-static inline void push_bitmapped_commit(struct commit *commit)
105
+static inline void push_bitmapped_commit(struct bitmap_writer *writer,
106
+ struct commit *commit)
107
{
120
- if (writer.selected_nr >= writer.selected_alloc) {
121
- writer.selected_alloc = (writer.selected_alloc + 32) * 2;
122
- REALLOC_ARRAY(writer.selected, writer.selected_alloc);
108
+ if (writer->selected_nr >= writer->selected_alloc) {
109
+ writer->selected_alloc = (writer->selected_alloc + 32) * 2;
110
+ REALLOC_ARRAY(writer->selected, writer->selected_alloc);
111
}
112
125
- writer.selected[writer.selected_nr].commit = commit;
126
- writer.selected[writer.selected_nr].bitmap = NULL;
127
- writer.selected[writer.selected_nr].flags = 0;
113
+ writer->selected[writer->selected_nr].commit = commit;
114
+ writer->selected[writer->selected_nr].bitmap = NULL;
115
+ writer->selected[writer->selected_nr].flags = 0;
116
129
- writer.selected_nr++;
117
+ writer->selected_nr++;
118
}
119
132
-static uint32_t find_object_pos(const struct object_id *oid, int *found)
120
+static uint32_t find_object_pos(struct bitmap_writer *writer,
121
+ const struct object_id *oid, int *found)
122
{
134
- struct object_entry *entry = packlist_find(writer.to_pack, oid);
123
+ struct object_entry *entry = packlist_find(writer->to_pack, oid);
124
125
if (!entry) {
126
if (found)
132
133
if (found)
134
*found = 1;
146
- return oe_in_pack_pos(writer.to_pack, entry);
135
+ return oe_in_pack_pos(writer->to_pack, entry);
136
}
137
149
-static void compute_xor_offsets(void)
138
+static void compute_xor_offsets(struct bitmap_writer *writer)
139
{
140
static const int MAX_XOR_OFFSET_SEARCH = 10;
141
142
int i, next = 0;
143
155
- while (next < writer.selected_nr) {
156
- struct bitmapped_commit *stored = &writer.selected[next];
144
+ while (next < writer->selected_nr) {
145
+ struct bitmapped_commit *stored = &writer->selected[next];
146
147
int best_offset = 0;
148
struct ewah_bitmap *best_bitmap = stored->bitmap;
155
break;
156
157
test_xor = ewah_pool_new();
169
- ewah_xor(writer.selected[curr].bitmap, stored->bitmap, test_xor);
158
+ ewah_xor(writer->selected[curr].bitmap, stored->bitmap, test_xor);
159
160
if (test_xor->buffer_size < best_bitmap->buffer_size) {
161
if (best_bitmap != stored->bitmap)
337
bb->commits_nr = bb->commits_alloc = 0;
338
}
339
351
-static int fill_bitmap_tree(struct bitmap *bitmap,
340
+static int fill_bitmap_tree(struct bitmap_writer *writer,
341
+ struct bitmap *bitmap,
342
struct tree *tree)
343
{
344
int found;
350
* If our bit is already set, then there is nothing to do. Both this
351
* tree and all of its children will be set.
352
*/
363
- pos = find_object_pos(&tree->object.oid, &found);
353
+ pos = find_object_pos(writer, &tree->object.oid, &found);
354
if (!found)
355
return -1;
356
if (bitmap_get(bitmap, pos))
365
while (tree_entry(&desc, &entry)) {
366
switch (object_type(entry.mode)) {
367
case OBJ_TREE:
378
- if (fill_bitmap_tree(bitmap,
368
+ if (fill_bitmap_tree(writer, bitmap,
369
lookup_tree(the_repository, &entry.oid)) < 0)
370
return -1;
371
break;
372
case OBJ_BLOB:
383
- pos = find_object_pos(&entry.oid, &found);
373
+ pos = find_object_pos(writer, &entry.oid, &found);
374
if (!found)
375
return -1;
376
bitmap_set(bitmap, pos);
387
388
static int reused_bitmaps_nr;
389
400
-static int fill_bitmap_commit(struct bb_commit *ent,
390
+static int fill_bitmap_commit(struct bitmap_writer *writer,
391
+ struct bb_commit *ent,
392
struct commit *commit,
393
struct prio_queue *queue,
394
struct prio_queue *tree_queue,
427
* Mark ourselves and queue our tree. The commit
428
* walk ensures we cover all parents.
429
*/
439
- pos = find_object_pos(&c->object.oid, &found);
430
+ pos = find_object_pos(writer, &c->object.oid, &found);
431
if (!found)
432
return -1;
433
bitmap_set(ent->bitmap, pos);
435
repo_get_commit_tree(the_repository, c));
436
437
for (p = c->parents; p; p = p->next) {
447
- pos = find_object_pos(&p->item->object.oid, &found);
438
+ pos = find_object_pos(writer, &p->item->object.oid,
439
+ &found);
440
if (!found)
441
return -1;
442
if (!bitmap_get(ent->bitmap, pos)) {
447
}
448
449
while (tree_queue->nr) {
458
- if (fill_bitmap_tree(ent->bitmap,
450
+ if (fill_bitmap_tree(writer, ent->bitmap,
451
prio_queue_get(tree_queue)) < 0)
452
return -1;
453
}
454
return 0;
455
}
456
465
-static void store_selected(struct bb_commit *ent, struct commit *commit)
457
+static void store_selected(struct bitmap_writer *writer,
458
+ struct bb_commit *ent, struct commit *commit)
459
{
467
- struct bitmapped_commit *stored = &writer.selected[ent->idx];
460
+ struct bitmapped_commit *stored = &writer->selected[ent->idx];
461
khiter_t hash_pos;
462
int hash_ret;
463
464
stored->bitmap = bitmap_to_ewah(ent->bitmap);
465
473
- hash_pos = kh_put_oid_map(writer.bitmaps, commit->object.oid, &hash_ret);
466
+ hash_pos = kh_put_oid_map(writer->bitmaps, commit->object.oid, &hash_ret);
467
if (hash_ret == 0)
468
die("Duplicate entry when writing index: %s",
469
oid_to_hex(&commit->object.oid));
477
- kh_value(writer.bitmaps, hash_pos) = stored;
470
+ kh_value(writer->bitmaps, hash_pos) = stored;
471
}
472
480
-int bitmap_writer_build(struct packing_data *to_pack)
473
+int bitmap_writer_build(struct bitmap_writer *writer,
474
+ struct packing_data *to_pack)
475
{
476
struct bitmap_builder bb;
477
size_t i;
482
uint32_t *mapping;
483
int closed = 1; /* until proven otherwise */
484
491
- writer.bitmaps = kh_init_oid_map();
492
- writer.to_pack = to_pack;
485
+ writer->bitmaps = kh_init_oid_map();
486
+ writer->to_pack = to_pack;
487
494
- if (writer.show_progress)
495
- writer.progress = start_progress("Building bitmaps", writer.selected_nr);
488
+ if (writer->show_progress)
489
+ writer->progress = start_progress("Building bitmaps",
490
+ writer->selected_nr);
491
trace2_region_enter("pack-bitmap-write", "building_bitmaps_total",
492
the_repository);
493
497
else
498
mapping = NULL;
499
505
- bitmap_builder_init(&bb, &writer, old_bitmap);
500
+ bitmap_builder_init(&bb, writer, old_bitmap);
501
for (i = bb.commits_nr; i > 0; i--) {
502
struct commit *commit = bb.commits[i-1];
503
struct bb_commit *ent = bb_data_at(&bb.data, commit);
504
struct commit *child;
505
int reused = 0;
506
512
- if (fill_bitmap_commit(ent, commit, &queue, &tree_queue,
507
+ if (fill_bitmap_commit(writer, ent, commit, &queue, &tree_queue,
508
old_bitmap, mapping) < 0) {
509
closed = 0;
510
break;
511
}
512
513
if (ent->selected) {
519
- store_selected(ent, commit);
514
+ store_selected(writer, ent, commit);
515
nr_stored++;
521
- display_progress(writer.progress, nr_stored);
516
+ display_progress(writer->progress, nr_stored);
517
}
518
519
while ((child = pop_commit(&ent->reverse_edges))) {
544
trace2_data_intmax("pack-bitmap-write", the_repository,
545
"building_bitmaps_reused", reused_bitmaps_nr);
546
552
- stop_progress(&writer.progress);
547
+ stop_progress(&writer->progress);
548
549
if (closed)
555
- compute_xor_offsets();
550
+ compute_xor_offsets(writer);
551
return closed ? 0 : -1;
552
}
553
585
return (long)b->date - (long)a->date;
586
}
587
593
-void bitmap_writer_select_commits(struct commit **indexed_commits,
588
+void bitmap_writer_select_commits(struct bitmap_writer *writer,
589
+ struct commit **indexed_commits,
590
unsigned int indexed_commits_nr,
591
int max_bitmaps)
592
{
596
597
if (indexed_commits_nr < 100) {
598
for (i = 0; i < indexed_commits_nr; ++i)
603
- push_bitmapped_commit(indexed_commits[i]);
599
+ push_bitmapped_commit(writer, indexed_commits[i]);
600
return;
601
}
602
607
- if (writer.show_progress)
608
- writer.progress = start_progress("Selecting bitmap commits", 0);
603
+ if (writer->show_progress)
604
+ writer->progress = start_progress("Selecting bitmap commits", 0);
605
606
for (;;) {
607
struct commit *chosen = NULL;
611
if (i + next >= indexed_commits_nr)
612
break;
613
618
- if (max_bitmaps > 0 && writer.selected_nr >= max_bitmaps) {
619
- writer.selected_nr = max_bitmaps;
614
+ if (max_bitmaps > 0 && writer->selected_nr >= max_bitmaps) {
615
+ writer->selected_nr = max_bitmaps;
616
break;
617
}
618
634
}
635
}
636
641
- push_bitmapped_commit(chosen);
637
+ push_bitmapped_commit(writer, chosen);
638
639
i += next + 1;
644
- display_progress(writer.progress, i);
640
+ display_progress(writer->progress, i);
641
}
642
647
- stop_progress(&writer.progress);
643
+ stop_progress(&writer->progress);
644
}
645
646
666
return &index[pos]->oid;
667
}
668
673
-static void write_selected_commits_v1(struct hashfile *f, off_t *offsets)
669
+static void write_selected_commits_v1(struct bitmap_writer *writer,
670
+ struct hashfile *f, off_t *offsets)
671
{
672
int i;
673
677
- for (i = 0; i < writer.selected_nr; ++i) {
678
- struct bitmapped_commit *stored = &writer.selected[i];
674
+ for (i = 0; i < writer->selected_nr; ++i) {
675
+ struct bitmapped_commit *stored = &writer->selected[i];
676
677
if (offsets)
678
offsets[i] = hashfile_total(f);
685
}
686
}
687
691
-static int table_cmp(const void *_va, const void *_vb)
688
+static int table_cmp(const void *_va, const void *_vb, void *_data)
689
{
693
- struct bitmapped_commit *a = &writer.selected[*(uint32_t *)_va];
694
- struct bitmapped_commit *b = &writer.selected[*(uint32_t *)_vb];
690
+ struct bitmap_writer *writer = _data;
691
+ struct bitmapped_commit *a = &writer->selected[*(uint32_t *)_va];
692
+ struct bitmapped_commit *b = &writer->selected[*(uint32_t *)_vb];
693
694
if (a->commit_pos < b->commit_pos)
695
return -1;
699
return 0;
700
}
701
704
-static void write_lookup_table(struct hashfile *f, off_t *offsets)
702
+static void write_lookup_table(struct bitmap_writer *writer, struct hashfile *f,
703
+ off_t *offsets)
704
{
705
uint32_t i;
706
uint32_t *table, *table_inv;
707
709
- ALLOC_ARRAY(table, writer.selected_nr);
710
- ALLOC_ARRAY(table_inv, writer.selected_nr);
708
+ ALLOC_ARRAY(table, writer->selected_nr);
709
+ ALLOC_ARRAY(table_inv, writer->selected_nr);
710
712
- for (i = 0; i < writer.selected_nr; i++)
711
+ for (i = 0; i < writer->selected_nr; i++)
712
table[i] = i;
713
714
/*
716
* bitmap corresponds to j'th bitmapped commit (among the selected
717
* commits) in lex order of OIDs.
718
*/
720
- QSORT(table, writer.selected_nr, table_cmp);
719
+ QSORT_S(table, writer->selected_nr, table_cmp, writer);
720
721
/* table_inv helps us discover that relationship (i'th bitmap
722
* to j'th commit by j = table_inv[i])
723
*/
725
- for (i = 0; i < writer.selected_nr; i++)
724
+ for (i = 0; i < writer->selected_nr; i++)
725
table_inv[table[i]] = i;
726
727
trace2_region_enter("pack-bitmap-write", "writing_lookup_table", the_repository);
729
- for (i = 0; i < writer.selected_nr; i++) {
730
- struct bitmapped_commit *selected = &writer.selected[table[i]];
728
+ for (i = 0; i < writer->selected_nr; i++) {
729
+ struct bitmapped_commit *selected = &writer->selected[table[i]];
730
uint32_t xor_offset = selected->xor_offset;
731
uint32_t xor_row;
732
747
xor_row = 0xffffffff;
748
}
749
751
- hashwrite_be32(f, writer.selected[table[i]].commit_pos);
750
+ hashwrite_be32(f, writer->selected[table[i]].commit_pos);
751
hashwrite_be64(f, (uint64_t)offsets[table[i]]);
752
hashwrite_be32(f, xor_row);
753
}
769
}
770
}
771
773
-void bitmap_writer_set_checksum(const unsigned char *sha1)
772
+void bitmap_writer_set_checksum(struct bitmap_writer *writer,
773
+ const unsigned char *sha1)
774
{
775
- hashcpy(writer.pack_checksum, sha1);
775
+ hashcpy(writer->pack_checksum, sha1);
776
}
777
778
-void bitmap_writer_finish(struct pack_idx_entry **index,
778
+void bitmap_writer_finish(struct bitmap_writer *writer,
779
+ struct pack_idx_entry **index,
780
uint32_t index_nr,
781
const char *filename,
782
uint16_t options)
797
memcpy(header.magic, BITMAP_IDX_SIGNATURE, sizeof(BITMAP_IDX_SIGNATURE));
798
header.version = htons(default_version);
799
header.options = htons(flags | options);
799
- header.entry_count = htonl(writer.selected_nr);
800
- hashcpy(header.checksum, writer.pack_checksum);
800
+ header.entry_count = htonl(writer->selected_nr);
801
+ hashcpy(header.checksum, writer->pack_checksum);
802
803
hashwrite(f, &header, sizeof(header) - GIT_MAX_RAWSZ + the_hash_algo->rawsz);
803
- dump_bitmap(f, writer.commits);
804
- dump_bitmap(f, writer.trees);
805
- dump_bitmap(f, writer.blobs);
806
- dump_bitmap(f, writer.tags);
804
+ dump_bitmap(f, writer->commits);
805
+ dump_bitmap(f, writer->trees);
806
+ dump_bitmap(f, writer->blobs);
807
+ dump_bitmap(f, writer->tags);
808
809
if (options & BITMAP_OPT_LOOKUP_TABLE)
810
CALLOC_ARRAY(offsets, index_nr);
811
811
- for (i = 0; i < writer.selected_nr; i++) {
812
- struct bitmapped_commit *stored = &writer.selected[i];
812
+ for (i = 0; i < writer->selected_nr; i++) {
813
+ struct bitmapped_commit *stored = &writer->selected[i];
814
int commit_pos = oid_pos(&stored->commit->object.oid, index,
815
index_nr, oid_access);
816
819
stored->commit_pos = commit_pos;
820
}
821
821
- write_selected_commits_v1(f, offsets);
822
+ write_selected_commits_v1(writer, f, offsets);
823
824
if (options & BITMAP_OPT_LOOKUP_TABLE)
824
- write_lookup_table(f, offsets);
825
+ write_lookup_table(writer, f, offsets);
826
827
if (options & BITMAP_OPT_HASH_CACHE)
828
write_hash_cache(f, index, index_nr);