refobject.c 3.19 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
refobject_t     refobject_new(size_t len, refobject_free_t freecb, void *userdata, const char *name, refobject_t associated)
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
{
    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));

39 40 41 42
    if (name) {
        ret->name = strdup(name);
        if (!ret->name) {
            refobject_unref(ret);
43
            return REFOBJECT_NULL;
44 45 46
        }
    }

47 48
    if (!REFOBJECT_IS_NULL(associated)) {
        if (refobject_ref(associated) != 0) {
49
            refobject_unref(ret);
50
            return REFOBJECT_NULL;
51 52
        }

53
        ret->associated = associated;
54 55
    }

56 57 58 59 60
    return (refobject_t)ret;
}

int             refobject_ref(refobject_t self)
{
61
    if (REFOBJECT_IS_NULL(self))
62 63 64 65 66 67 68 69 70 71 72 73 74
        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);

75
    if (REFOBJECT_IS_NULL(self))
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
        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);

91 92 93
    if (base->name)
        free(base->name);

94 95 96
    thread_mutex_unlock(&(base->lock));
    thread_mutex_destroy(&(base->lock));

97 98
    free(base);

99 100 101 102 103 104 105
    return 0;
}

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

106
    if (REFOBJECT_IS_NULL(self))
107 108 109 110 111 112 113 114 115 116 117
        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)
{
118
    if (REFOBJECT_IS_NULL(self))
119 120 121 122 123 124 125 126
        return -1;

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

    return 0;
}
127 128 129 130 131

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

132
    if (REFOBJECT_IS_NULL(self))
133 134 135 136 137 138 139 140 141
        return NULL;

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

    return ret;
}

142
refobject_t     refobject_get_associated(refobject_t self)
143 144 145
{
    refobject_t ret;

146 147
    if (REFOBJECT_IS_NULL(self))
        return REFOBJECT_NULL;
148 149

    thread_mutex_lock(&(TO_BASE(self)->lock));
150
    ret = TO_BASE(self)->associated;
151 152 153 154
    thread_mutex_unlock(&(TO_BASE(self)->lock));

    return ret;
}