1
0

list.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #ifndef LIST_H
  2. #define LIST_H
  3. /*
  4. * Copied from include/linux/...
  5. */
  6. #undef offsetof
  7. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  8. /**
  9. * container_of - cast a member of a structure out to the containing structure
  10. * @ptr: the pointer to the member.
  11. * @type: the type of the container struct this is embedded in.
  12. * @member: the name of the member within the struct.
  13. *
  14. */
  15. #define container_of(ptr, type, member) ({ \
  16. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  17. (type *)( (char *)__mptr - offsetof(type,member) );})
  18. struct list_head {
  19. struct list_head *next, *prev;
  20. };
  21. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  22. #define LIST_HEAD(name) \
  23. struct list_head name = LIST_HEAD_INIT(name)
  24. /**
  25. * list_entry - get the struct for this entry
  26. * @ptr: the &struct list_head pointer.
  27. * @type: the type of the struct this is embedded in.
  28. * @member: the name of the list_struct within the struct.
  29. */
  30. #define list_entry(ptr, type, member) \
  31. container_of(ptr, type, member)
  32. /**
  33. * list_for_each_entry - iterate over list of given type
  34. * @pos: the type * to use as a loop cursor.
  35. * @head: the head for your list.
  36. * @member: the name of the list_struct within the struct.
  37. */
  38. #define list_for_each_entry(pos, head, member) \
  39. for (pos = list_entry((head)->next, typeof(*pos), member); \
  40. &pos->member != (head); \
  41. pos = list_entry(pos->member.next, typeof(*pos), member))
  42. /**
  43. * list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  44. * @pos: the type * to use as a loop cursor.
  45. * @n: another type * to use as temporary storage
  46. * @head: the head for your list.
  47. * @member: the name of the list_struct within the struct.
  48. */
  49. #define list_for_each_entry_safe(pos, n, head, member) \
  50. for (pos = list_entry((head)->next, typeof(*pos), member), \
  51. n = list_entry(pos->member.next, typeof(*pos), member); \
  52. &pos->member != (head); \
  53. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  54. /**
  55. * list_empty - tests whether a list is empty
  56. * @head: the list to test.
  57. */
  58. static inline int list_empty(const struct list_head *head)
  59. {
  60. return head->next == head;
  61. }
  62. /*
  63. * Insert a new entry between two known consecutive entries.
  64. *
  65. * This is only for internal list manipulation where we know
  66. * the prev/next entries already!
  67. */
  68. static inline void __list_add(struct list_head *_new,
  69. struct list_head *prev,
  70. struct list_head *next)
  71. {
  72. next->prev = _new;
  73. _new->next = next;
  74. _new->prev = prev;
  75. prev->next = _new;
  76. }
  77. /**
  78. * list_add_tail - add a new entry
  79. * @new: new entry to be added
  80. * @head: list head to add it before
  81. *
  82. * Insert a new entry before the specified head.
  83. * This is useful for implementing queues.
  84. */
  85. static inline void list_add_tail(struct list_head *_new, struct list_head *head)
  86. {
  87. __list_add(_new, head->prev, head);
  88. }
  89. /*
  90. * Delete a list entry by making the prev/next entries
  91. * point to each other.
  92. *
  93. * This is only for internal list manipulation where we know
  94. * the prev/next entries already!
  95. */
  96. static inline void __list_del(struct list_head *prev, struct list_head *next)
  97. {
  98. next->prev = prev;
  99. prev->next = next;
  100. }
  101. #define LIST_POISON1 ((void *) 0x00100100)
  102. #define LIST_POISON2 ((void *) 0x00200200)
  103. /**
  104. * list_del - deletes entry from list.
  105. * @entry: the element to delete from the list.
  106. * Note: list_empty() on entry does not return true after this, the entry is
  107. * in an undefined state.
  108. */
  109. static inline void list_del(struct list_head *entry)
  110. {
  111. __list_del(entry->prev, entry->next);
  112. entry->next = (struct list_head*)LIST_POISON1;
  113. entry->prev = (struct list_head*)LIST_POISON2;
  114. }
  115. #endif