Commit 9635a5c8 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

objres: add simple memory allocation

parent 749d0390
......@@ -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 *);
/** @} */
......@@ -271,6 +271,9 @@ vlc_LogSet
vlc_vaLog
vlc_strerror
vlc_strerror_c
vlc_malloc
vlc_calloc
vlc_free
msleep
mwait
net_Accept
......
......@@ -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);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment