| 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_list.h $ |
|---|
| 11 | * $brcm_Revision: Hydra_Software_Devel/8 $ |
|---|
| 12 | * $brcm_Date: 12/15/10 12:42p $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: |
|---|
| 15 | * |
|---|
| 16 | * Revision History: |
|---|
| 17 | * |
|---|
| 18 | * $brcm_Log: /magnum/commonutils/lst/blst_list.h $ |
|---|
| 19 | * |
|---|
| 20 | * Hydra_Software_Devel/8 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/7 12/15/10 11:02a erickson |
|---|
| 25 | * SW7420-941: add BDBG_ASSERT to BLST macros to ensure proper removal |
|---|
| 26 | * |
|---|
| 27 | * Hydra_Software_Devel/6 11/21/07 10:58a erickson |
|---|
| 28 | * PR37423: add example code |
|---|
| 29 | * |
|---|
| 30 | * Hydra_Software_Devel/5 12/11/03 4:38p vsilyaev |
|---|
| 31 | * PR 8962: Remove semicolons after while(0) |
|---|
| 32 | * |
|---|
| 33 | * Hydra_Software_Devel/4 8/27/03 11:42a darnstein |
|---|
| 34 | * Fix minor errors in macro BLST_D_REMOVE_HEAD(). |
|---|
| 35 | * |
|---|
| 36 | * Hydra_Software_Devel/3 3/14/03 7:52p vsilyaev |
|---|
| 37 | * Added description. |
|---|
| 38 | * |
|---|
| 39 | * Hydra_Software_Devel/2 3/12/03 4:37p vsilyaev |
|---|
| 40 | * Fixed some macro names. |
|---|
| 41 | * |
|---|
| 42 | * Hydra_Software_Devel/1 3/10/03 4:32p vsilyaev |
|---|
| 43 | * Doubly linked list. |
|---|
| 44 | * |
|---|
| 45 | * |
|---|
| 46 | ***************************************************************************/ |
|---|
| 47 | |
|---|
| 48 | #ifndef BLST_LIST_H |
|---|
| 49 | #define BLST_LIST_H |
|---|
| 50 | |
|---|
| 51 | /*================== Module Overview ===================================== |
|---|
| 52 | This modules defines macros to control doubly linked list. |
|---|
| 53 | All operations are typesafe (doesn't required typecasting) and constant time. |
|---|
| 54 | |
|---|
| 55 | This list allow: |
|---|
| 56 | o Insert a new entry at the head of the list |
|---|
| 57 | o Insert a new entry after/before any element in the list |
|---|
| 58 | o O(1) removal of any entry in the list |
|---|
| 59 | o Forward/Backward traversal through the list |
|---|
| 60 | o Each element requires two pointers |
|---|
| 61 | o Code size and execution time is about twice that for singly-linked list |
|---|
| 62 | |
|---|
| 63 | Example: |
|---|
| 64 | |
|---|
| 65 | #include "blst_list.h" |
|---|
| 66 | typedef struct Child { |
|---|
| 67 | BLST_D_ENTRY(Child) link; |
|---|
| 68 | } Child; |
|---|
| 69 | |
|---|
| 70 | typedef struct Parent { |
|---|
| 71 | BLST_D_HEAD(childlist, Child) list; |
|---|
| 72 | } Parent; |
|---|
| 73 | |
|---|
| 74 | main() { |
|---|
| 75 | Parent parent; |
|---|
| 76 | Child child1, child2, *pChild; |
|---|
| 77 | |
|---|
| 78 | BLST_D_INIT(&parent.list); |
|---|
| 79 | BLST_D_INSERT_HEAD(&parent.list, &child1, link); |
|---|
| 80 | BLST_D_INSERT_HEAD(&parent.list, &child2, link); |
|---|
| 81 | for (pChild = BLST_D_FIRST(&parent.list);pChild;pChild=BLST_D_NEXT(pChild, link)) { |
|---|
| 82 | process(pChild); |
|---|
| 83 | } |
|---|
| 84 | BLST_D_REMOVE(&parent.list, &child1, link); |
|---|
| 85 | BLST_D_REMOVE(&parent.list, &child2, link); |
|---|
| 86 | assert(BLST_D_EMPTY(&parent.list)); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | ========================================================================*/ |
|---|
| 90 | |
|---|
| 91 | #include "bstd.h" |
|---|
| 92 | #include "bdbg.h" |
|---|
| 93 | |
|---|
| 94 | /*************************************************************************** |
|---|
| 95 | Summary: |
|---|
| 96 | Creates new data type for the list head |
|---|
| 97 | |
|---|
| 98 | Description: |
|---|
| 99 | Creates new data type for the list head, this type used to create variable for the lists head. |
|---|
| 100 | User should create new the list head data type for every different element datatype. |
|---|
| 101 | |
|---|
| 102 | Input: |
|---|
| 103 | name - name for the new list data type (this can be any unique structure name) |
|---|
| 104 | type - existing user data type used for the element of the list |
|---|
| 105 | |
|---|
| 106 | Example: |
|---|
| 107 | struct block { |
|---|
| 108 | BLST_D_ENTRY(block) link; |
|---|
| 109 | char string[256]; |
|---|
| 110 | }; |
|---|
| 111 | |
|---|
| 112 | BLST_D_HEAD(blocklist_type, block) list; |
|---|
| 113 | BLST_D_INIT(&list); |
|---|
| 114 | |
|---|
| 115 | Returns: |
|---|
| 116 | <none> |
|---|
| 117 | ****************************************************************************/ |
|---|
| 118 | #define BLST_D_HEAD(name, type) struct name { struct type *l_first; } |
|---|
| 119 | |
|---|
| 120 | /*************************************************************************** |
|---|
| 121 | Summary: |
|---|
| 122 | Defines links entry |
|---|
| 123 | |
|---|
| 124 | Description: |
|---|
| 125 | Defines entrys for the list inside the user structure.for the element. |
|---|
| 126 | |
|---|
| 127 | Input: |
|---|
| 128 | type - the existing user data type for the element of the list |
|---|
| 129 | |
|---|
| 130 | Example: |
|---|
| 131 | struct block { |
|---|
| 132 | BLST_D_ENTRY(block) link; |
|---|
| 133 | char string[256]; |
|---|
| 134 | }; |
|---|
| 135 | |
|---|
| 136 | Returns: |
|---|
| 137 | <none> |
|---|
| 138 | ****************************************************************************/ |
|---|
| 139 | #define BLST_D_ENTRY(type) struct { struct type *l_next, *l_prev; const void *l_head; } |
|---|
| 140 | |
|---|
| 141 | /*************************************************************************** |
|---|
| 142 | Summary: |
|---|
| 143 | Initializes lists head |
|---|
| 144 | |
|---|
| 145 | Description: |
|---|
| 146 | Initializes the head of the list. The head shall be initialized before list can be used. |
|---|
| 147 | This macro used for dynamic initialization. |
|---|
| 148 | |
|---|
| 149 | Input: |
|---|
| 150 | head - pointer to the list head |
|---|
| 151 | |
|---|
| 152 | See also: |
|---|
| 153 | BLST_D_INITIALIZER |
|---|
| 154 | |
|---|
| 155 | Example: |
|---|
| 156 | BLST_D_INIT(&head); |
|---|
| 157 | |
|---|
| 158 | Returns: |
|---|
| 159 | <none> |
|---|
| 160 | ****************************************************************************/ |
|---|
| 161 | #define BLST_D_INIT(head) ((head)->l_first=NULL) |
|---|
| 162 | |
|---|
| 163 | /*************************************************************************** |
|---|
| 164 | Summary: |
|---|
| 165 | Initializes lists head |
|---|
| 166 | |
|---|
| 167 | Description: |
|---|
| 168 | Initializes the head of the list. The head shall be initialized before list can be used. |
|---|
| 169 | This macro used for static initialization. |
|---|
| 170 | |
|---|
| 171 | Input: |
|---|
| 172 | head - pointer to the list head |
|---|
| 173 | |
|---|
| 174 | See also: |
|---|
| 175 | BLST_D_INIT |
|---|
| 176 | |
|---|
| 177 | Example: |
|---|
| 178 | static struct block_head head = BLST_D_INITIALIZER(head); |
|---|
| 179 | |
|---|
| 180 | Returns: |
|---|
| 181 | <none> |
|---|
| 182 | ****************************************************************************/ |
|---|
| 183 | #define BLST_D_INITIALIZER(head) {NULL} |
|---|
| 184 | |
|---|
| 185 | /*************************************************************************** |
|---|
| 186 | Summary: |
|---|
| 187 | Tests if list is empty |
|---|
| 188 | |
|---|
| 189 | Description: |
|---|
| 190 | Tests if list is empty. |
|---|
| 191 | |
|---|
| 192 | Input: |
|---|
| 193 | head - pointer to the list head |
|---|
| 194 | |
|---|
| 195 | Returns: |
|---|
| 196 | true - list empty |
|---|
| 197 | false - list has elements |
|---|
| 198 | |
|---|
| 199 | Example: |
|---|
| 200 | if (BLST_D_EMPTY(&head) { return ; } |
|---|
| 201 | |
|---|
| 202 | ****************************************************************************/ |
|---|
| 203 | #define BLST_D_EMPTY(head) ((head)->l_first == NULL) |
|---|
| 204 | |
|---|
| 205 | /*************************************************************************** |
|---|
| 206 | Summary: |
|---|
| 207 | Returns the lists first element |
|---|
| 208 | |
|---|
| 209 | Description: |
|---|
| 210 | Returns pointer to the first element from the list |
|---|
| 211 | |
|---|
| 212 | Input: |
|---|
| 213 | head - pointer to the list head |
|---|
| 214 | |
|---|
| 215 | Returns: |
|---|
| 216 | pointer to the first element from the list. |
|---|
| 217 | |
|---|
| 218 | Example: |
|---|
| 219 | struct block *first=BLST_D_FIRST(&head); |
|---|
| 220 | ****************************************************************************/ |
|---|
| 221 | #define BLST_D_FIRST(head) ((head)->l_first) |
|---|
| 222 | |
|---|
| 223 | /*************************************************************************** |
|---|
| 224 | Summary: |
|---|
| 225 | Returns next element from the lists |
|---|
| 226 | |
|---|
| 227 | Description: |
|---|
| 228 | Returns pointer to the next element from the list |
|---|
| 229 | |
|---|
| 230 | Input: |
|---|
| 231 | elm - pointer to the list element |
|---|
| 232 | field - name of the elements link field |
|---|
| 233 | |
|---|
| 234 | Returns: |
|---|
| 235 | pointer to the next element from the list |
|---|
| 236 | |
|---|
| 237 | Example: |
|---|
| 238 | struct block *second=BLST_D_NEXT(first); |
|---|
| 239 | ****************************************************************************/ |
|---|
| 240 | #define BLST_D_NEXT(elm, field) ((elm)->field.l_next) |
|---|
| 241 | |
|---|
| 242 | /*************************************************************************** |
|---|
| 243 | Summary: |
|---|
| 244 | Returns next element from the lists |
|---|
| 245 | |
|---|
| 246 | Description: |
|---|
| 247 | Returns pointer to the previous element from the list |
|---|
| 248 | |
|---|
| 249 | Input: |
|---|
| 250 | elm - pointer to the list element |
|---|
| 251 | field - name of the elements link field |
|---|
| 252 | |
|---|
| 253 | Returns: |
|---|
| 254 | pointer to the previous element from the list |
|---|
| 255 | |
|---|
| 256 | Example: |
|---|
| 257 | struct block *first=BLST_D_PREV(second); |
|---|
| 258 | ****************************************************************************/ |
|---|
| 259 | #define BLST_D_PREV(elm, field) ((elm)->field.l_prev) |
|---|
| 260 | |
|---|
| 261 | /*************************************************************************** |
|---|
| 262 | Summary: |
|---|
| 263 | Inserts element into the list |
|---|
| 264 | |
|---|
| 265 | Description: |
|---|
| 266 | Inserts new element into the head of the list. |
|---|
| 267 | |
|---|
| 268 | Input: |
|---|
| 269 | head - pointer to the list head |
|---|
| 270 | elm - pointer to the new element |
|---|
| 271 | field - name of the elements link field |
|---|
| 272 | |
|---|
| 273 | Returns: |
|---|
| 274 | <none> |
|---|
| 275 | |
|---|
| 276 | Example: |
|---|
| 277 | BLST_D_INSERT_HEAD(&head, new_block, link); |
|---|
| 278 | ****************************************************************************/ |
|---|
| 279 | #define BLST_D_INSERT_HEAD(head, new_elm, field) do { \ |
|---|
| 280 | (new_elm)->field.l_head = (const void *)head; \ |
|---|
| 281 | if ( ((new_elm)->field.l_next = (head)->l_first) != NULL ) (head)->l_first->field.l_prev = (new_elm); \ |
|---|
| 282 | (head)->l_first = (new_elm); (new_elm)->field.l_prev = NULL; \ |
|---|
| 283 | } while(0) |
|---|
| 284 | |
|---|
| 285 | /*************************************************************************** |
|---|
| 286 | Summary: |
|---|
| 287 | Inserts element into the list |
|---|
| 288 | |
|---|
| 289 | Description: |
|---|
| 290 | Inserts new element after existing element. |
|---|
| 291 | |
|---|
| 292 | Input: |
|---|
| 293 | head - pointer to the list head |
|---|
| 294 | elm - pointer to the element from the list |
|---|
| 295 | new_elm - pointer to the new element |
|---|
| 296 | field - name of the elements link field |
|---|
| 297 | |
|---|
| 298 | Returns: |
|---|
| 299 | <none> |
|---|
| 300 | |
|---|
| 301 | Example: |
|---|
| 302 | BLST_D_INSERT_AFTER(&head, first, second, link); |
|---|
| 303 | ****************************************************************************/ |
|---|
| 304 | #define BLST_D_INSERT_AFTER(head, elm, new_elm, field) do { \ |
|---|
| 305 | BDBG_ASSERT((elm)->field.l_head == (const void *)head); \ |
|---|
| 306 | (new_elm)->field.l_head = (const void *)head; \ |
|---|
| 307 | (new_elm)->field.l_prev = (elm); \ |
|---|
| 308 | if (((new_elm)->field.l_next = elm->field.l_next)!=NULL) elm->field.l_next->field.l_prev = new_elm; \ |
|---|
| 309 | (elm)->field.l_next = (new_elm); \ |
|---|
| 310 | } while(0) |
|---|
| 311 | |
|---|
| 312 | /*************************************************************************** |
|---|
| 313 | Summary: |
|---|
| 314 | Inserts element into the list |
|---|
| 315 | |
|---|
| 316 | Description: |
|---|
| 317 | Inserts new element after existing element. |
|---|
| 318 | |
|---|
| 319 | Input: |
|---|
| 320 | head - pointer to the list head |
|---|
| 321 | elm - pointer to the element from the list |
|---|
| 322 | new_elm - pointer to the new element |
|---|
| 323 | field - name of the elements link field |
|---|
| 324 | |
|---|
| 325 | Returns: |
|---|
| 326 | <none> |
|---|
| 327 | |
|---|
| 328 | Example: |
|---|
| 329 | BLST_D_INSERT_BEFORE(&head, second, first, link); |
|---|
| 330 | ****************************************************************************/ |
|---|
| 331 | #define BLST_D_INSERT_BEFORE(head, elm, new_elm, field) do { \ |
|---|
| 332 | BDBG_ASSERT((elm)->field.l_head == (const void *)head); \ |
|---|
| 333 | (new_elm)->field.l_head = (const void *)head; \ |
|---|
| 334 | (new_elm)->field.l_next = (elm); \ |
|---|
| 335 | if (((new_elm)->field.l_prev = (elm)->field.l_prev)!=NULL) elm->field.l_prev->field.l_next = new_elm; else (head)->l_first = (new_elm); \ |
|---|
| 336 | (elm)->field.l_prev = (new_elm); \ |
|---|
| 337 | } while(0) |
|---|
| 338 | |
|---|
| 339 | /*************************************************************************** |
|---|
| 340 | Summary: |
|---|
| 341 | Removes element from the list |
|---|
| 342 | |
|---|
| 343 | Description: |
|---|
| 344 | Removes element from the list. |
|---|
| 345 | |
|---|
| 346 | Input: |
|---|
| 347 | head - pointer to the list head |
|---|
| 348 | elm - pointer to the list element |
|---|
| 349 | field - name of the elements link field |
|---|
| 350 | |
|---|
| 351 | See also: |
|---|
| 352 | BLST_D_REMOVE_HEAD |
|---|
| 353 | |
|---|
| 354 | Returns: |
|---|
| 355 | <none> |
|---|
| 356 | |
|---|
| 357 | Example: |
|---|
| 358 | BLST_D_REMOVE(&head, first, link); |
|---|
| 359 | ****************************************************************************/ |
|---|
| 360 | #define BLST_D_REMOVE(head, elm, field) do { \ |
|---|
| 361 | BDBG_ASSERT((elm)->field.l_head == (const void *)head); \ |
|---|
| 362 | (elm)->field.l_head = NULL; \ |
|---|
| 363 | if ((elm)->field.l_next) (elm)->field.l_next->field.l_prev = (elm)->field.l_prev; \ |
|---|
| 364 | if ((elm)->field.l_prev) (elm)->field.l_prev->field.l_next = (elm)->field.l_next; else (head)->l_first = (elm)->field.l_next; \ |
|---|
| 365 | } while(0) |
|---|
| 366 | |
|---|
| 367 | /*************************************************************************** |
|---|
| 368 | Summary: |
|---|
| 369 | Removes element from the list |
|---|
| 370 | |
|---|
| 371 | Description: |
|---|
| 372 | Removes element from the head of the list. |
|---|
| 373 | |
|---|
| 374 | Input: |
|---|
| 375 | head - pointer to the list head |
|---|
| 376 | field - name of the elements link field |
|---|
| 377 | |
|---|
| 378 | See also: |
|---|
| 379 | BLST_D_REMOVE |
|---|
| 380 | |
|---|
| 381 | Returns: |
|---|
| 382 | <none> |
|---|
| 383 | |
|---|
| 384 | Example: |
|---|
| 385 | BLST_D_REMOVE_HEAD(&head, link); |
|---|
| 386 | ****************************************************************************/ |
|---|
| 387 | #define BLST_D_REMOVE_HEAD(head, field) do { \ |
|---|
| 388 | BDBG_ASSERT((head)->l_first); \ |
|---|
| 389 | BDBG_ASSERT((head)->l_first->field.l_head == (const void *)head); \ |
|---|
| 390 | (head)->l_first->field.l_head = NULL; \ |
|---|
| 391 | (head)->l_first = (head)->l_first->field.l_next; \ |
|---|
| 392 | if ((head)->l_first) { (head)->l_first->field.l_prev = NULL;} \ |
|---|
| 393 | } while(0) |
|---|
| 394 | |
|---|
| 395 | #endif /* BLST_LIST_H */ |
|---|