list.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #ifndef __HI_LIST_H__
  2. #define __HI_LIST_H__
  3. #ifndef _LINUX_LIST_H
  4. #define INIT_LIST_HEAD(ptr) \
  5. do { \
  6. (ptr)->next = (ptr); \
  7. (ptr)->prev = (ptr); \
  8. } while (0)
  9. #define LIST_HEAD_INIT(name) { &(name), &(name) }
  10. struct list_head {
  11. struct list_head *next, *prev;
  12. };
  13. static inline void __list_add(struct list_head * _new, struct list_head * prev, struct list_head * next)
  14. {
  15. next->prev = _new;
  16. _new->next = next;
  17. _new->prev = prev;
  18. prev->next = _new;
  19. }
  20. static inline void list_add(struct list_head *_new, struct list_head *head)
  21. {
  22. __list_add(_new, head, head->next);
  23. }
  24. static inline void list_add_tail(struct list_head *_new, struct list_head *head)
  25. {
  26. __list_add(_new, head->prev, head);
  27. }
  28. static inline void __list_del(struct list_head * prev, struct list_head * next)
  29. {
  30. next->prev = prev;
  31. prev->next = next;
  32. }
  33. static inline void list_del(struct list_head *entry)
  34. {
  35. __list_del(entry->prev, entry->next);
  36. }
  37. static inline void list_del_init(struct list_head *entry)
  38. {
  39. __list_del(entry->prev, entry->next);
  40. INIT_LIST_HEAD(entry);
  41. }
  42. static inline void list_move(struct list_head *list, struct list_head *head)
  43. {
  44. __list_del(list->prev, list->next);
  45. list_add(list, head);
  46. }
  47. static inline void list_move_tail(struct list_head *list,
  48. struct list_head *head)
  49. {
  50. __list_del(list->prev, list->next);
  51. list_add_tail(list, head);
  52. }
  53. static inline int list_empty(struct list_head *head)
  54. {
  55. return head->next == head;
  56. }
  57. static inline void __list_splice(struct list_head *list,
  58. struct list_head *head)
  59. {
  60. struct list_head *first = list->next;
  61. struct list_head *last = list->prev;
  62. struct list_head *at = head->next;
  63. first->prev = head;
  64. head->next = first;
  65. last->next = at;
  66. at->prev = last;
  67. }
  68. static inline void list_splice(struct list_head *list, struct list_head *head)
  69. {
  70. if (!list_empty(list))
  71. {
  72. __list_splice(list, head);
  73. }
  74. }
  75. static inline void list_splice_init(struct list_head *list, struct list_head *head)
  76. {
  77. if (!list_empty(list))
  78. {
  79. __list_splice(list, head);
  80. INIT_LIST_HEAD(list);
  81. }
  82. }
  83. #define list_entry(ptr, type, member) \
  84. ((type *)((unsigned long)(ptr)-((unsigned long)(&((type *)1)->member) - 1)))
  85. #define list_for_each(pos, head) \
  86. for (pos = (head)->next; pos != (head); pos = pos->next)
  87. #define list_for_each_safe(pos, n, head) \
  88. for (pos = (head)->next, n = pos->next; pos != (head); \
  89. pos = n, n = pos->next)
  90. #define get_first_item(attached, type, member) \
  91. ((type *)((char *)((attached)->next)-(unsigned long)(&((type *)0)->member)))
  92. #endif
  93. #endif