diff --git a/src/refobject.c b/src/refobject.c index 12fc7aa7..243819bb 100644 --- a/src/refobject.c +++ b/src/refobject.c @@ -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; +} diff --git a/src/refobject.h b/src/refobject.h index 4bb66050..4055a36d 100644 --- a/src/refobject.h +++ b/src/refobject.h @@ -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