Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PR] avutil/mem: Add av_malloc_with_paddingz() (PR #21509)
@ 2026-01-19  4:21 michaelni via ffmpeg-devel
  2026-01-19  8:44 ` [FFmpeg-devel] " Kieran Kunhya via ffmpeg-devel
  0 siblings, 1 reply; 3+ messages in thread
From: michaelni via ffmpeg-devel @ 2026-01-19  4:21 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: michaelni

PR #21509 opened by michaelni
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21509
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/21509.patch

Add a av_malloc() that adds padding like AV_INPUT_BUFFER_PADDING_SIZE
and clears it.

This is a operation we commonly use so this function should simplify
many cases

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>


>From cc424b2a5568ee35a3435ff58a84d2a18c1c6843 Mon Sep 17 00:00:00 2001
From: Michael Niedermayer <michael@niedermayer.cc>
Date: Mon, 19 Jan 2026 05:15:23 +0100
Subject: [PATCH] avutil/mem: Add av_malloc_with_paddingz()

Add a av_malloc() that adds padding like AV_INPUT_BUFFER_PADDING_SIZE
and clears it.

This is a operation we commonly use so this function should simplify
many cases

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavutil/mem.c | 10 ++++++++++
 libavutil/mem.h | 13 +++++++++++++
 2 files changed, 23 insertions(+)

diff --git a/libavutil/mem.c b/libavutil/mem.c
index b205d3fb25..aac7ef4d12 100644
--- a/libavutil/mem.c
+++ b/libavutil/mem.c
@@ -261,6 +261,16 @@ void *av_mallocz(size_t size)
     return ptr;
 }
 
+void *av_malloc_with_paddingz(size_t size, size_t padding)
+{
+    if (size > SIZE_MAX - padding)
+        return NULL;
+    uint8_t *p = av_malloc(size + padding);
+    if(p)
+        memset(p + size, 0, padding);
+    return p;
+}
+
 void *av_calloc(size_t nmemb, size_t size)
 {
     size_t result;
diff --git a/libavutil/mem.h b/libavutil/mem.h
index ab7648ac57..6a4c144f18 100644
--- a/libavutil/mem.h
+++ b/libavutil/mem.h
@@ -129,6 +129,19 @@ void *av_malloc(size_t size) av_malloc_attrib av_alloc_size(1);
  */
 void *av_mallocz(size_t size) av_malloc_attrib av_alloc_size(1);
 
+/**
+ * Allocate a memory block with alignment suitable for all memory accesses
+ * (including vectors if available on the CPU)
+ *
+ * An additional padding will be allocated and cleared.
+ *
+ * @param size Size in bytes for the memory block to be allocated
+ * @param padding Size in bytes of padding allocated and zeroed after size.
+ * @return Pointer to the allocated block, or `NULL` if it cannot be allocated
+ * @see av_malloc()
+ */
+void *av_malloc_with_paddingz(size_t size, size_t padding) av_malloc_attrib av_alloc_size(1);
+
 /**
  * Allocate a memory block for an array with av_malloc().
  *
-- 
2.52.0

_______________________________________________
ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org
To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-01-21 23:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-01-19  4:21 [FFmpeg-devel] [PR] avutil/mem: Add av_malloc_with_paddingz() (PR #21509) michaelni via ffmpeg-devel
2026-01-19  8:44 ` [FFmpeg-devel] " Kieran Kunhya via ffmpeg-devel
2026-01-21 18:38   ` Michael Niedermayer via ffmpeg-devel

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git