rtservice.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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 struct for this entry
  95. * @param node the entry point
  96. * @param type the type of structure
  97. * @param member the name of list in structure
  98. */
  99. #define rt_list_entry(node, type, member) \
  100. ((type *)((char *)(node) - (unsigned long)(&((type *)0)->member)))
  101. /**
  102. * rt_list_for_each_entry - iterate over list of given type
  103. * @pos: the type * to use as a loop cursor.
  104. * @head: the head for your list.
  105. * @member: the name of the list_struct within the struct.
  106. */
  107. #define rt_list_for_each_entry(pos, head, member) \
  108. for (pos = rt_list_entry((head)->next, typeof(*pos), member); \
  109. &pos->member != (head); \
  110. pos = rt_list_entry(pos->member.next, typeof(*pos), member))
  111. /**
  112. * rt_list_first_entry - get the first element from a list
  113. * @ptr: the list head to take the element from.
  114. * @type: the type of the struct this is embedded in.
  115. * @member: the name of the list_struct within the struct.
  116. *
  117. * Note, that list is expected to be not empty.
  118. */
  119. #define rt_list_first_entry(ptr, type, member) \
  120. rt_list_entry((ptr)->next, type, member)
  121. #define RT_SLIST_OBJECT_INIT(object) { RT_NULL }
  122. /**
  123. * @brief initialize a single list
  124. *
  125. * @param l the single list to be initialized
  126. */
  127. rt_inline void rt_slist_init(rt_slist_t *l)
  128. {
  129. l->next = RT_NULL;
  130. }
  131. rt_inline void rt_slist_append(rt_slist_t *l, rt_slist_t *n)
  132. {
  133. struct rt_slist_node *node;
  134. node = l;
  135. while (node->next) node = node->next;
  136. /* append the node to the tail */
  137. node->next = n;
  138. n->next = RT_NULL;
  139. }
  140. rt_inline void rt_slist_insert(rt_slist_t *l, rt_slist_t *n)
  141. {
  142. n->next = l->next;
  143. l->next = n;
  144. }
  145. rt_inline rt_slist_t *rt_slist_remove(rt_slist_t *l, rt_slist_t *n)
  146. {
  147. /* remove slist head */
  148. struct rt_slist_node *node = l;
  149. while (node->next && node->next != n) node = node->next;
  150. /* remove node */
  151. if (node->next != (rt_slist_t *)0) node->next = node->next->next;
  152. return l;
  153. }
  154. /**
  155. * @brief get the struct for this single list node
  156. * @param node the entry point
  157. * @param type the type of structure
  158. * @param member the name of list in structure
  159. */
  160. #define rt_slist_entry(node, type, member) \
  161. ((type *)((char*)(node)-(unsigned long)(&((type *)0)->member)))
  162. /**
  163. * rt_slist_for_each_entry - iterate over single list of given type
  164. * @node: the type * to use as a loop cursor.
  165. * @list: the head for your single list.
  166. */
  167. #define rt_slist_foreach(node, list) \
  168. for ((node) = (list)->next; (node) != RT_NULL; (node) = (node)->next)
  169. /**
  170. * rt_container_of - return the member address of ptr, if the type of ptr is the
  171. * struct type.
  172. */
  173. #define rt_container_of(ptr, type, member) \
  174. ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))
  175. /*@}*/
  176. #ifdef __cplusplus
  177. }
  178. #endif
  179. #endif