14
14
#include "diff.h"
15
15
#include "revision.h"
16
16
#include "list-objects.h"
17
+ #include "pack-objects.h"
17
18
#include "progress.h"
18
19
#include "refs.h"
19
20
#include "streaming.h"
@@ -25,42 +26,15 @@ static const char *pack_usage[] = {
25
26
NULL
26
27
};
27
28
28
- struct object_entry {
29
- struct pack_idx_entry idx ;
30
- unsigned long size ; /* uncompressed size */
31
- struct packed_git * in_pack ; /* already in pack */
32
- off_t in_pack_offset ;
33
- struct object_entry * delta ; /* delta base object */
34
- struct object_entry * delta_child ; /* deltified objects who bases me */
35
- struct object_entry * delta_sibling ; /* other deltified objects who
36
- * uses the same base as me
37
- */
38
- void * delta_data ; /* cached delta (uncompressed) */
39
- unsigned long delta_size ; /* delta data size (uncompressed) */
40
- unsigned long z_delta_size ; /* delta data size (compressed) */
41
- enum object_type type ;
42
- enum object_type in_pack_type ; /* could be delta */
43
- uint32_t hash ; /* name hint hash */
44
- unsigned char in_pack_header_size ;
45
- unsigned preferred_base :1 ; /*
46
- * we do not pack this, but is available
47
- * to be used as the base object to delta
48
- * objects against.
49
- */
50
- unsigned no_try_delta :1 ;
51
- unsigned tagged :1 ; /* near the very tip of refs */
52
- unsigned filled :1 ; /* assigned write-order */
53
- };
54
-
55
29
/*
56
- * Objects we are going to pack are collected in objects array (dynamically
57
- * expanded). nr_objects & nr_alloc controls this array. They are stored
58
- * in the order we see -- typically rev-list --objects order that gives us
59
- * nice "minimum seek" order.
30
+ * Objects we are going to pack are collected in the `to_pack` structure.
31
+ * It contains an array (dynamically expanded) of the object data, and a map
32
+ * that can resolve SHA1s to their position in the array.
60
33
*/
61
- static struct object_entry * objects ;
34
+ static struct packing_data to_pack ;
35
+
62
36
static struct pack_idx_entry * * written_list ;
63
- static uint32_t nr_objects , nr_alloc , nr_result , nr_written ;
37
+ static uint32_t nr_result , nr_written ;
64
38
65
39
static int non_empty ;
66
40
static int reuse_delta = 1 , reuse_object = 1 ;
@@ -89,22 +63,12 @@ static unsigned long cache_max_small_delta_size = 1000;
89
63
90
64
static unsigned long window_memory_limit = 0 ;
91
65
92
- /*
93
- * The object names in objects array are hashed with this hashtable,
94
- * to help looking up the entry by object name.
95
- * This hashtable is built after all the objects are seen.
96
- */
97
- static int * object_ix ;
98
- static int object_ix_hashsz ;
99
- static struct object_entry * locate_object_entry (const unsigned char * sha1 );
100
-
101
66
/*
102
67
* stats
103
68
*/
104
69
static uint32_t written , written_delta ;
105
70
static uint32_t reused , reused_delta ;
106
71
107
-
108
72
static void * get_delta (struct object_entry * entry )
109
73
{
110
74
unsigned long size , base_size , delta_size ;
@@ -553,12 +517,12 @@ static int mark_tagged(const char *path, const unsigned char *sha1, int flag,
553
517
void * cb_data )
554
518
{
555
519
unsigned char peeled [20 ];
556
- struct object_entry * entry = locate_object_entry ( sha1 );
520
+ struct object_entry * entry = packlist_find ( & to_pack , sha1 , NULL );
557
521
558
522
if (entry )
559
523
entry -> tagged = 1 ;
560
524
if (!peel_ref (path , peeled )) {
561
- entry = locate_object_entry ( peeled );
525
+ entry = packlist_find ( & to_pack , peeled , NULL );
562
526
if (entry )
563
527
entry -> tagged = 1 ;
564
528
}
@@ -633,9 +597,10 @@ static struct object_entry **compute_write_order(void)
633
597
{
634
598
unsigned int i , wo_end , last_untagged ;
635
599
636
- struct object_entry * * wo = xmalloc (nr_objects * sizeof (* wo ));
600
+ struct object_entry * * wo = xmalloc (to_pack .nr_objects * sizeof (* wo ));
601
+ struct object_entry * objects = to_pack .objects ;
637
602
638
- for (i = 0 ; i < nr_objects ; i ++ ) {
603
+ for (i = 0 ; i < to_pack . nr_objects ; i ++ ) {
639
604
objects [i ].tagged = 0 ;
640
605
objects [i ].filled = 0 ;
641
606
objects [i ].delta_child = NULL ;
@@ -647,7 +612,7 @@ static struct object_entry **compute_write_order(void)
647
612
* Make sure delta_sibling is sorted in the original
648
613
* recency order.
649
614
*/
650
- for (i = nr_objects ; i > 0 ;) {
615
+ for (i = to_pack . nr_objects ; i > 0 ;) {
651
616
struct object_entry * e = & objects [-- i ];
652
617
if (!e -> delta )
653
618
continue ;
@@ -665,7 +630,7 @@ static struct object_entry **compute_write_order(void)
665
630
* Give the objects in the original recency order until
666
631
* we see a tagged tip.
667
632
*/
668
- for (i = wo_end = 0 ; i < nr_objects ; i ++ ) {
633
+ for (i = wo_end = 0 ; i < to_pack . nr_objects ; i ++ ) {
669
634
if (objects [i ].tagged )
670
635
break ;
671
636
add_to_write_order (wo , & wo_end , & objects [i ]);
@@ -675,15 +640,15 @@ static struct object_entry **compute_write_order(void)
675
640
/*
676
641
* Then fill all the tagged tips.
677
642
*/
678
- for (; i < nr_objects ; i ++ ) {
643
+ for (; i < to_pack . nr_objects ; i ++ ) {
679
644
if (objects [i ].tagged )
680
645
add_to_write_order (wo , & wo_end , & objects [i ]);
681
646
}
682
647
683
648
/*
684
649
* And then all remaining commits and tags.
685
650
*/
686
- for (i = last_untagged ; i < nr_objects ; i ++ ) {
651
+ for (i = last_untagged ; i < to_pack . nr_objects ; i ++ ) {
687
652
if (objects [i ].type != OBJ_COMMIT &&
688
653
objects [i ].type != OBJ_TAG )
689
654
continue ;
@@ -693,7 +658,7 @@ static struct object_entry **compute_write_order(void)
693
658
/*
694
659
* And then all the trees.
695
660
*/
696
- for (i = last_untagged ; i < nr_objects ; i ++ ) {
661
+ for (i = last_untagged ; i < to_pack . nr_objects ; i ++ ) {
697
662
if (objects [i ].type != OBJ_TREE )
698
663
continue ;
699
664
add_to_write_order (wo , & wo_end , & objects [i ]);
@@ -702,13 +667,13 @@ static struct object_entry **compute_write_order(void)
702
667
/*
703
668
* Finally all the rest in really tight order
704
669
*/
705
- for (i = last_untagged ; i < nr_objects ; i ++ ) {
670
+ for (i = last_untagged ; i < to_pack . nr_objects ; i ++ ) {
706
671
if (!objects [i ].filled )
707
672
add_family_to_write_order (wo , & wo_end , & objects [i ]);
708
673
}
709
674
710
- if (wo_end != nr_objects )
711
- die ("ordered %u objects, expected %" PRIu32 , wo_end , nr_objects );
675
+ if (wo_end != to_pack . nr_objects )
676
+ die ("ordered %u objects, expected %" PRIu32 , wo_end , to_pack . nr_objects );
712
677
713
678
return wo ;
714
679
}
@@ -724,7 +689,7 @@ static void write_pack_file(void)
724
689
725
690
if (progress > pack_to_stdout )
726
691
progress_state = start_progress ("Writing objects" , nr_result );
727
- written_list = xmalloc (nr_objects * sizeof (* written_list ));
692
+ written_list = xmalloc (to_pack . nr_objects * sizeof (* written_list ));
728
693
write_order = compute_write_order ();
729
694
730
695
do {
@@ -740,7 +705,7 @@ static void write_pack_file(void)
740
705
if (!offset )
741
706
die_errno ("unable to write pack header" );
742
707
nr_written = 0 ;
743
- for (; i < nr_objects ; i ++ ) {
708
+ for (; i < to_pack . nr_objects ; i ++ ) {
744
709
struct object_entry * e = write_order [i ];
745
710
if (write_one (f , e , & offset ) == WRITE_ONE_BREAK )
746
711
break ;
@@ -803,7 +768,7 @@ static void write_pack_file(void)
803
768
written_list [j ]-> offset = (off_t )- 1 ;
804
769
}
805
770
nr_remaining -= nr_written ;
806
- } while (nr_remaining && i < nr_objects );
771
+ } while (nr_remaining && i < to_pack . nr_objects );
807
772
808
773
free (written_list );
809
774
free (write_order );
@@ -813,53 +778,6 @@ static void write_pack_file(void)
813
778
written , nr_result );
814
779
}
815
780
816
- static int locate_object_entry_hash (const unsigned char * sha1 )
817
- {
818
- int i ;
819
- unsigned int ui ;
820
- memcpy (& ui , sha1 , sizeof (unsigned int ));
821
- i = ui % object_ix_hashsz ;
822
- while (0 < object_ix [i ]) {
823
- if (!hashcmp (sha1 , objects [object_ix [i ] - 1 ].idx .sha1 ))
824
- return i ;
825
- if (++ i == object_ix_hashsz )
826
- i = 0 ;
827
- }
828
- return -1 - i ;
829
- }
830
-
831
- static struct object_entry * locate_object_entry (const unsigned char * sha1 )
832
- {
833
- int i ;
834
-
835
- if (!object_ix_hashsz )
836
- return NULL ;
837
-
838
- i = locate_object_entry_hash (sha1 );
839
- if (0 <= i )
840
- return & objects [object_ix [i ]- 1 ];
841
- return NULL ;
842
- }
843
-
844
- static void rehash_objects (void )
845
- {
846
- uint32_t i ;
847
- struct object_entry * oe ;
848
-
849
- object_ix_hashsz = nr_objects * 3 ;
850
- if (object_ix_hashsz < 1024 )
851
- object_ix_hashsz = 1024 ;
852
- object_ix = xrealloc (object_ix , sizeof (int ) * object_ix_hashsz );
853
- memset (object_ix , 0 , sizeof (int ) * object_ix_hashsz );
854
- for (i = 0 , oe = objects ; i < nr_objects ; i ++ , oe ++ ) {
855
- int ix = locate_object_entry_hash (oe -> idx .sha1 );
856
- if (0 <= ix )
857
- continue ;
858
- ix = -1 - ix ;
859
- object_ix [ix ] = i + 1 ;
860
- }
861
- }
862
-
863
781
static uint32_t name_hash (const char * name )
864
782
{
865
783
uint32_t c , hash = 0 ;
@@ -908,13 +826,12 @@ static int add_object_entry(const unsigned char *sha1, enum object_type type,
908
826
struct object_entry * entry ;
909
827
struct packed_git * p , * found_pack = NULL ;
910
828
off_t found_offset = 0 ;
911
- int ix ;
912
829
uint32_t hash = name_hash (name );
830
+ uint32_t index_pos ;
913
831
914
- ix = nr_objects ? locate_object_entry_hash ( sha1 ) : -1 ;
915
- if (ix >= 0 ) {
832
+ entry = packlist_find ( & to_pack , sha1 , & index_pos ) ;
833
+ if (entry ) {
916
834
if (exclude ) {
917
- entry = objects + object_ix [ix ] - 1 ;
918
835
if (!entry -> preferred_base )
919
836
nr_result -- ;
920
837
entry -> preferred_base = 1 ;
@@ -947,14 +864,7 @@ static int add_object_entry(const unsigned char *sha1, enum object_type type,
947
864
}
948
865
}
949
866
950
- if (nr_objects >= nr_alloc ) {
951
- nr_alloc = (nr_alloc + 1024 ) * 3 / 2 ;
952
- objects = xrealloc (objects , nr_alloc * sizeof (* entry ));
953
- }
954
-
955
- entry = objects + nr_objects ++ ;
956
- memset (entry , 0 , sizeof (* entry ));
957
- hashcpy (entry -> idx .sha1 , sha1 );
867
+ entry = packlist_alloc (& to_pack , sha1 , index_pos );
958
868
entry -> hash = hash ;
959
869
if (type )
960
870
entry -> type = type ;
@@ -967,12 +877,7 @@ static int add_object_entry(const unsigned char *sha1, enum object_type type,
967
877
entry -> in_pack_offset = found_offset ;
968
878
}
969
879
970
- if (object_ix_hashsz * 3 <= nr_objects * 4 )
971
- rehash_objects ();
972
- else
973
- object_ix [-1 - ix ] = nr_objects ;
974
-
975
- display_progress (progress_state , nr_objects );
880
+ display_progress (progress_state , to_pack .nr_objects );
976
881
977
882
if (name && no_try_delta (name ))
978
883
entry -> no_try_delta = 1 ;
@@ -1329,7 +1234,7 @@ static void check_object(struct object_entry *entry)
1329
1234
break ;
1330
1235
}
1331
1236
1332
- if (base_ref && (base_entry = locate_object_entry ( base_ref ))) {
1237
+ if (base_ref && (base_entry = packlist_find ( & to_pack , base_ref , NULL ))) {
1333
1238
/*
1334
1239
* If base_ref was set above that means we wish to
1335
1240
* reuse delta data, and we even found that base
@@ -1403,12 +1308,12 @@ static void get_object_details(void)
1403
1308
uint32_t i ;
1404
1309
struct object_entry * * sorted_by_offset ;
1405
1310
1406
- sorted_by_offset = xcalloc (nr_objects , sizeof (struct object_entry * ));
1407
- for (i = 0 ; i < nr_objects ; i ++ )
1408
- sorted_by_offset [i ] = objects + i ;
1409
- qsort (sorted_by_offset , nr_objects , sizeof (* sorted_by_offset ), pack_offset_sort );
1311
+ sorted_by_offset = xcalloc (to_pack . nr_objects , sizeof (struct object_entry * ));
1312
+ for (i = 0 ; i < to_pack . nr_objects ; i ++ )
1313
+ sorted_by_offset [i ] = to_pack . objects + i ;
1314
+ qsort (sorted_by_offset , to_pack . nr_objects , sizeof (* sorted_by_offset ), pack_offset_sort );
1410
1315
1411
- for (i = 0 ; i < nr_objects ; i ++ ) {
1316
+ for (i = 0 ; i < to_pack . nr_objects ; i ++ ) {
1412
1317
struct object_entry * entry = sorted_by_offset [i ];
1413
1318
check_object (entry );
1414
1319
if (big_file_threshold < entry -> size )
@@ -2034,7 +1939,7 @@ static int add_ref_tag(const char *path, const unsigned char *sha1, int flag, vo
2034
1939
2035
1940
if (!prefixcmp (path , "refs/tags/" ) && /* is a tag? */
2036
1941
!peel_ref (path , peeled ) && /* peelable? */
2037
- locate_object_entry ( peeled )) /* object packed? */
1942
+ packlist_find ( & to_pack , peeled , NULL )) /* object packed? */
2038
1943
add_object_entry (sha1 , OBJ_TAG , NULL , 0 );
2039
1944
return 0 ;
2040
1945
}
@@ -2057,14 +1962,14 @@ static void prepare_pack(int window, int depth)
2057
1962
if (!pack_to_stdout )
2058
1963
do_check_packed_object_crc = 1 ;
2059
1964
2060
- if (!nr_objects || !window || !depth )
1965
+ if (!to_pack . nr_objects || !window || !depth )
2061
1966
return ;
2062
1967
2063
- delta_list = xmalloc (nr_objects * sizeof (* delta_list ));
1968
+ delta_list = xmalloc (to_pack . nr_objects * sizeof (* delta_list ));
2064
1969
nr_deltas = n = 0 ;
2065
1970
2066
- for (i = 0 ; i < nr_objects ; i ++ ) {
2067
- struct object_entry * entry = objects + i ;
1971
+ for (i = 0 ; i < to_pack . nr_objects ; i ++ ) {
1972
+ struct object_entry * entry = to_pack . objects + i ;
2068
1973
2069
1974
if (entry -> delta )
2070
1975
/* This happens if we decided to reuse existing
@@ -2342,7 +2247,7 @@ static void loosen_unused_packed_objects(struct rev_info *revs)
2342
2247
2343
2248
for (i = 0 ; i < p -> num_objects ; i ++ ) {
2344
2249
sha1 = nth_packed_object_sha1 (p , i );
2345
- if (!locate_object_entry ( sha1 ) &&
2250
+ if (!packlist_find ( & to_pack , sha1 , NULL ) &&
2346
2251
!has_sha1_pack_kept_or_nonlocal (sha1 ))
2347
2252
if (force_object_loose (sha1 , p -> mtime ))
2348
2253
die ("unable to force loose object" );
0 commit comments