* [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* [FFmpeg-devel] Re: [PR] avutil/mem: Add av_malloc_with_paddingz() (PR #21509)
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 ` Kieran Kunhya via ffmpeg-devel
2026-01-21 18:38 ` Michael Niedermayer via ffmpeg-devel
0 siblings, 1 reply; 3+ messages in thread
From: Kieran Kunhya via ffmpeg-devel @ 2026-01-19 8:44 UTC (permalink / raw)
To: FFmpeg development discussions and patches; +Cc: michaelni, Kieran Kunhya
On Mon, 19 Jan 2026, 04:22 michaelni via ffmpeg-devel, <
ffmpeg-devel@ffmpeg.org> wrote:
> 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
Seems a bit wordy. av_malloc_paddedz instead?
Kieran
_______________________________________________
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* [FFmpeg-devel] Re: [PR] avutil/mem: Add av_malloc_with_paddingz() (PR #21509)
2026-01-19 8:44 ` [FFmpeg-devel] " Kieran Kunhya via ffmpeg-devel
@ 2026-01-21 18:38 ` Michael Niedermayer via ffmpeg-devel
0 siblings, 0 replies; 3+ messages in thread
From: Michael Niedermayer via ffmpeg-devel @ 2026-01-21 18:38 UTC (permalink / raw)
To: FFmpeg development discussions and patches; +Cc: Michael Niedermayer
[-- Attachment #1.1: Type: text/plain, Size: 3459 bytes --]
On Mon, Jan 19, 2026 at 08:44:23AM +0000, Kieran Kunhya via ffmpeg-devel wrote:
> On Mon, 19 Jan 2026, 04:22 michaelni via ffmpeg-devel, <
> ffmpeg-devel@ffmpeg.org> wrote:
>
> > 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
>
>
> Seems a bit wordy. av_malloc_paddedz instead?
yes, i changed it to james suggestions of av_padded_malloc(),
[...]
--
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
Complexity theory is the science of finding the exact solution to an
approximation. Benchmarking OTOH is finding an approximation of the exact
[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]
[-- Attachment #2: Type: text/plain, Size: 163 bytes --]
_______________________________________________
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