1
/***************************************************************************
3
* Copyright (c) 1998-1999 Niels M�ller
4
* Copyright (c) 1999 BalaBit Computing
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
* $Id: queue.c,v 1.3 1999/11/22 18:26:24 bazsi Exp $
22
***************************************************************************/
30
static void do_queue_mark(struct ol_queue *q,
31
void (*mark)(struct ol_object *o));
33
static void do_queue_free(struct ol_queue *q);
40
#define next np_links[LSH_QUEUE_NEXT]
41
#define prev np_links[LSH_QUEUE_PREV]
43
#define head ht_links[LSH_QUEUE_HEAD]
44
#define tail ht_links[LSH_QUEUE_TAIL]
45
#define tailprev ht_links[LSH_QUEUE_TAILPREV]
47
#define EMPTYP(q) ((q)->tailprev == (struct ol_queue_node *) (q))
50
static void sanity_check_queue(struct ol_queue *q)
52
struct ol_queue_node *n;
54
debug("sanity_check_queue: q = %xi\n", (UINT32) q);
56
fatal("sanity_check_queue: q->tail not NULL!\n");
63
debug(" queue is empty\n");
65
fatal("sanity_check_queue: "
66
"Queue looks empty, but n->prev not NULL!\n");
67
if (q->tail != (struct ol_queue_node *) q)
68
fatal("sanity_check_queue: "
69
"Queue looks empty, but q->tail != q !\n");
73
if (n->prev != (struct ol_queue_node *) q)
74
fatal("sanity_check_queue: head->next != &q->head !\n");
78
debug(" n = %xi\n", (UINT32) n);
80
if (n->prev->next != n)
81
fatal("n->prev->next != n !\n");
85
if (n != (struct ol_queue_node *) &(q->tail))
86
fatal("n != n &t->tail!\n");
89
#define sanity_check_queue(x)
92
void ol_queue_init(struct ol_queue *q)
94
q->head = (struct ol_queue_node *) &(q->tail);
96
q->tailprev = (struct ol_queue_node *) &(q->head);
97
sanity_check_queue(q);
100
int ol_queue_is_empty(struct ol_queue *q)
102
sanity_check_queue(q);
106
void ol_queue_add_head(struct ol_queue *q, struct ol_queue_node *n)
108
sanity_check_queue(q);
110
n->prev = (struct ol_queue_node *) &(q->head);
113
sanity_check_queue(q);
116
void ol_queue_add_tail(struct ol_queue *q, struct ol_queue_node *n)
118
sanity_check_queue(q);
119
n->next = (struct ol_queue_node *) &(q->tail);
120
n->prev = q->tailprev;
123
sanity_check_queue(q);
126
void ol_queue_remove(struct ol_queue_node *n)
130
n->next->prev = n->prev;
131
n->prev->next = n->next;
134
struct ol_queue_node *ol_queue_remove_head(struct ol_queue *q)
136
struct ol_queue_node *n = q->head;
138
sanity_check_queue(q);
141
sanity_check_queue(q);
146
struct ol_queue_node *ol_queue_remove_tail(struct ol_queue *q)
148
struct ol_queue_node *n = q->tailprev;
150
sanity_check_queue(q);
153
sanity_check_queue(q);
160
static struct object_queue_node *
161
make_object_queue_node(struct ol_object *o)
163
struct object_queue_node *n;
171
struct object_queue *make_object_queue(void)
173
NEW(object_queue, q);
174
ol_queue_init(&q->q);
178
int object_queue_is_empty(struct object_queue *q)
180
return EMPTYP(&q->q);
183
struct object_queue_node *
184
object_queue_add_head(struct object_queue *q, struct ol_object *o)
186
struct object_queue_node *n = make_object_queue_node(o);
188
ol_queue_add_head(&q->q, &n->header);
192
struct object_queue_node *
193
object_queue_add_tail(struct object_queue *q, struct ol_object *o)
195
struct object_queue_node *n = make_object_queue_node(o);
196
ol_queue_add_tail(&q->q, &n->header);
200
static struct ol_object *
201
object_queue_get_contents(struct ol_queue_node *l)
203
struct object_queue_node *n = (struct object_queue_node *) l;
205
struct ol_object *res = n->o;
211
static struct ol_object *
212
object_queue_peek(struct ol_queue_node *n)
214
return ( (struct object_queue_node *) n)->o;
217
void object_queue_remove(struct object_queue_node *n)
219
ol_queue_remove(&n->header);
223
struct ol_object *object_queue_remove_head(struct object_queue *q)
225
return object_queue_get_contents(ol_queue_remove_head(&q->q));
228
struct ol_object *object_queue_remove_tail(struct object_queue *q)
230
return object_queue_get_contents(ol_queue_remove_tail(&q->q));
233
struct ol_object *object_queue_peek_head(struct object_queue *q)
235
return EMPTYP(&q->q) ? NULL : object_queue_peek(q->q.head);
238
struct ol_object *object_queue_peek_tail(struct object_queue *q)
240
return EMPTYP(&q->q) ? NULL : object_queue_peek(q->q.tailprev);
244
static void do_queue_mark(struct ol_queue *q,
245
void (*mark)(struct ol_object *o))
247
FOR_QUEUE(q, struct object_queue_node *, n)
251
static void do_queue_free(struct ol_queue *q)
253
FOR_QUEUE(q, struct object_queue_node *, n)
257
void object_queue_kill(struct object_queue *q)
259
do_queue_free(&q->q);