libwebsockets
Lightweight C library for HTML5 websockets
lws-dll2.h
Go to the documentation of this file.
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;
202 
203 typedef struct lws_dll2_owner {
204  struct lws_dll2 *tail;
205  struct lws_dll2 *head;
206 
209 
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 
224 
227 
230 
231 typedef int (*lws_dll2_foreach_cb_t)(struct lws_dll2 *d, void *user);
232 
236 
239 
242 
244 lws_dll2_add_before(struct lws_dll2 *d, struct lws_dll2 *after);
245 
248 
251  int (*compare)(const lws_dll2_t *d, const lws_dll2_t *i));
252 
255  int (*compare3)(void *priv, const lws_dll2_t *d,
256  const lws_dll2_t *i));
257 
258 LWS_VISIBLE LWS_EXTERN void *
259 _lws_dll2_search_sz_pl(lws_dll2_owner_t *own, const char *name, size_t namelen,
260  size_t dll2_ofs, size_t ptr_ofs);
261 
262 /*
263  * Searches objects in an owner list linearly and returns one with a given
264  * member C-string matching a supplied length-provided string if it exists, else
265  * NULL.
266  */
267 
268 #define lws_dll2_search_sz_pl(own, name, namelen, type, membd2list, membptr) \
269  ((type *)_lws_dll2_search_sz_pl(own, name, namelen, \
270  offsetof(type, membd2list), \
271  offsetof(type, membptr)))
272 
273 #if defined(_DEBUG)
274 void
275 lws_dll2_describe(struct lws_dll2_owner *owner, const char *desc);
276 #else
277 #define lws_dll2_describe(x, y)
278 #endif
279 
280 /*
281  * these are safe against the current container object getting deleted,
282  * since the hold his next in a temp and go to that next. ___tmp is
283  * the temp.
284  */
285 
286 #define lws_start_foreach_dll_safe(___type, ___it, ___tmp, ___start) \
287 { \
288  ___type ___it = ___start; \
289  while (___it) { \
290  ___type ___tmp = (___it)->next;
291 
292 #define lws_end_foreach_dll_safe(___it, ___tmp) \
293  ___it = ___tmp; \
294  } \
295 }
296 
297 #define lws_start_foreach_dll(___type, ___it, ___start) \
298 { \
299  ___type ___it = ___start; \
300  while (___it) {
301 
302 #define lws_end_foreach_dll(___it) \
303  ___it = (___it)->next; \
304  } \
305 }
306 
308 
struct lws_dll2 * tail
Definition: lws-dll2.h:204
struct lws_dll2 * next
Definition: lws-dll2.h:199
struct lws_dll2_owner * owner
Definition: lws-dll2.h:200
struct lws_dll2 * prev
Definition: lws-dll2.h:198
struct lws_dll2 * head
Definition: lws-dll2.h:205
uint32_t count
Definition: lws-dll2.h:207
struct lws_dll2 lws_dll2_t
LWS_VISIBLE LWS_EXTERN void lws_dll2_add_tail(struct lws_dll2 *d, struct lws_dll2_owner *owner)
LWS_VISIBLE LWS_EXTERN void lws_dll2_owner_clear(struct lws_dll2_owner *d)
LWS_VISIBLE LWS_EXTERN void lws_dll2_add_sorted_priv(lws_dll2_t *d, lws_dll2_owner_t *own, void *priv, int(*compare3)(void *priv, const lws_dll2_t *d, const lws_dll2_t *i))
#define lws_dll2_describe(x, y)
Definition: lws-dll2.h:277
LWS_VISIBLE LWS_EXTERN void lws_dll2_add_sorted(lws_dll2_t *d, lws_dll2_owner_t *own, int(*compare)(const lws_dll2_t *d, const lws_dll2_t *i))
struct lws_dll2_owner lws_dll2_owner_t
LWS_VISIBLE LWS_EXTERN void lws_dll2_add_insert(struct lws_dll2 *d, struct lws_dll2 *prev)
LWS_VISIBLE LWS_EXTERN void lws_dll2_add_before(struct lws_dll2 *d, struct lws_dll2 *after)
int(* lws_dll2_foreach_cb_t)(struct lws_dll2 *d, void *user)
Definition: lws-dll2.h:231
LWS_VISIBLE LWS_EXTERN int lws_dll2_is_detached(const struct lws_dll2 *d)
LWS_VISIBLE LWS_EXTERN void lws_dll2_remove(struct lws_dll2 *d)
LWS_VISIBLE LWS_EXTERN void lws_dll2_clear(struct lws_dll2 *d)
LWS_VISIBLE LWS_EXTERN void * _lws_dll2_search_sz_pl(lws_dll2_owner_t *own, const char *name, size_t namelen, size_t dll2_ofs, size_t ptr_ofs)
LWS_VISIBLE LWS_EXTERN int lws_dll2_foreach_safe(struct lws_dll2_owner *owner, void *user, lws_dll2_foreach_cb_t cb)
LWS_VISIBLE LWS_EXTERN void lws_dll2_add_head(struct lws_dll2 *d, struct lws_dll2_owner *owner)
unsigned int uint32_t
#define LWS_INLINE
#define LWS_EXTERN
#define LWS_VISIBLE