[vlc-devel] [PATCH 3/4] objres: add simple memory allocation

RĂ©mi Denis-Courmont remi at remlab.net
Sat Jun 17 21:39:45 CEST 2017


---
 include/vlc_objects.h |  4 ++++
 src/libvlccore.sym    |  2 ++
 src/misc/objres.c     | 44 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 50 insertions(+)

diff --git a/include/vlc_objects.h b/include/vlc_objects.h
index 598313ef35..171af9cf23 100644
--- a/include/vlc_objects.h
+++ b/include/vlc_objects.h
@@ -67,4 +67,8 @@ VLC_API char *vlc_object_get_name( const vlc_object_t * ) VLC_USED;
 #define vlc_list_children(a) \
     vlc_list_children( VLC_OBJECT(a) )
 
+VLC_API VLC_MALLOC void *vlc_malloc(vlc_object_t *, size_t);
+VLC_API VLC_MALLOC void *vlc_calloc(vlc_object_t *, size_t, size_t);
+VLC_API void vlc_free(vlc_object_t *, void *);
+
 /** @} */
diff --git a/src/libvlccore.sym b/src/libvlccore.sym
index bc21ee682c..f23a6ec505 100644
--- a/src/libvlccore.sym
+++ b/src/libvlccore.sym
@@ -271,6 +271,8 @@ vlc_LogSet
 vlc_vaLog
 vlc_strerror
 vlc_strerror_c
+vlc_malloc
+vlc_calloc
 msleep
 mwait
 net_Accept
diff --git a/src/misc/objres.c b/src/misc/objres.c
index e6c9c46623..be02430d48 100644
--- a/src/misc/objres.c
+++ b/src/misc/objres.c
@@ -124,3 +124,47 @@ void vlc_objres_remove(vlc_object_t *obj, void *data,
     }
 }
 #endif
+
+static void dummy_release(void *data)
+{
+    (void) data;
+}
+
+static bool ptrcmp(void *a, void *b)
+{
+    return a == b;
+}
+
+void *vlc_malloc(vlc_object_t *obj, size_t size)
+{
+    void *ptr = vlc_objres_new(size, dummy_release);
+    if (likely(ptr != NULL))
+        vlc_objres_push(obj, ptr);
+    return ptr;
+}
+
+void *vlc_calloc(vlc_object_t *obj, size_t nmemb, size_t size)
+{
+    size_t tabsize = nmemb * size;
+
+    if (unlikely(tabsize < nmemb))
+    {
+        errno = ENOMEM;
+        return NULL;
+    }
+
+    void *ptr = vlc_objres_new(tabsize, dummy_release);
+    if (likely(ptr != NULL))
+    {
+        memset(ptr, 0, tabsize);
+        vlc_objres_push(obj, ptr);
+    }
+    return ptr;
+}
+
+#ifndef UNUSED_YET
+void vlc_free(vlc_object_t *obj, void *ptr)
+{
+    vlc_objres_remove(obj, ptr, ptrcmp);
+}
+#endif
-- 
2.11.0



More information about the vlc-devel mailing list