| 1 | /************************************************************** |
|---|
| 2 | ** |
|---|
| 3 | ** Copyright (c) 2002-2009, Broadcom Corporation |
|---|
| 4 | ** All Rights Reserver |
|---|
| 5 | ** Confidential Property of Broadcom Corporation |
|---|
| 6 | ** |
|---|
| 7 | ** THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE |
|---|
| 8 | ** AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR |
|---|
| 9 | ** EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 10 | ** |
|---|
| 11 | ** $brcm_Workfile: blst_squeue.h $ |
|---|
| 12 | ** $brcm_Revision: Hydra_Software_Devel/8 $ |
|---|
| 13 | ** $brcm_Date: 7/21/09 1:42p $ |
|---|
| 14 | ** |
|---|
| 15 | ** |
|---|
| 16 | ** Module description: |
|---|
| 17 | ** Queue management primitives for BMC7041 |
|---|
| 18 | ** |
|---|
| 19 | ** Revision History: |
|---|
| 20 | ** |
|---|
| 21 | ** $brcm_Log: /magnum/commonutils/lst/blst_squeue.h $ |
|---|
| 22 | ** |
|---|
| 23 | ** Hydra_Software_Devel/8 7/21/09 1:42p vsilyaev |
|---|
| 24 | ** PR 56629: Uses the name prefix for local variables to prevent name |
|---|
| 25 | ** clash |
|---|
| 26 | ** |
|---|
| 27 | ** Hydra_Software_Devel/7 2/24/04 7:28p vsilyaev |
|---|
| 28 | ** PR 9880: Added BLST_SQ_LAST |
|---|
| 29 | ** |
|---|
| 30 | ** Hydra_Software_Devel/6 12/11/03 4:39p vsilyaev |
|---|
| 31 | ** PR 8962: Remove semicolons after while(0) |
|---|
| 32 | ** |
|---|
| 33 | ** Hydra_Software_Devel/5 8/28/03 12:56p vsilyaev |
|---|
| 34 | ** Added parenthesis around macro arguments. |
|---|
| 35 | ** |
|---|
| 36 | ** Hydra_Software_Devel/4 6/23/03 10:39a jasonh |
|---|
| 37 | ** Fixed argument names for documentation. |
|---|
| 38 | ** |
|---|
| 39 | ** Hydra_Software_Devel/3 3/17/03 2:16p vsilyaev |
|---|
| 40 | ** Fixed INSERT_AFTER. |
|---|
| 41 | ** |
|---|
| 42 | ** Hydra_Software_Devel/2 3/14/03 7:52p vsilyaev |
|---|
| 43 | ** Added function description. |
|---|
| 44 | ** |
|---|
| 45 | ** Hydra_Software_Devel/1 3/10/03 4:31p vsilyaev |
|---|
| 46 | ** Singley linked queue |
|---|
| 47 | ** |
|---|
| 48 | ** |
|---|
| 49 | **************************************************************/ |
|---|
| 50 | |
|---|
| 51 | #ifndef BLST_SQUEUE_H |
|---|
| 52 | #define BLST_SQUEUE_H |
|---|
| 53 | |
|---|
| 54 | /*================== Module Overview ===================================== |
|---|
| 55 | This modules defines macros to control singly-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 to the tail of the list |
|---|
| 61 | o Insert a new entry after any element in the list |
|---|
| 62 | o O(1) removal of an entry from the list head |
|---|
| 63 | o O(n) removal of any entry from the list |
|---|
| 64 | o Forward traversal through the list |
|---|
| 65 | o Each element requires one pointers |
|---|
| 66 | o Code size and execution time is aboute 1.5 that for singly-linked list |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | ========================================================================*/ |
|---|
| 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 | field - name for the new data type (structure) |
|---|
| 81 | type - existing user data type used for element of the list |
|---|
| 82 | |
|---|
| 83 | Example: |
|---|
| 84 | BLST_SQ_HEAD(block_head, block); |
|---|
| 85 | struct block_head head; |
|---|
| 86 | |
|---|
| 87 | Returns: |
|---|
| 88 | <none> |
|---|
| 89 | ****************************************************************************/ |
|---|
| 90 | #define BLST_SQ_HEAD(itemname,type) struct itemname { struct type *sq_first, *sq_last;} |
|---|
| 91 | |
|---|
| 92 | /*************************************************************************** |
|---|
| 93 | Summary: |
|---|
| 94 | Defines links entry |
|---|
| 95 | |
|---|
| 96 | Description: |
|---|
| 97 | Defines entrys for the list inside the user structure.for the element. |
|---|
| 98 | |
|---|
| 99 | Input: |
|---|
| 100 | type - the datatype for element |
|---|
| 101 | |
|---|
| 102 | Example: |
|---|
| 103 | struct block { |
|---|
| 104 | BLST_SQ_ENTRY(block) link; |
|---|
| 105 | char string[256]; |
|---|
| 106 | }; |
|---|
| 107 | |
|---|
| 108 | Returns: |
|---|
| 109 | <none> |
|---|
| 110 | ****************************************************************************/ |
|---|
| 111 | #define BLST_SQ_ENTRY(type) struct { struct type *sq_next; } |
|---|
| 112 | |
|---|
| 113 | /*************************************************************************** |
|---|
| 114 | Summary: |
|---|
| 115 | Initializes lists head |
|---|
| 116 | |
|---|
| 117 | Description: |
|---|
| 118 | Initializes the head of the list. The head shall be initialized before list can be used. |
|---|
| 119 | This macro used for dynamic initialization. |
|---|
| 120 | |
|---|
| 121 | Input: |
|---|
| 122 | phead - pointer to the list head |
|---|
| 123 | |
|---|
| 124 | See also: |
|---|
| 125 | BLST_SQ_INITIALIZER |
|---|
| 126 | |
|---|
| 127 | Example: |
|---|
| 128 | BLST_SQ_INIT(&head); |
|---|
| 129 | |
|---|
| 130 | Returns: |
|---|
| 131 | <none> |
|---|
| 132 | ****************************************************************************/ |
|---|
| 133 | #define BLST_SQ_INIT(phead) ((phead)->sq_first = (phead)->sq_last = NULL) |
|---|
| 134 | |
|---|
| 135 | /*************************************************************************** |
|---|
| 136 | Summary: |
|---|
| 137 | Initializes lists head |
|---|
| 138 | |
|---|
| 139 | Description: |
|---|
| 140 | Initializes the head of the list. The head shall be initialized before list can be used. |
|---|
| 141 | This macro used for static initialization. |
|---|
| 142 | |
|---|
| 143 | Input: |
|---|
| 144 | phead - pointer to the list head |
|---|
| 145 | |
|---|
| 146 | See also: |
|---|
| 147 | BLST_S_INIT |
|---|
| 148 | |
|---|
| 149 | Example: |
|---|
| 150 | static struct block_head head = BLST_S_INITIALIZER(head); |
|---|
| 151 | |
|---|
| 152 | Returns: |
|---|
| 153 | <none> |
|---|
| 154 | ****************************************************************************/ |
|---|
| 155 | #define BLST_SQ_INITIALIZER(phead) {NULL, NULL} |
|---|
| 156 | |
|---|
| 157 | /*************************************************************************** |
|---|
| 158 | Summary: |
|---|
| 159 | Tests if list is empty |
|---|
| 160 | |
|---|
| 161 | Description: |
|---|
| 162 | Tests if list is empty. |
|---|
| 163 | |
|---|
| 164 | Input: |
|---|
| 165 | phead - pointer to the list head |
|---|
| 166 | |
|---|
| 167 | Returns: |
|---|
| 168 | true - list empty |
|---|
| 169 | false - list has elements |
|---|
| 170 | |
|---|
| 171 | Example: |
|---|
| 172 | if (BLST_SQ_EMPTY(&head) { return ; } |
|---|
| 173 | |
|---|
| 174 | ****************************************************************************/ |
|---|
| 175 | #define BLST_SQ_EMPTY(phead) ((phead)->sq_first==NULL) |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | /*************************************************************************** |
|---|
| 179 | Summary: |
|---|
| 180 | Returns the lists first element |
|---|
| 181 | |
|---|
| 182 | Description: |
|---|
| 183 | Returns pointer to the first element from the list |
|---|
| 184 | |
|---|
| 185 | Input: |
|---|
| 186 | phead - pointer to the list head |
|---|
| 187 | |
|---|
| 188 | Returns: |
|---|
| 189 | pointer to the first element from the list. |
|---|
| 190 | |
|---|
| 191 | Example: |
|---|
| 192 | struct block *first=BLST_SQ_FIRST(&head); |
|---|
| 193 | ****************************************************************************/ |
|---|
| 194 | #define BLST_SQ_FIRST(phead) ((phead)->sq_first) |
|---|
| 195 | |
|---|
| 196 | /*************************************************************************** |
|---|
| 197 | Summary: |
|---|
| 198 | Returns the lists last element |
|---|
| 199 | |
|---|
| 200 | Description: |
|---|
| 201 | Returns pointer to the last element in the list |
|---|
| 202 | |
|---|
| 203 | Input: |
|---|
| 204 | phead - pointer to the list head |
|---|
| 205 | |
|---|
| 206 | Returns: |
|---|
| 207 | pointer to the last element int the list. |
|---|
| 208 | |
|---|
| 209 | Example: |
|---|
| 210 | struct block *last=BLST_SQ_LAST(&head); |
|---|
| 211 | ****************************************************************************/ |
|---|
| 212 | #define BLST_SQ_LAST(phead) ((phead)->sq_last) |
|---|
| 213 | |
|---|
| 214 | /*************************************************************************** |
|---|
| 215 | Summary: |
|---|
| 216 | Returns next element from the lists |
|---|
| 217 | |
|---|
| 218 | Description: |
|---|
| 219 | Returns pointer to the next element from the list |
|---|
| 220 | |
|---|
| 221 | Input: |
|---|
| 222 | elm - pointer to the list element |
|---|
| 223 | field - name of the elements link field |
|---|
| 224 | |
|---|
| 225 | Returns: |
|---|
| 226 | pointer to the next element from the list |
|---|
| 227 | |
|---|
| 228 | Example: |
|---|
| 229 | struct block *second=BLST_S_NEXT(first); |
|---|
| 230 | ****************************************************************************/ |
|---|
| 231 | #define BLST_SQ_NEXT(pitem, field) ((pitem)->field.sq_next) |
|---|
| 232 | |
|---|
| 233 | /*************************************************************************** |
|---|
| 234 | Summary: |
|---|
| 235 | Inserts element into the list |
|---|
| 236 | |
|---|
| 237 | Description: |
|---|
| 238 | Inserts new element into the head of the list. |
|---|
| 239 | |
|---|
| 240 | Input: |
|---|
| 241 | phead - pointer to the list head |
|---|
| 242 | elm - pointer to the new element |
|---|
| 243 | field - name of the elements link field |
|---|
| 244 | |
|---|
| 245 | Returns: |
|---|
| 246 | <none> |
|---|
| 247 | |
|---|
| 248 | Example: |
|---|
| 249 | BLST_SQ_INSERT_HEAD(&head, new_block, link); |
|---|
| 250 | ****************************************************************************/ |
|---|
| 251 | #define BLST_SQ_INSERT_HEAD(phead, pitem, field) do { \ |
|---|
| 252 | (pitem)->field.sq_next = (phead)->sq_first; \ |
|---|
| 253 | if ((phead)->sq_last==NULL) { \ |
|---|
| 254 | (phead)->sq_last = (pitem); \ |
|---|
| 255 | } \ |
|---|
| 256 | (phead)->sq_first = (pitem); \ |
|---|
| 257 | } while(0) |
|---|
| 258 | |
|---|
| 259 | /*************************************************************************** |
|---|
| 260 | Summary: |
|---|
| 261 | Inserts element into the list |
|---|
| 262 | |
|---|
| 263 | Description: |
|---|
| 264 | Inserts new element after existing element. |
|---|
| 265 | |
|---|
| 266 | Input: |
|---|
| 267 | head - pointer to the list head |
|---|
| 268 | elm - pointer to the element from the list |
|---|
| 269 | new_elm - pointer to the new element |
|---|
| 270 | field - name of the elements link field |
|---|
| 271 | |
|---|
| 272 | Returns: |
|---|
| 273 | <none> |
|---|
| 274 | |
|---|
| 275 | Example: |
|---|
| 276 | BLST_SQ_INSERT_AFTER(&head, first, second, link); |
|---|
| 277 | ****************************************************************************/ |
|---|
| 278 | #define BLST_SQ_INSERT_AFTER(head, elm, new_elm, field) do { \ |
|---|
| 279 | (new_elm)->field.sq_next = (elm)->field.sq_next; \ |
|---|
| 280 | if((elm)->field.sq_next == NULL) (head)->sq_last = (new_elm); \ |
|---|
| 281 | (elm)->field.sq_next = new_elm; \ |
|---|
| 282 | } while(0) |
|---|
| 283 | |
|---|
| 284 | /*************************************************************************** |
|---|
| 285 | Summary: |
|---|
| 286 | Inserts element into the list |
|---|
| 287 | |
|---|
| 288 | Description: |
|---|
| 289 | Inserts new element into the tail list. |
|---|
| 290 | |
|---|
| 291 | Input: |
|---|
| 292 | phead - pointer to the list head |
|---|
| 293 | elm - pointer to the new element |
|---|
| 294 | field - name of the elements link field |
|---|
| 295 | |
|---|
| 296 | Returns: |
|---|
| 297 | <none> |
|---|
| 298 | |
|---|
| 299 | Example: |
|---|
| 300 | BLST_SQ_INSERT_TAIL(&head, first, second, link); |
|---|
| 301 | ****************************************************************************/ |
|---|
| 302 | #define BLST_SQ_INSERT_TAIL(phead, pitem, field) do { \ |
|---|
| 303 | (pitem)->field.sq_next = NULL; \ |
|---|
| 304 | if ((phead)->sq_last) { \ |
|---|
| 305 | (phead)->sq_last->field.sq_next = pitem; \ |
|---|
| 306 | } else { \ |
|---|
| 307 | (phead)->sq_first = pitem; \ |
|---|
| 308 | }\ |
|---|
| 309 | (phead)->sq_last = pitem; \ |
|---|
| 310 | } while(0) |
|---|
| 311 | |
|---|
| 312 | /*************************************************************************** |
|---|
| 313 | Summary: |
|---|
| 314 | Removes element from the list |
|---|
| 315 | |
|---|
| 316 | Description: |
|---|
| 317 | Removes element from the head of the list. |
|---|
| 318 | |
|---|
| 319 | Input: |
|---|
| 320 | phead - pointer to the list head |
|---|
| 321 | field - name of the elements link field |
|---|
| 322 | |
|---|
| 323 | See also: |
|---|
| 324 | BLST_SQ_REMOVE |
|---|
| 325 | |
|---|
| 326 | Returns: |
|---|
| 327 | <none> |
|---|
| 328 | |
|---|
| 329 | Example: |
|---|
| 330 | BLST_SQ_REMOVE_HEAD(&head, link); |
|---|
| 331 | ****************************************************************************/ |
|---|
| 332 | #define BLST_SQ_REMOVE_HEAD(phead, field) do { \ |
|---|
| 333 | if ((phead)->sq_first==(phead)->sq_last) { \ |
|---|
| 334 | (phead)->sq_first=(phead)->sq_last=NULL;\ |
|---|
| 335 | } else { \ |
|---|
| 336 | (phead)->sq_first = (phead)->sq_first->field.sq_next; \ |
|---|
| 337 | } \ |
|---|
| 338 | } while(0) |
|---|
| 339 | |
|---|
| 340 | /*************************************************************************** |
|---|
| 341 | Summary: |
|---|
| 342 | Removes element from the list |
|---|
| 343 | |
|---|
| 344 | Description: |
|---|
| 345 | Removes element from the of the list. This implementation is O(n), |
|---|
| 346 | where n it's position of the element in the list |
|---|
| 347 | |
|---|
| 348 | Input: |
|---|
| 349 | phead - pointer to the list head |
|---|
| 350 | elm - pointer to the list element |
|---|
| 351 | type - datatype for an element of the list |
|---|
| 352 | field - name of the elements link field |
|---|
| 353 | |
|---|
| 354 | See also: |
|---|
| 355 | BLST_S_REMOVE_HEAD |
|---|
| 356 | |
|---|
| 357 | Returns: |
|---|
| 358 | <none> |
|---|
| 359 | |
|---|
| 360 | Example: |
|---|
| 361 | BLST_SQ_REMOVE(&head, first, block, link); |
|---|
| 362 | ****************************************************************************/ |
|---|
| 363 | #define BLST_SQ_REMOVE(phead, pitem, type, field) do { \ |
|---|
| 364 | if ((phead)->sq_first == (pitem)) { \ |
|---|
| 365 | BLST_SQ_REMOVE_HEAD(phead, field);\ |
|---|
| 366 | } else { \ |
|---|
| 367 | struct type *blst_sq_remove_cur; \ |
|---|
| 368 | \ |
|---|
| 369 | for (blst_sq_remove_cur=(phead)->sq_first; blst_sq_remove_cur->field.sq_next != (pitem); blst_sq_remove_cur = blst_sq_remove_cur->field.sq_next) {} \ |
|---|
| 370 | blst_sq_remove_cur->field.sq_next=blst_sq_remove_cur->field.sq_next->field.sq_next; \ |
|---|
| 371 | if (blst_sq_remove_cur->field.sq_next==NULL) { \ |
|---|
| 372 | (phead)->sq_last = blst_sq_remove_cur; \ |
|---|
| 373 | } \ |
|---|
| 374 | } \ |
|---|
| 375 | } while(0) |
|---|
| 376 | |
|---|
| 377 | #endif /* BLST_SQUEUE_H */ |
|---|
| 378 | |
|---|