TuyaOS
tuya_slist.h
浏览该文件的文档.
1
11#ifndef __TUYA_SLIST_H__
12#define __TUYA_SLIST_H__
13
14#include "tuya_cloud_types.h"
15
16#ifdef __cplusplus
17extern "C" {
18#endif
19
24typedef struct slist_head {
25 struct slist_head *next;
27
32#define SLIST_HEAD(name) \
33SLIST_HEAD name = {NULL}
34
39#define INIT_SLIST_HEAD(ptr) do { \
40 (ptr)->next = NULL; \
41} while (0)
42
47#define NEW_SLIST_NODE(type,node) \
48{\
49 node = (type *)Malloc(sizeof(type));\
50}
51
56#define SLIST_ENTRY(ptr, type, member) CNTR_OF(ptr,type,member)
57
62#define SLIST_FOR_EACH_ENTRY(tpos, type, pos, list, member) \
63 for (pos = (list)->next; \
64 pos && (tpos = SLIST_ENTRY(pos, type, member), 1); \
65 pos = pos->next)
66
71#define SLIST_FOR_EACH_ENTRY_SAFE(tpos, type, pos, n, list, member) \
72 for (pos = (list)->next; \
73 pos && (n = pos->next, 1) && \
74 (tpos = SLIST_ENTRY(pos, type, member), 1); \
75 pos = n)
76
81#define SLIST_FOR_EACH(pos, list) \
82 for (pos = (list)->next; pos ; \
83 pos = pos->next)
84
89#define SLIST_FOR_EACH_SAFE(pos, n, list) \
90 for (pos = (list)->next; pos && ({ n = pos->next; 1; }); \
91 pos = n)
92
97#define FREE_SLIST_SAFE(tpos, type, pos, n, list, member) \
98{\
99 type *posnode; \
100 SLIST_FOR_EACH_ENTRY_SAFE(tpos, type, pos, n, list, member) { \
101 (list)->next = n; \
102 posnode = tpos; \
103 Free(posnode); \
104 } \
105}
106
113STATIC INLINE VOID tuya_init_slist_node(INOUT SLIST_HEAD *node)
114{
115 node->next = NULL;
116}
117
124STATIC INLINE INT_T tuya_slist_empty(IN CONST SLIST_HEAD *list)
125{
126 return !(list->next);
127}
128
136STATIC INLINE VOID tuya_slist_del(INOUT SLIST_HEAD *list,INOUT SLIST_HEAD *node)
137{
138 SLIST_HEAD *pos = NULL;
139 SLIST_HEAD *last = list;
140
141 SLIST_FOR_EACH(pos, list) {
142 if(pos != node) {
143 last = pos;
144 continue;
145 }
146
147 // success find and delete
148 last->next = pos->next;
149 node->next = NULL;
150 break;
151 }
152}
153
161STATIC INLINE VOID tuya_slist_add_head(INOUT SLIST_HEAD *list,INOUT SLIST_HEAD *n)
162{
163 n->next = list->next;
164 list->next = n;
165}
166
174STATIC INLINE VOID tuya_slist_add_tail(INOUT SLIST_HEAD *list,INOUT SLIST_HEAD *n)
175{
176 SLIST_HEAD *pos = NULL;
177 SLIST_HEAD *last = list;
178
179 SLIST_FOR_EACH(pos, list) {
180 last = pos;
181 }
182 last->next = n;
183}
184
185#ifdef __cplusplus
186}
187#endif /* __cplusplus */
188
189#endif
sigle list head
Definition: tuya_slist.h:24
#define SLIST_HEAD(name)
define a sigle list head and initialize to empty
Definition: tuya_slist.h:32
#define SLIST_FOR_EACH(pos, list)
traverse each node of the sigle list, cannot change the sigle list
Definition: tuya_slist.h:81