list.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #ifndef _LIST_H_
  26. #define _LIST_H_
  27. /* classic doubly-link circular list */
  28. struct list {
  29. struct list *next, *prev;
  30. };
  31. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  32. #define LIST_HEAD(name) \
  33. struct list name = LIST_HEAD_INIT(name)
  34. static void
  35. list_init(struct list *list)
  36. {
  37. list->next = list->prev = list;
  38. }
  39. static inline void
  40. __list_add(struct list *entry,
  41. struct list *prev,
  42. struct list *next)
  43. {
  44. next->prev = entry;
  45. entry->next = next;
  46. entry->prev = prev;
  47. prev->next = entry;
  48. }
  49. static inline void
  50. list_add(struct list *entry, struct list *head)
  51. {
  52. __list_add(entry, head, head->next);
  53. }
  54. static inline void
  55. __list_del(struct list *prev, struct list *next)
  56. {
  57. next->prev = prev;
  58. prev->next = next;
  59. }
  60. static inline void
  61. list_del(struct list *entry)
  62. {
  63. __list_del(entry->prev, entry->next);
  64. list_init(entry);
  65. }
  66. static inline int
  67. list_is_empty(struct list *head)
  68. {
  69. return head->next == head;
  70. }
  71. #ifndef container_of
  72. #define container_of(ptr, type, member) \
  73. (type *)((char *)(ptr) - (char *) &((type *)0)->member)
  74. #endif
  75. #define list_entry(ptr, type, member) \
  76. container_of(ptr, type, member)
  77. #define list_first_entry(ptr, type, member) \
  78. list_entry((ptr)->next, type, member)
  79. #define __container_of(ptr, sample, member) \
  80. (void *)((char *)(ptr) \
  81. - ((char *)&(sample)->member - (char *)(sample)))
  82. #define list_for_each_entry(pos, head, member) \
  83. for (pos = __container_of((head)->next, pos, member); \
  84. &pos->member != (head); \
  85. pos = __container_of(pos->member.next, pos, member))
  86. #define list_for_each_entry_safe(pos, tmp, head, member) \
  87. for (pos = __container_of((head)->next, pos, member), \
  88. tmp = __container_of(pos->member.next, pos, member); \
  89. &pos->member != (head); \
  90. pos = tmp, tmp = __container_of(pos->member.next, tmp, member))
  91. #endif