2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2015 Kern Sibbald
5 Copyright (C) 2000-2014 Free Software Foundation Europe e.V.
7 The original author of Bacula is Kern Sibbald, with contributions
8 from many others, a complete list can be found in the file AUTHORS.
10 You may use this file and others of this release according to the
11 license defined in the LICENSE file, which includes the Affero General
12 Public License, v3.0 ("AGPLv3") and some additional permissions and
13 terms pursuant to its AGPLv3 Section 7.
15 This notice must be preserved when any source code is
16 conveyed and/or propagated.
18 Bacula(R) is a registered trademark of Kern Sibbald.
23 Queue Handling Routines
25 Taken from smartall written by John Walker.
27 http://www.fourmilab.ch/smartall/
35 /* General purpose queue */
39 struct b_queue *qnext, /* Next item in queue */
40 *qprev; /* Previous item in queue */
45 * To define a queue, use the following
47 * static BQUEUE xyz = { &xyz, &xyz };
49 * Also, note, that the only real requirement is that
50 * the object that is passed to these routines contain
51 * a BQUEUE object as the very first member. The
52 * rest of the structure may be anything.
54 * NOTE!!!! The casting here is REALLY painful, but this avoids
55 * doing ugly casting every where else in the code.
59 /* Queue manipulation functions. */
62 /* QINSERT -- Insert object at end of queue */
64 void qinsert(BQUEUE *qhead, BQUEUE *object)
66 #define qh ((BQUEUE *)qhead)
67 #define obj ((BQUEUE *)object)
69 ASSERT(qh->qprev->qnext == qh);
70 ASSERT(qh->qnext->qprev == qh);
73 obj->qprev = qh->qprev;
75 obj->qprev->qnext = obj;
81 /* QREMOVE -- Remove next object from the queue given
82 the queue head (or any item).
83 Returns NULL if queue is empty */
85 BQUEUE *qremove(BQUEUE *qhead)
87 #define qh ((BQUEUE *)qhead)
90 ASSERT(qh->qprev->qnext == qh);
91 ASSERT(qh->qnext->qprev == qh);
93 if ((object = qh->qnext) == qh)
95 qh->qnext = object->qnext;
96 object->qnext->qprev = qh;
101 /* QNEXT -- Return next item from the queue
102 * returns NULL at the end of the queue.
103 * If qitem is NULL, the first item from
104 * the queue is returned.
107 BQUEUE *qnext(BQUEUE *qhead, BQUEUE *qitem)
109 #define qh ((BQUEUE *)qhead)
110 #define qi ((BQUEUE *)qitem)
116 ASSERT(qi->qprev->qnext == qi);
117 ASSERT(qi->qnext->qprev == qi);
119 if ((object = qi->qnext) == qh)
127 /* QDCHAIN -- Dequeue an item from the middle of a queue. Passed
128 the queue item, returns the (now dechained) queue item. */
130 BQUEUE *qdchain(BQUEUE *qitem)
132 #define qi ((BQUEUE *)qitem)
134 ASSERT(qi->qprev->qnext == qi);
135 ASSERT(qi->qnext->qprev == qi);
137 return qremove(qi->qprev);