2
* (C) 2006-2008 by Pablo Neira Ayuso <pablo@netfilter.org>
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25
struct queue *queue_create(size_t max_size)
29
b = malloc(sizeof(struct queue));
32
memset(b, 0, sizeof(struct queue));
34
b->max_size = max_size;
35
INIT_LIST_HEAD(&b->head);
40
void queue_destroy(struct queue *b)
42
struct list_head *i, *tmp;
43
struct queue_node *node;
45
/* XXX: set cur_size and num_elems */
46
list_for_each_safe(i, tmp, &b->head) {
47
node = (struct queue_node *) i;
54
static struct queue_node *queue_node_create(const void *data, size_t size)
58
n = malloc(sizeof(struct queue_node) + size);
63
memcpy(n->data, data, size);
68
int queue_add(struct queue *b, const void *data, size_t size)
73
/* does it fit this queue? */
74
if (size > b->max_size) {
81
/* queue is full: kill the oldest entry */
82
if (b->cur_size + size > b->max_size) {
83
n = (struct queue_node *) b->head.prev;
84
list_del(b->head.prev);
85
b->cur_size -= n->size;
90
n = queue_node_create(data, size);
96
list_add(&n->head, &b->head);
104
void queue_del(struct queue *b, void *data)
106
struct queue_node *n = container_of(data, struct queue_node, data);
109
b->cur_size -= n->size;
114
void queue_iterate(struct queue *b,
116
int (*iterate)(void *data1, const void *data2))
118
struct list_head *i, *tmp;
119
struct queue_node *n;
121
list_for_each_safe(i, tmp, &b->head) {
122
n = (struct queue_node *) i;
123
if (iterate(n->data, data))
128
unsigned int queue_len(const struct queue *b)