yc_list.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. #ifndef _YC_LLIST_H
  2. #define _YC_LLIST_H
  3. #ifdef __cplusplus
  4. extern "C"
  5. {
  6. #endif
  7. struct yc_list_head {
  8. struct yc_list_head *next;
  9. struct yc_list_head *prev;
  10. };
  11. /**
  12. * initiate the list
  13. */
  14. #define INIT_LIST_HEAD(head) do { \
  15. (head)->next = (head)->prev = head; \
  16. } while (0)
  17. /**
  18. * add new list_head into one list at the beginning
  19. * (generally speaking, the first element in one list doesn't contain an structure, i.e. simple list_head object)
  20. */
  21. static inline void yc_list_add (struct yc_list_head *_new, struct yc_list_head *head)
  22. {
  23. _new->prev = head;
  24. _new->next = head->next;
  25. _new->prev->next = _new;
  26. _new->next->prev = _new;
  27. }
  28. /**
  29. * add _new list_head into one list at the ending
  30. * (generally speaking, the first element in one list doesn't contain an structure, i.e. simple list_head object)
  31. */
  32. static inline void yc_list_add_tail (struct yc_list_head *_new, struct yc_list_head *head)
  33. {
  34. _new->next = head;
  35. _new->prev = head->prev;
  36. _new->prev->next = _new;
  37. _new->next->prev = _new;
  38. }
  39. /**
  40. * delete an yc_list_head from the list containing it
  41. */
  42. static inline void yc_list_del (struct yc_list_head *old)
  43. {
  44. old->prev->next = old->next;
  45. old->next->prev = old->prev;
  46. old->next = (struct yc_list_head *)0xaaaaaaaa;
  47. old->prev = (struct yc_list_head *)0xbbbbbbbb;
  48. }
  49. /**
  50. * delete an list_head from the list containing it
  51. * and initite itself
  52. */
  53. static inline void yc_list_del_init (struct yc_list_head *old)
  54. {
  55. old->prev->next = old->next;
  56. old->next->prev = old->prev;
  57. old->next = old;
  58. old->prev = old;
  59. }
  60. /**
  61. * check if the list is empty
  62. * (generally speaking, the first element in one list doesn't contain an structure, i.e. simple yc_list_head object
  63. */
  64. static inline int yc_list_empty (struct yc_list_head *head)
  65. {
  66. return (head->next == head);
  67. }
  68. static inline int yc_list_size(struct yc_list_head *head){
  69. struct yc_list_head* find;
  70. int num;
  71. num = 0;
  72. find = head->next;
  73. while(find != head){
  74. num ++;
  75. find = find->next;
  76. }
  77. return num;
  78. }
  79. /**
  80. * from the structure member to the the structure
  81. * eg:
  82. * struct exa_struct{
  83. * int a;
  84. * list_head here_pos;
  85. * ...;
  86. * }
  87. *
  88. * list_head *my_head = ...; //which is contained in the exa_sturct structure
  89. *
  90. * //you can get the exa_structure pointer by
  91. * exa_struct *p = list_entry(my_head, struct exa_struct, here_pos);
  92. *
  93. */
  94. #define list_entry(ptr, type, member) \
  95. ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
  96. /**
  97. * traverse the whole list. Notice, the first one list_head not containing
  98. * the structure, which only stands for an list_head object
  99. *
  100. * you can use like this:
  101. *
  102. * list_head* head;
  103. * ...
  104. * ...
  105. *
  106. * list_head* trav_head;
  107. * list_for_each(trav_head, head){
  108. * ....
  109. * ....
  110. * ....
  111. * }
  112. */
  113. #define list_for_each(pos, head) \
  114. for (pos = (head)->next; pos != (head); pos = pos->next)
  115. /**
  116. * pos: structure pointer
  117. * head: list header
  118. * member: name of list_head in the structure
  119. */
  120. #define list_for_each_entry(pos, head, member) \
  121. for (pos = list_entry((head)->next, typeof(*pos), member); \
  122. &pos->member != (head); \
  123. pos = list_entry(pos->member.next, typeof(*pos), member))
  124. /**
  125. * pos: structure pointer, used in the loop body
  126. * n: structure pointer, not used in the loop body
  127. * head: list header
  128. * member: name of list_head in the structure
  129. */
  130. #define list_for_each_entry_safe(pos, n, head, member) \
  131. for (pos = list_entry((head)->next, typeof(*pos), member), \
  132. n = list_entry(pos->member.next, typeof(*pos), member); \
  133. &pos->member != (head); \
  134. pos = n, n = list_entry(n->member.next, typeof(*n), member))
  135. typedef struct yc_list_head LHead_t;
  136. #ifdef __cplusplus
  137. }
  138. #endif
  139. #endif /* _LLIST_H */