rtservice.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. * 2017-11-15 JasonJia Modify rt_slist_foreach to rt_slist_for_each_entry.
  27. * Make code cleanup.
  28. */
  29. #ifndef __RT_SERVICE_H__
  30. #define __RT_SERVICE_H__
  31. #ifdef __cplusplus
  32. extern "C" {
  33. #endif
  34. /**
  35. * @addtogroup KernelService
  36. */
  37. /*@{*/
  38. /**
  39. * rt_container_of - return the member address of ptr, if the type of ptr is the
  40. * struct type.
  41. */
  42. #define rt_container_of(ptr, type, member) \
  43. ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))
  44. /**
  45. * @brief initialize a list object
  46. */
  47. #define RT_LIST_OBJECT_INIT(object) { &(object), &(object) }
  48. /**
  49. * @brief initialize a list
  50. *
  51. * @param l list to be initialized
  52. */
  53. rt_inline void rt_list_init(rt_list_t *l)
  54. {
  55. l->next = l->prev = l;
  56. }
  57. /**
  58. * @brief insert a node after a list
  59. *
  60. * @param l list to insert it
  61. * @param n new node to be inserted
  62. */
  63. rt_inline void rt_list_insert_after(rt_list_t *l, rt_list_t *n)
  64. {
  65. l->next->prev = n;
  66. n->next = l->next;
  67. l->next = n;
  68. n->prev = l;
  69. }
  70. /**
  71. * @brief insert a node before a list
  72. *
  73. * @param n new node to be inserted
  74. * @param l list to insert it
  75. */
  76. rt_inline void rt_list_insert_before(rt_list_t *l, rt_list_t *n)
  77. {
  78. l->prev->next = n;
  79. n->prev = l->prev;
  80. l->prev = n;
  81. n->next = l;
  82. }
  83. /**
  84. * @brief remove node from list.
  85. * @param n the node to remove from the list.
  86. */
  87. rt_inline void rt_list_remove(rt_list_t *n)
  88. {
  89. n->next->prev = n->prev;
  90. n->prev->next = n->next;
  91. n->next = n->prev = n;
  92. }
  93. /**
  94. * @brief tests whether a list is empty
  95. * @param l the list to test.
  96. */
  97. rt_inline int rt_list_isempty(const rt_list_t *l)
  98. {
  99. return l->next == l;
  100. }
  101. /**
  102. * @brief get the list length
  103. * @param l the list to get.
  104. */
  105. rt_inline unsigned int rt_list_len(const rt_list_t *l)
  106. {
  107. unsigned int len = 0;
  108. const rt_list_t *p = l;
  109. while (p->next != l)
  110. {
  111. p = p->next;
  112. len ++;
  113. }
  114. return len;
  115. }
  116. /**
  117. * @brief get the struct for this entry
  118. * @param node the entry point
  119. * @param type the type of structure
  120. * @param member the name of list in structure
  121. */
  122. #define rt_list_entry(node, type, member) \
  123. rt_container_of(node, type, member)
  124. /**
  125. * rt_list_for_each_entry - iterate over list of given type
  126. * @pos: the type * to use as a loop cursor.
  127. * @head: the head for your list.
  128. * @member: the name of the list_struct within the struct.
  129. */
  130. #define rt_list_for_each_entry(pos, head, member) \
  131. for (pos = rt_list_entry((head)->next, typeof(*pos), member); \
  132. &pos->member != (head); \
  133. pos = rt_list_entry(pos->member.next, typeof(*pos), member))
  134. /**
  135. * rt_list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  136. * @pos: the type * to use as a loop cursor.
  137. * @n: another type * to use as temporary storage
  138. * @head: the head for your list.
  139. * @member: the name of the list_struct within the struct.
  140. */
  141. #define rt_list_for_each_entry_safe(pos, n, head, member) \
  142. for (pos = rt_list_entry((head)->next, typeof(*pos), member), \
  143. n = rt_list_entry(pos->member.next, typeof(*pos), member); \
  144. &pos->member != (head); \
  145. pos = n, n = rt_list_entry(n->member.next, typeof(*n), member))
  146. /**
  147. * rt_list_first_entry - get the first element from a list
  148. * @ptr: the list head to take the element from.
  149. * @type: the type of the struct this is embedded in.
  150. * @member: the name of the list_struct within the struct.
  151. *
  152. * Note, that list is expected to be not empty.
  153. */
  154. #define rt_list_first_entry(ptr, type, member) \
  155. rt_list_entry((ptr)->next, type, member)
  156. #define RT_SLIST_OBJECT_INIT(object) { RT_NULL }
  157. /**
  158. * @brief initialize a single list
  159. *
  160. * @param l the single list to be initialized
  161. */
  162. rt_inline void rt_slist_init(rt_slist_t *l)
  163. {
  164. l->next = RT_NULL;
  165. }
  166. rt_inline void rt_slist_append(rt_slist_t *l, rt_slist_t *n)
  167. {
  168. struct rt_slist_node *node;
  169. node = l;
  170. while (node->next) node = node->next;
  171. /* append the node to the tail */
  172. node->next = n;
  173. n->next = RT_NULL;
  174. }
  175. rt_inline void rt_slist_insert(rt_slist_t *l, rt_slist_t *n)
  176. {
  177. n->next = l->next;
  178. l->next = n;
  179. }
  180. rt_inline unsigned int rt_slist_len(const rt_slist_t *l)
  181. {
  182. unsigned int len = 0;
  183. const rt_slist_t *list = l->next;
  184. while (list != RT_NULL)
  185. {
  186. list = list->next;
  187. len ++;
  188. }
  189. return len;
  190. }
  191. rt_inline rt_slist_t *rt_slist_remove(rt_slist_t *l, rt_slist_t *n)
  192. {
  193. /* remove slist head */
  194. struct rt_slist_node *node = l;
  195. while (node->next && node->next != n) node = node->next;
  196. /* remove node */
  197. if (node->next != (rt_slist_t *)0) node->next = node->next->next;
  198. return l;
  199. }
  200. rt_inline int rt_slist_isempty(rt_slist_t *l)
  201. {
  202. return l->next == RT_NULL;
  203. }
  204. /**
  205. * @brief get the struct for this single list node
  206. * @param node the entry point
  207. * @param type the type of structure
  208. * @param member the name of list in structure
  209. */
  210. #define rt_slist_entry(node, type, member) \
  211. rt_container_of(node, type, member)
  212. /**
  213. * rt_slist_for_each_entry - iterate over single list of given type
  214. * @pos: the type * to use as a loop cursor.
  215. * @head: the head for your single list.
  216. * @member: the name of the list_struct within the struct.
  217. */
  218. #define rt_slist_for_each_entry(pos, head, member) \
  219. for (pos = rt_slist_entry((head)->next, typeof(*pos), member); \
  220. &pos->member != (RT_NULL); \
  221. pos = rt_slist_entry(pos->member.next, typeof(*pos), member))
  222. /**
  223. * rt_slist_first_entry - get the first element from a slist
  224. * @ptr: the slist head to take the element from.
  225. * @type: the type of the struct this is embedded in.
  226. * @member: the name of the slist_struct within the struct.
  227. *
  228. * Note, that slist is expected to be not empty.
  229. */
  230. #define rt_slist_first_entry(ptr, type, member) \
  231. rt_slist_entry((ptr)->next, type, member)
  232. /*@}*/
  233. #ifdef __cplusplus
  234. }
  235. #endif
  236. #endif