Skip to content

Commit a8791ef

Browse files
newrengitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 6697ee0 commit a8791ef

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

diffcore-rename.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,6 +1328,47 @@ static void handle_early_known_dir_renames(struct dir_rename_info *info,
13281328
rename_src_nr = new_num_src;
13291329
}
13301330

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+
13311372
void diffcore_rename_extended(struct diff_options *options,
13321373
struct strintmap *relevant_sources,
13331374
struct strintmap *dirs_removed,

diffcore.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ struct diff_filepair {
127127
#define DIFF_PAIR_MODE_CHANGED(p) ((p)->one->mode != (p)->two->mode)
128128

129129
void diff_free_filepair(struct diff_filepair *);
130+
void pool_diff_free_filepair(struct mem_pool *pool,
131+
struct diff_filepair *p);
130132

131133
int diff_unmodified_pair(struct diff_filepair *);
132134

merge-ort.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,48 @@ static void path_msg(struct merge_options *opt,
690690
strbuf_addch(sb, '\n');
691691
}
692692

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+
693735
static void *pool_calloc(struct mem_pool *pool, size_t count, size_t size)
694736
{
695737
if (!pool)

0 commit comments

Comments
 (0)