4 Queue Handling Routines
6 Taken from smartall written by John Walker.
8 http://www.fourmilab.ch/smartall/
14 Bacula® - The Network Backup Solution
16 Copyright (C) 2000-2014 Free Software Foundation Europe e.V.
18 The main author of Bacula is Kern Sibbald, with contributions from many
19 others, a complete list can be found in the file AUTHORS.
21 You may use this file and others of this release according to the
22 license defined in the LICENSE file, which includes the Affero General
23 Public License, v3.0 ("AGPLv3") and some additional permissions and
24 terms pursuant to its AGPLv3 Section 7.
26 Bacula® is a registered trademark of Kern Sibbald.
32 /* General purpose queue */
36 struct b_queue *qnext, /* Next item in queue */
37 *qprev; /* Previous item in queue */
42 * To define a queue, use the following
44 * static BQUEUE xyz = { &xyz, &xyz };
46 * Also, note, that the only real requirement is that
47 * the object that is passed to these routines contain
48 * a BQUEUE object as the very first member. The
49 * rest of the structure may be anything.
51 * NOTE!!!! The casting here is REALLY painful, but this avoids
52 * doing ugly casting every where else in the code.
56 /* Queue manipulation functions. */
59 /* QINSERT -- Insert object at end of queue */
61 void qinsert(BQUEUE *qhead, BQUEUE *object)
63 #define qh ((BQUEUE *)qhead)
64 #define obj ((BQUEUE *)object)
66 ASSERT(qh->qprev->qnext == qh);
67 ASSERT(qh->qnext->qprev == qh);
70 obj->qprev = qh->qprev;
72 obj->qprev->qnext = obj;
78 /* QREMOVE -- Remove next object from the queue given
79 the queue head (or any item).
80 Returns NULL if queue is empty */
82 BQUEUE *qremove(BQUEUE *qhead)
84 #define qh ((BQUEUE *)qhead)
87 ASSERT(qh->qprev->qnext == qh);
88 ASSERT(qh->qnext->qprev == qh);
90 if ((object = qh->qnext) == qh)
92 qh->qnext = object->qnext;
93 object->qnext->qprev = qh;
98 /* QNEXT -- Return next item from the queue
99 * returns NULL at the end of the queue.
100 * If qitem is NULL, the first item from
101 * the queue is returned.
104 BQUEUE *qnext(BQUEUE *qhead, BQUEUE *qitem)
106 #define qh ((BQUEUE *)qhead)
107 #define qi ((BQUEUE *)qitem)
113 ASSERT(qi->qprev->qnext == qi);
114 ASSERT(qi->qnext->qprev == qi);
116 if ((object = qi->qnext) == qh)
124 /* QDCHAIN -- Dequeue an item from the middle of a queue. Passed
125 the queue item, returns the (now dechained) queue item. */
127 BQUEUE *qdchain(BQUEUE *qitem)
129 #define qi ((BQUEUE *)qitem)
131 ASSERT(qi->qprev->qnext == qi);
132 ASSERT(qi->qnext->qprev == qi);
134 return qremove(qi->qprev);