On Sat, 15 Jun 2013, Luca Barbato wrote:

---
libavutil/mem.c | 16 ++++++++++++++++
libavutil/mem.h | 13 +++++++++++++
2 files changed, 29 insertions(+)

diff --git a/libavutil/mem.c b/libavutil/mem.c
index e901533..1e2f914 100644
--- a/libavutil/mem.c
+++ b/libavutil/mem.c
@@ -136,6 +136,22 @@ void *av_realloc(void *ptr, size_t size)
#endif
}

+int av_reallocp(void *ptr, size_t size)
+{
+    void **ptrptr = ptr;
+    void *ret;
+
+    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 <= 0 || nmemb >= INT_MAX / size)
diff --git a/libavutil/mem.h b/libavutil/mem.h
index 8a4fcd9..e71de57 100644
--- a/libavutil/mem.h
+++ b/libavutil/mem.h
@@ -112,6 +112,19 @@ av_alloc_size(1, 2) static inline void 
*av_malloc_array(size_t nmemb, size_t siz
void *av_realloc(void *ptr, size_t size) av_alloc_size(2);

/**
+ * Allocate or reallocate an block of memory.
+ * If *ptr is NULL and size > 0, allocate a new block. If
+ * size is zero, free the memory block pointed to by ptr.
+ * @param ptr Pointer to a pointer to a memory block already allocated
+ * with av_realloc(), or pointer to a pointer to NULL.
+ * The pointer is updated on success, or freed on failure.
+ * @param size Size in bytes for the memory block to be allocated or
+ * reallocated
+ * @return Zero on success, an AVERROR error code on failure.
+ */
+int av_reallocp(void *ptr, size_t size);
+
+/**
 * Allocate or reallocate an array.
 * If ptr is NULL and nmemb > 0, allocate a new block. If
 * nmemb is zero, free the memory block pointed to by ptr.
--
1.8.2.1

LGTM, although the docs might need to be updated with the latest clarifications from the other patch.

// Martin
_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to