summaryrefslogtreecommitdiff
path: root/libavutil
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2013-10-27 22:21:59 +0100
committerAnton Khirnov <anton@khirnov.net>2013-11-14 09:42:22 +0100
commitcce3e0a49f0dd030262c28d9c53de0bd2fd909c4 (patch)
tree8976499166d5a5d4425ed647d139e4c5c4604ccf /libavutil
parentaa241229891173b0357eee04e6ca78f806cc9c0c (diff)
Move av_fast_{m,re}alloc from lavc to lavu.
Diffstat (limited to 'libavutil')
-rw-r--r--libavutil/mem.c33
-rw-r--r--libavutil/mem.h21
-rw-r--r--libavutil/version.h2
3 files changed, 55 insertions, 1 deletions
diff --git a/libavutil/mem.c b/libavutil/mem.c
index 15a8c07381..be42342de2 100644
--- a/libavutil/mem.c
+++ b/libavutil/mem.c
@@ -35,6 +35,7 @@
#endif
#include "avutil.h"
+#include "common.h"
#include "intreadwrite.h"
#include "mem.h"
@@ -344,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;
+}
diff --git a/libavutil/mem.h b/libavutil/mem.h
index 99dc4cc156..4a5e362cec 100644
--- a/libavutil/mem.h
+++ b/libavutil/mem.h
@@ -238,6 +238,27 @@ void av_freep(void *ptr);
void av_memcpy_backptr(uint8_t *dst, int back, int cnt);
/**
+ * Reallocate the given block if it is not large enough, otherwise do nothing.
+ *
+ * @see av_realloc
+ */
+void *av_fast_realloc(void *ptr, unsigned int *size, size_t min_size);
+
+/**
+ * Allocate a buffer, reusing the given one if large enough.
+ *
+ * Contrary to av_fast_realloc the current buffer contents might not be
+ * preserved and on error the old buffer is freed, thus no special
+ * handling to avoid memleaks is necessary.
+ *
+ * @param ptr pointer to pointer to already allocated buffer, overwritten with pointer to new buffer
+ * @param size size of the buffer *ptr points to
+ * @param min_size minimum size of *ptr buffer after returning, *ptr will be NULL and
+ * *size 0 if an error occurred.
+ */
+void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size);
+
+/**
* @}
*/
diff --git a/libavutil/version.h b/libavutil/version.h
index 4fabb1bfe6..4f0f469c62 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -37,7 +37,7 @@
*/
#define LIBAVUTIL_VERSION_MAJOR 52
-#define LIBAVUTIL_VERSION_MINOR 17
+#define LIBAVUTIL_VERSION_MINOR 18
#define LIBAVUTIL_VERSION_MICRO 0
#define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \