source: svn/trunk/newcon3bcm2_21bu/magnum/commonutils/lst/blst_queue.h @ 33

Last change on this file since 33 was 2, checked in by jglee, 11 years ago

first commit

  • Property svn:executable set to *
File size: 12.5 KB
Line 
1/***************************************************************************
2 *     Copyright (c) 2003-2010, Broadcom Corporation
3 *     All Rights Reserved
4 *     Confidential Property of Broadcom Corporation
5 *
6 *  THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE
7 *  AGREEMENT  BETWEEN THE USER AND BROADCOM.  YOU HAVE NO RIGHT TO USE OR
8 *  EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT.
9 *
10 * $brcm_Workfile: blst_queue.h $
11 * $brcm_Revision: Hydra_Software_Devel/7 $
12 * $brcm_Date: 12/15/10 12:42p $
13 *
14 * Module Description:
15 *
16 * Revision History:
17 *
18 * $brcm_Log: /magnum/commonutils/lst/blst_queue.h $
19 *
20 * Hydra_Software_Devel/7   12/15/10 12:42p erickson
21 * SW7420-941: add #include for bstd.h. too many files #include blst
22 * before bstd.
23 *
24 * Hydra_Software_Devel/6   12/15/10 11:02a erickson
25 * SW7420-941: add BDBG_ASSERT to BLST macros to ensure proper removal
26 *
27 * Hydra_Software_Devel/5   1/15/04 10:04a erickson
28 * PR9321: fixed BLST_Q_INSERT_TAIL and BLST_Q_REMOVE_HEAD
29 *
30 * Hydra_Software_Devel/4   12/11/03 4:38p vsilyaev
31 * PR 8962: Remove semicolons after while(0)
32 *
33 * Hydra_Software_Devel/3   10/8/03 5:21p darnstein
34 * Fix BLST_Q_INSERT_TAIL.  Well, at least it compiles now.
35 *
36 * Hydra_Software_Devel/2   10/8/03 5:15p darnstein
37 * Get rid of DOS line control. A nontrivial checkin will follow shortly.
38 *
39 * Hydra_Software_Devel/1   3/14/03 7:50p vsilyaev
40 * Double linked queue.
41 *
42 * Hydra_Software_Devel/2   3/12/03 4:37p vsilyaev
43 * Fixed some macro names.
44 *
45 * Hydra_Software_Devel/1   3/10/03 4:32p vsilyaev
46 * Doubly linked list.
47 *
48 *
49 ***************************************************************************/
50
51#ifndef BLST_QUEUE_H
52#define BLST_QUEUE_H   
53
54/*================== Module Overview =====================================
55This modules defines macros to control doubly linked queue.
56All operations are typesafe (doesn't required typecasting) and constant time.
57
58This list allow:
59  o Insert a new entry at the head of the list
60  o Insert a new entry after/before any element in the list
61  o O(1) removal of any entry in the list
62  o Forward traversal through the list
63  o Each element requires two pointers
64  o Code size and execution time is about 2.5 that for singly-linked list
65 
66========================================================================*/
67
68#include "bstd.h"
69#include "bdbg.h"
70 
71/***************************************************************************
72Summary:
73    Creates new data type for the list head
74   
75Description:
76    Creates new data type for the list head, this type used to create variable for the lists head.
77    User should create new the list head data type for every different element datatype.
78   
79Input:
80    name - name for the new data type (structure)
81    type - existing user data type used for element of the list
82
83Example:   
84     struct block {
85        BLST_Q_ENTRY(block) link;
86        char string[256];
87     }; 
88     
89     BLST_Q_HEAD(blockqueue, block) queue;
90     BLST_Q_INIT(&queue);
91
92Returns:
93    <none>
94****************************************************************************/
95#define BLST_Q_HEAD(name, type) struct name { struct type *q_first, *q_last; }
96
97/***************************************************************************
98Summary:
99    Defines links entry
100   
101Description:
102    Defines entrys for the list inside the user structure.for the element.
103   
104Input:
105    type - the datatype for element
106
107Example:
108     struct block {
109        BLST_Q_ENTRY(block) link;
110        char string[256];
111     }; 
112
113Returns:
114    <none>
115****************************************************************************/
116#define BLST_Q_ENTRY(type)  struct { struct type *q_next, *q_prev; const void *l_head; }
117
118/***************************************************************************
119Summary:
120    Initializes lists head
121   
122Description:
123    Initializes the head of the list. The head shall be initialized before list can be used.
124    This macro used for dynamic initialization.
125   
126Input:
127    head - pointer to the list head
128
129See also:
130    BLST_Q_INITIALIZER
131
132Example:
133    BLST_Q_INIT(&head);
134
135Returns:
136    <none>
137****************************************************************************/
138#define BLST_Q_INIT(head) ((head)->q_last = (head)->q_first=NULL)
139
140/***************************************************************************
141Summary:
142    Initializes lists head
143   
144Description:
145    Initializes the head of the list. The head shall be initialized before list can be used.
146    This macro used for static initialization.
147   
148Input:
149    head - pointer to the list head
150
151See also:
152    BLST_Q_INIT
153
154Example:
155    static struct block_head  head = BLST_Q_INITIALIZER(&head);
156
157Returns:
158    <none>
159****************************************************************************/
160#define BLST_Q_INITIALIZER(head) {NULL, NULL}
161
162/***************************************************************************
163Summary:
164    Tests if list is empty
165   
166Description:
167    Tests if list is empty.
168
169Input:
170    head - pointer to the list head
171
172Returns:
173    true - list empty
174    false - list has elements
175
176Example:
177    if (BLST_Q_EMPTY(&head) { return ; }
178
179****************************************************************************/
180#define BLST_Q_EMPTY(head) ((head)->q_first == NULL)
181
182/***************************************************************************
183Summary:
184    Returns the lists first element
185   
186Description:
187    Returns pointer to the first element from the list
188   
189Input:
190    head - pointer to the list head
191
192Returns:
193    pointer to the first element from the list.
194
195Example:
196    struct block *first=BLST_Q_FIRST(&head);
197****************************************************************************/
198#define BLST_Q_FIRST(head) ((head)->q_first)
199
200/***************************************************************************
201Summary:
202    Returns the lists first element
203   
204Description:
205    Returns pointer to the last element from the list
206
207Input:
208    head - pointer to the list head
209
210Returns:
211    pointer to the last element from the list.
212
213Example:
214    struct block *first=BLST_Q_FIRST(&head);
215****************************************************************************/
216#define BLST_Q_LAST(head) ((head)->q_last)
217
218/***************************************************************************
219Summary:
220    Returns next element from the lists
221   
222Description:
223    Returns pointer to the next element from the list
224   
225Input:
226    elm - pointer to the list element
227    field - name of the elements link field
228
229Returns:
230    pointer to the next element from the list
231
232Example:
233    struct block *second=BLST_Q_NEXT(first);
234****************************************************************************/
235#define BLST_Q_NEXT(elm, field) ((elm)->field.q_next)
236
237
238/***************************************************************************
239Summary:
240    Returns next element from the lists
241   
242Description:
243    Returns pointer to the previous element from the list
244   
245Input:
246    elm - pointer to the list element
247    field - name of the elements link field
248
249Returns:
250    pointer to the previous element from the list
251
252Example:
253    struct block *first=BLST_Q_PREV(second);
254****************************************************************************/
255#define BLST_Q_PREV(elm, field) ((elm)->field.q_prev)
256
257/***************************************************************************
258Summary:
259    Inserts element into the list
260
261Description:
262    Inserts new element into the head of the list.
263
264Input:
265    head - pointer to the list head
266    new_elm - pointer to the new element
267    field - name of the elements link field
268
269Returns:
270    <none>
271
272Example:
273    BLST_Q_INSERT_HEAD(&head, new_block, link);
274****************************************************************************/
275#define BLST_Q_INSERT_HEAD(head, new_elm, field) do { \
276    (new_elm)->field.l_head = (const void *)head; \
277    if ( ((new_elm)->field.q_next = (head)->q_first) != NULL ) (head)->q_first->field.q_prev = (new_elm); \
278    if ( (head)->q_last == NULL) (head)->q_last = (new_elm); \
279    (head)->q_first = (new_elm); (new_elm)->field.q_prev = NULL;}  while(0)
280
281/***************************************************************************
282Summary:
283    Inserts element into the list
284   
285Description:
286Description:
287    Inserts new element into the tail list.
288   
289Input:
290    head - pointer to the list head
291    new_elm - pointer to the new element
292    field - name of the elements link field
293
294Returns:
295    <none>
296
297Example:
298    BLST_Q_INSERT_TAIL(&head, elm, link);
299****************************************************************************/
300#define BLST_Q_INSERT_TAIL(head, new_elm, field) do { \
301    (new_elm)->field.l_head = (const void *)head; \
302    (new_elm)->field.q_next = NULL;               \
303    (new_elm)->field.q_prev = (head)->q_last;     \
304    if ((head)->q_last) {                         \
305            (head)->q_last->field.q_next = (new_elm); \
306    } else {                                      \
307            (head)->q_first = (new_elm);          \
308    }                                             \
309    (head)->q_last = (new_elm);                   \
310   } while(0)
311   
312
313/***************************************************************************
314Summary:
315    Inserts element into the list
316   
317Description:
318    Inserts new element after existing element.
319   
320Input:
321    head - pointer to the list head
322    elm - pointer to the element from the list
323    new_elm - pointer to the new element
324    field - name of the elements link field
325
326Returns:
327    <none>
328
329Example:
330    BLST_Q_INSERT_AFTER(&head, first, second, link);
331****************************************************************************/
332#define BLST_Q_INSERT_AFTER(head, elm, new_elm, field) do { \
333    BDBG_ASSERT((elm)->field.l_head == (const void *)head); \
334    (new_elm)->field.l_head = (const void *)head; \
335    (new_elm)->field.q_prev = (elm); \
336    if (((new_elm)->field.q_next = (elm)->field.q_next)!=NULL)  (elm)->field.q_next->field.q_prev = (new_elm); \
337    if ((elm)->field.q_next == NULL)  (head)->q_last = (new_elm);\
338    (elm)->field.q_next = (new_elm); } while(0)
339
340/***************************************************************************
341Summary:
342    Inserts element into the list
343   
344Description:
345    Inserts new element after existing element.
346   
347Input:
348    head - pointer to the list head
349    elm - pointer to the element from the list
350    new_elm - pointer to the new element
351    field - name of the elements link field
352
353Returns:
354    <none>
355
356Example:
357    BLST_Q_INSERT_BEFORE(&head, second, first, link);
358****************************************************************************/
359#define BLST_Q_INSERT_BEFORE(head, elm, new_elm, field) do { \
360    BDBG_ASSERT((elm)->field.l_head == (const void *)head); \
361    (new_elm)->field.l_head = (const void *)head; \
362    (new_elm)->field.q_next = (elm); \
363    if (((new_elm)->field.q_prev = (elm)->field.q_prev)!=NULL) elm->field.q_prev->field.q_next = new_elm; \
364    else (head)->q_first = (new_elm); \
365    (elm)->field.q_prev = (new_elm); } while(0)
366
367
368
369/***************************************************************************
370Summary:
371    Removes element from the list
372
373Description:
374    Removes element from the list.
375
376Input:
377    head - pointer to the list head
378    elm - pointer to the list element
379    field - name of the elements link field
380
381See also:
382    BLST_Q_REMOVE_HEAD
383
384Returns:
385    <none>
386
387Example:
388    BLST_Q_REMOVE(&head, first, link);
389****************************************************************************/
390#define BLST_Q_REMOVE(head, elm, field) do { \
391    BDBG_ASSERT((elm)->field.l_head == (const void *)head); \
392    if ((elm)->field.q_next) (elm)->field.q_next->field.q_prev = (elm)->field.q_prev;  else (head)->q_last = (elm)->field.q_prev; \
393    if ((elm)->field.q_prev) (elm)->field.q_prev->field.q_next = (elm)->field.q_next; else (head)->q_first = (elm)->field.q_next; \
394    } while(0)
395
396/***************************************************************************
397Summary:
398    Removes element from the list
399
400Description:
401    Removes element from the head of the list.
402
403Input:
404    head - pointer to the list head
405    field - name of the elements link field
406
407See also:
408    BLST_Q_REMOVE
409
410Returns:
411    <none>
412
413Example:
414    BLST_Q_REMOVE_HEAD(&head, first, link);
415****************************************************************************/
416#define BLST_Q_REMOVE_HEAD(head, field) do { \
417    BDBG_ASSERT((head)->q_first); \
418    BDBG_ASSERT((head)->q_first->field.l_head == (const void *)head); \
419    (head)->q_first = (head)->q_first->field.q_next; \
420    if ((head)->q_first) { (head)->q_first->field.q_prev = NULL;} else (head)->q_last=NULL; \
421    } while(0)
422
423
424#endif /* BLST_QUEUE_H  */
Note: See TracBrowser for help on using the repository browser.