Use uint32_t for pack-objects counters.
As we technically try to support up to a maximum of 2**32-1 objects in a single packfile we should act like it and use unsigned 32 bit integers for all of our object counts and progress output. This change does not modify everything in pack-objects that probably needs to change to fully support the maximum of 2**32-1 objects. I'm intentionally breaking the improvements into slightly smaller commits to make them easier to follow. No logic change should be occuring here, with the exception that some comparsions will now work properly when the number of objects exceeds 2**31-1. Signed-off-by: Shawn O. Pearce <spearce@spearce.org> Signed-off-by: Junio C Hamano <junkio@cox.net>
Shawn O. Pearce committed
Mar 6, 2007 at 20:44 UTC
7cadf491c6d7a29e345e1608c23b0d98cee5e848
1 file changed
+30
-33
builtin-pack-objects.c
+30
-33
@@ -68,7 +68,7 @@ static int allow_ofs_delta;
68
69
static struct object_entry **sorted_by_sha, **sorted_by_type;
70
static struct object_entry *objects;
71
-static int nr_objects, nr_alloc, nr_result;
71
+static uint32_t nr_objects, nr_alloc, nr_result;
72
static const char *base_name;
73
static unsigned char pack_file_sha1[20];
74
static int progress = 1;
@@ -114,10 +114,8 @@ static int pack_revindex_hashsz;
114
/*
115
* stats
116
*/
117
-static int written;
118
-static int written_delta;
119
-static int reused;
120
-static int reused_delta;
117
+static uint32_t written, written_delta;
118
+static uint32_t reused, reused_delta;
119
120
static int pack_revindex_ix(struct packed_git *p)
121
{
@@ -518,7 +516,7 @@ static unsigned long write_one(struct sha1file *f,
516
517
static void write_pack_file(void)
518
{
521
- int i;
519
+ uint32_t i;
520
struct sha1file *f;
521
unsigned long offset;
522
struct pack_header hdr;
@@ -533,7 +531,7 @@ static void write_pack_file(void)
531
f = sha1create("%s-%s.%s", base_name,
532
sha1_to_hex(object_list_sha1), "pack");
533
if (do_progress)
536
- fprintf(stderr, "Writing %d objects.\n", nr_result);
534
+ fprintf(stderr, "Writing %u objects.\n", nr_result);
535
536
hdr.hdr_signature = htonl(PACK_SIGNATURE);
537
hdr.hdr_version = htonl(PACK_VERSION);
@@ -558,13 +556,13 @@ static void write_pack_file(void)
556
fputc('\n', stderr);
557
done:
558
if (written != nr_result)
561
- die("wrote %d objects while expecting %d", written, nr_result);
559
+ die("wrote %u objects while expecting %u", written, nr_result);
560
sha1close(f, pack_file_sha1, 1);
561
}
562
563
static void write_index_file(void)
564
{
567
- int i;
565
+ uint32_t i;
566
struct sha1file *f = sha1create("%s-%s.%s", base_name,
567
sha1_to_hex(object_list_sha1), "idx");
568
struct object_entry **list = sorted_by_sha;
@@ -633,7 +631,7 @@ static struct object_entry *locate_object_entry(const unsigned char *sha1)
631
632
static void rehash_objects(void)
633
{
636
- int i;
634
+ uint32_t i;
635
struct object_entry *oe;
636
637
object_ix_hashsz = nr_objects * 3;
@@ -670,7 +668,7 @@ static unsigned name_hash(const char *name)
668
669
static int add_object_entry(const unsigned char *sha1, unsigned hash, int exclude)
670
{
673
- unsigned int idx = nr_objects;
671
+ uint32_t idx = nr_objects;
672
struct object_entry *entry;
673
struct packed_git *p;
674
unsigned int found_offset = 0;
@@ -696,9 +694,8 @@ static int add_object_entry(const unsigned char *sha1, unsigned hash, int exclud
694
goto already_added;
695
696
if (idx >= nr_alloc) {
699
- unsigned int needed = (idx + 1024) * 3 / 2;
700
- objects = xrealloc(objects, needed * sizeof(*entry));
701
- nr_alloc = needed;
697
+ nr_alloc = (idx + 1024) * 3 / 2;
698
+ objects = xrealloc(objects, nr_alloc * sizeof(*entry));
699
}
700
entry = objects + idx;
701
nr_objects = idx + 1;
@@ -718,7 +715,7 @@ static int add_object_entry(const unsigned char *sha1, unsigned hash, int exclud
715
716
already_added:
717
if (progress_update) {
721
- fprintf(stderr, "Counting objects...%d\r", nr_objects);
718
+ fprintf(stderr, "Counting objects...%u\r", nr_objects);
719
progress_update = 0;
720
}
721
if (exclude)
@@ -1081,7 +1078,7 @@ static unsigned int check_delta_limit(struct object_entry *me, unsigned int n)
1078
1079
static void get_object_details(void)
1080
{
1084
- int i;
1081
+ uint32_t i;
1082
struct object_entry *entry;
1083
1084
prepare_pack_ix();
@@ -1120,7 +1117,7 @@ static int sort_comparator(const void *_a, const void *_b)
1117
static struct object_entry **create_sorted_list(entry_sort_t sort)
1118
{
1119
struct object_entry **list = xmalloc(nr_objects * sizeof(struct object_entry *));
1123
- int i;
1120
+ uint32_t i;
1121
1122
for (i = 0; i < nr_objects; i++)
1123
list[i] = objects + i;
@@ -1137,7 +1134,7 @@ static int sha1_sort(const struct object_entry *a, const struct object_entry *b)
1134
static struct object_entry **create_final_object_list(void)
1135
{
1136
struct object_entry **list;
1140
- int i, j;
1137
+ uint32_t i, j;
1138
1139
for (i = nr_result = 0; i < nr_objects; i++)
1140
if (!objects[i].preferred_base)
@@ -1279,20 +1276,20 @@ static void progress_interval(int signum)
1276
1277
static void find_deltas(struct object_entry **list, int window, int depth)
1278
{
1282
- int i, idx;
1279
+ uint32_t i = nr_objects, idx = 0, processed = 0;
1280
unsigned int array_size = window * sizeof(struct unpacked);
1284
- struct unpacked *array = xmalloc(array_size);
1285
- unsigned processed = 0;
1281
+ struct unpacked *array;
1282
unsigned last_percent = 999;
1283
1284
+ if (!nr_objects)
1285
+ return;
1286
+ array = xmalloc(array_size);
1287
memset(array, 0, array_size);
1289
- i = nr_objects;
1290
- idx = 0;
1288
if (progress)
1292
- fprintf(stderr, "Deltifying %d objects.\n", nr_result);
1289
+ fprintf(stderr, "Deltifying %u objects.\n", nr_result);
1290
1294
- while (--i >= 0) {
1295
- struct object_entry *entry = list[i];
1291
+ do {
1292
+ struct object_entry *entry = list[--i];
1293
struct unpacked *n = array + idx;
1294
int j;
1295
@@ -1325,7 +1322,7 @@ static void find_deltas(struct object_entry **list, int window, int depth)
1322
1323
j = window;
1324
while (--j > 0) {
1328
- unsigned int other_idx = idx + j;
1325
+ uint32_t other_idx = idx + j;
1326
struct unpacked *m;
1327
if (other_idx >= window)
1328
other_idx -= window;
@@ -1345,7 +1342,7 @@ static void find_deltas(struct object_entry **list, int window, int depth)
1342
idx++;
1343
if (idx >= window)
1344
idx = 0;
1348
- }
1345
+ } while (i > 0);
1346
1347
if (progress)
1348
fputc('\n', stderr);
@@ -1386,7 +1383,7 @@ static int reuse_cached_pack(unsigned char *sha1)
1383
}
1384
1385
if (progress)
1389
- fprintf(stderr, "Reusing %d objects pack %s\n", nr_objects,
1386
+ fprintf(stderr, "Reusing %u objects pack %s\n", nr_objects,
1387
sha1_to_hex(sha1));
1388
1389
if (pack_to_stdout) {
@@ -1537,7 +1534,7 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
1534
struct object_entry **list;
1535
int use_internal_rev_list = 0;
1536
int thin = 0;
1540
- int i;
1537
+ uint32_t i;
1538
const char **rp_av;
1539
int rp_ac_alloc = 64;
1540
int rp_ac;
@@ -1670,7 +1667,7 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
1667
}
1668
1669
if (progress)
1673
- fprintf(stderr, "Done counting %d objects.\n", nr_objects);
1670
+ fprintf(stderr, "Done counting %u objects.\n", nr_objects);
1671
sorted_by_sha = create_final_object_list();
1672
if (non_empty && !nr_result)
1673
return 0;
@@ -1683,7 +1680,7 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
1680
}
1681
SHA1_Final(object_list_sha1, &ctx);
1682
if (progress && (nr_objects != nr_result))
1686
- fprintf(stderr, "Result has %d objects.\n", nr_result);
1683
+ fprintf(stderr, "Result has %u objects.\n", nr_result);
1684
1685
if (reuse_cached_pack(object_list_sha1))
1686
;
@@ -1704,7 +1701,7 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
1701
}
1702
}
1703
if (progress)
1707
- fprintf(stderr, "Total %d (delta %d), reused %d (delta %d)\n",
1704
+ fprintf(stderr, "Total %u (delta %u), reused %u (delta %u)\n",
1705
written, written_delta, reused, reused_delta);
1706
return 0;
1707
}