get rid of num_packed_objects()
The coming index format change doesn't allow for the number of objects to be determined from the size of the index file directly. Instead, Let's initialize a field in the packed_git structure with the object count when the index is validated since the count is always known at that point. While at it let's reorder some struct packed_git fields to avoid padding due to needed 64-bit alignment for some of them. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Nicolas Pitre committed
Apr 9, 2007 at 01:06 UTC
57059091fad25427bce9b3d47e073ce0518d164b
7 files changed
+17
-22
builtin-count-objects.c
+1
-1
@@ -111,7 +111,7 @@ int cmd_count_objects(int ac, const char **av, const char *prefix)
111
for (p = packed_git; p; p = p->next) {
112
if (!p->pack_local)
113
continue;
114
- packed += num_packed_objects(p);
114
+ packed += p->num_objects;
115
num_pack++;
116
}
117
printf("count: %lu\n", loose);
builtin-fsck.c
+1
-1
@@ -653,7 +653,7 @@ int cmd_fsck(int argc, char **argv, const char *prefix)
653
verify_pack(p, 0);
654
655
for (p = packed_git; p; p = p->next) {
656
- uint32_t i, num = num_packed_objects(p);
656
+ uint32_t i, num = p->num_objects;
657
for (i = 0; i < num; i++)
658
fsck_sha1(nth_packed_object_sha1(p, i));
659
}
builtin-pack-objects.c
+2
-2
@@ -164,7 +164,7 @@ static int cmp_offset(const void *a_, const void *b_)
164
static void prepare_pack_revindex(struct pack_revindex *rix)
165
{
166
struct packed_git *p = rix->p;
167
- int num_ent = num_packed_objects(p);
167
+ int num_ent = p->num_objects;
168
int i;
169
const char *index = p->index_data;
170
@@ -198,7 +198,7 @@ static struct revindex_entry * find_packed_object(struct packed_git *p,
198
prepare_pack_revindex(rix);
199
revindex = rix->revindex;
200
lo = 0;
201
- hi = num_packed_objects(p) + 1;
201
+ hi = p->num_objects + 1;
202
do {
203
int mi = (lo + hi) / 2;
204
if (revindex[mi].offset == ofs) {
cache.h
+4
-4
@@ -376,11 +376,12 @@ struct pack_window {
376
extern struct packed_git {
377
struct packed_git *next;
378
struct pack_window *windows;
379
- const void *index_data;
380
- off_t index_size;
379
off_t pack_size;
382
- time_t mtime;
380
+ const void *index_data;
381
+ size_t index_size;
382
+ uint32_t num_objects;
383
int index_version;
384
+ time_t mtime;
385
int pack_fd;
386
int pack_local;
387
unsigned char sha1[20];
@@ -431,7 +432,6 @@ extern void pack_report(void);
432
extern unsigned char* use_pack(struct packed_git *, struct pack_window **, off_t, unsigned int *);
433
extern void unuse_pack(struct pack_window **);
434
extern struct packed_git *add_packed_git(const char *, int, int);
434
-extern uint32_t num_packed_objects(const struct packed_git *p);
435
extern const unsigned char *nth_packed_object_sha1(const struct packed_git *, uint32_t);
436
extern off_t find_pack_entry_one(const unsigned char *, struct packed_git *);
437
extern void *unpack_entry(struct packed_git *, off_t, enum object_type *, unsigned long *);
pack-check.c
+2
-2
@@ -40,7 +40,7 @@ static int verify_packfile(struct packed_git *p,
40
* have verified that nr_objects matches between idx and pack,
41
* we do not do scan-streaming check on the pack file.
42
*/
43
- nr_objects = num_packed_objects(p);
43
+ nr_objects = p->num_objects;
44
for (i = 0, err = 0; i < nr_objects; i++) {
45
const unsigned char *sha1;
46
void *data;
@@ -79,7 +79,7 @@ static void show_pack_info(struct packed_git *p)
79
{
80
uint32_t nr_objects, i, chain_histogram[MAX_CHAIN];
81
82
- nr_objects = num_packed_objects(p);
82
+ nr_objects = p->num_objects;
83
memset(chain_histogram, 0, sizeof(chain_histogram));
84
85
for (i = 0; i < nr_objects; i++) {
sha1_file.c
+6
-11
@@ -494,6 +494,7 @@ static int check_packed_git_idx(const char *path, struct packed_git *p)
494
p->index_version = 1;
495
p->index_data = idx_map;
496
p->index_size = idx_size;
497
+ p->num_objects = nr;
498
return 0;
499
}
500
@@ -605,11 +606,11 @@ static int open_packed_git_1(struct packed_git *p)
606
p->pack_name, ntohl(hdr.hdr_version));
607
608
/* Verify the pack matches its index. */
608
- if (num_packed_objects(p) != ntohl(hdr.hdr_entries))
609
+ if (p->num_objects != ntohl(hdr.hdr_entries))
610
return error("packfile %s claims to have %u objects"
610
- " while index size indicates %u objects",
611
- p->pack_name, ntohl(hdr.hdr_entries),
612
- num_packed_objects(p));
611
+ " while index indicates %u objects",
612
+ p->pack_name, ntohl(hdr.hdr_entries),
613
+ p->num_objects);
614
if (lseek(p->pack_fd, p->pack_size - sizeof(sha1), SEEK_SET) == -1)
615
return error("end of packfile %s is unavailable", p->pack_name);
616
if (read_in_full(p->pack_fd, sha1, sizeof(sha1)) != sizeof(sha1))
@@ -1526,18 +1527,12 @@ void *unpack_entry(struct packed_git *p, off_t obj_offset,
1527
return data;
1528
}
1529
1529
-uint32_t num_packed_objects(const struct packed_git *p)
1530
-{
1531
- /* See check_packed_git_idx() */
1532
- return (uint32_t)((p->index_size - 20 - 20 - 4*256) / 24);
1533
-}
1534
-
1530
const unsigned char *nth_packed_object_sha1(const struct packed_git *p,
1531
uint32_t n)
1532
{
1533
const unsigned char *index = p->index_data;
1534
index += 4 * 256;
1540
- if (num_packed_objects(p) <= n)
1535
+ if (n >= p->num_objects)
1536
return NULL;
1537
return index + 24 * n + 4;
1538
}
sha1_name.c
+1
-1
@@ -76,7 +76,7 @@ static int find_short_packed_object(int len, const unsigned char *match, unsigne
76
77
prepare_packed_git();
78
for (p = packed_git; p && found < 2; p = p->next) {
79
- uint32_t num = num_packed_objects(p);
79
+ uint32_t num = p->num_objects;
80
uint32_t first = 0, last = num;
81
while (first < last) {
82
uint32_t mid = (first + last) / 2;