]> git.sesse.net Git - ffmpeg/blobdiff - libavutil/mem.c
float_dsp: fix errors in documentation
[ffmpeg] / libavutil / mem.c
index 391eb7db38e8629c2cc184b3143a95246b99b1e2..be42342de2585bbf92e6ea8c77ee5afe1b864f7a 100644 (file)
@@ -35,6 +35,7 @@
 #endif
 
 #include "avutil.h"
+#include "common.h"
 #include "intreadwrite.h"
 #include "mem.h"
 
@@ -65,7 +66,7 @@ void *av_malloc(size_t size)
     long diff;
 #endif
 
-    /* let's disallow possible ambiguous cases */
+    /* let's disallow possibly ambiguous cases */
     if (size > (INT_MAX - 32) || !size)
         return NULL;
 
@@ -119,7 +120,7 @@ void *av_realloc(void *ptr, size_t size)
     int diff;
 #endif
 
-    /* let's disallow possible ambiguous cases */
+    /* let's disallow possibly ambiguous cases */
     if (size > (INT_MAX - 16))
         return NULL;
 
@@ -136,6 +137,52 @@ void *av_realloc(void *ptr, size_t size)
 #endif
 }
 
+int av_reallocp(void *ptr, size_t size)
+{
+    void **ptrptr = ptr;
+    void *ret;
+
+    if (!size) {
+        av_freep(ptr);
+        return 0;
+    }
+    ret = av_realloc(*ptrptr, size);
+
+    if (!ret) {
+        av_freep(ptr);
+        return AVERROR(ENOMEM);
+    }
+
+    *ptrptr = ret;
+    return 0;
+}
+
+void *av_realloc_array(void *ptr, size_t nmemb, size_t size)
+{
+    if (!size || nmemb >= INT_MAX / size)
+        return NULL;
+    return av_realloc(ptr, nmemb * size);
+}
+
+int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
+{
+    void **ptrptr = ptr;
+    void *ret;
+    if (!size || nmemb >= INT_MAX / size)
+        return AVERROR(ENOMEM);
+    if (!nmemb) {
+        av_freep(ptr);
+        return 0;
+    }
+    ret = av_realloc(*ptrptr, nmemb * size);
+    if (!ret) {
+        av_freep(ptr);
+        return AVERROR(ENOMEM);
+    }
+    *ptrptr = ret;
+    return 0;
+}
+
 void av_free(void *ptr)
 {
 #if CONFIG_MEMALIGN_HACK
@@ -168,7 +215,7 @@ char *av_strdup(const char *s)
     char *ptr = NULL;
     if (s) {
         int len = strlen(s) + 1;
-        ptr = av_malloc(len);
+        ptr = av_realloc(NULL, len);
         if (ptr)
             memcpy(ptr, s, len);
     }
@@ -298,3 +345,35 @@ void av_memcpy_backptr(uint8_t *dst, int back, int cnt)
             *dst = *src;
     }
 }
+
+void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
+{
+    if (min_size < *size)
+        return ptr;
+
+    min_size = FFMAX(17 * min_size / 16 + 32, min_size);
+
+    ptr = av_realloc(ptr, min_size);
+    /* we could set this to the unmodified min_size but this is safer
+     * if the user lost the ptr and uses NULL now
+     */
+    if (!ptr)
+        min_size = 0;
+
+    *size = min_size;
+
+    return ptr;
+}
+
+void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
+{
+    void **p = ptr;
+    if (min_size < *size)
+        return;
+    min_size = FFMAX(17 * min_size / 16 + 32, min_size);
+    av_free(*p);
+    *p = av_malloc(min_size);
+    if (!*p)
+        min_size = 0;
+    *size = min_size;
+}