1/* 2 * Copyright © 2010 Intel Corporation 3 * Copyright © 2010 Francisco Jerez <currojerez@riseup.net> 4 * 5 * Permission is hereby granted, free of charge, to any person obtaining a 6 * copy of this software and associated documentation files (the "Software"), 7 * to deal in the Software without restriction, including without limitation 8 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 * and/or sell copies of the Software, and to permit persons to whom the 10 * Software is furnished to do so, subject to the following conditions: 11 * 12 * The above copyright notice and this permission notice (including the next 13 * paragraph) shall be included in all copies or substantial portions of the 14 * Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 22 * IN THE SOFTWARE. 23 * 24 */ 25 26#ifndef _XORG_LIST_H_ 27#define _XORG_LIST_H_ 28 29#include <stddef.h> /* offsetof() */ 30 31/** 32 * @file Classic doubly-link circular list implementation. 33 * For real usage examples of the linked list, see the file test/list.c 34 * 35 * Example: 36 * We need to keep a list of struct foo in the parent struct bar, i.e. what 37 * we want is something like this. 38 * 39 * struct bar { 40 * ... 41 * struct foo *list_of_foos; -----> struct foo {}, struct foo {}, struct foo{} 42 * ... 43 * } 44 * 45 * We need one list head in bar and a list element in all list_of_foos (both are of 46 * data type 'struct xorg_list'). 47 * 48 * struct bar { 49 * ... 50 * struct xorg_list list_of_foos; 51 * ... 52 * } 53 * 54 * struct foo { 55 * ... 56 * struct xorg_list entry; 57 * ... 58 * } 59 * 60 * Now we initialize the list head: 61 * 62 * struct bar bar; 63 * ... 64 * xorg_list_init(&bar.list_of_foos); 65 * 66 * Then we create the first element and add it to this list: 67 * 68 * struct foo *foo = malloc(...); 69 * .... 70 * xorg_list_add(&foo->entry, &bar.list_of_foos); 71 * 72 * Repeat the above for each element you want to add to the list. Deleting 73 * works with the element itself. 74 * xorg_list_del(&foo->entry); 75 * free(foo); 76 * 77 * Note: calling xorg_list_del(&bar.list_of_foos) will set bar.list_of_foos to an empty 78 * list again. 79 * 80 * Looping through the list requires a 'struct foo' as iterator and the 81 * name of the field the subnodes use. 82 * 83 * struct foo *iterator; 84 * xorg_list_for_each_entry(iterator, &bar.list_of_foos, entry) { 85 * if (iterator->something == ...) 86 * ... 87 * } 88 * 89 * Note: You must not call xorg_list_del() on the iterator if you continue the 90 * loop. You need to run the safe for-each loop instead: 91 * 92 * struct foo *iterator, *next; 93 * xorg_list_for_each_entry_safe(iterator, next, &bar.list_of_foos, entry) { 94 * if (...) 95 * xorg_list_del(&iterator->entry); 96 * } 97 * 98 */ 99 100/** 101 * The linkage struct for list nodes. This struct must be part of your 102 * to-be-linked struct. struct xorg_list is required for both the head of the 103 * list and for each list node. 104 * 105 * Position and name of the struct xorg_list field is irrelevant. 106 * There are no requirements that elements of a list are of the same type. 107 * There are no requirements for a list head, any struct xorg_list can be a list 108 * head. 109 */ 110struct xorg_list { 111 struct xorg_list *next, *prev; 112}; 113 114/** 115 * Initialize the list as an empty list. 116 * 117 * Example: 118 * xorg_list_init(&bar->list_of_foos); 119 * 120 * @param list The list to initialize 121 */ 122static inline void 123xorg_list_init(struct xorg_list *list) 124{ 125 list->next = list->prev = list; 126} 127 128static inline void 129__xorg_list_add(struct xorg_list *entry, 130 struct xorg_list *prev, struct xorg_list *next) 131{ 132 next->prev = entry; 133 entry->next = next; 134 entry->prev = prev; 135 prev->next = entry; 136} 137 138/** 139 * Insert a new element after the given list head. The new element does not 140 * need to be initialised as empty list. 141 * The list changes from: 142 * head → some element → ... 143 * to 144 * head → new element → older element → ... 145 * 146 * Example: 147 * struct foo *newfoo = malloc(...); 148 * xorg_list_add(&newfoo->entry, &bar->list_of_foos); 149 * 150 * @param entry The new element to prepend to the list. 151 * @param head The existing list. 152 */ 153static inline void 154xorg_list_add(struct xorg_list *entry, struct xorg_list *head) 155{ 156 __xorg_list_add(entry, head, head->next); 157} 158 159/** 160 * Append a new element to the end of the list given with this list head. 161 * 162 * The list changes from: 163 * head → some element → ... → lastelement 164 * to 165 * head → some element → ... → lastelement → new element 166 * 167 * Example: 168 * struct foo *newfoo = malloc(...); 169 * xorg_list_append(&newfoo->entry, &bar->list_of_foos); 170 * 171 * @param entry The new element to prepend to the list. 172 * @param head The existing list. 173 */ 174static inline void 175xorg_list_append(struct xorg_list *entry, struct xorg_list *head) 176{ 177 __xorg_list_add(entry, head->prev, head); 178} 179 180static inline void 181__xorg_list_del(struct xorg_list *prev, struct xorg_list *next) 182{ 183 next->prev = prev; 184 prev->next = next; 185} 186 187/** 188 * Remove the element from the list it is in. Using this function will reset 189 * the pointers to/from this element so it is removed from the list. It does 190 * NOT free the element itself or manipulate it otherwise. 191 * 192 * Using xorg_list_del on a pure list head (like in the example at the top of 193 * this file) will NOT remove the first element from 194 * the list but rather reset the list as empty list. 195 * 196 * Example: 197 * xorg_list_del(&foo->entry); 198 * 199 * @param entry The element to remove. 200 */ 201static inline void 202xorg_list_del(struct xorg_list *entry) 203{ 204 __xorg_list_del(entry->prev, entry->next); 205 xorg_list_init(entry); 206} 207 208/** 209 * Check if the list is empty. 210 * 211 * Example: 212 * xorg_list_is_empty(&bar->list_of_foos); 213 * 214 * @return True if the list is empty or False if the list contains one or more 215 * elements. 216 */ 217static inline int 218xorg_list_is_empty(struct xorg_list *head) 219{ 220 return head->next == head; 221} 222 223/** 224 * Returns a pointer to the container of this list element. 225 * 226 * Example: 227 * struct foo* f; 228 * f = container_of(&foo->entry, struct foo, entry); 229 * assert(f == foo); 230 * 231 * @param ptr Pointer to the struct xorg_list. 232 * @param type Data type of the list element. 233 * @param member Member name of the struct xorg_list field in the list element. 234 * @return A pointer to the data struct containing the list head. 235 */ 236#ifndef container_of 237#define container_of(ptr, type, member) \ 238 (type *)((char *)(ptr) - offsetof(type, member)) 239#endif 240 241/** 242 * Alias of container_of 243 */ 244#define xorg_list_entry(ptr, type, member) \ 245 container_of(ptr, type, member) 246 247/** 248 * Retrieve the first list entry for the given list pointer. 249 * 250 * Example: 251 * struct foo *first; 252 * first = xorg_list_first_entry(&bar->list_of_foos, struct foo, list_of_foos); 253 * 254 * @param ptr The list head 255 * @param type Data type of the list element to retrieve 256 * @param member Member name of the struct xorg_list field in the list element. 257 * @return A pointer to the first list element. 258 */ 259#define xorg_list_first_entry(ptr, type, member) \ 260 xorg_list_entry((ptr)->next, type, member) 261 262/** 263 * Retrieve the last list entry for the given listpointer. 264 * 265 * Example: 266 * struct foo *first; 267 * first = xorg_list_last_entry(&bar->list_of_foos, struct foo, list_of_foos); 268 * 269 * @param ptr The list head 270 * @param type Data type of the list element to retrieve 271 * @param member Member name of the struct xorg_list field in the list element. 272 * @return A pointer to the last list element. 273 */ 274#define xorg_list_last_entry(ptr, type, member) \ 275 xorg_list_entry((ptr)->prev, type, member) 276 277#ifdef HAVE_TYPEOF 278#define __container_of(ptr, sample, member) \ 279 container_of(ptr, typeof(*sample), member) 280#else 281/* This implementation of __container_of has undefined behavior according 282 * to the C standard, but it works in many cases. If your compiler doesn't 283 * support typeof() and fails with this implementation, please try a newer 284 * compiler. 285 */ 286#define __container_of(ptr, sample, member) \ 287 (void *)((char *)(ptr) \ 288 - ((char *)&(sample)->member - (char *)(sample))) 289#endif 290 291/** 292 * Loop through the list given by head and set pos to struct in the list. 293 * 294 * Example: 295 * struct foo *iterator; 296 * xorg_list_for_each_entry(iterator, &bar->list_of_foos, entry) { 297 * [modify iterator] 298 * } 299 * 300 * This macro is not safe for node deletion. Use xorg_list_for_each_entry_safe 301 * instead. 302 * 303 * @param pos Iterator variable of the type of the list elements. 304 * @param head List head 305 * @param member Member name of the struct xorg_list in the list elements. 306 * 307 */ 308#define xorg_list_for_each_entry(pos, head, member) \ 309 for (pos = NULL, \ 310 pos = __container_of((head)->next, pos, member); \ 311 &pos->member != (head); \ 312 pos = __container_of(pos->member.next, pos, member)) 313 314/** 315 * Loop through the list, keeping a backup pointer to the element. This 316 * macro allows for the deletion of a list element while looping through the 317 * list. 318 * 319 * See xorg_list_for_each_entry for more details. 320 */ 321#define xorg_list_for_each_entry_safe(pos, tmp, head, member) \ 322 for (pos = NULL, \ 323 pos = __container_of((head)->next, pos, member), \ 324 tmp = __container_of(pos->member.next, pos, member); \ 325 &pos->member != (head); \ 326 pos = tmp, tmp = __container_of(pos->member.next, tmp, member)) 327 328/* NULL-Terminated List Interface 329 * 330 * The interface below does _not_ use the struct xorg_list as described above. 331 * It is mainly for legacy structures that cannot easily be switched to 332 * struct xorg_list. 333 * 334 * This interface is for structs like 335 * struct foo { 336 * [...] 337 * struct foo *next; 338 * [...] 339 * }; 340 * 341 * The position and field name of "next" are arbitrary. 342 */ 343 344/** 345 * Init the element as null-terminated list. 346 * 347 * Example: 348 * struct foo *list = malloc(); 349 * nt_list_init(list, next); 350 * 351 * @param list The list element that will be the start of the list 352 * @param member Member name of the field pointing to next struct 353 */ 354#define nt_list_init(_list, _member) \ 355 (_list)->_member = NULL 356 357/** 358 * Returns the next element in the list or NULL on termination. 359 * 360 * Example: 361 * struct foo *element = list; 362 * while ((element = nt_list_next(element, next)) { } 363 * 364 * This macro is not safe for node deletion. Use nt_list_for_each_entry_safe 365 * instead. 366 * 367 * @param list The list or current element. 368 * @param member Member name of the field pointing to next struct. 369 */ 370#define nt_list_next(_list, _member) \ 371 (_list)->_member 372 373/** 374 * Iterate through each element in the list. 375 * 376 * Example: 377 * struct foo *iterator; 378 * nt_list_for_each_entry(iterator, list, next) { 379 * [modify iterator] 380 * } 381 * 382 * @param entry Assigned to the current list element 383 * @param list The list to iterate through. 384 * @param member Member name of the field pointing to next struct. 385 */ 386#define nt_list_for_each_entry(_entry, _list, _member) \ 387 for (_entry = _list; _entry; _entry = (_entry)->_member) 388 389/** 390 * Iterate through each element in the list, keeping a backup pointer to the 391 * element. This macro allows for the deletion of a list element while 392 * looping through the list. 393 * 394 * See nt_list_for_each_entry for more details. 395 * 396 * @param entry Assigned to the current list element 397 * @param tmp The pointer to the next element 398 * @param list The list to iterate through. 399 * @param member Member name of the field pointing to next struct. 400 */ 401#define nt_list_for_each_entry_safe(_entry, _tmp, _list, _member) \ 402 for (_entry = _list, _tmp = (_entry) ? (_entry)->_member : NULL;\ 403 _entry; \ 404 _entry = _tmp, _tmp = (_tmp) ? (_tmp)->_member: NULL) 405 406/** 407 * Append the element to the end of the list. This macro may be used to 408 * merge two lists. 409 * 410 * Example: 411 * struct foo *elem = malloc(...); 412 * nt_list_init(elem, next) 413 * nt_list_append(elem, list, struct foo, next); 414 * 415 * Resulting list order: 416 * list_item_0 -> list_item_1 -> ... -> elem_item_0 -> elem_item_1 ... 417 * 418 * @param entry An entry (or list) to append to the list 419 * @param list The list to append to. This list must be a valid list, not 420 * NULL. 421 * @param type The list type 422 * @param member Member name of the field pointing to next struct 423 */ 424#define nt_list_append(_entry, _list, _type, _member) \ 425 do { \ 426 _type *__iterator = _list; \ 427 while (__iterator->_member) { __iterator = __iterator->_member;}\ 428 __iterator->_member = _entry; \ 429 } while (0) 430 431/** 432 * Insert the element at the next position in the list. This macro may be 433 * used to insert a list into a list. 434 * 435 * struct foo *elem = malloc(...); 436 * nt_list_init(elem, next) 437 * nt_list_insert(elem, list, struct foo, next); 438 * 439 * Resulting list order: 440 * list_item_0 -> elem_item_0 -> elem_item_1 ... -> list_item_1 -> ... 441 * 442 * @param entry An entry (or list) to append to the list 443 * @param list The list to insert to. This list must be a valid list, not 444 * NULL. 445 * @param type The list type 446 * @param member Member name of the field pointing to next struct 447 */ 448#define nt_list_insert(_entry, _list, _type, _member) \ 449 do { \ 450 nt_list_append((_list)->_member, _entry, _type, _member); \ 451 (_list)->_member = _entry; \ 452 } while (0) 453 454/** 455 * Delete the entry from the list by iterating through the list and 456 * removing any reference from the list to the entry. 457 * 458 * Example: 459 * struct foo *elem = <assign to right element> 460 * nt_list_del(elem, list, struct foo, next); 461 * 462 * @param entry The entry to delete from the list. entry is always 463 * re-initialized as a null-terminated list. 464 * @param list The list containing the entry, set to the new list without 465 * the removed entry. 466 * @param type The list type 467 * @param member Member name of the field pointing to the next entry 468 */ 469#define nt_list_del(_entry, _list, _type, _member) \ 470 do { \ 471 _type *__e = _entry; \ 472 if (__e == NULL || _list == NULL) break; \ 473 if ((_list) == __e) { \ 474 _list = __e->_member; \ 475 } else { \ 476 _type *__prev = _list; \ 477 while (__prev->_member && __prev->_member != __e) \ 478 __prev = nt_list_next(__prev, _member); \ 479 if (__prev->_member) \ 480 __prev->_member = __e->_member; \ 481 } \ 482 nt_list_init(__e, _member); \ 483 } while(0) 484 485/** 486 * DO NOT USE THIS. 487 * This is a remainder of the xfree86 DDX attempt of having a set of generic 488 * list functions. Unfortunately, the xf86OptionRec uses it and we can't 489 * easily get rid of it. Do not use for new code. 490 */ 491typedef struct generic_list_rec { 492 void *next; 493} GenericListRec, *GenericListPtr, *glp; 494 495#endif 496