rtservice.h 6.3 KB

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