rtservice.h 7.7 KB

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