1
0
mirror of https://gitlab.xiph.org/xiph/icecast-common.git synced 2024-09-22 04:15:55 -04:00

Feature: Implemented refobject API

This commit is contained in:
Philipp Schafft 2018-10-31 17:07:27 +00:00
parent 904ddf1c39
commit 2bee2ee3a4
2 changed files with 185 additions and 1 deletions

View File

@ -61,6 +61,7 @@ struct igloo_ro_type_tag {
/* Size of this control structure */
size_t control_length;
/* ABI version of this structure */
#define igloo_RO__CONTROL_VERSION 1
int control_version;
/* Total length of the objects to be created */

185
src/ro.c
View File

@ -18,7 +18,190 @@
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include <igloo/ro.h>
static inline int check_type(const igloo_ro_type_t *type)
{
return type->control_length == sizeof(igloo_ro_type_t) && type->control_version == igloo_RO__CONTROL_VERSION &&
type->type_length >= sizeof(igloo_ro_base_t);
}
igloo_ro_t igloo_ro_new__raw(const igloo_ro_type_t *type, const char *name, igloo_ro_t associated)
{
igloo_ro_base_t *base;
if (!check_type(type))
return igloo_RO_NULL;
base = calloc(1, type->type_length);
if (!base)
return igloo_RO_NULL;
base->type = type;
base->refc = 1;
igloo_thread_mutex_create(&(base->lock));
if (name) {
base->name = strdup(name);
if (!base->name) {
igloo_ro_unref(base);
return igloo_RO_NULL;
}
}
if (!igloo_RO_IS_NULL(associated)) {
if (igloo_ro_ref(associated) != 0) {
igloo_ro_unref(base);
return igloo_RO_NULL;
}
base->associated = associated;
}
return (igloo_ro_t)base;
}
igloo_ro_t igloo_ro_new__simple(const igloo_ro_type_t *type, const char *name, igloo_ro_t associated, ...)
{
igloo_ro_t ret;
int res;
va_list ap;
if (!check_type(type))
return igloo_RO_NULL;
if (!type->type_newcb)
return igloo_RO_NULL;
ret = igloo_ro_new__raw(type, name, associated);
if (igloo_RO_IS_NULL(ret))
return igloo_RO_NULL;
va_start(ap, associated);
res = type->type_newcb(ret, type, ap);
va_end(ap);
if (res != 0) {
igloo_ro_unref(ret);
return igloo_RO_NULL;
}
return ret;
}
int igloo_ro_ref(igloo_ro_t self)
{
igloo_ro_base_t *base = igloo_RO__GETBASE(self);
if (!base)
return -1;
igloo_thread_mutex_lock(&(base->lock));
base->refc++;
igloo_thread_mutex_unlock(&(base->lock));
return 0;
}
int igloo_ro_unref(igloo_ro_t self)
{
igloo_ro_base_t *base = igloo_RO__GETBASE(self);
if (!base)
return -1;
igloo_thread_mutex_lock(&(base->lock));
base->refc--;
if (base->refc) {
igloo_thread_mutex_unlock(&(base->lock));
return 0;
}
if (base->type->type_freecb)
base->type->type_freecb(self);
igloo_ro_unref(base->associated);
if (base->name)
free(base->name);
igloo_thread_mutex_unlock(&(base->lock));
igloo_thread_mutex_destroy(&(base->lock));
free(base);
return 0;
}
const char * igloo_ro_get_name(igloo_ro_t self)
{
igloo_ro_base_t *base = igloo_RO__GETBASE(self);
const char *ret;
if (!base)
return NULL;
igloo_thread_mutex_lock(&(base->lock));
ret = base->name;
igloo_thread_mutex_unlock(&(base->lock));
return ret;
}
igloo_ro_t igloo_ro_get_associated(igloo_ro_t self)
{
igloo_ro_base_t *base = igloo_RO__GETBASE(self);
igloo_ro_t ret;
if (!base)
return igloo_RO_NULL;
igloo_thread_mutex_lock(&(base->lock));
ret = base->associated;
if (!igloo_RO_IS_NULL(ret)) {
if (igloo_ro_ref(ret) != 0) {
igloo_thread_mutex_unlock(&(base->lock));
return igloo_RO_NULL;
}
}
igloo_thread_mutex_unlock(&(base->lock));
return ret;
}
int igloo_ro_set_associated(igloo_ro_t self, igloo_ro_t associated)
{
igloo_ro_base_t *base = igloo_RO__GETBASE(self);
igloo_ro_t old;
if (!base)
return 0;
/* We can not set ourself to be our associated. */
if (base == igloo_RO__GETBASE(associated))
return -1;
if (!igloo_RO_IS_NULL(associated)) {
if (igloo_ro_ref(associated) != 0) {
/* Could not get a reference on the new associated object. */
return -1;
}
}
igloo_thread_mutex_lock(&(base->lock));
old = base->associated;
base->associated = associated;
igloo_thread_mutex_unlock(&(base->lock));
igloo_ro_unref(old);
return 0;
}