refobject.c 4.29 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/* 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>
14
#include <string.h>
15 16 17 18 19

#include "common/thread/thread.h"

#include "refobject.h"

20
#define TO_BASE(x) REFOBJECT_TO_TYPE((x), refobject_base_t *)
21

22
int refobject_new__return_zero(refobject_t self, const refobject_type_t *type, va_list ap)
23 24 25 26 27 28
{
    (void)self, (void)type, (void)ap;
    return 0;
}

REFOBJECT_DEFINE_TYPE(refobject_base_t,
29
        REFOBJECT_DEFINE_TYPE_NEW_NOOP()
30
        );
31 32 33 34 35 36 37 38

static inline int check_type(const refobject_type_t *type)
{
    return type->control_length == sizeof(refobject_type_t) && type->control_version == REFOBJECT_CONTROL_VERSION &&
        type->type_length >= sizeof(refobject_base_t);
}

refobject_t     refobject_new__real(const refobject_type_t *type, void *userdata, const char *name, refobject_t associated)
39 40 41
{
    refobject_base_t *ret = NULL;

42
    if (!check_type(type))
43 44
        return (refobject_t)ret;

45
    ret = calloc(1, type->type_length);
46 47 48
    if (ret == NULL)
        return (refobject_t)ret;

49
    ret->type = type;
50 51 52 53 54
    ret->refc = 1;
    ret->userdata = userdata;

    thread_mutex_create(&(ret->lock));

55 56 57 58
    if (name) {
        ret->name = strdup(name);
        if (!ret->name) {
            refobject_unref(ret);
59
            return REFOBJECT_NULL;
60 61 62
        }
    }

63 64
    if (!REFOBJECT_IS_NULL(associated)) {
        if (refobject_ref(associated) != 0) {
65
            refobject_unref(ret);
66
            return REFOBJECT_NULL;
67 68
        }

69
        ret->associated = associated;
70 71
    }

72 73 74
    return (refobject_t)ret;
}

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
refobject_t     refobject_new__simple(const refobject_type_t *type, void *userdata, const char *name, refobject_t associated, ...)
{
    refobject_t ret;
    int res;
    va_list ap;

    if (!check_type(type))
        return REFOBJECT_NULL;

    if (!type->type_newcb)
        return REFOBJECT_NULL;

    ret = refobject_new__real(type, userdata, name, associated);
    if (REFOBJECT_IS_NULL(ret))
        return REFOBJECT_NULL;

    va_start(ap, associated);
    res = type->type_newcb(ret, type, ap);
    va_end(ap);

    if (res != 0) {
        refobject_unref(ret);
        return REFOBJECT_NULL;
    }

    return ret;
}

103 104
int             refobject_ref(refobject_t self)
{
105
    if (REFOBJECT_IS_NULL(self))
106 107 108 109 110 111 112 113 114 115 116 117 118
        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);

119
    if (REFOBJECT_IS_NULL(self))
120 121 122 123 124 125 126 127 128
        return -1;

    thread_mutex_lock(&(base->lock));
    base->refc--;
    if (base->refc) {
        thread_mutex_unlock(&(base->lock));
        return 0;
    }

129 130
    if (base->type->type_freecb)
        base->type->type_freecb(self, &(base->userdata));
131 132 133 134

    if (base->userdata)
        free(base->userdata);

135 136 137
    if (base->name)
        free(base->name);

138 139 140
    thread_mutex_unlock(&(base->lock));
    thread_mutex_destroy(&(base->lock));

141 142
    free(base);

143 144 145 146 147 148 149
    return 0;
}

void *          refobject_get_userdata(refobject_t self)
{
    void *ret;

150
    if (REFOBJECT_IS_NULL(self))
151 152 153 154 155 156 157 158 159 160 161
        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)
{
162
    if (REFOBJECT_IS_NULL(self))
163 164 165 166 167 168 169 170
        return -1;

    thread_mutex_lock(&(TO_BASE(self)->lock));
    TO_BASE(self)->userdata = userdata;
    thread_mutex_unlock(&(TO_BASE(self)->lock));

    return 0;
}
171 172 173 174 175

const char *    refobject_get_name(refobject_t self)
{
    const char *ret;

176
    if (REFOBJECT_IS_NULL(self))
177 178 179 180 181 182 183 184 185
        return NULL;

    thread_mutex_lock(&(TO_BASE(self)->lock));
    ret = TO_BASE(self)->name;
    thread_mutex_unlock(&(TO_BASE(self)->lock));

    return ret;
}

186
refobject_t     refobject_get_associated(refobject_t self)
187 188 189
{
    refobject_t ret;

190 191
    if (REFOBJECT_IS_NULL(self))
        return REFOBJECT_NULL;
192 193

    thread_mutex_lock(&(TO_BASE(self)->lock));
194
    ret = TO_BASE(self)->associated;
195 196 197 198
    thread_mutex_unlock(&(TO_BASE(self)->lock));

    return ret;
}