dlist.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * File : dlist.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2006-03-16 Bernard the first version
  13. * 2006-09-07 Bernard move the kservice APIs to rtthread.h
  14. * 2007-06-27 Bernard fix the rt_list_remove bug
  15. * 2012-02-25 Grissiom move to rtgui/include/rtgui and some misc changes
  16. */
  17. #ifndef __RTGUI_DLIST_H__
  18. #define __RTGUI_DLIST_H__
  19. /* This file is copied from kservice.h in RTT kernel. There are some differences:
  20. * 1, naming. Use rtgui_dlist_ prefix instead of rt_list.
  21. * 2, add rtgui_dlist_foreach for convenience.
  22. * 3, move the definition of list node into this file.
  23. *
  24. * Please keep both of the files synchronized when fixing bugs.
  25. */
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif
  29. struct rtgui_dlist_node
  30. {
  31. struct rtgui_dlist_node *next; /* point to next node. */
  32. struct rtgui_dlist_node *prev; /* point to prev node. */
  33. };
  34. /**
  35. * @brief initialize a list
  36. *
  37. * @param l list to be initialized
  38. */
  39. rt_inline void rtgui_dlist_init(struct rtgui_dlist_node *l)
  40. {
  41. l->next = l->prev = l;
  42. }
  43. /**
  44. * @brief insert a node after a list
  45. *
  46. * @param l list to insert it
  47. * @param n new node to be inserted
  48. */
  49. rt_inline void rtgui_dlist_insert_after(struct rtgui_dlist_node *l, struct rtgui_dlist_node *n)
  50. {
  51. l->next->prev = n;
  52. n->next = l->next;
  53. l->next = n;
  54. n->prev = l;
  55. }
  56. /**
  57. * @brief insert a node before a list
  58. *
  59. * @param n new node to be inserted
  60. * @param l list to insert it
  61. */
  62. rt_inline void rtgui_dlist_insert_before(struct rtgui_dlist_node *l, struct rtgui_dlist_node *n)
  63. {
  64. l->prev->next = n;
  65. n->prev = l->prev;
  66. l->prev = n;
  67. n->next = l;
  68. }
  69. /**
  70. * @brief remove node from list.
  71. * @param n the node to remove from the list.
  72. */
  73. rt_inline void rtgui_dlist_remove(struct rtgui_dlist_node *n)
  74. {
  75. n->next->prev = n->prev;
  76. n->prev->next = n->next;
  77. rtgui_dlist_init(n);
  78. }
  79. /**
  80. * @brief tests whether a list is empty
  81. * @param l the list to test.
  82. */
  83. rt_inline int rtgui_dlist_isempty(const struct rtgui_dlist_node *l)
  84. {
  85. return l->next == l;
  86. }
  87. /**
  88. * @brief get the struct for this entry
  89. * @param node the entry point
  90. * @param type the type of structure
  91. * @param member the name of list in structure
  92. */
  93. #define rtgui_dlist_entry(node, type, member) \
  94. ((type *)((char *)(node) - (unsigned long)(&((type *)0)->member)))
  95. /* the direction can only be next or prev. If you want to iterate the list in
  96. * normal order, use next. If you want to iterate the list with reverse order,
  97. * use prev.*/
  98. #define rtgui_dlist_foreach(node, list, direction) \
  99. for ((node) = (list)->direction; (node) != list; (node) = (node)->direction)
  100. #ifdef __cplusplus
  101. }
  102. #endif
  103. #endif