Skip to content

Commit 43c1dcc

Browse files
newrengitster
authored andcommitted
merge-ort: add a paths_to_free field to merge_options_internal
This field will be used in future patches to allow removal of paths from opt->priv->paths. Signed-off-by: Elijah Newren <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 1c7873c commit 43c1dcc

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

merge-ort.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ struct merge_options_internal {
5858
* * these keys serve to intern all the path strings, which allows
5959
* us to do pointer comparison on directory names instead of
6060
* strcmp; we just have to be careful to use the interned strings.
61+
* (Technically paths_to_free may track some strings that were
62+
* removed from froms paths.)
6163
*
6264
* The values of paths:
6365
* * either a pointer to a merged_info, or a conflict_info struct
@@ -92,6 +94,16 @@ struct merge_options_internal {
9294
*/
9395
struct strmap conflicted;
9496

97+
/*
98+
* paths_to_free: additional list of strings to free
99+
*
100+
* If keys are removed from "paths", they are added to paths_to_free
101+
* to ensure they are later freed. We avoid free'ing immediately since
102+
* other places (e.g. conflict_info.pathnames[]) may still be
103+
* referencing these paths.
104+
*/
105+
struct string_list paths_to_free;
106+
95107
/*
96108
* current_dir_name: temporary var used in collect_merge_info_callback()
97109
*
@@ -249,6 +261,17 @@ static void clear_internal_opts(struct merge_options_internal *opti,
249261
* don't free the keys and we pass 0 for free_values.
250262
*/
251263
strmap_clear(&opti->conflicted, 0);
264+
265+
/*
266+
* opti->paths_to_free is similar to opti->paths; we created it with
267+
* strdup_strings = 0 to avoid making _another_ copy of the fullpath
268+
* but now that we've used it and have no other references to these
269+
* strings, it is time to deallocate them. We do so by temporarily
270+
* setting strdup_strings to 1.
271+
*/
272+
opti->paths_to_free.strdup_strings = 1;
273+
string_list_clear(&opti->paths_to_free, 0);
274+
opti->paths_to_free.strdup_strings = 0;
252275
}
253276

254277
static int err(struct merge_options *opt, const char *err, ...)
@@ -1243,13 +1266,14 @@ static void merge_start(struct merge_options *opt, struct merge_result *result)
12431266
* Although we initialize opt->priv->paths with strdup_strings=0,
12441267
* that's just to avoid making yet another copy of an allocated
12451268
* string. Putting the entry into paths means we are taking
1246-
* ownership, so we will later free it.
1269+
* ownership, so we will later free it. paths_to_free is similar.
12471270
*
12481271
* In contrast, conflicted just has a subset of keys from paths, so
12491272
* we don't want to free those (it'd be a duplicate free).
12501273
*/
12511274
strmap_init_with_options(&opt->priv->paths, NULL, 0);
12521275
strmap_init_with_options(&opt->priv->conflicted, NULL, 0);
1276+
string_list_init(&opt->priv->paths_to_free, 0);
12531277
}
12541278

12551279
/*

0 commit comments

Comments
 (0)