rtservice.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2006-03-16 Bernard the first version
  9. * 2006-09-07 Bernard move the kservice APIs to rtthread.h
  10. * 2007-06-27 Bernard fix the rt_list_remove bug
  11. * 2012-03-22 Bernard rename kservice.h to rtservice.h
  12. * 2017-11-15 JasonJia Modify rt_slist_foreach to rt_slist_for_each_entry.
  13. * Make code cleanup.
  14. */
  15. #ifndef __RT_SERVICE_H__
  16. #define __RT_SERVICE_H__
  17. #include <rtdef.h>
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. /**
  22. * @addtogroup KernelService
  23. */
  24. /**@{*/
  25. /**
  26. * rt_container_of - return the start address of struct type, while ptr is the
  27. * member of struct type.
  28. */
  29. #define rt_container_of(ptr, type, member) \
  30. ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))
  31. /**
  32. * @brief initialize a list object
  33. */
  34. #define RT_LIST_OBJECT_INIT(object) { &(object), &(object) }
  35. /**
  36. * @brief initialize a list
  37. *
  38. * @param l list to be initialized
  39. */
  40. rt_inline void rt_list_init(rt_list_t *l)
  41. {
  42. l->next = l->prev = l;
  43. }
  44. /**
  45. * @brief insert a node after a list
  46. *
  47. * @param l list to insert it
  48. * @param n new node to be inserted
  49. */
  50. rt_inline void rt_list_insert_after(rt_list_t *l, rt_list_t *n)
  51. {
  52. l->next->prev = n;
  53. n->next = l->next;
  54. l->next = n;
  55. n->prev = l;
  56. }
  57. /**
  58. * @brief insert a node before a list
  59. *
  60. * @param n new node to be inserted
  61. * @param l list to insert it
  62. */
  63. rt_inline void rt_list_insert_before(rt_list_t *l, rt_list_t *n)
  64. {
  65. l->prev->next = n;
  66. n->prev = l->prev;
  67. l->prev = n;
  68. n->next = l;
  69. }
  70. /**
  71. * @brief remove node from list.
  72. * @param n the node to remove from the list.
  73. */
  74. rt_inline void rt_list_remove(rt_list_t *n)
  75. {
  76. n->next->prev = n->prev;
  77. n->prev->next = n->next;
  78. n->next = n->prev = n;
  79. }
  80. /**
  81. * @brief tests whether a list is empty
  82. * @param l the list to test.
  83. */
  84. rt_inline int rt_list_isempty(const rt_list_t *l)
  85. {
  86. return l->next == l;
  87. }
  88. /**
  89. * @brief get the list length
  90. * @param l the list to get.
  91. */
  92. rt_inline unsigned int rt_list_len(const rt_list_t *l)
  93. {
  94. unsigned int len = 0;
  95. const rt_list_t *p = l;
  96. while (p->next != l)
  97. {
  98. p = p->next;
  99. len ++;
  100. }
  101. return len;
  102. }
  103. /**
  104. * @brief get the struct for this entry
  105. * @param node the entry point
  106. * @param type the type of structure
  107. * @param member the name of list in structure
  108. */
  109. #define rt_list_entry(node, type, member) \
  110. rt_container_of(node, type, member)
  111. /**
  112. * rt_list_for_each - iterate over a list
  113. * @param pos the rt_list_t * to use as a loop cursor.
  114. * @param head the head for your list.
  115. */
  116. #define rt_list_for_each(pos, head) \
  117. for (pos = (head)->next; pos != (head); pos = pos->next)
  118. /**
  119. * rt_list_for_each_safe - iterate over a list safe against removal of list entry
  120. * @param pos the rt_list_t * to use as a loop cursor.
  121. * @param n another rt_list_t * to use as temporary storage
  122. * @param head the head for your list.
  123. */
  124. #define rt_list_for_each_safe(pos, n, head) \
  125. for (pos = (head)->next, n = pos->next; pos != (head); \
  126. pos = n, n = pos->next)
  127. /**
  128. * rt_list_for_each_entry - iterate over list of given type
  129. * @param pos the type * to use as a loop cursor.
  130. * @param head the head for your list.
  131. * @param member the name of the list_struct within the struct.
  132. */
  133. #define rt_list_for_each_entry(pos, head, member) \
  134. for (pos = rt_list_entry((head)->next, rt_typeof(*pos), member); \
  135. &pos->member != (head); \
  136. pos = rt_list_entry(pos->member.next, rt_typeof(*pos), member))
  137. /**
  138. * rt_list_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  139. * @param pos the type * to use as a loop cursor.
  140. * @param n another type * to use as temporary storage
  141. * @param head the head for your list.
  142. * @param member the name of the list_struct within the struct.
  143. */
  144. #define rt_list_for_each_entry_safe(pos, n, head, member) \
  145. for (pos = rt_list_entry((head)->next, rt_typeof(*pos), member), \
  146. n = rt_list_entry(pos->member.next, rt_typeof(*pos), member); \
  147. &pos->member != (head); \
  148. pos = n, n = rt_list_entry(n->member.next, rt_typeof(*n), member))
  149. /**
  150. * rt_list_first_entry - get the first element from a list
  151. * @param ptr the list head to take the element from.
  152. * @param type the type of the struct this is embedded in.
  153. * @param member the name of the list_struct within the struct.
  154. *
  155. * Note, that list is expected to be not empty.
  156. */
  157. #define rt_list_first_entry(ptr, type, member) \
  158. rt_list_entry((ptr)->next, type, member)
  159. #define RT_SLIST_OBJECT_INIT(object) { RT_NULL }
  160. /**
  161. * @brief initialize a single list
  162. *
  163. * @param l the single list to be initialized
  164. */
  165. rt_inline void rt_slist_init(rt_slist_t *l)
  166. {
  167. l->next = RT_NULL;
  168. }
  169. rt_inline void rt_slist_append(rt_slist_t *l, rt_slist_t *n)
  170. {
  171. struct rt_slist_node *node;
  172. node = l;
  173. while (node->next) node = node->next;
  174. /* append the node to the tail */
  175. node->next = n;
  176. n->next = RT_NULL;
  177. }
  178. rt_inline void rt_slist_insert(rt_slist_t *l, rt_slist_t *n)
  179. {
  180. n->next = l->next;
  181. l->next = n;
  182. }
  183. rt_inline unsigned int rt_slist_len(const rt_slist_t *l)
  184. {
  185. unsigned int len = 0;
  186. const rt_slist_t *list = l->next;
  187. while (list != RT_NULL)
  188. {
  189. list = list->next;
  190. len ++;
  191. }
  192. return len;
  193. }
  194. rt_inline rt_slist_t *rt_slist_remove(rt_slist_t *l, rt_slist_t *n)
  195. {
  196. /* remove slist head */
  197. struct rt_slist_node *node = l;
  198. while (node->next && node->next != n) node = node->next;
  199. /* remove node */
  200. if (node->next != (rt_slist_t *)0) node->next = node->next->next;
  201. return l;
  202. }
  203. rt_inline rt_slist_t *rt_slist_first(rt_slist_t *l)
  204. {
  205. return l->next;
  206. }
  207. rt_inline rt_slist_t *rt_slist_tail(rt_slist_t *l)
  208. {
  209. while (l->next) l = l->next;
  210. return l;
  211. }
  212. rt_inline rt_slist_t *rt_slist_next(rt_slist_t *n)
  213. {
  214. return n->next;
  215. }
  216. rt_inline int rt_slist_isempty(rt_slist_t *l)
  217. {
  218. return l->next == RT_NULL;
  219. }
  220. /**
  221. * @brief get the struct for this single list node
  222. * @param node the entry point
  223. * @param type the type of structure
  224. * @param member the name of list in structure
  225. */
  226. #define rt_slist_entry(node, type, member) \
  227. rt_container_of(node, type, member)
  228. /**
  229. * rt_slist_for_each - iterate over a single list
  230. * @param pos the rt_slist_t * to use as a loop cursor.
  231. * @param head the head for your single list.
  232. */
  233. #define rt_slist_for_each(pos, head) \
  234. for (pos = (head)->next; pos != RT_NULL; pos = pos->next)
  235. /**
  236. * rt_slist_for_each_entry - iterate over single list of given type
  237. * @param pos the type * to use as a loop cursor.
  238. * @param head the head for your single list.
  239. * @param member the name of the list_struct within the struct.
  240. */
  241. #define rt_slist_for_each_entry(pos, head, member) \
  242. for (pos = ((head)->next == (RT_NULL) ? (RT_NULL) : rt_slist_entry((head)->next, rt_typeof(*pos), member)); \
  243. pos != (RT_NULL) && &pos->member != (RT_NULL); \
  244. pos = (pos->member.next == (RT_NULL) ? (RT_NULL) : rt_slist_entry(pos->member.next, rt_typeof(*pos), member)))
  245. /**
  246. * rt_slist_first_entry - get the first element from a slist
  247. * @param ptr the slist head to take the element from.
  248. * @param type the type of the struct this is embedded in.
  249. * @param member the name of the slist_struct within the struct.
  250. *
  251. * Note, that slist is expected to be not empty.
  252. */
  253. #define rt_slist_first_entry(ptr, type, member) \
  254. rt_slist_entry((ptr)->next, type, member)
  255. /**
  256. * rt_slist_tail_entry - get the tail element from a slist
  257. * @param ptr the slist head to take the element from.
  258. * @param type the type of the struct this is embedded in.
  259. * @param member the name of the slist_struct within the struct.
  260. *
  261. * Note, that slist is expected to be not empty.
  262. */
  263. #define rt_slist_tail_entry(ptr, type, member) \
  264. rt_slist_entry(rt_slist_tail(ptr), type, member)
  265. /**@}*/
  266. #ifdef __cplusplus
  267. }
  268. #endif
  269. #endif