8
8
* size is easily available by examining the pack entry header). It is
9
9
* also rather expensive to find the sha1 for an object given its offset.
10
10
*
11
- * We build a hashtable of existing packs (pack_revindex), and keep reverse
12
- * index here -- pack index file is sorted by object name mapping to offset;
13
- * this pack_revindex[].revindex array is a list of offset/index_nr pairs
11
+ * The pack index file is sorted by object name mapping to offset;
12
+ * this revindex array is a list of offset/index_nr pairs
14
13
* ordered by offset, so if you know the offset of an object, next offset
15
14
* is where its packed representation ends and the index_nr can be used to
16
15
* get the object sha1 from the main index.
17
16
*/
18
17
19
- static struct pack_revindex * pack_revindex ;
20
- static int pack_revindex_hashsz ;
21
-
22
- static int pack_revindex_ix (struct packed_git * p )
23
- {
24
- unsigned long ui = (unsigned long )(intptr_t )p ;
25
- int i ;
26
-
27
- ui = ui ^ (ui >> 16 ); /* defeat structure alignment */
28
- i = (int )(ui % pack_revindex_hashsz );
29
- while (pack_revindex [i ].p ) {
30
- if (pack_revindex [i ].p == p )
31
- return i ;
32
- if (++ i == pack_revindex_hashsz )
33
- i = 0 ;
34
- }
35
- return -1 - i ;
36
- }
37
-
38
- static void init_pack_revindex (void )
39
- {
40
- int num ;
41
- struct packed_git * p ;
42
-
43
- for (num = 0 , p = packed_git ; p ; p = p -> next )
44
- num ++ ;
45
- if (!num )
46
- return ;
47
- pack_revindex_hashsz = num * 11 ;
48
- pack_revindex = xcalloc (pack_revindex_hashsz , sizeof (* pack_revindex ));
49
- for (p = packed_git ; p ; p = p -> next ) {
50
- num = pack_revindex_ix (p );
51
- num = - 1 - num ;
52
- pack_revindex [num ].p = p ;
53
- }
54
- /* revindex elements are lazily initialized */
55
- }
56
-
57
18
/*
58
19
* This is a least-significant-digit radix sort.
59
20
*
@@ -154,14 +115,13 @@ static void sort_revindex(struct revindex_entry *entries, unsigned n, off_t max)
154
115
/*
155
116
* Ordered list of offsets of objects in the pack.
156
117
*/
157
- static void create_pack_revindex (struct pack_revindex * rix )
118
+ static void create_pack_revindex (struct packed_git * p )
158
119
{
159
- struct packed_git * p = rix -> p ;
160
120
unsigned num_ent = p -> num_objects ;
161
121
unsigned i ;
162
122
const char * index = p -> index_data ;
163
123
164
- rix -> revindex = xmalloc (sizeof (* rix -> revindex ) * (num_ent + 1 ));
124
+ p -> revindex = xmalloc (sizeof (* p -> revindex ) * (num_ent + 1 ));
165
125
index += 4 * 256 ;
166
126
167
127
if (p -> index_version > 1 ) {
@@ -171,55 +131,42 @@ static void create_pack_revindex(struct pack_revindex *rix)
171
131
for (i = 0 ; i < num_ent ; i ++ ) {
172
132
uint32_t off = ntohl (* off_32 ++ );
173
133
if (!(off & 0x80000000 )) {
174
- rix -> revindex [i ].offset = off ;
134
+ p -> revindex [i ].offset = off ;
175
135
} else {
176
- rix -> revindex [i ].offset =
136
+ p -> revindex [i ].offset =
177
137
((uint64_t )ntohl (* off_64 ++ )) << 32 ;
178
- rix -> revindex [i ].offset |=
138
+ p -> revindex [i ].offset |=
179
139
ntohl (* off_64 ++ );
180
140
}
181
- rix -> revindex [i ].nr = i ;
141
+ p -> revindex [i ].nr = i ;
182
142
}
183
143
} else {
184
144
for (i = 0 ; i < num_ent ; i ++ ) {
185
145
uint32_t hl = * ((uint32_t * )(index + 24 * i ));
186
- rix -> revindex [i ].offset = ntohl (hl );
187
- rix -> revindex [i ].nr = i ;
146
+ p -> revindex [i ].offset = ntohl (hl );
147
+ p -> revindex [i ].nr = i ;
188
148
}
189
149
}
190
150
191
151
/* This knows the pack format -- the 20-byte trailer
192
152
* follows immediately after the last object data.
193
153
*/
194
- rix -> revindex [num_ent ].offset = p -> pack_size - 20 ;
195
- rix -> revindex [num_ent ].nr = -1 ;
196
- sort_revindex (rix -> revindex , num_ent , p -> pack_size );
154
+ p -> revindex [num_ent ].offset = p -> pack_size - 20 ;
155
+ p -> revindex [num_ent ].nr = -1 ;
156
+ sort_revindex (p -> revindex , num_ent , p -> pack_size );
197
157
}
198
158
199
- struct pack_revindex * revindex_for_pack (struct packed_git * p )
159
+ void load_pack_revindex (struct packed_git * p )
200
160
{
201
- int num ;
202
- struct pack_revindex * rix ;
203
-
204
- if (!pack_revindex_hashsz )
205
- init_pack_revindex ();
206
-
207
- num = pack_revindex_ix (p );
208
- if (num < 0 )
209
- die ("internal error: pack revindex fubar" );
210
-
211
- rix = & pack_revindex [num ];
212
- if (!rix -> revindex )
213
- create_pack_revindex (rix );
214
-
215
- return rix ;
161
+ if (!p -> revindex )
162
+ create_pack_revindex (p );
216
163
}
217
164
218
- int find_revindex_position (struct pack_revindex * pridx , off_t ofs )
165
+ int find_revindex_position (struct packed_git * p , off_t ofs )
219
166
{
220
167
int lo = 0 ;
221
- int hi = pridx -> p -> num_objects + 1 ;
222
- struct revindex_entry * revindex = pridx -> revindex ;
168
+ int hi = p -> num_objects + 1 ;
169
+ struct revindex_entry * revindex = p -> revindex ;
223
170
224
171
do {
225
172
unsigned mi = lo + (hi - lo ) / 2 ;
@@ -237,11 +184,13 @@ int find_revindex_position(struct pack_revindex *pridx, off_t ofs)
237
184
238
185
struct revindex_entry * find_pack_revindex (struct packed_git * p , off_t ofs )
239
186
{
240
- struct pack_revindex * pridx = revindex_for_pack (p );
241
- int pos = find_revindex_position (pridx , ofs );
187
+ int pos ;
188
+
189
+ load_pack_revindex (p );
190
+ pos = find_revindex_position (p , ofs );
242
191
243
192
if (pos < 0 )
244
193
return NULL ;
245
194
246
- return pridx -> revindex + pos ;
195
+ return p -> revindex + pos ;
247
196
}
0 commit comments