| 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 ===================================== |
|---|
| 55 | This modules defines macros to control doubly linked queue. |
|---|
| 56 | All operations are typesafe (doesn't required typecasting) and constant time. |
|---|
| 57 | |
|---|
| 58 | This 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 | /*************************************************************************** |
|---|
| 72 | Summary: |
|---|
| 73 | Creates new data type for the list head |
|---|
| 74 | |
|---|
| 75 | Description: |
|---|
| 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 | |
|---|
| 79 | Input: |
|---|
| 80 | name - name for the new data type (structure) |
|---|
| 81 | type - existing user data type used for element of the list |
|---|
| 82 | |
|---|
| 83 | Example: |
|---|
| 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 | |
|---|
| 92 | Returns: |
|---|
| 93 | <none> |
|---|
| 94 | ****************************************************************************/ |
|---|
| 95 | #define BLST_Q_HEAD(name, type) struct name { struct type *q_first, *q_last; } |
|---|
| 96 | |
|---|
| 97 | /*************************************************************************** |
|---|
| 98 | Summary: |
|---|
| 99 | Defines links entry |
|---|
| 100 | |
|---|
| 101 | Description: |
|---|
| 102 | Defines entrys for the list inside the user structure.for the element. |
|---|
| 103 | |
|---|
| 104 | Input: |
|---|
| 105 | type - the datatype for element |
|---|
| 106 | |
|---|
| 107 | Example: |
|---|
| 108 | struct block { |
|---|
| 109 | BLST_Q_ENTRY(block) link; |
|---|
| 110 | char string[256]; |
|---|
| 111 | }; |
|---|
| 112 | |
|---|
| 113 | Returns: |
|---|
| 114 | <none> |
|---|
| 115 | ****************************************************************************/ |
|---|
| 116 | #define BLST_Q_ENTRY(type) struct { struct type *q_next, *q_prev; const void *l_head; } |
|---|
| 117 | |
|---|
| 118 | /*************************************************************************** |
|---|
| 119 | Summary: |
|---|
| 120 | Initializes lists head |
|---|
| 121 | |
|---|
| 122 | Description: |
|---|
| 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 | |
|---|
| 126 | Input: |
|---|
| 127 | head - pointer to the list head |
|---|
| 128 | |
|---|
| 129 | See also: |
|---|
| 130 | BLST_Q_INITIALIZER |
|---|
| 131 | |
|---|
| 132 | Example: |
|---|
| 133 | BLST_Q_INIT(&head); |
|---|
| 134 | |
|---|
| 135 | Returns: |
|---|
| 136 | <none> |
|---|
| 137 | ****************************************************************************/ |
|---|
| 138 | #define BLST_Q_INIT(head) ((head)->q_last = (head)->q_first=NULL) |
|---|
| 139 | |
|---|
| 140 | /*************************************************************************** |
|---|
| 141 | Summary: |
|---|
| 142 | Initializes lists head |
|---|
| 143 | |
|---|
| 144 | Description: |
|---|
| 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 | |
|---|
| 148 | Input: |
|---|
| 149 | head - pointer to the list head |
|---|
| 150 | |
|---|
| 151 | See also: |
|---|
| 152 | BLST_Q_INIT |
|---|
| 153 | |
|---|
| 154 | Example: |
|---|
| 155 | static struct block_head head = BLST_Q_INITIALIZER(&head); |
|---|
| 156 | |
|---|
| 157 | Returns: |
|---|
| 158 | <none> |
|---|
| 159 | ****************************************************************************/ |
|---|
| 160 | #define BLST_Q_INITIALIZER(head) {NULL, NULL} |
|---|
| 161 | |
|---|
| 162 | /*************************************************************************** |
|---|
| 163 | Summary: |
|---|
| 164 | Tests if list is empty |
|---|
| 165 | |
|---|
| 166 | Description: |
|---|
| 167 | Tests if list is empty. |
|---|
| 168 | |
|---|
| 169 | Input: |
|---|
| 170 | head - pointer to the list head |
|---|
| 171 | |
|---|
| 172 | Returns: |
|---|
| 173 | true - list empty |
|---|
| 174 | false - list has elements |
|---|
| 175 | |
|---|
| 176 | Example: |
|---|
| 177 | if (BLST_Q_EMPTY(&head) { return ; } |
|---|
| 178 | |
|---|
| 179 | ****************************************************************************/ |
|---|
| 180 | #define BLST_Q_EMPTY(head) ((head)->q_first == NULL) |
|---|
| 181 | |
|---|
| 182 | /*************************************************************************** |
|---|
| 183 | Summary: |
|---|
| 184 | Returns the lists first element |
|---|
| 185 | |
|---|
| 186 | Description: |
|---|
| 187 | Returns pointer to the first element from the list |
|---|
| 188 | |
|---|
| 189 | Input: |
|---|
| 190 | head - pointer to the list head |
|---|
| 191 | |
|---|
| 192 | Returns: |
|---|
| 193 | pointer to the first element from the list. |
|---|
| 194 | |
|---|
| 195 | Example: |
|---|
| 196 | struct block *first=BLST_Q_FIRST(&head); |
|---|
| 197 | ****************************************************************************/ |
|---|
| 198 | #define BLST_Q_FIRST(head) ((head)->q_first) |
|---|
| 199 | |
|---|
| 200 | /*************************************************************************** |
|---|
| 201 | Summary: |
|---|
| 202 | Returns the lists first element |
|---|
| 203 | |
|---|
| 204 | Description: |
|---|
| 205 | Returns pointer to the last element from the list |
|---|
| 206 | |
|---|
| 207 | Input: |
|---|
| 208 | head - pointer to the list head |
|---|
| 209 | |
|---|
| 210 | Returns: |
|---|
| 211 | pointer to the last element from the list. |
|---|
| 212 | |
|---|
| 213 | Example: |
|---|
| 214 | struct block *first=BLST_Q_FIRST(&head); |
|---|
| 215 | ****************************************************************************/ |
|---|
| 216 | #define BLST_Q_LAST(head) ((head)->q_last) |
|---|
| 217 | |
|---|
| 218 | /*************************************************************************** |
|---|
| 219 | Summary: |
|---|
| 220 | Returns next element from the lists |
|---|
| 221 | |
|---|
| 222 | Description: |
|---|
| 223 | Returns pointer to the next element from the list |
|---|
| 224 | |
|---|
| 225 | Input: |
|---|
| 226 | elm - pointer to the list element |
|---|
| 227 | field - name of the elements link field |
|---|
| 228 | |
|---|
| 229 | Returns: |
|---|
| 230 | pointer to the next element from the list |
|---|
| 231 | |
|---|
| 232 | Example: |
|---|
| 233 | struct block *second=BLST_Q_NEXT(first); |
|---|
| 234 | ****************************************************************************/ |
|---|
| 235 | #define BLST_Q_NEXT(elm, field) ((elm)->field.q_next) |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | /*************************************************************************** |
|---|
| 239 | Summary: |
|---|
| 240 | Returns next element from the lists |
|---|
| 241 | |
|---|
| 242 | Description: |
|---|
| 243 | Returns pointer to the previous element from the list |
|---|
| 244 | |
|---|
| 245 | Input: |
|---|
| 246 | elm - pointer to the list element |
|---|
| 247 | field - name of the elements link field |
|---|
| 248 | |
|---|
| 249 | Returns: |
|---|
| 250 | pointer to the previous element from the list |
|---|
| 251 | |
|---|
| 252 | Example: |
|---|
| 253 | struct block *first=BLST_Q_PREV(second); |
|---|
| 254 | ****************************************************************************/ |
|---|
| 255 | #define BLST_Q_PREV(elm, field) ((elm)->field.q_prev) |
|---|
| 256 | |
|---|
| 257 | /*************************************************************************** |
|---|
| 258 | Summary: |
|---|
| 259 | Inserts element into the list |
|---|
| 260 | |
|---|
| 261 | Description: |
|---|
| 262 | Inserts new element into the head of the list. |
|---|
| 263 | |
|---|
| 264 | Input: |
|---|
| 265 | head - pointer to the list head |
|---|
| 266 | new_elm - pointer to the new element |
|---|
| 267 | field - name of the elements link field |
|---|
| 268 | |
|---|
| 269 | Returns: |
|---|
| 270 | <none> |
|---|
| 271 | |
|---|
| 272 | Example: |
|---|
| 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 | /*************************************************************************** |
|---|
| 282 | Summary: |
|---|
| 283 | Inserts element into the list |
|---|
| 284 | |
|---|
| 285 | Description: |
|---|
| 286 | Description: |
|---|
| 287 | Inserts new element into the tail list. |
|---|
| 288 | |
|---|
| 289 | Input: |
|---|
| 290 | head - pointer to the list head |
|---|
| 291 | new_elm - pointer to the new element |
|---|
| 292 | field - name of the elements link field |
|---|
| 293 | |
|---|
| 294 | Returns: |
|---|
| 295 | <none> |
|---|
| 296 | |
|---|
| 297 | Example: |
|---|
| 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 | /*************************************************************************** |
|---|
| 314 | Summary: |
|---|
| 315 | Inserts element into the list |
|---|
| 316 | |
|---|
| 317 | Description: |
|---|
| 318 | Inserts new element after existing element. |
|---|
| 319 | |
|---|
| 320 | Input: |
|---|
| 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 | |
|---|
| 326 | Returns: |
|---|
| 327 | <none> |
|---|
| 328 | |
|---|
| 329 | Example: |
|---|
| 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 | /*************************************************************************** |
|---|
| 341 | Summary: |
|---|
| 342 | Inserts element into the list |
|---|
| 343 | |
|---|
| 344 | Description: |
|---|
| 345 | Inserts new element after existing element. |
|---|
| 346 | |
|---|
| 347 | Input: |
|---|
| 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 | |
|---|
| 353 | Returns: |
|---|
| 354 | <none> |
|---|
| 355 | |
|---|
| 356 | Example: |
|---|
| 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 | /*************************************************************************** |
|---|
| 370 | Summary: |
|---|
| 371 | Removes element from the list |
|---|
| 372 | |
|---|
| 373 | Description: |
|---|
| 374 | Removes element from the list. |
|---|
| 375 | |
|---|
| 376 | Input: |
|---|
| 377 | head - pointer to the list head |
|---|
| 378 | elm - pointer to the list element |
|---|
| 379 | field - name of the elements link field |
|---|
| 380 | |
|---|
| 381 | See also: |
|---|
| 382 | BLST_Q_REMOVE_HEAD |
|---|
| 383 | |
|---|
| 384 | Returns: |
|---|
| 385 | <none> |
|---|
| 386 | |
|---|
| 387 | Example: |
|---|
| 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 | /*************************************************************************** |
|---|
| 397 | Summary: |
|---|
| 398 | Removes element from the list |
|---|
| 399 | |
|---|
| 400 | Description: |
|---|
| 401 | Removes element from the head of the list. |
|---|
| 402 | |
|---|
| 403 | Input: |
|---|
| 404 | head - pointer to the list head |
|---|
| 405 | field - name of the elements link field |
|---|
| 406 | |
|---|
| 407 | See also: |
|---|
| 408 | BLST_Q_REMOVE |
|---|
| 409 | |
|---|
| 410 | Returns: |
|---|
| 411 | <none> |
|---|
| 412 | |
|---|
| 413 | Example: |
|---|
| 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 */ |
|---|