util_double_list.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. *
  3. * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND. USA.
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  18. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * The above copyright notice and this permission notice (including the
  23. * next paragraph) shall be included in all copies or substantial portions
  24. * of the Software.
  25. *
  26. */
  27. /**
  28. * \file
  29. * List macros heavily inspired by the Linux kernel
  30. * list handling. No list looping yet.
  31. *
  32. * Is not threadsafe, so common operations need to
  33. * be protected using an external mutex.
  34. */
  35. #ifndef _U_DOUBLE_LIST_H_
  36. #define _U_DOUBLE_LIST_H_
  37. #include <stddef.h>
  38. struct list_head
  39. {
  40. struct list_head *prev;
  41. struct list_head *next;
  42. };
  43. static inline void list_inithead(struct list_head *item)
  44. {
  45. item->prev = item;
  46. item->next = item;
  47. }
  48. static inline void list_add(struct list_head *item, struct list_head *list)
  49. {
  50. item->prev = list;
  51. item->next = list->next;
  52. list->next->prev = item;
  53. list->next = item;
  54. }
  55. static inline void list_addtail(struct list_head *item, struct list_head *list)
  56. {
  57. item->next = list;
  58. item->prev = list->prev;
  59. list->prev->next = item;
  60. list->prev = item;
  61. }
  62. static inline void list_replace(struct list_head *from, struct list_head *to)
  63. {
  64. to->prev = from->prev;
  65. to->next = from->next;
  66. from->next->prev = to;
  67. from->prev->next = to;
  68. }
  69. static inline void list_del(struct list_head *item)
  70. {
  71. item->prev->next = item->next;
  72. item->next->prev = item->prev;
  73. }
  74. static inline void list_delinit(struct list_head *item)
  75. {
  76. item->prev->next = item->next;
  77. item->next->prev = item->prev;
  78. item->next = item;
  79. item->prev = item;
  80. }
  81. #define LIST_INITHEAD(__item) list_inithead(__item)
  82. #define LIST_ADD(__item, __list) list_add(__item, __list)
  83. #define LIST_ADDTAIL(__item, __list) list_addtail(__item, __list)
  84. #define LIST_REPLACE(__from, __to) list_replace(__from, __to)
  85. #define LIST_DEL(__item) list_del(__item)
  86. #define LIST_DELINIT(__item) list_delinit(__item)
  87. #define LIST_ENTRY(__type, __item, __field) \
  88. ((__type *)(((char *)(__item)) - offsetof(__type, __field)))
  89. #define LIST_FIRST_ENTRY(__ptr, __type, __field) \
  90. LIST_ENTRY(__type, (__ptr)->next, __field)
  91. #define LIST_LAST_ENTRY(__ptr, __type, __field) \
  92. LIST_ENTRY(__type, (__ptr)->prev, __field)
  93. #define LIST_IS_EMPTY(__list) \
  94. ((__list)->next == (__list))
  95. #ifndef container_of
  96. #define container_of(ptr, sample, member) \
  97. (void *)((char *)(ptr) \
  98. - ((char *)&((typeof(sample))0)->member))
  99. #endif
  100. #define LIST_FOR_EACH_ENTRY(pos, head, member) \
  101. for (pos = container_of((head)->next, pos, member); \
  102. &pos->member != (head); \
  103. pos = container_of(pos->member.next, pos, member))
  104. #define LIST_FOR_EACH_ENTRY_SAFE(pos, storage, head, member) \
  105. for (pos = container_of((head)->next, pos, member), \
  106. storage = container_of(pos->member.next, pos, member); \
  107. &pos->member != (head); \
  108. pos = storage, storage = container_of(storage->member.next, storage, member))
  109. #define LIST_FOR_EACH_ENTRY_SAFE_REV(pos, storage, head, member) \
  110. for (pos = container_of((head)->prev, pos, member), \
  111. storage = container_of(pos->member.prev, pos, member); \
  112. &pos->member != (head); \
  113. pos = storage, storage = container_of(storage->member.prev, storage, member))
  114. #define LIST_FOR_EACH_ENTRY_FROM(pos, start, head, member) \
  115. for (pos = container_of((start), pos, member); \
  116. &pos->member != (head); \
  117. pos = container_of(pos->member.next, pos, member))
  118. #define LIST_FOR_EACH_ENTRY_FROM_REV(pos, start, head, member) \
  119. for (pos = container_of((start), pos, member); \
  120. &pos->member != (head); \
  121. pos = container_of(pos->member.prev, pos, member))
  122. #endif /*_U_DOUBLE_LIST_H_*/