]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/queue.c
update version
[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
12 */
13 /*
14    Bacula® - The Network Backup Solution
15
16    Copyright (C) 2000-2006 Free Software Foundation Europe e.V.
17
18    The main author of Bacula is Kern Sibbald, with contributions from
19    many others, a complete list can be found in the file AUTHORS.
20    This program is Free Software; you can redistribute it and/or
21    modify it under the terms of version three of the GNU Affero General Public
22    License as published by the Free Software Foundation and included
23    in the file LICENSE.
24
25    This program is distributed in the hope that it will be useful, but
26    WITHOUT ANY WARRANTY; without even the implied warranty of
27    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
28    General Public License for more details.
29
30    You should have received a copy of the GNU Affero General Public License
31    along with this program; if not, write to the Free Software
32    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
33    02110-1301, USA.
34
35    Bacula® is a registered trademark of Kern Sibbald.
36    The licensor of Bacula is the Free Software Foundation Europe
37    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
38    Switzerland, email:ftf@fsfeurope.org.
39 */
40
41
42 #include "bacula.h"
43
44 /*  General purpose queue  */
45
46 #ifdef REALLY_NEEDED
47 struct b_queue {
48         struct b_queue *qnext,       /* Next item in queue */
49                      *qprev;       /* Previous item in queue */
50 };
51 #endif
52
53 /*
54  * To define a queue, use the following
55  *
56  *  static BQUEUE xyz = { &xyz, &xyz };
57  *
58  *   Also, note, that the only real requirement is that
59  *   the object that is passed to these routines contain
60  *   a BQUEUE object as the very first member. The
61  *   rest of the structure may be anything.
62  *
63  *   NOTE!!!! The casting here is REALLY painful, but this avoids
64  *            doing ugly casting every where else in the code.
65  */
66
67
68 /*  Queue manipulation functions.  */
69
70
71 /*  QINSERT  --  Insert object at end of queue  */
72
73 void qinsert(BQUEUE *qhead, BQUEUE *object)
74 {
75 #define qh ((BQUEUE *)qhead)
76 #define obj ((BQUEUE *)object)
77
78         ASSERT(qh->qprev->qnext == qh);
79         ASSERT(qh->qnext->qprev == qh);
80
81         obj->qnext = qh;
82         obj->qprev = qh->qprev;
83         qh->qprev = obj;
84         obj->qprev->qnext = obj;
85 #undef qh
86 #undef obj
87 }
88
89
90 /*  QREMOVE  --  Remove next object from the queue given
91                  the queue head (or any item).
92      Returns NULL if queue is empty  */
93
94 BQUEUE *qremove(BQUEUE *qhead)
95 {
96 #define qh ((BQUEUE *)qhead)
97         BQUEUE *object;
98
99         ASSERT(qh->qprev->qnext == qh);
100         ASSERT(qh->qnext->qprev == qh);
101
102         if ((object = qh->qnext) == qh)
103            return NULL;
104         qh->qnext = object->qnext;
105         object->qnext->qprev = qh;
106         return object;
107 #undef qh
108 }
109
110 /*  QNEXT   --   Return next item from the queue
111  *               returns NULL at the end of the queue.
112  *               If qitem is NULL, the first item from
113  *               the queue is returned.
114  */
115
116 BQUEUE *qnext(BQUEUE *qhead, BQUEUE *qitem)
117 {
118 #define qh ((BQUEUE *)qhead)
119 #define qi ((BQUEUE *)qitem)
120
121         BQUEUE *object;
122
123         if (qi == NULL)
124            qitem = qhead;
125         ASSERT(qi->qprev->qnext == qi);
126         ASSERT(qi->qnext->qprev == qi);
127
128         if ((object = qi->qnext) == qh)
129            return NULL;
130         return object;
131 #undef qh
132 #undef qi
133 }
134
135
136 /*  QDCHAIN  --  Dequeue an item from the middle of a queue.  Passed
137                  the queue item, returns the (now dechained) queue item. */
138
139 BQUEUE *qdchain(BQUEUE *qitem)
140 {
141 #define qi ((BQUEUE *)qitem)
142
143         ASSERT(qi->qprev->qnext == qi);
144         ASSERT(qi->qnext->qprev == qi);
145
146         return qremove(qi->qprev);
147 #undef qi
148 }