link_list.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*----------------------------------------------------------------------------
  2. * Copyright (c) <2018>, <Huawei Technologies Co., Ltd>
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without modification,
  5. * are permitted provided that the following conditions are met:
  6. * 1. Redistributions of source code must retain the above copyright notice, this list of
  7. * conditions and the following disclaimer.
  8. * 2. Redistributions in binary form must reproduce the above copyright notice, this list
  9. * of conditions and the following disclaimer in the documentation and/or other materials
  10. * provided with the distribution.
  11. * 3. Neither the name of the copyright holder nor the names of its contributors may be used
  12. * to endorse or promote products derived from this software without specific prior written
  13. * permission.
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  16. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  18. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  21. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  22. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  23. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  24. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *---------------------------------------------------------------------------*/
  26. /*----------------------------------------------------------------------------
  27. * Notice of Export Control Law
  28. * ===============================================
  29. * Huawei LiteOS may be subject to applicable export control laws and regulations, which might
  30. * include those applicable to Huawei LiteOS of U.S. and the country in which you are located.
  31. * Import, export and usage of Huawei LiteOS in any manner by you shall be in compliance with such
  32. * applicable export control laws and regulations.
  33. *---------------------------------------------------------------------------*/
  34. /**
  35. * DATE AUTHOR INSTRUCTION
  36. * 2020-04-22 17:08 zhangqianfu The first version
  37. *
  38. */
  39. #ifndef LITEOS_LAB_IOT_LINK_INC_LINK_LIST_H_
  40. #define LITEOS_LAB_IOT_LINK_INC_LINK_LIST_H_
  41. #include <stdint.h>
  42. #include <stddef.h>
  43. ///< this is common double-list , and you could implement the one yourself
  44. /*
  45. * you could add the node to the data entry like
  46. *
  47. * struct {
  48. *
  49. * link_node_t node;
  50. *
  51. * xxx value;
  52. *
  53. * }
  54. *
  55. *
  56. * */
  57. typedef struct link_node
  58. {
  59. struct link_node *nxt, *pre;
  60. }link_node_t;
  61. #define LINK_NODE_ENTRY(node, type, member) \
  62. ((type *)((char *)(node) - (size_t)&((type *)0)->member))
  63. typedef void (*fn_link_list_iterate)(link_node_t *node, void *args);
  64. typedef struct
  65. {
  66. link_node_t root;
  67. }link_list_t;
  68. static inline void link_list_init(link_list_t *list)
  69. {
  70. list->root.nxt = &list->root;
  71. list->root.pre = &list->root;
  72. }
  73. static inline int link_list_isempty(link_list_t *list)
  74. {
  75. return list->root.pre == &list->root?1:0;
  76. }
  77. static inline int link_list_ishead(link_list_t *list, link_node_t *node)
  78. {
  79. return list->root.nxt == node?1:0;
  80. }
  81. static inline int link_list_istail(link_list_t *list, link_node_t *node)
  82. {
  83. return list->root.pre == node?1:0;
  84. }
  85. ///< insert the new_node before the old_node
  86. static inline void link_list_insert(link_node_t *old_node, link_node_t *new_node)
  87. {
  88. new_node->pre = old_node->pre;
  89. new_node->nxt = old_node;
  90. new_node->pre->nxt = new_node;
  91. old_node->pre = new_node;
  92. }
  93. static inline void link_list_addhead(link_list_t *list, link_node_t *node)
  94. {
  95. link_list_insert(list->root.nxt,node);
  96. }
  97. static inline void link_list_addtail(link_list_t *list, link_node_t *node)
  98. {
  99. link_list_insert(&list->root,node);
  100. }
  101. ///< node could not bee root
  102. static inline void link_list_removenode( link_node_t *node)
  103. {
  104. node->pre->nxt = node->nxt;
  105. node->nxt->pre = node->pre;
  106. }
  107. static inline link_node_t *link_list_removehead(link_list_t *list)
  108. {
  109. link_node_t *node = list->root.nxt;
  110. link_list_removenode(node);
  111. return node;
  112. }
  113. static inline link_node_t *link_list_removetail(link_list_t *list)
  114. {
  115. link_node_t *node = list->root.pre;
  116. link_list_removenode(node);
  117. return node;
  118. }
  119. static inline void link_list_replace(link_node_t *new_node, link_node_t *old_node)
  120. {
  121. new_node->nxt = old_node->nxt;
  122. new_node->nxt->pre = new_node;
  123. new_node->pre = old_node->pre;
  124. new_node->pre->nxt = new_node;
  125. }
  126. #define LINK_LIST_FOREACH(node, list) \
  127. for(node = (list)->root.nxt; \
  128. node != &(list)->root; \
  129. node = (node)->nxt )
  130. #define LINK_LIST_FOREACH_SAFE(node, tmp,list) \
  131. for(node = (list)->root.nxt, tmp = (node)->nxt; \
  132. node != &(list)->root; \
  133. node = tmp,tmp = (node)->nxt )
  134. static inline void link_list_iterate(link_list_t *list, fn_link_list_iterate fn_iterate, void *args)
  135. {
  136. link_node_t *node;
  137. LINK_LIST_FOREACH(node, list)
  138. {
  139. fn_iterate(node, args);
  140. }
  141. }
  142. static inline void link_list_iteratesafe(link_list_t *list, fn_link_list_iterate fn_iterate, void *args)
  143. {
  144. link_node_t *node;
  145. link_node_t *tmp;
  146. LINK_LIST_FOREACH_SAFE(node, tmp,list)
  147. {
  148. fn_iterate(node, args);
  149. }
  150. }
  151. #endif /* LITEOS_LAB_IOT_LINK_INC_LINK_LIST_H_ */