Commit 7cba708a authored by Philipp Schafft's avatar Philipp Schafft 🦁

Feature: Make refobject more general useful

parent a9d391ff
......@@ -22,7 +22,7 @@
#define TO_BASE(x) ((refobject_base_t*)(x))
#endif
refobject_t refobject_new(size_t len, refobject_free freecb, void *userdata)
refobject_t refobject_new(size_t len, refobject_free_t freecb, void *userdata, const char *name, refobject_t parent)
{
refobject_base_t *ret = NULL;
......@@ -39,6 +39,23 @@ refobject_t refobject_new(size_t len, refobject_free freecb, void *userdata)
thread_mutex_create(&(ret->lock));
if (name) {
ret->name = strdup(name);
if (!ret->name) {
refobject_unref(ret);
return (refobject_t)(refobject_base_t*)NULL;
}
}
if (TO_BASE(parent) != NULL) {
if (refobject_ref(parent) != 0) {
refobject_unref(ret);
return (refobject_t)(refobject_base_t*)NULL;
}
ret->parent = parent;
}
return (refobject_t)ret;
}
......@@ -74,6 +91,9 @@ int refobject_unref(refobject_t self)
if (base->userdata)
free(base->userdata);
if (base->name)
free(base->name);
thread_mutex_unlock(&(base->lock));
thread_mutex_destroy(&(base->lock));
......@@ -105,3 +125,31 @@ int refobject_set_userdata(refobject_t self, void *userdata)
return 0;
}
const char * refobject_get_name(refobject_t self)
{
const char *ret;
if (TO_BASE(self) == NULL)
return NULL;
thread_mutex_lock(&(TO_BASE(self)->lock));
ret = TO_BASE(self)->name;
thread_mutex_unlock(&(TO_BASE(self)->lock));
return ret;
}
refobject_t refobject_get_parent(refobject_t self)
{
refobject_t ret;
if (TO_BASE(self) == NULL)
return (refobject_t)(refobject_base_t*)NULL;
thread_mutex_lock(&(TO_BASE(self)->lock));
ret = TO_BASE(self)->parent;
thread_mutex_unlock(&(TO_BASE(self)->lock));
return ret;
}
......@@ -14,19 +14,23 @@
#include "icecasttypes.h"
#include "compat.h"
typedef void (*refobject_free)(refobject_t self, void **userdata);
typedef void (*refobject_free_t)(refobject_t self, void **userdata);
struct refobject_base_tag {
size_t refc;
mutex_t lock;
void *userdata;
refobject_free freecb;
refobject_free_t freecb;
char *name;
refobject_t parent;
};
refobject_t refobject_new(size_t len, refobject_free freecb, void *userdata);
refobject_t refobject_new(size_t len, refobject_free_t freecb, void *userdata, const char *name, refobject_t parent);
int refobject_ref(refobject_t self);
int refobject_unref(refobject_t self);
void * refobject_get_userdata(refobject_t self);
int refobject_set_userdata(refobject_t self, void *userdata);
const char * refobject_get_name(refobject_t self);
refobject_t refobject_get_parent(refobject_t self);
#endif
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