Commit 02fb5253 authored by Philipp Schafft's avatar Philipp Schafft 🦁

Merge branch 'ph3-refobject'

parents b3359a24 a3f29ae4
Pipeline #213 passed with stage
in 43 seconds
......@@ -35,6 +35,9 @@ AM_MAINTAINER_MODE([enable])
LT_INIT
dnl Check for attributes
AX_GCC_TYPE_ATTRIBUTE([transparent_union])
dnl Checks for header files.
AC_HEADER_ASSERT
AC_HEADER_STDC
......
Subproject commit 22e4bc9b07982ced62289290e8d496b1c1a74853
Subproject commit 2d8eb25218daa933d337df939b2427895b344344
......@@ -29,6 +29,7 @@ noinst_HEADERS = \
md5.h \
matchfile.h \
tls.h \
refobject.h \
event.h \
event_log.h \
event_exec.h \
......@@ -68,6 +69,7 @@ icecast_SOURCES = \
md5.c \
matchfile.c \
tls.c \
refobject.c \
format.c \
format_ogg.c \
format_mp3.c \
......
......@@ -9,6 +9,10 @@
#ifndef __ICECASTTYPES_H__
#define __ICECASTTYPES_H__
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "compat.h"
/* ---[ client.[ch] ]--- */
......@@ -88,4 +92,16 @@ typedef enum {
typedef struct _relay_server relay_server;
/* ---[ refobject.[ch] ]--- */
typedef struct refobject_base_tag refobject_base_t;
#ifdef HAVE_TYPE_ATTRIBUTE_TRANSPARENT_UNION
typedef union __attribute__ ((__transparent_union__)) {
refobject_base_t *refobject_base;
} refobject_t;
#else
typedef void * refobject_t;
#endif
#endif
/* Icecast
*
* This program is distributed under the GNU General Public License, version 2.
* A copy of this license is included with this source.
*
* Copyright 2018, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#include "common/thread/thread.h"
#include "refobject.h"
#define TO_BASE(x) REFOBJECT_TO_TYPE((x), refobject_base_t *)
refobject_t refobject_new(size_t len, refobject_free_t freecb, void *userdata, const char *name, refobject_t associated)
{
refobject_base_t *ret = NULL;
if (len < sizeof(refobject_base_t))
return (refobject_t)ret;
ret = calloc(1, len);
if (ret == NULL)
return (refobject_t)ret;
ret->refc = 1;
ret->freecb = freecb;
ret->userdata = userdata;
thread_mutex_create(&(ret->lock));
if (name) {
ret->name = strdup(name);
if (!ret->name) {
refobject_unref(ret);
return REFOBJECT_NULL;
}
}
if (!REFOBJECT_IS_NULL(associated)) {
if (refobject_ref(associated) != 0) {
refobject_unref(ret);
return REFOBJECT_NULL;
}
ret->associated = associated;
}
return (refobject_t)ret;
}
int refobject_ref(refobject_t self)
{
if (REFOBJECT_IS_NULL(self))
return -1;
thread_mutex_lock(&(TO_BASE(self)->lock));
TO_BASE(self)->refc++;
thread_mutex_unlock(&(TO_BASE(self)->lock));
return 0;
}
int refobject_unref(refobject_t self)
{
register refobject_base_t *base = TO_BASE(self);
if (REFOBJECT_IS_NULL(self))
return -1;
thread_mutex_lock(&(base->lock));
base->refc--;
if (base->refc) {
thread_mutex_unlock(&(base->lock));
return 0;
}
if (base->freecb)
base->freecb(self, &(base->userdata));
if (base->userdata)
free(base->userdata);
if (base->name)
free(base->name);
thread_mutex_unlock(&(base->lock));
thread_mutex_destroy(&(base->lock));
free(base);
return 0;
}
void * refobject_get_userdata(refobject_t self)
{
void *ret;
if (REFOBJECT_IS_NULL(self))
return NULL;
thread_mutex_lock(&(TO_BASE(self)->lock));
ret = TO_BASE(self)->userdata;
thread_mutex_unlock(&(TO_BASE(self)->lock));
return ret;
}
int refobject_set_userdata(refobject_t self, void *userdata)
{
if (REFOBJECT_IS_NULL(self))
return -1;
thread_mutex_lock(&(TO_BASE(self)->lock));
TO_BASE(self)->userdata = userdata;
thread_mutex_unlock(&(TO_BASE(self)->lock));
return 0;
}
const char * refobject_get_name(refobject_t self)
{
const char *ret;
if (REFOBJECT_IS_NULL(self))
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_associated(refobject_t self)
{
refobject_t ret;
if (REFOBJECT_IS_NULL(self))
return REFOBJECT_NULL;
thread_mutex_lock(&(TO_BASE(self)->lock));
ret = TO_BASE(self)->associated;
thread_mutex_unlock(&(TO_BASE(self)->lock));
return ret;
}
/* Icecast
*
* This program is distributed under the GNU General Public License, version 2.
* A copy of this license is included with this source.
*
* Copyright 2018, Philipp "ph3-der-loewe" Schafft <lion@lion.leolix.org>,
*/
#ifndef __REFOBJECT_H__
#define __REFOBJECT_H__
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "common/thread/thread.h"
#include "icecasttypes.h"
#include "compat.h"
#ifdef HAVE_TYPE_ATTRIBUTE_TRANSPARENT_UNION
#define REFOBJECT_NULL ((refobject_t)(refobject_base_t*)NULL)
#define REFOBJECT_IS_NULL(x) (((refobject_t)(x)).refobject_base == NULL)
#define REFOBJECT_TO_TYPE(x,y) ((y)(((refobject_t)(x)).refobject_base))
#else
#define REFOBJECT_NULL NULL
#define REFOBJECT_IS_NULL(x) ((x) == NULL)
#define REFOBJECT_TO_TYPE(x,y) ((y)(x))
#endif
typedef void (*refobject_free_t)(refobject_t self, void **userdata);
struct refobject_base_tag {
size_t refc;
mutex_t lock;
void *userdata;
refobject_free_t freecb;
char *name;
refobject_t associated;
};
refobject_t refobject_new(size_t len, refobject_free_t freecb, void *userdata, const char *name, refobject_t associated);
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_associated(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