1 /* *********************************************************************
2 * Broadcom Common Firmware Environment (CFE)
4 * Queue Management routines File: lib_queue.c
6 * Routines to manage doubly-linked queues.
8 * Author: Mitch Lichtenberg (mpl@broadcom.com)
10 *********************************************************************
12 * Copyright 2000,2001,2002,2003
13 * Broadcom Corporation. All rights reserved.
15 * This software is furnished under license and may be used and
16 * copied only in accordance with the following terms and
17 * conditions. Subject to these conditions, you may download,
18 * copy, install, use, modify and distribute modified or unmodified
19 * copies of this software in source and/or binary form. No title
20 * or ownership is transferred hereby.
22 * 1) Any source code used, modified or distributed must reproduce
23 * and retain this copyright notice and list of conditions
24 * as they appear in the source file.
26 * 2) No right is granted to use any trade name, trademark, or
27 * logo of Broadcom Corporation. The "Broadcom Corporation"
28 * name may not be used to endorse or promote products derived
29 * from this software without the prior written permission of
30 * Broadcom Corporation.
32 * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
33 * IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
34 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
35 * PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT
36 * SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN
37 * PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
38 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
39 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
40 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
42 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
43 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF
44 * THE POSSIBILITY OF SUCH DAMAGE.
45 ********************************************************************* */
48 #include "lib_types.h"
49 #include "lib_queue.h"
52 /* *********************************************************************
63 ********************************************************************* */
65 void q_enqueue(queue_t
*qb
,queue_t
*item
)
67 qb
->q_prev
->q_next
= item
;
69 item
->q_prev
= qb
->q_prev
;
74 /* *********************************************************************
77 * Remove an element from the queue
80 * element - element to remove
84 ********************************************************************* */
86 void q_dequeue(queue_t
*item
)
88 item
->q_prev
->q_next
= item
->q_next
;
89 item
->q_next
->q_prev
= item
->q_prev
;
93 /* *********************************************************************
96 * Dequeue next element from the specified queue
102 * next element, or NULL
103 ********************************************************************* */
105 queue_t
*q_deqnext(queue_t
*qb
)
107 if (qb
->q_next
== qb
) {
113 qb
->q_prev
->q_next
= qb
->q_next
;
114 qb
->q_next
->q_prev
= qb
->q_prev
;
120 /* *********************************************************************
123 * "Map" a queue, calling the specified function for each
124 * element in the queue
126 * If the function returns nonzero, q_map will terminate.
130 * fn - function pointer
131 * a,b - parameters for the function
134 * return value from function, or zero if entire queue
136 ********************************************************************* */
138 int q_map(queue_t
*qb
, int (*func
)(queue_t
*,unsigned int,unsigned int),
139 unsigned int a
,unsigned int b
)
151 if ((res
= (*func
)(qe
,a
,b
))) return res
;
162 /* *********************************************************************
165 * Counts the elements on a queue (not interlocked) *
167 * Input Parameters: *
171 * number of elements *
172 ********************************************************************* */
173 int q_count(queue_t
*qb
)
180 while (qe
->q_next
!= qb
) {
191 /* *********************************************************************
194 * Determines if a particular element is on a queue.
198 * item - queue element
202 * >0 - position on queue
203 ********************************************************************* */
204 int q_find(queue_t
*qb
,queue_t
*item
)
212 if (q
== qb
) return 0;