[vlc-commits] objres: add simple memory allocation
Rémi Denis-Courmont
git at videolan.org
Mon Jun 19 21:52:27 CEST 2017
vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Wed Jun 14 20:17:24 2017 +0300| [9635a5c855f7eeb3648d0cdb77ba7f6ff564a595] | committer: Rémi Denis-Courmont
objres: add simple memory allocation
> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=9635a5c855f7eeb3648d0cdb77ba7f6ff564a595
---
include/vlc_objects.h | 4 ++++
src/libvlccore.sym | 3 +++
src/misc/objres.c | 42 ++++++++++++++++++++++++++++++++++++++++++
3 files changed, 49 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..58a54bb0f0 100644
--- a/src/libvlccore.sym
+++ b/src/libvlccore.sym
@@ -271,6 +271,9 @@ vlc_LogSet
vlc_vaLog
vlc_strerror
vlc_strerror_c
+vlc_malloc
+vlc_calloc
+vlc_free
msleep
mwait
net_Accept
diff --git a/src/misc/objres.c b/src/misc/objres.c
index 7f215bfccc..1afaccb700 100644
--- a/src/misc/objres.c
+++ b/src/misc/objres.c
@@ -122,3 +122,45 @@ void vlc_objres_remove(vlc_object_t *obj, void *data,
pp = &res->prev;
}
}
+
+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;
+}
+
+void vlc_free(vlc_object_t *obj, void *ptr)
+{
+ vlc_objres_remove(obj, ptr, ptrcmp);
+}
More information about the vlc-commits
mailing list