rtservice.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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 - iterate over a list
  126. * @pos: the rt_list_t * to use as a loop cursor.
  127. * @head: the head for your list.
  128. */
  129. #define rt_list_for_each(pos, head) \
  130. for (pos = (head)->next; pos != (head); pos = pos->next)
  131. /**
  132. * rt_list_for_each_safe - iterate over a list safe against removal of list entry
  133. * @pos: the rt_list_t * to use as a loop cursor.
  134. * @n: another rt_list_t * to use as temporary storage
  135. * @head: the head for your list.
  136. */
  137. #define rt_list_for_each_safe(pos, n, head) \
  138. for (pos = (head)->next, n = pos->next; pos != (head); \
  139. pos = n, n = pos->next)
  140. /**
  141. * rt_list_for_each_entry - iterate over list of given type
  142. * @pos: the type * to use as a loop cursor.
  143. * @head: the head for your list.
  144. * @member: the name of the list_struct within the struct.
  145. */
  146. #define rt_list_for_each_entry(pos, head, member) \
  147. for (pos = rt_list_entry((head)->next, typeof(*pos), member); \
  148. &pos->member != (head); \
  149. pos = rt_list_entry(pos->member.next, typeof(*pos), member))
  150. /**
  151. * rt_list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  152. * @pos: the type * to use as a loop cursor.
  153. * @n: another type * to use as temporary storage
  154. * @head: the head for your list.
  155. * @member: the name of the list_struct within the struct.
  156. */
  157. #define rt_list_for_each_entry_safe(pos, n, head, member) \
  158. for (pos = rt_list_entry((head)->next, typeof(*pos), member), \
  159. n = rt_list_entry(pos->member.next, typeof(*pos), member); \
  160. &pos->member != (head); \
  161. pos = n, n = rt_list_entry(n->member.next, typeof(*n), member))
  162. /**
  163. * rt_list_first_entry - get the first element from a list
  164. * @ptr: the list head to take the element from.
  165. * @type: the type of the struct this is embedded in.
  166. * @member: the name of the list_struct within the struct.
  167. *
  168. * Note, that list is expected to be not empty.
  169. */
  170. #define rt_list_first_entry(ptr, type, member) \
  171. rt_list_entry((ptr)->next, type, member)
  172. #define RT_SLIST_OBJECT_INIT(object) { RT_NULL }
  173. /**
  174. * @brief initialize a single list
  175. *
  176. * @param l the single list to be initialized
  177. */
  178. rt_inline void rt_slist_init(rt_slist_t *l)
  179. {
  180. l->next = RT_NULL;
  181. }
  182. rt_inline void rt_slist_append(rt_slist_t *l, rt_slist_t *n)
  183. {
  184. struct rt_slist_node *node;
  185. node = l;
  186. while (node->next) node = node->next;
  187. /* append the node to the tail */
  188. node->next = n;
  189. n->next = RT_NULL;
  190. }
  191. rt_inline void rt_slist_insert(rt_slist_t *l, rt_slist_t *n)
  192. {
  193. n->next = l->next;
  194. l->next = n;
  195. }
  196. rt_inline unsigned int rt_slist_len(const rt_slist_t *l)
  197. {
  198. unsigned int len = 0;
  199. const rt_slist_t *list = l->next;
  200. while (list != RT_NULL)
  201. {
  202. list = list->next;
  203. len ++;
  204. }
  205. return len;
  206. }
  207. rt_inline rt_slist_t *rt_slist_remove(rt_slist_t *l, rt_slist_t *n)
  208. {
  209. /* remove slist head */
  210. struct rt_slist_node *node = l;
  211. while (node->next && node->next != n) node = node->next;
  212. /* remove node */
  213. if (node->next != (rt_slist_t *)0) node->next = node->next->next;
  214. return l;
  215. }
  216. rt_inline rt_slist_t *rt_slist_first(rt_slist_t *l)
  217. {
  218. return l->next;
  219. }
  220. rt_inline rt_slist_t *rt_slist_next(rt_slist_t *n)
  221. {
  222. return n->next;
  223. }
  224. rt_inline int rt_slist_isempty(rt_slist_t *l)
  225. {
  226. return l->next == RT_NULL;
  227. }
  228. /**
  229. * @brief get the struct for this single list node
  230. * @param node the entry point
  231. * @param type the type of structure
  232. * @param member the name of list in structure
  233. */
  234. #define rt_slist_entry(node, type, member) \
  235. rt_container_of(node, type, member)
  236. /**
  237. * rt_slist_for_each - iterate over a single list
  238. * @pos: the rt_slist_t * to use as a loop cursor.
  239. * @head: the head for your single list.
  240. */
  241. #define rt_slist_for_each(pos, head) \
  242. for (pos = (head)->next; pos != RT_NULL; pos = pos->next)
  243. /**
  244. * rt_slist_for_each_entry - iterate over single list of given type
  245. * @pos: the type * to use as a loop cursor.
  246. * @head: the head for your single list.
  247. * @member: the name of the list_struct within the struct.
  248. */
  249. #define rt_slist_for_each_entry(pos, head, member) \
  250. for (pos = rt_slist_entry((head)->next, typeof(*pos), member); \
  251. &pos->member != (RT_NULL); \
  252. pos = rt_slist_entry(pos->member.next, typeof(*pos), member))
  253. /**
  254. * rt_slist_first_entry - get the first element from a slist
  255. * @ptr: the slist head to take the element from.
  256. * @type: the type of the struct this is embedded in.
  257. * @member: the name of the slist_struct within the struct.
  258. *
  259. * Note, that slist is expected to be not empty.
  260. */
  261. #define rt_slist_first_entry(ptr, type, member) \
  262. rt_slist_entry((ptr)->next, type, member)
  263. /*@}*/
  264. #ifdef __cplusplus
  265. }
  266. #endif
  267. #endif