diff mbox series

[FFmpeg-devel,3/3] avutil/mem_internal: use av_max_alloc_get() in ff_fast_malloc()

Message ID 20210522220904.7012-3-jamrial@gmail.com
State Superseded
Headers show
Series [FFmpeg-devel,1/3] avutil/mem: make max_alloc_size an atomic type
Related show

Checks

Context Check Description
andriy/x86_make success Make finished
andriy/x86_make_fate success Make fate finished
andriy/PPC64_make success Make finished
andriy/PPC64_make_fate success Make fate finished

Commit Message

James Almer May 22, 2021, 10:09 p.m. UTC
This puts ff_fast_malloc() and av_fast_malloc() in line with ff_fast_realloc()

Signed-off-by: James Almer <jamrial@gmail.com>
---
The alternative to this set would be to move av_fast_padded_malloc() from
avcodec to avutil, and moving ff_fast_malloc() from mem_internal.h to mem.c, in
which case it will no longer be inlined for easy use.

 libavutil/mem_internal.h | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

Comments

James Almer May 23, 2021, 12:51 p.m. UTC | #1
On 5/22/2021 7:09 PM, James Almer wrote:
> This puts ff_fast_malloc() and av_fast_malloc() in line with ff_fast_realloc()
> 
> Signed-off-by: James Almer <jamrial@gmail.com>
> ---
> The alternative to this set would be to move av_fast_padded_malloc() from
> avcodec to avutil, and moving ff_fast_malloc() from mem_internal.h to mem.c, in
> which case it will no longer be inlined for easy use.

av_fast_padded_malloc() can't be moved since it uses 
AV_INPUT_BUFFER_PADDING_SIZE, so the only alternative is to move 
ff_fast_malloc() to mem.c, have it use max_alloc_size, and make 
av_fast_padded_malloc() call av_fast_mallocz().

If that's preferred to adding a function to get the max alloc value to 
lavu, i'll send a patchset for it.

> 
>   libavutil/mem_internal.h | 10 +++++++++-
>   1 file changed, 9 insertions(+), 1 deletion(-)
> 
> diff --git a/libavutil/mem_internal.h b/libavutil/mem_internal.h
> index ee2575c85f..76277d3fe4 100644
> --- a/libavutil/mem_internal.h
> +++ b/libavutil/mem_internal.h
> @@ -24,6 +24,7 @@
>   #include "config.h"
>   
>   #include <stdint.h>
> +#include <stdatomic.h>
>   
>   #include "avassert.h"
>   #include "mem.h"
> @@ -138,14 +139,21 @@
>   
>   static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
>   {
> +    size_t max_size;
>       void *val;
>   
> +    av_max_alloc_get(&max_size);
> +
>       memcpy(&val, ptr, sizeof(val));
>       if (min_size <= *size) {
>           av_assert0(val || !min_size);
>           return 0;
>       }
> -    min_size = FFMAX(min_size + min_size / 16 + 32, min_size);
> +    if (min_size > max_size) {
> +        *size = 0;
> +        return 1;
> +    }
> +    min_size = FFMIN(max_size, FFMAX(min_size + min_size / 16 + 32, min_size));
>       av_freep(ptr);
>       val = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
>       memcpy(ptr, &val, sizeof(val));
>
Andreas Rheinhardt May 23, 2021, 2:10 p.m. UTC | #2
James Almer:
> On 5/22/2021 7:09 PM, James Almer wrote:
>> This puts ff_fast_malloc() and av_fast_malloc() in line with
>> ff_fast_realloc()
>>
>> Signed-off-by: James Almer <jamrial@gmail.com>
>> ---
>> The alternative to this set would be to move av_fast_padded_malloc() from
>> avcodec to avutil, and moving ff_fast_malloc() from mem_internal.h to
>> mem.c, in
>> which case it will no longer be inlined for easy use.
> 
> av_fast_padded_malloc() can't be moved since it uses
> AV_INPUT_BUFFER_PADDING_SIZE, so the only alternative is to move
> ff_fast_malloc() to mem.c, have it use max_alloc_size, and make
> av_fast_padded_malloc() call av_fast_mallocz().
> 
> If that's preferred to adding a function to get the max alloc value to
> lavu, i'll send a patchset for it.
> 

Yeah, that sounds better than adding yet another public function.

- Andreas
diff mbox series

Patch

diff --git a/libavutil/mem_internal.h b/libavutil/mem_internal.h
index ee2575c85f..76277d3fe4 100644
--- a/libavutil/mem_internal.h
+++ b/libavutil/mem_internal.h
@@ -24,6 +24,7 @@ 
 #include "config.h"
 
 #include <stdint.h>
+#include <stdatomic.h>
 
 #include "avassert.h"
 #include "mem.h"
@@ -138,14 +139,21 @@ 
 
 static inline int ff_fast_malloc(void *ptr, unsigned int *size, size_t min_size, int zero_realloc)
 {
+    size_t max_size;
     void *val;
 
+    av_max_alloc_get(&max_size);
+
     memcpy(&val, ptr, sizeof(val));
     if (min_size <= *size) {
         av_assert0(val || !min_size);
         return 0;
     }
-    min_size = FFMAX(min_size + min_size / 16 + 32, min_size);
+    if (min_size > max_size) {
+        *size = 0;
+        return 1;
+    }
+    min_size = FFMIN(max_size, FFMAX(min_size + min_size / 16 + 32, min_size));
     av_freep(ptr);
     val = zero_realloc ? av_mallocz(min_size) : av_malloc(min_size);
     memcpy(ptr, &val, sizeof(val));