diffcore-rename, merge-ort: add wrapper functions for filepair alloc/dealloc

We want to be able to allocate filespecs and filepairs using a mem_pool. However, filespec data will still remain outside the pool (perhaps in the future we could plumb the pool through the various diff APIs to allocate the filespec data too, but for now we are limiting the scope). Add some extra functions to allocate these appropriately based on the non-NULL-ness of opt->priv->pool, as well as some extra functions to handle correctly deallocating the relevant parts of them. A future commit will make use of these new functions. Signed-off-by: Elijah Newren <newren@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Elijah Newren committed Jul 30, 2021 at 11:47 UTC a8791ef6492bf702ba70352009cbd28fb581c6e2
3 files changed +85
diffcore-rename.c
+41
@@ -1328,6 +1328,47 @@ static void handle_early_known_dir_renames(struct dir_rename_info *info,
1328 rename_src_nr = new_num_src;
1329 }
1330
1331 +static void free_filespec_data(struct diff_filespec *spec)
1332 +{
1333 + if (!--spec->count)
1334 + diff_free_filespec_data(spec);
1335 +}
1336 +
1337 +MAYBE_UNUSED
1338 +static void pool_free_filespec(struct mem_pool *pool,
1339 + struct diff_filespec *spec)
1340 +{
1341 + if (!pool) {
1342 + free_filespec(spec);
1343 + return;
1344 + }
1345 +
1346 + /*
1347 + * Similar to free_filespec(), but only frees the data. The spec
1348 + * itself was allocated in the pool and should not be individually
1349 + * freed.
1350 + */
1351 + free_filespec_data(spec);
1352 +}
1353 +
1354 +MAYBE_UNUSED
1355 +void pool_diff_free_filepair(struct mem_pool *pool,
1356 + struct diff_filepair *p)
1357 +{
1358 + if (!pool) {
1359 + diff_free_filepair(p);
1360 + return;
1361 + }
1362 +
1363 + /*
1364 + * Similar to diff_free_filepair() but only frees the data from the
1365 + * filespecs; not the filespecs or the filepair which were
1366 + * allocated from the pool.
1367 + */
1368 + free_filespec_data(p->one);
1369 + free_filespec_data(p->two);
1370 +}
1371 +
1372 void diffcore_rename_extended(struct diff_options *options,
1373 struct strintmap *relevant_sources,
1374 struct strintmap *dirs_removed,
diffcore.h
+2
@@ -127,6 +127,8 @@ struct diff_filepair {
127 #define DIFF_PAIR_MODE_CHANGED(p) ((p)->one->mode != (p)->two->mode)
128
129 void diff_free_filepair(struct diff_filepair *);
130 +void pool_diff_free_filepair(struct mem_pool *pool,
131 + struct diff_filepair *p);
132
133 int diff_unmodified_pair(struct diff_filepair *);
134
merge-ort.c
+42
@@ -690,6 +690,48 @@ static void path_msg(struct merge_options *opt,
690 strbuf_addch(sb, '\n');
691 }
692
693 +MAYBE_UNUSED
694 +static struct diff_filespec *pool_alloc_filespec(struct mem_pool *pool,
695 + const char *path)
696 +{
697 + struct diff_filespec *spec;
698 + size_t len;
699 +
700 + if (!pool)
701 + return alloc_filespec(path);
702 +
703 + /* Same code as alloc_filespec, except allocate from pool */
704 + len = strlen(path);
705 +
706 + spec = mem_pool_calloc(pool, 1, st_add3(sizeof(*spec), len, 1));
707 + memcpy(spec+1, path, len);
708 + spec->path = (void*)(spec+1);
709 +
710 + spec->count = 1;
711 + spec->is_binary = -1;
712 + return spec;
713 +}
714 +
715 +MAYBE_UNUSED
716 +static struct diff_filepair *pool_diff_queue(struct mem_pool *pool,
717 + struct diff_queue_struct *queue,
718 + struct diff_filespec *one,
719 + struct diff_filespec *two)
720 +{
721 + struct diff_filepair *dp;
722 +
723 + if (!pool)
724 + return diff_queue(queue, one, two);
725 +
726 + /* Same code as diff_queue, except allocate from pool */
727 + dp = mem_pool_calloc(pool, 1, sizeof(*dp));
728 + dp->one = one;
729 + dp->two = two;
730 + if (queue)
731 + diff_q(queue, dp);
732 + return dp;
733 +}
734 +
735 static void *pool_calloc(struct mem_pool *pool, size_t count, size_t size)
736 {
737 if (!pool)