list.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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_empty - tests whether a list is empty
  44. * @head: the list to test.
  45. */
  46. static inline int list_empty(const struct list_head *head)
  47. {
  48. return head->next == head;
  49. }
  50. /*
  51. * Insert a new entry between two known consecutive entries.
  52. *
  53. * This is only for internal list manipulation where we know
  54. * the prev/next entries already!
  55. */
  56. static inline void __list_add(struct list_head *_new,
  57. struct list_head *prev,
  58. struct list_head *next)
  59. {
  60. next->prev = _new;
  61. _new->next = next;
  62. _new->prev = prev;
  63. prev->next = _new;
  64. }
  65. /**
  66. * list_add_tail - add a new entry
  67. * @new: new entry to be added
  68. * @head: list head to add it before
  69. *
  70. * Insert a new entry before the specified head.
  71. * This is useful for implementing queues.
  72. */
  73. static inline void list_add_tail(struct list_head *_new, struct list_head *head)
  74. {
  75. __list_add(_new, head->prev, head);
  76. }
  77. #endif