combine-diff: drop public declaration of combine_diff_path_size()
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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Jan 9, 2025 at 03:50 UTC
69f6dea44cf272dc80be6dffd0ac8db5c50585b4
2 files changed
+3
-5
combine-diff.c
+3
-2
@@ -1658,8 +1658,9 @@ struct combine_diff_path *combine_diff_path_new(const char *path,
1658
size_t num_parents)
1659
{
1660
struct combine_diff_path *p;
1661
+ size_t parent_len = st_mult(sizeof(p->parent[0]), num_parents);
1662
1662
- p = xmalloc(combine_diff_path_size(num_parents, path_len));
1663
+ p = xmalloc(st_add4(sizeof(*p), path_len, 1, parent_len));
1664
p->path = (char *)&(p->parent[num_parents]);
1665
memcpy(p->path, path, path_len);
1666
p->path[path_len] = 0;
@@ -1667,7 +1668,7 @@ struct combine_diff_path *combine_diff_path_new(const char *path,
1668
p->mode = mode;
1669
oidcpy(&p->oid, oid);
1670
1670
- memset(p->parent, 0, sizeof(p->parent[0]) * num_parents);
1671
+ memset(p->parent, 0, parent_len);
1672
1673
return p;
1674
}
diff.h
-3
@@ -489,9 +489,6 @@ struct combine_diff_path {
489
char *path;
490
} parent[FLEX_ARRAY];
491
};
492
-#define combine_diff_path_size(n, l) \
493
- st_add4(sizeof(struct combine_diff_path), (l), 1, \
494
- st_mult(sizeof(struct combine_diff_parent), (n)))
492
struct combine_diff_path *combine_diff_path_new(const char *path,
493
size_t path_len,
494
unsigned int mode,