summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorElijah Newren <newren@gmail.com>2021-07-30 11:47:41 (GMT)
committerJunio C Hamano <gitster@pobox.com>2021-07-30 16:01:19 (GMT)
commita8791ef6492bf702ba70352009cbd28fb581c6e2 (patch)
tree069fb2c73de4a9df8c8f0b973169e60f151ccab3
parent6697ee01b5d31df5c83ace9eabb6285cf42ff172 (diff)
downloadgit-a8791ef6492bf702ba70352009cbd28fb581c6e2.zip
git-a8791ef6492bf702ba70352009cbd28fb581c6e2.tar.gz
git-a8791ef6492bf702ba70352009cbd28fb581c6e2.tar.bz2
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>
-rw-r--r--diffcore-rename.c41
-rw-r--r--diffcore.h2
-rw-r--r--merge-ort.c42
3 files changed, 85 insertions, 0 deletions
diff --git a/diffcore-rename.c b/diffcore-rename.c
index 73d8840..5bc559f 100644
--- a/diffcore-rename.c
+++ b/diffcore-rename.c
@@ -1328,6 +1328,47 @@ static void handle_early_known_dir_renames(struct dir_rename_info *info,
rename_src_nr = new_num_src;
}
+static void free_filespec_data(struct diff_filespec *spec)
+{
+ if (!--spec->count)
+ diff_free_filespec_data(spec);
+}
+
+MAYBE_UNUSED
+static void pool_free_filespec(struct mem_pool *pool,
+ struct diff_filespec *spec)
+{
+ if (!pool) {
+ free_filespec(spec);
+ return;
+ }
+
+ /*
+ * Similar to free_filespec(), but only frees the data. The spec
+ * itself was allocated in the pool and should not be individually
+ * freed.
+ */
+ free_filespec_data(spec);
+}
+
+MAYBE_UNUSED
+void pool_diff_free_filepair(struct mem_pool *pool,
+ struct diff_filepair *p)
+{
+ if (!pool) {
+ diff_free_filepair(p);
+ return;
+ }
+
+ /*
+ * Similar to diff_free_filepair() but only frees the data from the
+ * filespecs; not the filespecs or the filepair which were
+ * allocated from the pool.
+ */
+ free_filespec_data(p->one);
+ free_filespec_data(p->two);
+}
+
void diffcore_rename_extended(struct diff_options *options,
struct strintmap *relevant_sources,
struct strintmap *dirs_removed,
diff --git a/diffcore.h b/diffcore.h
index 533b30e..b58ee6b 100644
--- a/diffcore.h
+++ b/diffcore.h
@@ -127,6 +127,8 @@ struct diff_filepair {
#define DIFF_PAIR_MODE_CHANGED(p) ((p)->one->mode != (p)->two->mode)
void diff_free_filepair(struct diff_filepair *);
+void pool_diff_free_filepair(struct mem_pool *pool,
+ struct diff_filepair *p);
int diff_unmodified_pair(struct diff_filepair *);
diff --git a/merge-ort.c b/merge-ort.c
index 99c7569..e79830f 100644
--- a/merge-ort.c
+++ b/merge-ort.c
@@ -690,6 +690,48 @@ static void path_msg(struct merge_options *opt,
strbuf_addch(sb, '\n');
}
+MAYBE_UNUSED
+static struct diff_filespec *pool_alloc_filespec(struct mem_pool *pool,
+ const char *path)
+{
+ struct diff_filespec *spec;
+ size_t len;
+
+ if (!pool)
+ return alloc_filespec(path);
+
+ /* Same code as alloc_filespec, except allocate from pool */
+ len = strlen(path);
+
+ spec = mem_pool_calloc(pool, 1, st_add3(sizeof(*spec), len, 1));
+ memcpy(spec+1, path, len);
+ spec->path = (void*)(spec+1);
+
+ spec->count = 1;
+ spec->is_binary = -1;
+ return spec;
+}
+
+MAYBE_UNUSED
+static struct diff_filepair *pool_diff_queue(struct mem_pool *pool,
+ struct diff_queue_struct *queue,
+ struct diff_filespec *one,
+ struct diff_filespec *two)
+{
+ struct diff_filepair *dp;
+
+ if (!pool)
+ return diff_queue(queue, one, two);
+
+ /* Same code as diff_queue, except allocate from pool */
+ dp = mem_pool_calloc(pool, 1, sizeof(*dp));
+ dp->one = one;
+ dp->two = two;
+ if (queue)
+ diff_q(queue, dp);
+ return dp;
+}
+
static void *pool_calloc(struct mem_pool *pool, size_t count, size_t size)
{
if (!pool)