summaryrefslogtreecommitdiff
path: root/src/buffer.c
blob: 23f7797ec20df661d0cbd71098037a1fa1e9e6ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * (C) 2006-2007 by Pablo Neira Ayuso <pablo@netfilter.org>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "buffer.h"

struct buffer *buffer_create(size_t max_size)
{
	struct buffer *b;

	b = malloc(sizeof(struct buffer));
	if (b == NULL)
		return NULL;
	memset(b, 0, sizeof(struct buffer));

	b->max_size = max_size;
	INIT_LIST_HEAD(&b->head);

	return b;
}

void buffer_destroy(struct buffer *b)
{
	struct list_head *i, *tmp;
	struct buffer_node *node;

	/* XXX: set cur_size and num_elems */
	list_for_each_safe(i, tmp, &b->head) {
		node = (struct buffer_node *) i;
		list_del(i);
		free(node);
	}
	free(b);
}

static struct buffer_node *buffer_node_create(const void *data, size_t size)
{
	struct buffer_node *n;

	n = malloc(sizeof(struct buffer_node) + size);
	if (n == NULL)
		return NULL;

	INIT_LIST_HEAD(&n->head);
	n->size = size;
	memcpy(n->data, data, size);

	return n;
}

int buffer_add(struct buffer *b, const void *data, size_t size)
{
	int ret = 0;
	struct buffer_node *n;

	/* does it fit this buffer? */
	if (size > b->max_size) {
		errno = ENOSPC;
		ret = -1;
		goto err;
	}

retry:
	/* buffer is full: kill the oldest entry */
	if (b->cur_size + size > b->max_size) {
		n = (struct buffer_node *) b->head.prev;
		list_del(b->head.prev);
		b->cur_size -= n->size;
		free(n);
		goto retry;
	}

	n = buffer_node_create(data, size);
	if (n == NULL) {
		ret = -1;
		goto err;
	}

	list_add(&n->head, &b->head);
	b->cur_size += size;
	b->num_elems++;

err:
	return ret;
}

void buffer_del(struct buffer *b, void *data)
{
	struct buffer_node *n = container_of(data, struct buffer_node, data); 

	list_del(&n->head);
	b->cur_size -= n->size;
	b->num_elems--;
	free(n);
}

void buffer_iterate(struct buffer *b, 
		    void *data, 
		    int (*iterate)(void *data1, void *data2))
{
	struct list_head *i, *tmp;
	struct buffer_node *n;

	list_for_each_safe(i, tmp, &b->head) {
		n = (struct buffer_node *) i;
		if (iterate(n->data, data))
			break;
	}
}

unsigned int buffer_len(struct buffer *b)
{
	return b->num_elems;
}