Skip to content

Commit 99d5197

Browse files
ttaylorrgitster
authored andcommitted
repack: move pack_geometry struct to the stack
The `pack_geometry` struct is used to maintain and partition a list of packfiles into a "frozen" set (to be left alone), and a non-frozen set (to be combined into a single new pack). In the previous commit, we removed a leak caused by neglecting to free() the heap allocated space used to store the structure itself. But there is no need for this structure to live on the heap anyway. Instead, let's move it to be stack allocated, eliminating the possibility of a direct leak like the one addressed in the previous patch. The one minor hitch is that we use the NULL-ness of the pack_geometry's struct pointer to determine whether or not we are performing a geometric repack with `--geometric=<d>`. But since we only initialize the pack_geometry structure when the `geometric_factor` is non-zero, we can use that variable (based on whether or not it is equal to zero) to determine whether or not we are performing a geometric repack. There are a couple of spots that have access to a pointer to the pack_geometry struct, but not the geometric_factor itself. Instead of passing in an additional variable, let's make the geometric_factor a field of the pack_geometry struct. Signed-off-by: Taylor Blau <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cb888bb commit 99d5197

File tree

1 file changed

+31
-31
lines changed

1 file changed

+31
-31
lines changed

builtin/repack.c

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ struct pack_geometry {
303303
struct packed_git **pack;
304304
uint32_t pack_nr, pack_alloc;
305305
uint32_t split;
306+
307+
int split_factor;
306308
};
307309

308310
static uint32_t geometry_pack_weight(struct packed_git *p)
@@ -324,17 +326,13 @@ static int geometry_cmp(const void *va, const void *vb)
324326
return 0;
325327
}
326328

327-
static void init_pack_geometry(struct pack_geometry **geometry_p,
329+
static void init_pack_geometry(struct pack_geometry *geometry,
328330
struct string_list *existing_kept_packs,
329331
const struct pack_objects_args *args)
330332
{
331333
struct packed_git *p;
332-
struct pack_geometry *geometry;
333334
struct strbuf buf = STRBUF_INIT;
334335

335-
*geometry_p = xcalloc(1, sizeof(struct pack_geometry));
336-
geometry = *geometry_p;
337-
338336
for (p = get_all_packs(the_repository); p; p = p->next) {
339337
if (args->local && !p->pack_local)
340338
/*
@@ -380,7 +378,7 @@ static void init_pack_geometry(struct pack_geometry **geometry_p,
380378
strbuf_release(&buf);
381379
}
382380

383-
static void split_pack_geometry(struct pack_geometry *geometry, int factor)
381+
static void split_pack_geometry(struct pack_geometry *geometry)
384382
{
385383
uint32_t i;
386384
uint32_t split;
@@ -399,12 +397,14 @@ static void split_pack_geometry(struct pack_geometry *geometry, int factor)
399397
struct packed_git *ours = geometry->pack[i];
400398
struct packed_git *prev = geometry->pack[i - 1];
401399

402-
if (unsigned_mult_overflows(factor, geometry_pack_weight(prev)))
400+
if (unsigned_mult_overflows(geometry->split_factor,
401+
geometry_pack_weight(prev)))
403402
die(_("pack %s too large to consider in geometric "
404403
"progression"),
405404
prev->pack_name);
406405

407-
if (geometry_pack_weight(ours) < factor * geometry_pack_weight(prev))
406+
if (geometry_pack_weight(ours) <
407+
geometry->split_factor * geometry_pack_weight(prev))
408408
break;
409409
}
410410

@@ -439,10 +439,12 @@ static void split_pack_geometry(struct pack_geometry *geometry, int factor)
439439
for (i = split; i < geometry->pack_nr; i++) {
440440
struct packed_git *ours = geometry->pack[i];
441441

442-
if (unsigned_mult_overflows(factor, total_size))
442+
if (unsigned_mult_overflows(geometry->split_factor,
443+
total_size))
443444
die(_("pack %s too large to roll up"), ours->pack_name);
444445

445-
if (geometry_pack_weight(ours) < factor * total_size) {
446+
if (geometry_pack_weight(ours) <
447+
geometry->split_factor * total_size) {
446448
if (unsigned_add_overflows(total_size,
447449
geometry_pack_weight(ours)))
448450
die(_("pack %s too large to roll up"),
@@ -498,7 +500,6 @@ static void free_pack_geometry(struct pack_geometry *geometry)
498500
return;
499501

500502
free(geometry->pack);
501-
free(geometry);
502503
}
503504

504505
struct midx_snapshot_ref_data {
@@ -575,7 +576,7 @@ static void midx_included_packs(struct string_list *include,
575576
string_list_insert(include, xstrfmt("%s.idx", item->string));
576577
for_each_string_list_item(item, names)
577578
string_list_insert(include, xstrfmt("pack-%s.idx", item->string));
578-
if (geometry) {
579+
if (geometry->split_factor) {
579580
struct strbuf buf = STRBUF_INIT;
580581
uint32_t i;
581582
for (i = geometry->split; i < geometry->pack_nr; i++) {
@@ -779,7 +780,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
779780
struct string_list names = STRING_LIST_INIT_DUP;
780781
struct string_list existing_nonkept_packs = STRING_LIST_INIT_DUP;
781782
struct string_list existing_kept_packs = STRING_LIST_INIT_DUP;
782-
struct pack_geometry *geometry = NULL;
783+
struct pack_geometry geometry = { 0 };
783784
struct strbuf line = STRBUF_INIT;
784785
struct tempfile *refs_snapshot = NULL;
785786
int i, ext, ret;
@@ -793,7 +794,6 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
793794
struct string_list keep_pack_list = STRING_LIST_INIT_NODUP;
794795
struct pack_objects_args po_args = {NULL};
795796
struct pack_objects_args cruft_po_args = {NULL};
796-
int geometric_factor = 0;
797797
int write_midx = 0;
798798
const char *cruft_expiration = NULL;
799799
const char *expire_to = NULL;
@@ -842,7 +842,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
842842
N_("repack objects in packs marked with .keep")),
843843
OPT_STRING_LIST(0, "keep-pack", &keep_pack_list, N_("name"),
844844
N_("do not repack this pack")),
845-
OPT_INTEGER('g', "geometric", &geometric_factor,
845+
OPT_INTEGER('g', "geometric", &geometry.split_factor,
846846
N_("find a geometric progression with factor <N>")),
847847
OPT_BOOL('m', "write-midx", &write_midx,
848848
N_("write a multi-pack index of the resulting packs")),
@@ -918,11 +918,11 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
918918
collect_pack_filenames(&existing_nonkept_packs, &existing_kept_packs,
919919
&keep_pack_list);
920920

921-
if (geometric_factor) {
921+
if (geometry.split_factor) {
922922
if (pack_everything)
923923
die(_("options '%s' and '%s' cannot be used together"), "--geometric", "-A/-a");
924924
init_pack_geometry(&geometry, &existing_kept_packs, &po_args);
925-
split_pack_geometry(geometry, geometric_factor);
925+
split_pack_geometry(&geometry);
926926
}
927927

928928
prepare_pack_objects(&cmd, &po_args, packtmp);
@@ -936,7 +936,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
936936
strvec_pushf(&cmd.args, "--keep-pack=%s",
937937
keep_pack_list.items[i].string);
938938
strvec_push(&cmd.args, "--non-empty");
939-
if (!geometry) {
939+
if (!geometry.split_factor) {
940940
/*
941941
* We need to grab all reachable objects, including those that
942942
* are reachable from reflogs and the index.
@@ -983,15 +983,15 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
983983
strvec_push(&cmd.args, "--pack-loose-unreachable");
984984
}
985985
}
986-
} else if (geometry) {
986+
} else if (geometry.split_factor) {
987987
strvec_push(&cmd.args, "--stdin-packs");
988988
strvec_push(&cmd.args, "--unpacked");
989989
} else {
990990
strvec_push(&cmd.args, "--unpacked");
991991
strvec_push(&cmd.args, "--incremental");
992992
}
993993

994-
if (geometry)
994+
if (geometry.split_factor)
995995
cmd.in = -1;
996996
else
997997
cmd.no_stdin = 1;
@@ -1000,17 +1000,17 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
10001000
if (ret)
10011001
goto cleanup;
10021002

1003-
if (geometry) {
1003+
if (geometry.split_factor) {
10041004
FILE *in = xfdopen(cmd.in, "w");
10051005
/*
10061006
* The resulting pack should contain all objects in packs that
10071007
* are going to be rolled up, but exclude objects in packs which
10081008
* are being left alone.
10091009
*/
1010-
for (i = 0; i < geometry->split; i++)
1011-
fprintf(in, "%s\n", pack_basename(geometry->pack[i]));
1012-
for (i = geometry->split; i < geometry->pack_nr; i++)
1013-
fprintf(in, "^%s\n", pack_basename(geometry->pack[i]));
1010+
for (i = 0; i < geometry.split; i++)
1011+
fprintf(in, "%s\n", pack_basename(geometry.pack[i]));
1012+
for (i = geometry.split; i < geometry.pack_nr; i++)
1013+
fprintf(in, "^%s\n", pack_basename(geometry.pack[i]));
10141014
fclose(in);
10151015
}
10161016

@@ -1153,9 +1153,9 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
11531153
if (write_midx) {
11541154
struct string_list include = STRING_LIST_INIT_NODUP;
11551155
midx_included_packs(&include, &existing_nonkept_packs,
1156-
&existing_kept_packs, &names, geometry);
1156+
&existing_kept_packs, &names, &geometry);
11571157

1158-
ret = write_midx_included_packs(&include, geometry,
1158+
ret = write_midx_included_packs(&include, &geometry,
11591159
refs_snapshot ? get_tempfile_path(refs_snapshot) : NULL,
11601160
show_progress, write_bitmaps > 0);
11611161

@@ -1178,12 +1178,12 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
11781178
remove_redundant_pack(packdir, item->string);
11791179
}
11801180

1181-
if (geometry) {
1181+
if (geometry.split_factor) {
11821182
struct strbuf buf = STRBUF_INIT;
11831183

11841184
uint32_t i;
1185-
for (i = 0; i < geometry->split; i++) {
1186-
struct packed_git *p = geometry->pack[i];
1185+
for (i = 0; i < geometry.split; i++) {
1186+
struct packed_git *p = geometry.pack[i];
11871187
if (string_list_has_string(&names,
11881188
hash_to_hex(p->hash)))
11891189
continue;
@@ -1226,7 +1226,7 @@ int cmd_repack(int argc, const char **argv, const char *prefix)
12261226
string_list_clear(&names, 1);
12271227
string_list_clear(&existing_nonkept_packs, 0);
12281228
string_list_clear(&existing_kept_packs, 0);
1229-
free_pack_geometry(geometry);
1229+
free_pack_geometry(&geometry);
12301230

12311231
return ret;
12321232
}

0 commit comments

Comments
 (0)