refbuf.c 2.23 KB
Newer Older
Jack Moffitt's avatar
Jack Moffitt committed
1 2 3 4 5 6
/* refbuf.c
**
** reference counting buffer implementation
**
*/

7 8 9 10
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

Jack Moffitt's avatar
Jack Moffitt committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#include <stdlib.h>
#include <string.h>

#include "refbuf.h"

void refbuf_initialize(void)
{
}

void refbuf_shutdown(void)
{
}

refbuf_t *refbuf_new(unsigned long size)
{
26
    refbuf_t *refbuf;
Jack Moffitt's avatar
Jack Moffitt committed
27

28 29 30 31
    refbuf = (refbuf_t *)malloc(sizeof(refbuf_t));
    refbuf->data = (void *)malloc(size);
    refbuf->len = size;
    refbuf->_count = 1;
Jack Moffitt's avatar
Jack Moffitt committed
32

33
    return refbuf;
Jack Moffitt's avatar
Jack Moffitt committed
34 35 36 37
}

void refbuf_addref(refbuf_t *self)
{
38
    self->_count++;
Jack Moffitt's avatar
Jack Moffitt committed
39 40 41 42
}

void refbuf_release(refbuf_t *self)
{
43 44 45 46
    self->_count--;
    if (self->_count == 0) {
        free(self->data);
        free(self);
47
        return;
48
    }
Jack Moffitt's avatar
Jack Moffitt committed
49 50 51 52
}

void refbuf_queue_add(refbuf_queue_t **queue, refbuf_t *refbuf)
{
53 54
    refbuf_queue_t *node;
    refbuf_queue_t *item = (refbuf_queue_t *)malloc(sizeof(refbuf_queue_t));
Jack Moffitt's avatar
Jack Moffitt committed
55

56 57
    item->refbuf = refbuf;
    item->next = NULL;
Jack Moffitt's avatar
Jack Moffitt committed
58

59 60
    if (*queue == NULL) {
        *queue = item;
Michael Smith's avatar
Michael Smith committed
61
        (*queue)->total_length = item->refbuf->len;
62 63 64 65
    } else {
        node = *queue;
        while (node->next) node = node->next;
        node->next = item;
Michael Smith's avatar
Michael Smith committed
66
        (*queue)->total_length += item->refbuf->len;
67
    }
Jack Moffitt's avatar
Jack Moffitt committed
68 69 70 71
}

refbuf_t *refbuf_queue_remove(refbuf_queue_t **queue)
{
72 73
    refbuf_queue_t *item;
    refbuf_t *refbuf;
Jack Moffitt's avatar
Jack Moffitt committed
74

75
    if (*queue == NULL) return NULL;
Jack Moffitt's avatar
Jack Moffitt committed
76

77 78 79
    item = *queue;
    *queue = item->next;
    item->next = NULL;
Jack Moffitt's avatar
Jack Moffitt committed
80

81 82
    refbuf = item->refbuf;
    item->refbuf = NULL;
83 84 85

    if(*queue)
        (*queue)->total_length = item->total_length - refbuf->len;
86 87
    
    free(item);
88

Jack Moffitt's avatar
Jack Moffitt committed
89
       
90
    return refbuf;
Jack Moffitt's avatar
Jack Moffitt committed
91 92 93 94
}

void refbuf_queue_insert(refbuf_queue_t **queue, refbuf_t *refbuf)
{
95
    refbuf_queue_t *item = (refbuf_queue_t *)malloc(sizeof(refbuf_queue_t));
Jack Moffitt's avatar
Jack Moffitt committed
96

97 98
    item->refbuf = refbuf;
    item->next = *queue;
99 100 101 102
    if(item->next)
        item->total_length = item->next->total_length + item->refbuf->len;
    else
        item->total_length = item->refbuf->len;
103
    *queue = item;
Jack Moffitt's avatar
Jack Moffitt committed
104 105 106 107
}

int refbuf_queue_size(refbuf_queue_t **queue)
{
108 109 110 111 112 113 114 115 116
    refbuf_queue_t *node = *queue;
    int size = 0;

    while (node) {
        node = node->next;
        size++;
    }
    
    return size;
Jack Moffitt's avatar
Jack Moffitt committed
117 118
}

119 120 121 122 123 124 125
int refbuf_queue_length(refbuf_queue_t **queue)
{
    if(*queue)
        return (*queue)->total_length;
    else
        return 0;
}
Jack Moffitt's avatar
Jack Moffitt committed
126 127