| 1 | /*************************************************************** |
|---|
| 2 | ** |
|---|
| 3 | ** Broadcom Corp. Confidential |
|---|
| 4 | ** Copyright 2003-2008 Broadcom Corp. All Rights Reserved. |
|---|
| 5 | ** |
|---|
| 6 | ** THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED |
|---|
| 7 | ** SOFTWARE LICENSE AGREEMENT BETWEEN THE USER AND BROADCOM. |
|---|
| 8 | ** YOU HAVE NO RIGHT TO USE OR EXPLOIT THIS MATERIAL EXCEPT |
|---|
| 9 | ** SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 10 | ** |
|---|
| 11 | ** File: si_list.h |
|---|
| 12 | ** Description: defines and function prototypes for general list |
|---|
| 13 | ** functions that can be used in SI processing. |
|---|
| 14 | ** |
|---|
| 15 | ** Created: 03/08/2001 |
|---|
| 16 | ** |
|---|
| 17 | ** REVISION: |
|---|
| 18 | ** |
|---|
| 19 | ** $Log: $ |
|---|
| 20 | ** |
|---|
| 21 | ** |
|---|
| 22 | ****************************************************************/ |
|---|
| 23 | #ifndef SI_LIST_H |
|---|
| 24 | #define SI_LIST_H |
|---|
| 25 | |
|---|
| 26 | |
|---|
| 27 | /*================== Module Overview ===================================== |
|---|
| 28 | This modules defines macros to control doubly linked list. |
|---|
| 29 | All operations are typesafe (doesn't required typecasting) and constant time. |
|---|
| 30 | |
|---|
| 31 | This list allow: |
|---|
| 32 | o Insert a new entry at the head of the list |
|---|
| 33 | o Insert a new entry after/before any element in the list |
|---|
| 34 | o O(1) removal of any entry in the list |
|---|
| 35 | o Forward/Backward traversal through the list |
|---|
| 36 | o Each element requires two pointers |
|---|
| 37 | o Code size and execution time is about twice that for singly-linked list |
|---|
| 38 | |
|---|
| 39 | ========================================================================*/ |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | /*************************************************************************** |
|---|
| 43 | Summary: |
|---|
| 44 | Creates new data type for the list head |
|---|
| 45 | |
|---|
| 46 | Description: |
|---|
| 47 | Creates new data type for the list head, this type used to create |
|---|
| 48 | variable for the lists head. |
|---|
| 49 | User should create new the list head data type for every different |
|---|
| 50 | element datatype. |
|---|
| 51 | |
|---|
| 52 | Input: |
|---|
| 53 | name - name for the new data type (structure) |
|---|
| 54 | type - existing user data type used for element of the list |
|---|
| 55 | |
|---|
| 56 | Example: |
|---|
| 57 | SI_LST_D_HEAD(block_head, block); |
|---|
| 58 | struct block_head head; |
|---|
| 59 | |
|---|
| 60 | Returns: |
|---|
| 61 | <none> |
|---|
| 62 | ****************************************************************************/ |
|---|
| 63 | #define SI_LST_D_HEAD(name, type) struct name { struct type *l_first; } |
|---|
| 64 | |
|---|
| 65 | /*************************************************************************** |
|---|
| 66 | Summary: |
|---|
| 67 | Defines links entry |
|---|
| 68 | |
|---|
| 69 | Description: |
|---|
| 70 | Defines entrys for the list inside the user structure.for the element. |
|---|
| 71 | |
|---|
| 72 | Input: |
|---|
| 73 | type - the datatype for element |
|---|
| 74 | |
|---|
| 75 | Example: |
|---|
| 76 | struct block { |
|---|
| 77 | SI_LST_D_ENTRY(block) link; |
|---|
| 78 | char string[256]; |
|---|
| 79 | }; |
|---|
| 80 | |
|---|
| 81 | Returns: |
|---|
| 82 | <none> |
|---|
| 83 | ****************************************************************************/ |
|---|
| 84 | #define SI_LST_D_ENTRY(type) struct { struct type *l_next, *l_prev; } |
|---|
| 85 | |
|---|
| 86 | /*************************************************************************** |
|---|
| 87 | Summary: |
|---|
| 88 | Initializes lists head |
|---|
| 89 | |
|---|
| 90 | Description: |
|---|
| 91 | Initializes the head of the list. The head shall be initialized before |
|---|
| 92 | list can be used. |
|---|
| 93 | This macro used for dynamic initialization. |
|---|
| 94 | |
|---|
| 95 | Input: |
|---|
| 96 | head - pointer to the list head |
|---|
| 97 | |
|---|
| 98 | See also: |
|---|
| 99 | SI_LST_D_INITIALIZER |
|---|
| 100 | |
|---|
| 101 | Example: |
|---|
| 102 | SI_LST_D_INIT(&head); |
|---|
| 103 | |
|---|
| 104 | Returns: |
|---|
| 105 | <none> |
|---|
| 106 | ****************************************************************************/ |
|---|
| 107 | #define SI_LST_D_INIT(head) ((head)->l_first=NULL) |
|---|
| 108 | |
|---|
| 109 | |
|---|
| 110 | /*************************************************************************** |
|---|
| 111 | Summary: |
|---|
| 112 | Initializes link entry |
|---|
| 113 | |
|---|
| 114 | Description: |
|---|
| 115 | Initializes the link entry. The entry shall be initialized before |
|---|
| 116 | link can be used. |
|---|
| 117 | This macro used for dynamic initialization. |
|---|
| 118 | |
|---|
| 119 | Input: |
|---|
| 120 | entry - pointer to the link entry |
|---|
| 121 | |
|---|
| 122 | See also: |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | Example: |
|---|
| 126 | SI_LST_D_INIT_ENTRY(&entry); |
|---|
| 127 | |
|---|
| 128 | Returns: |
|---|
| 129 | <none> |
|---|
| 130 | ****************************************************************************/ |
|---|
| 131 | #define SI_LST_D_INIT_ENTRY(entry) ((entry)->l_next=(entry)->l_prev=NULL) |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | /*************************************************************************** |
|---|
| 135 | Summary: |
|---|
| 136 | Initializes lists head |
|---|
| 137 | |
|---|
| 138 | Description: |
|---|
| 139 | Initializes the head of the list. The head shall be initialized before |
|---|
| 140 | list can be used. |
|---|
| 141 | This macro used for static initialization. |
|---|
| 142 | |
|---|
| 143 | Input: |
|---|
| 144 | head - pointer to the list head |
|---|
| 145 | |
|---|
| 146 | See also: |
|---|
| 147 | SI_LST_D_INIT |
|---|
| 148 | |
|---|
| 149 | Example: |
|---|
| 150 | static struct block_head head = SI_LST_D_INITIALIZER(head); |
|---|
| 151 | |
|---|
| 152 | Returns: |
|---|
| 153 | <none> |
|---|
| 154 | ****************************************************************************/ |
|---|
| 155 | #define SI_LST_D_INITIALIZER(head) {NULL} |
|---|
| 156 | |
|---|
| 157 | /*************************************************************************** |
|---|
| 158 | Summary: |
|---|
| 159 | Tests if list is empty |
|---|
| 160 | |
|---|
| 161 | Description: |
|---|
| 162 | Tests if list is empty. |
|---|
| 163 | |
|---|
| 164 | Input: |
|---|
| 165 | head - pointer to the list head |
|---|
| 166 | |
|---|
| 167 | Returns: |
|---|
| 168 | true - list empty |
|---|
| 169 | false - list has elements |
|---|
| 170 | |
|---|
| 171 | Example: |
|---|
| 172 | if (SI_LST_D_EMPTY(&head) { return ; } |
|---|
| 173 | |
|---|
| 174 | ****************************************************************************/ |
|---|
| 175 | #define SI_LST_D_EMPTY(head) ((head)->l_first == NULL) |
|---|
| 176 | |
|---|
| 177 | /*************************************************************************** |
|---|
| 178 | Summary: |
|---|
| 179 | Returns the lists first element |
|---|
| 180 | |
|---|
| 181 | Description: |
|---|
| 182 | Returns pointer to the first element from the list |
|---|
| 183 | |
|---|
| 184 | Input: |
|---|
| 185 | head - pointer to the list head |
|---|
| 186 | |
|---|
| 187 | Returns: |
|---|
| 188 | pointer to the first element from the list. |
|---|
| 189 | |
|---|
| 190 | Example: |
|---|
| 191 | struct block *first=SI_LST_D_FIRST(&head); |
|---|
| 192 | ****************************************************************************/ |
|---|
| 193 | #define SI_LST_D_FIRST(head) ((head)->l_first) |
|---|
| 194 | |
|---|
| 195 | /*************************************************************************** |
|---|
| 196 | Summary: |
|---|
| 197 | Returns next element from the lists |
|---|
| 198 | |
|---|
| 199 | Description: |
|---|
| 200 | Returns pointer to the next element from the list |
|---|
| 201 | |
|---|
| 202 | Input: |
|---|
| 203 | elm - pointer to the list element |
|---|
| 204 | field - name of the elements link field |
|---|
| 205 | |
|---|
| 206 | Returns: |
|---|
| 207 | pointer to the next element from the list |
|---|
| 208 | |
|---|
| 209 | Example: |
|---|
| 210 | struct block *second=SI_LST_D_NEXT(first); |
|---|
| 211 | ****************************************************************************/ |
|---|
| 212 | #define SI_LST_D_NEXT(elm, field) ((elm)->field.l_next) |
|---|
| 213 | |
|---|
| 214 | /*************************************************************************** |
|---|
| 215 | Summary: |
|---|
| 216 | Returns next element from the lists |
|---|
| 217 | |
|---|
| 218 | Description: |
|---|
| 219 | Returns pointer to the previous 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 previous element from the list |
|---|
| 227 | |
|---|
| 228 | Example: |
|---|
| 229 | struct block *first=SI_LST_D_PREV(second); |
|---|
| 230 | ****************************************************************************/ |
|---|
| 231 | #define SI_LST_D_PREV(elm, field) ((elm)->field.l_prev) |
|---|
| 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 | head - 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 | SI_LST_D_INSERT_HEAD(&head, new_block, link); |
|---|
| 250 | ****************************************************************************/ |
|---|
| 251 | #define SI_LST_D_INSERT_HEAD(head, elm, field) do { \ |
|---|
| 252 | if ( ((elm)->field.l_next = (head)->l_first) != NULL ) \ |
|---|
| 253 | (head)->l_first->field.l_prev = (elm); \ |
|---|
| 254 | (head)->l_first = (elm); \ |
|---|
| 255 | (elm)->field.l_prev = NULL; \ |
|---|
| 256 | } while(0) |
|---|
| 257 | |
|---|
| 258 | /*************************************************************************** |
|---|
| 259 | Summary: |
|---|
| 260 | Inserts element into the list |
|---|
| 261 | |
|---|
| 262 | Description: |
|---|
| 263 | Inserts new element after existing element. |
|---|
| 264 | |
|---|
| 265 | Input: |
|---|
| 266 | elm - pointer to the element from the list |
|---|
| 267 | new_elm - pointer to the new element |
|---|
| 268 | field - name of the elements link field |
|---|
| 269 | |
|---|
| 270 | Returns: |
|---|
| 271 | <none> |
|---|
| 272 | |
|---|
| 273 | Example: |
|---|
| 274 | SI_LST_D_INSERT_AFTER(first, second, link); |
|---|
| 275 | ****************************************************************************/ |
|---|
| 276 | #define SI_LST_D_INSERT_AFTER(elm, new_elm, field) do { \ |
|---|
| 277 | (new_elm)->field.l_prev = (elm); \ |
|---|
| 278 | if (((new_elm)->field.l_next = elm->field.l_next)!=NULL) \ |
|---|
| 279 | elm->field.l_next->field.l_prev = new_elm; \ |
|---|
| 280 | (elm)->field.l_next = (new_elm); \ |
|---|
| 281 | } while(0) |
|---|
| 282 | |
|---|
| 283 | /*************************************************************************** |
|---|
| 284 | Summary: |
|---|
| 285 | Inserts element into the list |
|---|
| 286 | |
|---|
| 287 | Description: |
|---|
| 288 | Inserts new element after existing element. |
|---|
| 289 | |
|---|
| 290 | Input: |
|---|
| 291 | head - pointer to the list head |
|---|
| 292 | elm - pointer to the element from the list |
|---|
| 293 | new_elm - pointer to the new element |
|---|
| 294 | field - name of the elements link field |
|---|
| 295 | |
|---|
| 296 | Returns: |
|---|
| 297 | <none> |
|---|
| 298 | |
|---|
| 299 | Example: |
|---|
| 300 | SI_LST_D_INSERT_BEFORE(&head, second, first, link); |
|---|
| 301 | ****************************************************************************/ |
|---|
| 302 | #define SI_LST_D_INSERT_BEFORE(head, elm, new_elm, field) do { \ |
|---|
| 303 | (new_elm)->field.l_next = (elm); \ |
|---|
| 304 | if (((new_elm)->field.l_prev = (elm)->field.l_prev)!=NULL) \ |
|---|
| 305 | elm->field.l_prev->field.l_next = new_elm; \ |
|---|
| 306 | else \ |
|---|
| 307 | (head)->l_first = (new_elm); \ |
|---|
| 308 | (elm)->field.l_prev = (new_elm); \ |
|---|
| 309 | } while(0) |
|---|
| 310 | |
|---|
| 311 | |
|---|
| 312 | |
|---|
| 313 | /*************************************************************************** |
|---|
| 314 | Summary: |
|---|
| 315 | Removes element from the list |
|---|
| 316 | |
|---|
| 317 | Description: |
|---|
| 318 | Removes element from the list. |
|---|
| 319 | |
|---|
| 320 | Input: |
|---|
| 321 | head - pointer to the list head |
|---|
| 322 | elm - pointer to the list element |
|---|
| 323 | field - name of the elements link field |
|---|
| 324 | |
|---|
| 325 | See also: |
|---|
| 326 | SI_LST_D_REMOVE_HEAD |
|---|
| 327 | |
|---|
| 328 | Returns: |
|---|
| 329 | <none> |
|---|
| 330 | |
|---|
| 331 | Example: |
|---|
| 332 | SI_LST_D_REMOVE(&head, first, link); |
|---|
| 333 | ****************************************************************************/ |
|---|
| 334 | #define SI_LST_D_REMOVE(head, elm, field) do { \ |
|---|
| 335 | if ((elm)->field.l_next) \ |
|---|
| 336 | (elm)->field.l_next->field.l_prev = (elm)->field.l_prev; \ |
|---|
| 337 | if ((elm)->field.l_prev) \ |
|---|
| 338 | (elm)->field.l_prev->field.l_next = (elm)->field.l_next; \ |
|---|
| 339 | else \ |
|---|
| 340 | (head)->l_first = (elm)->field.l_next; \ |
|---|
| 341 | } while(0) |
|---|
| 342 | |
|---|
| 343 | |
|---|
| 344 | /*************************************************************************** |
|---|
| 345 | Summary: |
|---|
| 346 | Removes element from the list |
|---|
| 347 | |
|---|
| 348 | Description: |
|---|
| 349 | Removes element from the head of the list. |
|---|
| 350 | |
|---|
| 351 | Input: |
|---|
| 352 | head - pointer to the list head |
|---|
| 353 | field - name of the elements link field |
|---|
| 354 | |
|---|
| 355 | See also: |
|---|
| 356 | SI_LST_D_REMOVE |
|---|
| 357 | |
|---|
| 358 | Returns: |
|---|
| 359 | <none> |
|---|
| 360 | |
|---|
| 361 | Example: |
|---|
| 362 | SI_LST_D_REMOVE_HEAD(&head, first, link); |
|---|
| 363 | ****************************************************************************/ |
|---|
| 364 | #define SI_LST_D_REMOVE_HEAD(head, field) do { \ |
|---|
| 365 | (head)->l_first = (head)->l_first->field.l_next; \ |
|---|
| 366 | if ((head)->l_first) \ |
|---|
| 367 | { \ |
|---|
| 368 | (head)->l_first->field.l_prev = NULL;\ |
|---|
| 369 | } \ |
|---|
| 370 | } while(0) |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | |
|---|
| 375 | #endif |
|---|
| 376 | |
|---|