Skip to content

Commit 6c91e97

Browse files
committed
midx-write: use uint32_t for preferred_pack_idx
midx-write.c has the DISABLE_SIGN_COMPARE_WARNINGS macro defined for a few reasons, but the biggest one is the use of a signed preferred_pack_idx member inside the write_midx_context struct. The code currently uses -1 to indicate an unset preferred pack but pack int ids are normally handled as uint32_t. There are also a few loops that search for the preferred pack by name and those iterators will need updates to uint32_t in the next change. For now, replace the use of -1 with a 'NO_PREFERRED_PACK' macro and an equality check. The macro stores the max value of a uint32_t, so we cannot store a preferred pack that appears last in a list of 2^32 total packs, but that's expected to be unreasonable already. This improves the range from 2^31 already. There are some careful things to worry about with initializing the preferred pack in the struct and using that value when searching for a preferred pack that was already incorrect but accidentally working when the index was initialized to zero. Signed-off-by: Derrick Stolee <[email protected]>
1 parent 06429f5 commit 6c91e97

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

midx-write.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#define BITMAP_POS_UNKNOWN (~((uint32_t)0))
2525
#define MIDX_CHUNK_FANOUT_SIZE (sizeof(uint32_t) * 256)
2626
#define MIDX_CHUNK_LARGE_OFFSET_WIDTH (sizeof(uint64_t))
27+
#define NO_PREFERRED_PACK (~((uint32_t)0))
2728

2829
extern int midx_checksum_valid(struct multi_pack_index *m);
2930
extern void clear_midx_files_ext(const char *object_dir, const char *ext,
@@ -104,7 +105,7 @@ struct write_midx_context {
104105
unsigned large_offsets_needed:1;
105106
uint32_t num_large_offsets;
106107

107-
int preferred_pack_idx;
108+
uint32_t preferred_pack_idx;
108109

109110
int incremental;
110111
uint32_t num_multi_pack_indexes_before;
@@ -260,7 +261,7 @@ static void midx_fanout_sort(struct midx_fanout *fanout)
260261
static void midx_fanout_add_midx_fanout(struct midx_fanout *fanout,
261262
struct multi_pack_index *m,
262263
uint32_t cur_fanout,
263-
int preferred_pack)
264+
uint32_t preferred_pack)
264265
{
265266
uint32_t start = m->num_objects_in_base, end;
266267
uint32_t cur_object;
@@ -274,7 +275,7 @@ static void midx_fanout_add_midx_fanout(struct midx_fanout *fanout,
274275
end = m->num_objects_in_base + ntohl(m->chunk_oid_fanout[cur_fanout]);
275276

276277
for (cur_object = start; cur_object < end; cur_object++) {
277-
if ((preferred_pack > -1) &&
278+
if ((preferred_pack != NO_PREFERRED_PACK) &&
278279
(preferred_pack == nth_midxed_pack_int_id(m, cur_object))) {
279280
/*
280281
* Objects from preferred packs are added
@@ -364,7 +365,8 @@ static void compute_sorted_entries(struct write_midx_context *ctx,
364365
preferred, cur_fanout);
365366
}
366367

367-
if (-1 < ctx->preferred_pack_idx && ctx->preferred_pack_idx < start_pack)
368+
if (ctx->preferred_pack_idx != NO_PREFERRED_PACK &&
369+
ctx->preferred_pack_idx < start_pack)
368370
midx_fanout_add_pack_fanout(&fanout, ctx->info,
369371
ctx->preferred_pack_idx, 1,
370372
cur_fanout);
@@ -1042,7 +1044,9 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
10421044
struct hashfile *f = NULL;
10431045
struct lock_file lk;
10441046
struct tempfile *incr;
1045-
struct write_midx_context ctx = { 0 };
1047+
struct write_midx_context ctx = {
1048+
.preferred_pack_idx = NO_PREFERRED_PACK,
1049+
};
10461050
int bitmapped_packs_concat_len = 0;
10471051
int pack_name_concat_len = 0;
10481052
int dropped_packs = 0;
@@ -1150,7 +1154,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
11501154
goto cleanup; /* nothing to do */
11511155

11521156
if (preferred_pack_name) {
1153-
ctx.preferred_pack_idx = -1;
1157+
ctx.preferred_pack_idx = NO_PREFERRED_PACK;
11541158

11551159
for (i = 0; i < ctx.nr; i++) {
11561160
if (!cmp_idx_or_pack_name(preferred_pack_name,
@@ -1160,12 +1164,12 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
11601164
}
11611165
}
11621166

1163-
if (ctx.preferred_pack_idx == -1)
1167+
if (ctx.preferred_pack_idx == NO_PREFERRED_PACK)
11641168
warning(_("unknown preferred pack: '%s'"),
11651169
preferred_pack_name);
11661170
} else if (ctx.nr &&
11671171
(flags & (MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP))) {
1168-
struct packed_git *oldest = ctx.info[ctx.preferred_pack_idx].p;
1172+
struct packed_git *oldest = ctx.info[0].p;
11691173
ctx.preferred_pack_idx = 0;
11701174

11711175
if (packs_to_drop && packs_to_drop->nr)
@@ -1193,17 +1197,17 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
11931197
* objects to resolve, so the preferred value doesn't
11941198
* matter.
11951199
*/
1196-
ctx.preferred_pack_idx = -1;
1200+
ctx.preferred_pack_idx = NO_PREFERRED_PACK;
11971201
}
11981202
} else {
11991203
/*
12001204
* otherwise don't mark any pack as preferred to avoid
12011205
* interfering with expiration logic below
12021206
*/
1203-
ctx.preferred_pack_idx = -1;
1207+
ctx.preferred_pack_idx = NO_PREFERRED_PACK;
12041208
}
12051209

1206-
if (ctx.preferred_pack_idx > -1) {
1210+
if (ctx.preferred_pack_idx != NO_PREFERRED_PACK) {
12071211
struct packed_git *preferred = ctx.info[ctx.preferred_pack_idx].p;
12081212

12091213
if (open_pack_index(preferred))

0 commit comments

Comments
 (0)