4 Queue Handling Routines
6 Taken from smartall written by John Walker.
8 http://www.fourmilab.ch/smartall/
16 Copyright (C) 2000, 2001, 2002 Kern Sibbald and John Walker
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.
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.
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,
38 /* General purpose queue */
42 struct b_queue *qnext, /* Next item in queue */
43 *qprev; /* Previous item in queue */
48 * To define a queue, use the following
50 * static BQUEUE xyz = { &xyz, &xyz };
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.
57 * NOTE!!!! The casting here is REALLY painful, but this avoids
58 * doing ugly casting every where else in the code.
62 /* Queue manipulation functions. */
65 /* QINSERT -- Insert object at end of queue */
67 void qinsert(BQUEUE *qhead, BQUEUE *object)
69 #define qh ((BQUEUE *)qhead)
70 #define obj ((BQUEUE *)object)
72 ASSERT(qh->qprev->qnext == qh);
73 ASSERT(qh->qnext->qprev == qh);
76 obj->qprev = qh->qprev;
78 obj->qprev->qnext = obj;
84 /* QREMOVE -- Remove next object from the queue given
85 the queue head (or any item).
86 Returns NULL if queue is empty */
88 BQUEUE *qremove(BQUEUE *qhead)
90 #define qh ((BQUEUE *)qhead)
93 ASSERT(qh->qprev->qnext == qh);
94 ASSERT(qh->qnext->qprev == qh);
96 if ((object = qh->qnext) == qh)
98 qh->qnext = object->qnext;
99 object->qnext->qprev = qh;
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.
110 BQUEUE *qnext(BQUEUE *qhead, BQUEUE *qitem)
112 #define qh ((BQUEUE *)qhead)
113 #define qi ((BQUEUE *)qitem)
119 ASSERT(qi->qprev->qnext == qi);
120 ASSERT(qi->qnext->qprev == qi);
122 if ((object = qi->qnext) == qh)
130 /* QDCHAIN -- Dequeue an item from the middle of a queue. Passed
131 the queue item, returns the (now dechained) queue item. */
133 BQUEUE *qdchain(BQUEUE *qitem)
135 #define qi ((BQUEUE *)qitem)
137 ASSERT(qi->qprev->qnext == qi);
138 ASSERT(qi->qnext->qprev == qi);
140 return qremove(qi->qprev);