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