rtservice.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * File : rtservice.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2006-03-16 Bernard the first version
  23. * 2006-09-07 Bernard move the kservice APIs to rtthread.h
  24. * 2007-06-27 Bernard fix the rt_list_remove bug
  25. * 2012-03-22 Bernard rename kservice.h to rtservice.h
  26. */
  27. #ifndef __RT_SERVICE_H__
  28. #define __RT_SERVICE_H__
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. /**
  33. * @addtogroup KernelService
  34. */
  35. /*@{*/
  36. /**
  37. * @brief initialize a list object
  38. */
  39. #define RT_LIST_OBJECT_INIT(object) { &(object), &(object) }
  40. /**
  41. * @brief initialize a list
  42. *
  43. * @param l list to be initialized
  44. */
  45. rt_inline void rt_list_init(rt_list_t *l)
  46. {
  47. l->next = l->prev = l;
  48. }
  49. /**
  50. * @brief insert a node after a list
  51. *
  52. * @param l list to insert it
  53. * @param n new node to be inserted
  54. */
  55. rt_inline void rt_list_insert_after(rt_list_t *l, rt_list_t *n)
  56. {
  57. l->next->prev = n;
  58. n->next = l->next;
  59. l->next = n;
  60. n->prev = l;
  61. }
  62. /**
  63. * @brief insert a node before a list
  64. *
  65. * @param n new node to be inserted
  66. * @param l list to insert it
  67. */
  68. rt_inline void rt_list_insert_before(rt_list_t *l, rt_list_t *n)
  69. {
  70. l->prev->next = n;
  71. n->prev = l->prev;
  72. l->prev = n;
  73. n->next = l;
  74. }
  75. /**
  76. * @brief remove node from list.
  77. * @param n the node to remove from the list.
  78. */
  79. rt_inline void rt_list_remove(rt_list_t *n)
  80. {
  81. n->next->prev = n->prev;
  82. n->prev->next = n->next;
  83. n->next = n->prev = n;
  84. }
  85. /**
  86. * @brief tests whether a list is empty
  87. * @param l the list to test.
  88. */
  89. rt_inline int rt_list_isempty(const rt_list_t *l)
  90. {
  91. return l->next == l;
  92. }
  93. /**
  94. * @brief get the list length
  95. * @param l the list to get.
  96. */
  97. rt_inline unsigned int rt_list_len(const rt_list_t *l)
  98. {
  99. unsigned int len = 0;
  100. const rt_list_t *p = l;
  101. while (p->next != l)
  102. {
  103. p = p->next;
  104. len ++;
  105. }
  106. return len;
  107. }
  108. /**
  109. * @brief get the struct for this entry
  110. * @param node the entry point
  111. * @param type the type of structure
  112. * @param member the name of list in structure
  113. */
  114. #define rt_list_entry(node, type, member) \
  115. ((type *)((char *)(node) - (unsigned long)(&((type *)0)->member)))
  116. /**
  117. * rt_list_for_each_entry - iterate over list of given type
  118. * @pos: the type * to use as a loop cursor.
  119. * @head: the head for your list.
  120. * @member: the name of the list_struct within the struct.
  121. */
  122. #define rt_list_for_each_entry(pos, head, member) \
  123. for (pos = rt_list_entry((head)->next, typeof(*pos), member); \
  124. &pos->member != (head); \
  125. pos = rt_list_entry(pos->member.next, typeof(*pos), member))
  126. /**
  127. * rt_list_first_entry - get the first element from a list
  128. * @ptr: the list head to take the element from.
  129. * @type: the type of the struct this is embedded in.
  130. * @member: the name of the list_struct within the struct.
  131. *
  132. * Note, that list is expected to be not empty.
  133. */
  134. #define rt_list_first_entry(ptr, type, member) \
  135. rt_list_entry((ptr)->next, type, member)
  136. #define RT_SLIST_OBJECT_INIT(object) { RT_NULL }
  137. /**
  138. * @brief initialize a single list
  139. *
  140. * @param l the single list to be initialized
  141. */
  142. rt_inline void rt_slist_init(rt_slist_t *l)
  143. {
  144. l->next = RT_NULL;
  145. }
  146. rt_inline void rt_slist_append(rt_slist_t *l, rt_slist_t *n)
  147. {
  148. struct rt_slist_node *node;
  149. node = l;
  150. while (node->next) node = node->next;
  151. /* append the node to the tail */
  152. node->next = n;
  153. n->next = RT_NULL;
  154. }
  155. rt_inline void rt_slist_insert(rt_slist_t *l, rt_slist_t *n)
  156. {
  157. n->next = l->next;
  158. l->next = n;
  159. }
  160. rt_inline rt_slist_t *rt_slist_remove(rt_slist_t *l, rt_slist_t *n)
  161. {
  162. /* remove slist head */
  163. struct rt_slist_node *node = l;
  164. while (node->next && node->next != n) node = node->next;
  165. /* remove node */
  166. if (node->next != (rt_slist_t *)0) node->next = node->next->next;
  167. return l;
  168. }
  169. rt_inline unsigned int rt_slist_len(const rt_slist_t *l)
  170. {
  171. unsigned int len = 0;
  172. const rt_slist_t *list = l->next;
  173. while (list != RT_NULL)
  174. {
  175. list = list->next;
  176. len ++;
  177. }
  178. return len;
  179. }
  180. /**
  181. * @brief get the struct for this single list node
  182. * @param node the entry point
  183. * @param type the type of structure
  184. * @param member the name of list in structure
  185. */
  186. #define rt_slist_entry(node, type, member) \
  187. ((type *)((char*)(node)-(unsigned long)(&((type *)0)->member)))
  188. /**
  189. * rt_slist_for_each_entry - iterate over single list of given type
  190. * @node: the type * to use as a loop cursor.
  191. * @list: the head for your single list.
  192. */
  193. #define rt_slist_foreach(node, list) \
  194. for ((node) = (list)->next; (node) != RT_NULL; (node) = (node)->next)
  195. /**
  196. * rt_container_of - return the member address of ptr, if the type of ptr is the
  197. * struct type.
  198. */
  199. #define rt_container_of(ptr, type, member) \
  200. ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))
  201. /*@}*/
  202. #ifdef __cplusplus
  203. }
  204. #endif
  205. #endif