Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: James Almer <jamrial@gmail.com>
To: ffmpeg-devel@ffmpeg.org
Subject: Re: [FFmpeg-devel] [PATCH] avutil/mem: use C11 aligned_malloc()
Date: Sun, 18 Feb 2024 13:29:32 -0300
Message-ID: <7c7d92bf-e7c6-451e-85ff-925772b5e6ea@gmail.com> (raw)
In-Reply-To: <AS8P250MB0744BFE59FFFAF61864AE6558F522@AS8P250MB0744.EURP250.PROD.OUTLOOK.COM>

On 2/18/2024 1:27 PM, Andreas Rheinhardt wrote:
> James Almer:
>> Save for the Microsoft C Runtime library, where free() can't handle aligned
>> buffers, aligned_malloc() should be available and working on all supported
>> targets.
>> Also, malloc() alone may be sufficient if alignment requirement is low, so add
>> a check for it.
>>
>> Signed-off-by: James Almer <jamrial@gmail.com>
>> ---
>>   configure       |  2 --
>>   libavutil/mem.c | 42 ++++++------------------------------------
>>   2 files changed, 6 insertions(+), 38 deletions(-)
>>
>> diff --git a/configure b/configure
>> index 7c45ac25c8..8fd2895ac2 100755
>> --- a/configure
>> +++ b/configure
>> @@ -6450,8 +6450,6 @@ if test -n "$custom_allocator"; then
>>   fi
>>   
>>   check_func_headers malloc.h _aligned_malloc     && enable aligned_malloc
>> -check_func  ${malloc_prefix}memalign            && enable memalign
>> -check_func  ${malloc_prefix}posix_memalign      && enable posix_memalign
>>   
>>   check_func  access
>>   check_func_headers stdlib.h arc4random_buf
>> diff --git a/libavutil/mem.c b/libavutil/mem.c
>> index 36b8940a0c..a72981d1ab 100644
>> --- a/libavutil/mem.c
>> +++ b/libavutil/mem.c
>> @@ -100,44 +100,14 @@ void *av_malloc(size_t size)
>>       if (size > atomic_load_explicit(&max_alloc_size, memory_order_relaxed))
>>           return NULL;
>>   
>> -#if HAVE_POSIX_MEMALIGN
>> -    if (size) //OS X on SDK 10.6 has a broken posix_memalign implementation
>> -    if (posix_memalign(&ptr, ALIGN, size))
>> -        ptr = NULL;
>> -#elif HAVE_ALIGNED_MALLOC
>> +#if HAVE_ALIGNED_MALLOC
>>       ptr = _aligned_malloc(size, ALIGN);
>> -#elif HAVE_MEMALIGN
>> -#ifndef __DJGPP__
>> -    ptr = memalign(ALIGN, size);
>> -#else
>> -    ptr = memalign(size, ALIGN);
>> -#endif
>> -    /* Why 64?
>> -     * Indeed, we should align it:
>> -     *   on  4 for 386
>> -     *   on 16 for 486
>> -     *   on 32 for 586, PPro - K6-III
>> -     *   on 64 for K7 (maybe for P3 too).
>> -     * Because L1 and L2 caches are aligned on those values.
>> -     * But I don't want to code such logic here!
>> -     */
>> -    /* Why 32?
>> -     * For AVX ASM. SSE / NEON needs only 16.
>> -     * Why not larger? Because I did not see a difference in benchmarks ...
>> -     */
>> -    /* benchmarks with P3
>> -     * memalign(64) + 1          3071, 3051, 3032
>> -     * memalign(64) + 2          3051, 3032, 3041
>> -     * memalign(64) + 4          2911, 2896, 2915
>> -     * memalign(64) + 8          2545, 2554, 2550
>> -     * memalign(64) + 16         2543, 2572, 2563
>> -     * memalign(64) + 32         2546, 2545, 2571
>> -     * memalign(64) + 64         2570, 2533, 2558
>> -     *
>> -     * BTW, malloc seems to do 8-byte alignment by default here.
>> -     */
>>   #else
>> -    ptr = malloc(size);
>> +    // malloc may already allocate sufficiently aligned buffers
>> +    if (ALIGN > _Alignof(max_align_t))
>> +        ptr = aligned_malloc(size, ALIGN);
>> +    else
>> +        ptr = malloc(size);
>>   #endif
>>       if(!ptr && !size) {
>>           size = 1;
> 
> 1. The function is called aligned_alloc (how did you test this?).

By compiling on a target with _aligned_malloc(), which hid my mistake.

> 2. C11: "The value of alignment shall be a valid alignment supported by
> the implementation and the value of size shall be an integral multiple
> of alignment."

Well, that sure is not very useful...

> a) To use this, you would have to round size upwards; but this will make
> sanitiziers more lenient.
> b) If ALIGN is just not supported by the implementation, then everything
> is UB in C11.
> 3. What's the advantage of this patch anyway?

Removing all the different target specific allocation functions in favor 
of the standard one. But your second point makes it moot, so patch 
withdrawn.

> 
> - Andreas
> 
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

  reply	other threads:[~2024-02-18 16:29 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-18 16:16 James Almer
2024-02-18 16:27 ` Andreas Rheinhardt
2024-02-18 16:29   ` James Almer [this message]
2024-02-18 18:39     ` Rémi Denis-Courmont
2024-02-18 18:35   ` Rémi Denis-Courmont
2024-02-18 18:37 ` Rémi Denis-Courmont
2024-02-19  0:08 ` Michael Niedermayer
2024-02-19  1:37   ` James Almer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7c7d92bf-e7c6-451e-85ff-925772b5e6ea@gmail.com \
    --to=jamrial@gmail.com \
    --cc=ffmpeg-devel@ffmpeg.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

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