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 <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed Dec 3, 2020 at 15:59 UTC 43c1dccb91c0d56b0b00f1b452a1a7204c4242da
1 file changed +25 -1
merge-ort.c
+25 -1
@@ -58,6 +58,8 @@ struct merge_options_internal {
58 * * these keys serve to intern all the path strings, which allows
59 * us to do pointer comparison on directory names instead of
60 * 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.)
63 *
64 * The values of paths:
65 * * either a pointer to a merged_info, or a conflict_info struct
@@ -92,6 +94,16 @@ struct merge_options_internal {
94 */
95 struct strmap conflicted;
96
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 +
107 /*
108 * current_dir_name: temporary var used in collect_merge_info_callback()
109 *
@@ -249,6 +261,17 @@ static void clear_internal_opts(struct merge_options_internal *opti,
261 * don't free the keys and we pass 0 for free_values.
262 */
263 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;
275 }
276
277 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)
1266 * Although we initialize opt->priv->paths with strdup_strings=0,
1267 * that's just to avoid making yet another copy of an allocated
1268 * 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.
1270 *
1271 * In contrast, conflicted just has a subset of keys from paths, so
1272 * we don't want to free those (it'd be a duplicate free).
1273 */
1274 strmap_init_with_options(&opt->priv->paths, NULL, 0);
1275 strmap_init_with_options(&opt->priv->conflicted, NULL, 0);
1276 + string_list_init(&opt->priv->paths_to_free, 0);
1277 }
1278
1279 /*