Skip to content

Commit 7a3ada1

Browse files
derrickstoleegitster
authored andcommitted
midx: add pack_perm to write_midx_context
In an effort to align write_midx_internal() with the chunk-format API, continue to group necessary data into "struct write_midx_context". This change collects the "uint32_t *pack_perm" and large_offsets_needed bit into the context. Update write_midx_object_offsets() to match chunk_write_fn. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 31bda9a commit 7a3ada1

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

midx.c

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,9 @@ struct write_midx_context {
461461

462462
struct pack_midx_entry *entries;
463463
uint32_t entries_nr;
464+
465+
uint32_t *pack_perm;
466+
unsigned large_offsets_needed:1;
464467
};
465468

466469
static void add_pack_to_midx(const char *full_path, size_t full_path_len,
@@ -736,27 +739,27 @@ static size_t write_midx_oid_lookup(struct hashfile *f,
736739
return written;
737740
}
738741

739-
static size_t write_midx_object_offsets(struct hashfile *f, int large_offset_needed,
740-
uint32_t *perm,
741-
struct pack_midx_entry *objects, uint32_t nr_objects)
742+
static size_t write_midx_object_offsets(struct hashfile *f,
743+
void *data)
742744
{
743-
struct pack_midx_entry *list = objects;
745+
struct write_midx_context *ctx = data;
746+
struct pack_midx_entry *list = ctx->entries;
744747
uint32_t i, nr_large_offset = 0;
745748
size_t written = 0;
746749

747-
for (i = 0; i < nr_objects; i++) {
750+
for (i = 0; i < ctx->entries_nr; i++) {
748751
struct pack_midx_entry *obj = list++;
749752

750-
if (perm[obj->pack_int_id] == PACK_EXPIRED)
753+
if (ctx->pack_perm[obj->pack_int_id] == PACK_EXPIRED)
751754
BUG("object %s is in an expired pack with int-id %d",
752755
oid_to_hex(&obj->oid),
753756
obj->pack_int_id);
754757

755-
hashwrite_be32(f, perm[obj->pack_int_id]);
758+
hashwrite_be32(f, ctx->pack_perm[obj->pack_int_id]);
756759

757-
if (large_offset_needed && obj->offset >> 31)
760+
if (ctx->large_offsets_needed && obj->offset >> 31)
758761
hashwrite_be32(f, MIDX_LARGE_OFFSET_NEEDED | nr_large_offset++);
759-
else if (!large_offset_needed && obj->offset >> 32)
762+
else if (!ctx->large_offsets_needed && obj->offset >> 32)
760763
BUG("object %s requires a large offset (%"PRIx64") but the MIDX is not writing large offsets!",
761764
oid_to_hex(&obj->oid),
762765
obj->offset);
@@ -805,13 +808,11 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
805808
struct hashfile *f = NULL;
806809
struct lock_file lk;
807810
struct write_midx_context ctx = { 0 };
808-
uint32_t *pack_perm = NULL;
809811
uint64_t written = 0;
810812
uint32_t chunk_ids[MIDX_MAX_CHUNKS + 1];
811813
uint64_t chunk_offsets[MIDX_MAX_CHUNKS + 1];
812814
uint32_t num_large_offsets = 0;
813815
struct progress *progress = NULL;
814-
int large_offsets_needed = 0;
815816
int pack_name_concat_len = 0;
816817
int dropped_packs = 0;
817818
int result = 0;
@@ -857,11 +858,12 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
857858

858859
ctx.entries = get_sorted_entries(ctx.m, ctx.info, ctx.nr, &ctx.entries_nr);
859860

861+
ctx.large_offsets_needed = 0;
860862
for (i = 0; i < ctx.entries_nr; i++) {
861863
if (ctx.entries[i].offset > 0x7fffffff)
862864
num_large_offsets++;
863865
if (ctx.entries[i].offset > 0xffffffff)
864-
large_offsets_needed = 1;
866+
ctx.large_offsets_needed = 1;
865867
}
866868

867869
QSORT(ctx.info, ctx.nr, pack_info_compare);
@@ -900,13 +902,13 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
900902
*
901903
* pack_perm[old_id] = new_id
902904
*/
903-
ALLOC_ARRAY(pack_perm, ctx.nr);
905+
ALLOC_ARRAY(ctx.pack_perm, ctx.nr);
904906
for (i = 0; i < ctx.nr; i++) {
905907
if (ctx.info[i].expired) {
906908
dropped_packs++;
907-
pack_perm[ctx.info[i].orig_pack_int_id] = PACK_EXPIRED;
909+
ctx.pack_perm[ctx.info[i].orig_pack_int_id] = PACK_EXPIRED;
908910
} else {
909-
pack_perm[ctx.info[i].orig_pack_int_id] = i - dropped_packs;
911+
ctx.pack_perm[ctx.info[i].orig_pack_int_id] = i - dropped_packs;
910912
}
911913
}
912914

@@ -927,7 +929,7 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
927929
close_midx(ctx.m);
928930

929931
cur_chunk = 0;
930-
num_chunks = large_offsets_needed ? 5 : 4;
932+
num_chunks = ctx.large_offsets_needed ? 5 : 4;
931933

932934
if (ctx.nr - dropped_packs == 0) {
933935
error(_("no pack files to index."));
@@ -954,7 +956,7 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
954956

955957
cur_chunk++;
956958
chunk_offsets[cur_chunk] = chunk_offsets[cur_chunk - 1] + ctx.entries_nr * MIDX_CHUNK_OFFSET_WIDTH;
957-
if (large_offsets_needed) {
959+
if (ctx.large_offsets_needed) {
958960
chunk_ids[cur_chunk] = MIDX_CHUNKID_LARGEOFFSETS;
959961

960962
cur_chunk++;
@@ -1004,7 +1006,7 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
10041006
break;
10051007

10061008
case MIDX_CHUNKID_OBJECTOFFSETS:
1007-
written += write_midx_object_offsets(f, large_offsets_needed, pack_perm, ctx.entries, ctx.entries_nr);
1009+
written += write_midx_object_offsets(f, &ctx);
10081010
break;
10091011

10101012
case MIDX_CHUNKID_LARGEOFFSETS:
@@ -1039,7 +1041,7 @@ static int write_midx_internal(const char *object_dir, struct multi_pack_index *
10391041

10401042
free(ctx.info);
10411043
free(ctx.entries);
1042-
free(pack_perm);
1044+
free(ctx.pack_perm);
10431045
free(midx_name);
10441046
return result;
10451047
}

0 commit comments

Comments
 (0)