diff mbox series

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

Message ID 20210522221810.7076-1-jamrial@gmail.com
State New
Headers show
Series Untitled series #4019
Related show

Commit Message

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

Signed-off-by: James Almer <jamrial@gmail.com>
---
Now freeing the buffer on error, since that's what the doxy for
av_fast_malloc() states is meant to happen.

 libavutil/mem_internal.h | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
diff mbox series

Patch

diff --git a/libavutil/mem_internal.h b/libavutil/mem_internal.h
index ee2575c85f..fe3cf7ad23 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,22 @@ 
 
 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) {
+        av_freep(ptr);
+        *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));