Skip to content

Commit 194db7c

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 49b6afa commit 194db7c

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);
@@ -1060,7 +1062,9 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
10601062
struct hashfile *f = NULL;
10611063
struct lock_file lk;
10621064
struct tempfile *incr;
1063-
struct write_midx_context ctx = { 0 };
1065+
struct write_midx_context ctx = {
1066+
.preferred_pack_idx = NO_PREFERRED_PACK,
1067+
};
10641068
int bitmapped_packs_concat_len = 0;
10651069
int pack_name_concat_len = 0;
10661070
int dropped_packs = 0;
@@ -1168,7 +1172,7 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
11681172
goto cleanup; /* nothing to do */
11691173

11701174
if (preferred_pack_name) {
1171-
ctx.preferred_pack_idx = -1;
1175+
ctx.preferred_pack_idx = NO_PREFERRED_PACK;
11721176

11731177
for (i = 0; i < ctx.nr; i++) {
11741178
if (!cmp_idx_or_pack_name(preferred_pack_name,
@@ -1178,12 +1182,12 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
11781182
}
11791183
}
11801184

1181-
if (ctx.preferred_pack_idx == -1)
1185+
if (ctx.preferred_pack_idx == NO_PREFERRED_PACK)
11821186
warning(_("unknown preferred pack: '%s'"),
11831187
preferred_pack_name);
11841188
} else if (ctx.nr &&
11851189
(flags & (MIDX_WRITE_REV_INDEX | MIDX_WRITE_BITMAP))) {
1186-
struct packed_git *oldest = ctx.info[ctx.preferred_pack_idx].p;
1190+
struct packed_git *oldest = ctx.info[0].p;
11871191
ctx.preferred_pack_idx = 0;
11881192

11891193
if (packs_to_drop && packs_to_drop->nr)
@@ -1211,17 +1215,17 @@ static int write_midx_internal(struct repository *r, const char *object_dir,
12111215
* objects to resolve, so the preferred value doesn't
12121216
* matter.
12131217
*/
1214-
ctx.preferred_pack_idx = -1;
1218+
ctx.preferred_pack_idx = NO_PREFERRED_PACK;
12151219
}
12161220
} else {
12171221
/*
12181222
* otherwise don't mark any pack as preferred to avoid
12191223
* interfering with expiration logic below
12201224
*/
1221-
ctx.preferred_pack_idx = -1;
1225+
ctx.preferred_pack_idx = NO_PREFERRED_PACK;
12221226
}
12231227

1224-
if (ctx.preferred_pack_idx > -1) {
1228+
if (ctx.preferred_pack_idx != NO_PREFERRED_PACK) {
12251229
struct packed_git *preferred = ctx.info[ctx.preferred_pack_idx].p;
12261230

12271231
if (open_pack_index(preferred))

0 commit comments

Comments
 (0)