refbuf.c 1.79 KB
Newer Older
1 2 3 4 5
/* Icecast
 *
 * This program is distributed under the GNU General Public License, version 2.
 * A copy of this license is included with this source.
 *
6
 * Copyright 2000-2004, Jack Moffitt <jack@xiph.org,
7 8 9 10 11 12
 *                      Michael Smith <msmith@xiph.org>,
 *                      oddsock <oddsock@xiph.org>,
 *                      Karl Heyes <karl@xiph.org>
 *                      and others (see AUTHORS for details).
 */

Jack Moffitt's avatar
Jack Moffitt committed
13
/* refbuf.c
14 15 16 17
 **
 ** reference counting buffer implementation
 **
 */
Jack Moffitt's avatar
Jack Moffitt committed
18

19 20 21 22
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

Jack Moffitt's avatar
Jack Moffitt committed
23 24 25 26 27
#include <stdlib.h>
#include <string.h>

#include "refbuf.h"

28 29 30 31 32
#define CATMODULE "refbuf"

#include "logging.h"


Jack Moffitt's avatar
Jack Moffitt committed
33 34 35 36 37 38 39 40
void refbuf_initialize(void)
{
}

void refbuf_shutdown(void)
{
}

41
refbuf_t *refbuf_new (unsigned int size)
Jack Moffitt's avatar
Jack Moffitt committed
42
{
43
    refbuf_t *refbuf;
Jack Moffitt's avatar
Jack Moffitt committed
44

45
    refbuf = (refbuf_t *)malloc(sizeof(refbuf_t));
46
    if (refbuf == NULL)
47
        abort();
48 49 50 51 52
    refbuf->data = NULL;
    if (size)
    {
        refbuf->data = malloc (size);
        if (refbuf->data == NULL)
53
            abort();
54
    }
55
    refbuf->len = size;
56
    refbuf->sync_point = 0;
57
    refbuf->_count = 1;
58 59
    refbuf->next = NULL;
    refbuf->associated = NULL;
Jack Moffitt's avatar
Jack Moffitt committed
60

61
    return refbuf;
Jack Moffitt's avatar
Jack Moffitt committed
62 63 64 65
}

void refbuf_addref(refbuf_t *self)
{
66
    self->_count++;
Jack Moffitt's avatar
Jack Moffitt committed
67 68
}

69 70 71 72
static void refbuf_release_associated (refbuf_t *ref)
{
    if (ref == NULL)
        return;
73
    while (ref)
74 75 76
    {
        refbuf_t *to_go = ref;
        ref = to_go->next;
77
        if ( to_go->_count == 1 )
78
            to_go->next = NULL;
79 80 81 82
        refbuf_release (to_go);
    }
}

Jack Moffitt's avatar
Jack Moffitt committed
83 84
void refbuf_release(refbuf_t *self)
{
85 86
    if (self == NULL)
        return;
87
    self->_count--;
88 89 90 91
    if (self->_count == 0)
    {
        refbuf_release_associated (self->associated);
        if (self->next)
92
            ICECAST_LOG_ERROR("next not null");
93 94 95
        free(self->data);
        free(self);
    }
Jack Moffitt's avatar
Jack Moffitt committed
96 97
}