diff mbox

[FFmpeg-devel,01/21] libavutil/mem: added av_arraydup to accompany av_realloc*_array functions

Message ID 1471943019-14136-2-git-send-email-erkki.seppala.ext@nokia.com
State Rejected
Headers show

Commit Message

erkki.seppala.ext@nokia.com Aug. 23, 2016, 9:03 a.m. UTC
From: Erkki Seppälä <erkki.seppala.ext@nokia.com>

This allows copying an array so that is compatible with the array
reallocation functions. av_memdup won't do, as it uses av_malloc
underneath, but this one uses av_realloc_array for the allocation.

Signed-off-by: Erkki Seppälä <erkki.seppala.ext@nokia.com>
Signed-off-by: OZOPlayer <OZOPL@nokia.com>
---
 libavutil/mem.c | 11 +++++++++++
 libavutil/mem.h | 11 +++++++++++
 2 files changed, 22 insertions(+)

Comments

Michael Niedermayer Aug. 23, 2016, 11:05 a.m. UTC | #1
On Tue, Aug 23, 2016 at 12:03:19PM +0300, erkki.seppala.ext@nokia.com wrote:
> From: Erkki Seppälä <erkki.seppala.ext@nokia.com>
> 
> This allows copying an array so that is compatible with the array
> reallocation functions. av_memdup won't do, as it uses av_malloc
> underneath, but this one uses av_realloc_array for the allocation.

on which platform does av_memdup() not work with av_realloc_array() ?

[...]
erkki.seppala.ext@nokia.com Aug. 23, 2016, 11:10 a.m. UTC | #2
On 08/23/2016 02:05 PM, Michael Niedermayer wrote:
 > on which platform does av_memdup() not work with av_realloc_array() ?

I cannot indicate such a platform. However, the documentation for 
av_malloc says:

"Pointers originating from the av_malloc() family of functions
must not be passed to av_realloc(). The former can be
implemented using memalign() (or other functions), and
there is no guarantee that pointers from such functions
can be passed to realloc() at all.  The situation is
undefined according to POSIX and may crash with some
libc implementations."

And av_memdup uses av_malloc.
Michael Niedermayer Aug. 23, 2016, 11:18 a.m. UTC | #3
On Tue, Aug 23, 2016 at 02:10:28PM +0300, Erkki Seppälä wrote:
> On 08/23/2016 02:05 PM, Michael Niedermayer wrote:
> > on which platform does av_memdup() not work with av_realloc_array() ?
> 
> I cannot indicate such a platform. However, the documentation for
> av_malloc says:
> 
> "Pointers originating from the av_malloc() family of functions
> must not be passed to av_realloc(). The former can be
> implemented using memalign() (or other functions), and
> there is no guarantee that pointers from such functions
> can be passed to realloc() at all.  The situation is
> undefined according to POSIX and may crash with some
> libc implementations."
> 
> And av_memdup uses av_malloc.

see:
0421 15:12 Michael Niederm (3.9K) [FFmpeg-devel] [PATCH] avutil/mem: remove av_realloc / av_malloc incompatibility warning
i think thats a better choice than maintaining all memory allocation
in 2 incompatible systems

[...]
erkki.seppala.ext@nokia.com Aug. 23, 2016, 11:36 a.m. UTC | #4
On 08/23/2016 02:18 PM, Michael Niedermayer wrote:
> see:
> 0421 15:12 Michael Niederm (3.9K) [FFmpeg-devel] [PATCH] avutil/mem: remove av_realloc / av_malloc incompatibility warning
> i think thats a better choice than maintaining all memory allocation
> in 2 incompatible systems

Well, it's still not completely useless even if redundant, given that 
the interface is more similar to av_malloc_array and av_realloc_array. 
Code that resizes arrays needing to make a copy of the array would be 
more uniform with a function such as av_arraydup (better called 
av_dup_array?).

But that's only a philosophical reason. And now checking for my use 
scenario I noticed that the code that made use of the function does not 
exist anymore, so this patch can be just dropped.
diff mbox

Patch

diff --git a/libavutil/mem.c b/libavutil/mem.c
index 1a8fc21..c74374e 100644
--- a/libavutil/mem.c
+++ b/libavutil/mem.c
@@ -307,6 +307,17 @@  void *av_memdup(const void *p, size_t size)
     return ptr;
 }
 
+void *av_arraydup(const void *p, size_t nmemb, size_t size)
+{
+    void *ptr = NULL;
+    if (p) {
+        ptr = av_realloc_array(NULL, nmemb, size);
+        if (ptr)
+            memcpy(ptr, p, nmemb * size);
+    }
+    return ptr;
+}
+
 int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem)
 {
     void **tab;
diff --git a/libavutil/mem.h b/libavutil/mem.h
index 7f0c610..08ed520 100644
--- a/libavutil/mem.h
+++ b/libavutil/mem.h
@@ -514,6 +514,17 @@  char *av_strndup(const char *s, size_t len) av_malloc_attrib;
 void *av_memdup(const void *p, size_t size);
 
 /**
+ * Duplicate the array p. This array is compatible with the av_realloc
+ * functions.
+ * @param p array to be duplicated
+ * @param nmemb number of elements in the array
+ * @param size size of an element in the array
+ * @return Pointer to a newly allocated array containing a
+ * copy of p or NULL if the buffer cannot be allocated.
+ */
+void *av_arraydup(const void *p, size_t nmemb, size_t size);
+
+/**
  * Overlapping memcpy() implementation.
  *
  * @param dst  Destination buffer