rtservice.h 8.2 KB

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