diff mbox series

[10/14] combine-diff: drop public declaration of combine_diff_path_size()

Message ID 20250109085019.GJ2748836@coredump.intra.peff.net (mailing list archive)
State New
Headers show
Series combine-diff cleanups | expand

Commit Message

Jeff King Jan. 9, 2025, 8:50 a.m. UTC
We want callers to use combine_diff_path_new() to allocate structs,
rather than using combine_diff_path_size() and xmalloc(). That gives us
more consistency over the initialization of the fields.

Now that the final external user of combine_diff_path_size() is gone, we
can stop declaring it publicly. And since our constructor is the only
caller, we can just inline it there.

Breaking the size computation into two parts also lets us reuse the
intermediate multiplication result of the parent length, since we need
to know it to perform our memset(). The result is a little easier to
read.

Signed-off-by: Jeff King <peff@peff.net>
---
 combine-diff.c | 5 +++--
 diff.h         | 3 ---
 2 files changed, 3 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/combine-diff.c b/combine-diff.c
index ae3cbfc699..f21e1f58ba 100644
--- a/combine-diff.c
+++ b/combine-diff.c
@@ -1658,16 +1658,17 @@  struct combine_diff_path *combine_diff_path_new(const char *path,
 						size_t num_parents)
 {
 	struct combine_diff_path *p;
+	size_t parent_len = st_mult(sizeof(p->parent[0]), num_parents);
 
-	p = xmalloc(combine_diff_path_size(num_parents, path_len));
+	p = xmalloc(st_add4(sizeof(*p), path_len, 1, parent_len));
 	p->path = (char *)&(p->parent[num_parents]);
 	memcpy(p->path, path, path_len);
 	p->path[path_len] = 0;
 	p->next = NULL;
 	p->mode = mode;
 	oidcpy(&p->oid, oid);
 
-	memset(p->parent, 0, sizeof(p->parent[0]) * num_parents);
+	memset(p->parent, 0, parent_len);
 
 	return p;
 }
diff --git a/diff.h b/diff.h
index 60e7db4ad6..32ad17fd38 100644
--- a/diff.h
+++ b/diff.h
@@ -489,9 +489,6 @@  struct combine_diff_path {
 		char *path;
 	} parent[FLEX_ARRAY];
 };
-#define combine_diff_path_size(n, l) \
-	st_add4(sizeof(struct combine_diff_path), (l), 1, \
-		st_mult(sizeof(struct combine_diff_parent), (n)))
 struct combine_diff_path *combine_diff_path_new(const char *path,
 						size_t path_len,
 						unsigned int mode,