On 08/04/2026 15:11, Pádraig Brady wrote:
* src/cat.c (main): Only resize the allocated buffer when needed,
which avoids per file heap manipulation and mmap/munmap syscalls.
---
+static char *
+ensure_buf_size (char *buf, idx_t *buf_alloc, idx_t alignment, idx_t size)
+{
+ if (*buf_alloc < size)
+ {
+ alignfree (buf);
+ buf = xalignalloc (alignment, size);
+ *buf_alloc = size;
+ }
+
+ return buf;
+}
+
BTW I was tempted to avoid the valgrind / ASAN complaints
re using align_alloc() with non page aligned size, with the following,
but refrained since this was already considered as OK
as per the references in lib/alignalloc.h.
Hmm, maybe it's worth putting something like that in #ifdef lint.
diff --git a/src/cat.c b/src/cat.c
index 01a703e9b..04dd98a56 100644
--- a/src/cat.c
+++ b/src/cat.c
@@ -648,11 +648,17 @@ splice_cat (void)
static char *
ensure_buf_size (char *buf, idx_t *buf_alloc, idx_t alignment, idx_t size)
{
- if (*buf_alloc < size)
+ idx_t alloc_size = size;
+ idx_t remainder = size % alignment;
+ if (remainder
+ && ckd_add (&alloc_size, size, alignment - remainder))
+ xalloc_die ();
+
+ if (*buf_alloc < alloc_size)
{
alignfree (buf);
- buf = xalignalloc (alignment, size);
- *buf_alloc = size;
+ buf = xalignalloc (alignment, alloc_size);
+ *buf_alloc = alloc_size;
}
return buf;