]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/queue.c
- Use a bigger buffer 32K as suggested by Arno in bpipe.c.
[bacula/bacula] / bacula / src / lib / queue.c
1 /*
2
3                          Q U E U E
4                      Queue Handling Routines
5
6         Taken from smartall written by John Walker.
7
8                   http://www.fourmilab.ch/smartall/
9
10
11         Version $Id$
12
13 */
14
15 /*
16    Copyright (C) 2000-2004 Kern Sibbald and John Walker
17
18    This program is free software; you can redistribute it and/or
19    modify it under the terms of the GNU General Public License as
20    published by the Free Software Foundation; either version 2 of
21    the License, or (at your option) any later version.
22
23    This program is distributed in the hope that it will be useful,
24    but WITHOUT ANY WARRANTY; without even the implied warranty of
25    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26    General Public License for more details.
27
28    You should have received a copy of the GNU General Public
29    License along with this program; if not, write to the Free
30    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
31    MA 02111-1307, USA.
32
33  */
34
35
36 #include "bacula.h"
37
38 /*  General purpose queue  */
39
40 #ifdef REALLY_NEEDED
41 struct b_queue {
42         struct b_queue *qnext,       /* Next item in queue */
43                      *qprev;       /* Previous item in queue */
44 };
45 #endif
46
47 /*
48  * To define a queue, use the following
49  *
50  *  static BQUEUE xyz = { &xyz, &xyz };
51  *
52  *   Also, note, that the only real requirement is that
53  *   the object that is passed to these routines contain
54  *   a BQUEUE object as the very first member. The
55  *   rest of the structure may be anything.
56  *
57  *   NOTE!!!! The casting here is REALLY painful, but this avoids
58  *            doing ugly casting every where else in the code.
59  */
60
61
62 /*  Queue manipulation functions.  */
63
64
65 /*  QINSERT  --  Insert object at end of queue  */
66
67 void qinsert(BQUEUE *qhead, BQUEUE *object)
68 {
69 #define qh ((BQUEUE *)qhead)
70 #define obj ((BQUEUE *)object)
71
72         ASSERT(qh->qprev->qnext == qh);
73         ASSERT(qh->qnext->qprev == qh);
74
75         obj->qnext = qh;
76         obj->qprev = qh->qprev;
77         qh->qprev = obj;
78         obj->qprev->qnext = obj;
79 #undef qh
80 #undef obj
81 }
82
83
84 /*  QREMOVE  --  Remove next object from the queue given
85                  the queue head (or any item).
86      Returns NULL if queue is empty  */
87
88 BQUEUE *qremove(BQUEUE *qhead)
89 {
90 #define qh ((BQUEUE *)qhead)
91         BQUEUE *object;
92
93         ASSERT(qh->qprev->qnext == qh);
94         ASSERT(qh->qnext->qprev == qh);
95
96         if ((object = qh->qnext) == qh)
97            return NULL;
98         qh->qnext = object->qnext;
99         object->qnext->qprev = qh;
100         return object;
101 #undef qh
102 }
103
104 /*  QNEXT   --   Return next item from the queue
105  *               returns NULL at the end of the queue.
106  *               If qitem is NULL, the first item from
107  *               the queue is returned.
108  */
109
110 BQUEUE *qnext(BQUEUE *qhead, BQUEUE *qitem)
111 {
112 #define qh ((BQUEUE *)qhead)
113 #define qi ((BQUEUE *)qitem)
114
115         BQUEUE *object;
116
117         if (qi == NULL)
118            qitem = qhead;
119         ASSERT(qi->qprev->qnext == qi);
120         ASSERT(qi->qnext->qprev == qi);
121
122         if ((object = qi->qnext) == qh)
123            return NULL;
124         return object;
125 #undef qh
126 #undef qi
127 }
128
129
130 /*  QDCHAIN  --  Dequeue an item from the middle of a queue.  Passed
131                  the queue item, returns the (now dechained) queue item. */
132
133 BQUEUE *qdchain(BQUEUE *qitem)
134 {
135 #define qi ((BQUEUE *)qitem)
136
137         ASSERT(qi->qprev->qnext == qi);
138         ASSERT(qi->qnext->qprev == qi);
139
140         return qremove(qi->qprev);
141 #undef qi
142 }