libwebsockets
Lightweight C library for HTML5 websockets
lws-dll2.h
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
31 
44 #define lws_start_foreach_ll(type, it, start)\
45 { \
46  type it = start; \
47  while (it) {
48 
59 #define lws_end_foreach_ll(it, nxt) \
60  it = it->nxt; \
61  } \
62 }
63 
79 #define lws_start_foreach_ll_safe(type, it, start, nxt)\
80 { \
81  type it = start; \
82  while (it) { \
83  type next_##it = it->nxt;
84 
95 #define lws_end_foreach_ll_safe(it) \
96  it = next_##it; \
97  } \
98 }
99 
116 #define lws_start_foreach_llp(type, it, start)\
117 { \
118  type it = &(start); \
119  while (*(it)) {
120 
121 #define lws_start_foreach_llp_safe(type, it, start, nxt)\
122 { \
123  type it = &(start); \
124  type next; \
125  while (*(it)) { \
126  next = &((*(it))->nxt); \
127 
138 #define lws_end_foreach_llp(it, nxt) \
139  it = &(*(it))->nxt; \
140  } \
141 }
142 
143 #define lws_end_foreach_llp_safe(it) \
144  it = next; \
145  } \
146 }
147 
148 #define lws_ll_fwd_insert(\
149  ___new_object, /* pointer to new object */ \
150  ___m_list, /* member for next list object ptr */ \
151  ___list_head /* list head */ \
152  ) {\
153  ___new_object->___m_list = ___list_head; \
154  ___list_head = ___new_object; \
155  }
156 
157 #define lws_ll_fwd_remove(\
158  ___type, /* type of listed object */ \
159  ___m_list, /* member for next list object ptr */ \
160  ___target, /* object to remove from list */ \
161  ___list_head /* list head */ \
162  ) { \
163  lws_start_foreach_llp(___type **, ___ppss, ___list_head) { \
164  if (*___ppss == ___target) { \
165  *___ppss = ___target->___m_list; \
166  break; \
167  } \
168  } lws_end_foreach_llp(___ppss, ___m_list); \
169  }
170 
171 
172 /*
173  * doubly linked-list
174  */
175 
176 /*
177  * lws_dll2_owner / lws_dll2 : more capable version of lws_dll. Differences:
178  *
179  * - there's an explicit lws_dll2_owner struct which holds head, tail and
180  * count of members.
181  *
182  * - list members all hold a pointer to their owner. So user code does not
183  * have to track anything about exactly what lws_dll2_owner list the object
184  * is a member of.
185  *
186  * - you can use lws_dll unless you want the member count or the ability to
187  * not track exactly which list it's on.
188  *
189  * - layout is compatible with lws_dll (but lws_dll apis will not update the
190  * new stuff)
191  */
192 
193 
194 struct lws_dll2;
195 struct lws_dll2_owner;
196 
197 typedef struct lws_dll2 {
198  struct lws_dll2 *prev;
199  struct lws_dll2 *next;
200  struct lws_dll2_owner *owner;
201 } lws_dll2_t;
202 
203 typedef struct lws_dll2_owner {
204  struct lws_dll2 *tail;
205  struct lws_dll2 *head;
206 
207  uint32_t count;
209 
210 static LWS_INLINE int
211 lws_dll2_is_detached(const struct lws_dll2 *d) { return !d->owner; }
212 
213 static LWS_INLINE const struct lws_dll2_owner *
214 lws_dll2_owner(const struct lws_dll2 *d) { return d->owner; }
215 
216 static LWS_INLINE struct lws_dll2 *
217 lws_dll2_get_head(struct lws_dll2_owner *owner) { return owner->head; }
218 
219 static LWS_INLINE struct lws_dll2 *
220 lws_dll2_get_tail(struct lws_dll2_owner *owner) { return owner->tail; }
221 
222 LWS_VISIBLE LWS_EXTERN void
223 lws_dll2_add_head(struct lws_dll2 *d, struct lws_dll2_owner *owner);
224 
225 LWS_VISIBLE LWS_EXTERN void
226 lws_dll2_add_tail(struct lws_dll2 *d, struct lws_dll2_owner *owner);
227 
228 LWS_VISIBLE LWS_EXTERN void
229 lws_dll2_remove(struct lws_dll2 *d);
230 
231 LWS_VISIBLE LWS_EXTERN int
232 lws_dll2_foreach_safe(struct lws_dll2_owner *owner, void *user,
233  int (*cb)(struct lws_dll2 *d, void *user));
234 
235 LWS_VISIBLE LWS_EXTERN void
236 lws_dll2_clear(struct lws_dll2 *d);
237 
238 LWS_VISIBLE LWS_EXTERN void
239 lws_dll2_owner_clear(struct lws_dll2_owner *d);
240 
241 LWS_VISIBLE LWS_EXTERN void
242 lws_dll2_add_before(struct lws_dll2 *d, struct lws_dll2 *after);
243 
244 LWS_VISIBLE LWS_EXTERN void
245 lws_dll2_add_sorted(lws_dll2_t *d, lws_dll2_owner_t *own,
246  int (*compare)(const lws_dll2_t *d, const lws_dll2_t *i));
247 
248 LWS_VISIBLE LWS_EXTERN void *
249 _lws_dll2_search_sz_pl(lws_dll2_owner_t *own, const char *name, size_t namelen,
250  size_t dll2_ofs, size_t ptr_ofs);
251 
252 /*
253  * Searches objects in an owner list linearly and returns one with a given
254  * member C-string matching a supplied length-provided string if it exists, else
255  * NULL.
256  */
257 
258 #define lws_dll2_search_sz_pl(own, name, namelen, type, membd2list, membptr) \
259  ((type *)_lws_dll2_search_sz_pl(own, name, namelen, \
260  offsetof(type, membd2list), \
261  offsetof(type, membptr)))
262 
263 #if defined(_DEBUG)
264 void
265 lws_dll2_describe(struct lws_dll2_owner *owner, const char *desc);
266 #else
267 #define lws_dll2_describe(x, y)
268 #endif
269 
270 /*
271  * these are safe against the current container object getting deleted,
272  * since the hold his next in a temp and go to that next. ___tmp is
273  * the temp.
274  */
275 
276 #define lws_start_foreach_dll_safe(___type, ___it, ___tmp, ___start) \
277 { \
278  ___type ___it = ___start; \
279  while (___it) { \
280  ___type ___tmp = (___it)->next;
281 
282 #define lws_end_foreach_dll_safe(___it, ___tmp) \
283  ___it = ___tmp; \
284  } \
285 }
286 
287 #define lws_start_foreach_dll(___type, ___it, ___start) \
288 { \
289  ___type ___it = ___start; \
290  while (___it) {
291 
292 #define lws_end_foreach_dll(___it) \
293  ___it = (___it)->next; \
294  } \
295 }
296 
298 
Definition: lws-dll2.h:203
Definition: lws-dll2.h:197