~ubuntu-branches/ubuntu/feisty/syslog-ng/feisty-updates

« back to all changes in this revision

Viewing changes to libol-0.3.17/src/queue.c

  • Committer: Bazaar Package Importer
  • Author(s): SZALAY Attila
  • Date: 2006-05-25 11:21:50 UTC
  • mfrom: (1.1.5 upstream)
  • Revision ID: james.westby@ubuntu.com-20060525112150-b18srkxcrz980xi9
Tags: 1.9.11-1
* New upstream version
  - Fixed log facility and priority detecting. (Closes: #350120, #350344, #357071, #367256)
* Added bison to Build-Dependency. (Closes: #368765)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
 *
3
 
 * Copyright (c) 1998-1999 Niels M�ller
4
 
 * Copyright (c) 1999 BalaBit Computing
5
 
 *
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.
10
 
 *
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.
15
 
 *
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.
19
 
 *
20
 
 * $Id: queue.c,v 1.3 1999/11/22 18:26:24 bazsi Exp $
21
 
 *
22
 
 ***************************************************************************/
23
 
 
24
 
#include "queue.h"
25
 
 
26
 
#include "werror.h"
27
 
 
28
 
#include <assert.h>
29
 
 
30
 
static void do_queue_mark(struct ol_queue *q,
31
 
                                 void (*mark)(struct ol_object *o));
32
 
 
33
 
static void do_queue_free(struct ol_queue *q);
34
 
 
35
 
#define CLASS_DEFINE
36
 
#include "queue.h.x"
37
 
#undef CLASS_DEFINE
38
 
 
39
 
/* Short cuts */
40
 
#define next np_links[LSH_QUEUE_NEXT]
41
 
#define prev np_links[LSH_QUEUE_PREV]
42
 
 
43
 
#define head ht_links[LSH_QUEUE_HEAD]
44
 
#define tail ht_links[LSH_QUEUE_TAIL]
45
 
#define tailprev ht_links[LSH_QUEUE_TAILPREV]
46
 
 
47
 
#define EMPTYP(q) ((q)->tailprev == (struct ol_queue_node *) (q))
48
 
 
49
 
#if 0
50
 
static void sanity_check_queue(struct ol_queue *q)
51
 
{
52
 
  struct ol_queue_node *n;
53
 
 
54
 
  debug("sanity_check_queue: q = %xi\n", (UINT32) q);
55
 
  if (q->tail)
56
 
    fatal("sanity_check_queue: q->tail not NULL!\n");
57
 
 
58
 
  n = q->head;
59
 
 
60
 
#if 0
61
 
  if (EMPTYP(q))
62
 
    {
63
 
      debug("  queue is empty\n");
64
 
      if (n->prev)
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");
70
 
      return;
71
 
    }
72
 
#endif
73
 
  if (n->prev != (struct ol_queue_node *) q)
74
 
    fatal("sanity_check_queue: head->next != &q->head !\n");
75
 
 
76
 
  while (n->next)
77
 
    {
78
 
      debug("  n = %xi\n", (UINT32) n);
79
 
      
80
 
      if (n->prev->next != n)
81
 
        fatal("n->prev->next != n !\n");
82
 
 
83
 
      n = n->next;
84
 
    }
85
 
  if (n != (struct ol_queue_node *) &(q->tail))
86
 
    fatal("n != n &t->tail!\n");
87
 
}
88
 
#else
89
 
#define sanity_check_queue(x)
90
 
#endif
91
 
 
92
 
void ol_queue_init(struct ol_queue *q)
93
 
{
94
 
  q->head = (struct ol_queue_node *) &(q->tail);
95
 
  q->tail = NULL;
96
 
  q->tailprev = (struct ol_queue_node *) &(q->head);
97
 
  sanity_check_queue(q);
98
 
}
99
 
 
100
 
int ol_queue_is_empty(struct ol_queue *q)
101
 
{
102
 
  sanity_check_queue(q);
103
 
  return EMPTYP(q);
104
 
}
105
 
 
106
 
void ol_queue_add_head(struct ol_queue *q, struct ol_queue_node *n)
107
 
{
108
 
  sanity_check_queue(q);
109
 
  n->next = q->head;
110
 
  n->prev = (struct ol_queue_node *) &(q->head);
111
 
  n->prev->next = n;
112
 
  n->next->prev = n;
113
 
  sanity_check_queue(q);
114
 
}
115
 
 
116
 
void ol_queue_add_tail(struct ol_queue *q, struct ol_queue_node *n)
117
 
{
118
 
  sanity_check_queue(q);
119
 
  n->next = (struct ol_queue_node *) &(q->tail);
120
 
  n->prev = q->tailprev;
121
 
  n->prev->next = n;
122
 
  n->next->prev = n;
123
 
  sanity_check_queue(q);
124
 
}
125
 
 
126
 
void ol_queue_remove(struct ol_queue_node *n)
127
 
{
128
 
  assert(n->next);
129
 
  assert(n->prev);
130
 
  n->next->prev = n->prev;
131
 
  n->prev->next = n->next;
132
 
}
133
 
 
134
 
struct ol_queue_node *ol_queue_remove_head(struct ol_queue *q)
135
 
{
136
 
  struct ol_queue_node *n = q->head;
137
 
 
138
 
  sanity_check_queue(q);
139
 
  assert(!EMPTYP(q));
140
 
  ol_queue_remove(n);
141
 
  sanity_check_queue(q);
142
 
 
143
 
  return n;
144
 
}
145
 
 
146
 
struct ol_queue_node *ol_queue_remove_tail(struct ol_queue *q)
147
 
{
148
 
  struct ol_queue_node *n = q->tailprev;
149
 
  
150
 
  sanity_check_queue(q);
151
 
  assert(!EMPTYP(q));
152
 
  ol_queue_remove(n);
153
 
  sanity_check_queue(q);
154
 
 
155
 
  return n;
156
 
}
157
 
 
158
 
  
159
 
/* object_queue */
160
 
static struct object_queue_node *
161
 
make_object_queue_node(struct ol_object *o)
162
 
{
163
 
  struct object_queue_node *n;
164
 
 
165
 
  NEW_SPACE(n);
166
 
  n->o = o;
167
 
 
168
 
  return n;
169
 
}
170
 
 
171
 
struct object_queue *make_object_queue(void)
172
 
{
173
 
  NEW(object_queue, q);
174
 
  ol_queue_init(&q->q);
175
 
  return q;
176
 
}
177
 
 
178
 
int object_queue_is_empty(struct object_queue *q)
179
 
{
180
 
  return EMPTYP(&q->q);
181
 
}
182
 
 
183
 
struct object_queue_node *
184
 
object_queue_add_head(struct object_queue *q, struct ol_object *o)
185
 
{
186
 
  struct object_queue_node *n = make_object_queue_node(o);
187
 
 
188
 
  ol_queue_add_head(&q->q, &n->header);
189
 
  return n;
190
 
}
191
 
 
192
 
struct object_queue_node *
193
 
object_queue_add_tail(struct object_queue *q, struct ol_object *o)
194
 
{
195
 
  struct object_queue_node *n = make_object_queue_node(o);
196
 
  ol_queue_add_tail(&q->q, &n->header);
197
 
  return n;
198
 
}
199
 
 
200
 
static struct ol_object *
201
 
object_queue_get_contents(struct ol_queue_node *l)
202
 
{
203
 
  struct object_queue_node *n = (struct object_queue_node *) l;
204
 
 
205
 
  struct ol_object *res = n->o;
206
 
  ol_space_free(n);
207
 
 
208
 
  return res;
209
 
}
210
 
 
211
 
static struct ol_object *
212
 
object_queue_peek(struct ol_queue_node *n)
213
 
{
214
 
  return ( (struct object_queue_node *) n)->o;
215
 
}
216
 
 
217
 
void object_queue_remove(struct object_queue_node *n)
218
 
{
219
 
  ol_queue_remove(&n->header);
220
 
  ol_space_free(n);
221
 
}
222
 
 
223
 
struct ol_object *object_queue_remove_head(struct object_queue *q)
224
 
{
225
 
  return object_queue_get_contents(ol_queue_remove_head(&q->q));
226
 
}
227
 
 
228
 
struct ol_object *object_queue_remove_tail(struct object_queue *q)
229
 
{
230
 
  return object_queue_get_contents(ol_queue_remove_tail(&q->q));
231
 
}
232
 
 
233
 
struct ol_object *object_queue_peek_head(struct object_queue *q)
234
 
{
235
 
  return EMPTYP(&q->q) ? NULL : object_queue_peek(q->q.head);
236
 
}
237
 
 
238
 
struct ol_object *object_queue_peek_tail(struct object_queue *q)
239
 
{
240
 
  return EMPTYP(&q->q) ? NULL : object_queue_peek(q->q.tailprev);
241
 
}
242
 
 
243
 
/* For gc */
244
 
static void do_queue_mark(struct ol_queue *q,
245
 
                                 void (*mark)(struct ol_object *o))
246
 
{
247
 
  FOR_QUEUE(q, struct object_queue_node *, n)
248
 
    mark(n->o);
249
 
}
250
 
 
251
 
static void do_queue_free(struct ol_queue *q)
252
 
{
253
 
  FOR_QUEUE(q, struct object_queue_node *, n)
254
 
    ol_space_free(n);
255
 
}
256
 
 
257
 
void object_queue_kill(struct object_queue *q)
258
 
{
259
 
  do_queue_free(&q->q);
260
 
}