2 * libwebsockets - small server side websockets and web server implementation
4 * Copyright (C) 2010-2019 Andy Green <andy@warmcat.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation:
9 * version 2.1 of the License.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
22 #include "private-lib-core.h"
24 #ifdef LWS_HAVE_SYS_TYPES_H
25 #include <sys/types.h>
29 lws_dll2_foreach_safe(struct lws_dll2_owner *owner, void *user,
30 int (*cb)(struct lws_dll2 *d, void *user))
32 lws_start_foreach_dll_safe(struct lws_dll2 *, p, tp, owner->head) {
35 } lws_end_foreach_dll_safe(p, tp);
41 lws_dll2_add_head(struct lws_dll2 *d, struct lws_dll2_owner *owner)
43 if (!lws_dll2_is_detached(d)) {
44 assert(0); /* only wholly detached things can be added */
48 /* our next guy is current first guy, if any */
50 d->next = owner->head;
52 /* if there is a next guy, set his prev ptr to our next ptr */
55 /* there is nobody previous to us, we are the head */
58 /* set the first guy to be us */
69 * add us to the list that 'after' is in, just before him
73 lws_dll2_add_before(struct lws_dll2 *d, struct lws_dll2 *after)
75 lws_dll2_owner_t *owner = after->owner;
77 if (!lws_dll2_is_detached(d)) {
78 assert(0); /* only wholly detached things can be added */
82 if (lws_dll2_is_detached(after)) {
83 assert(0); /* can't add after something detached */
89 /* we need to point forward to after */
93 /* we need to point back to after->prev */
95 d->prev = after->prev;
97 /* guy that used to point to after, needs to point to us */
100 after->prev->next = d;
104 /* then after needs to point back to us */
112 lws_dll2_add_tail(struct lws_dll2 *d, struct lws_dll2_owner *owner)
114 if (!lws_dll2_is_detached(d)) {
115 assert(0); /* only wholly detached things can be added */
119 /* our previous guy is current last guy */
120 d->prev = owner->tail;
121 /* if there is a prev guy, set his next ptr to our prev ptr */
124 /* our next ptr is NULL */
126 /* set the last guy to be us */
129 /* list head is also us if we're the first */
138 lws_dll2_remove(struct lws_dll2 *d)
140 if (lws_dll2_is_detached(d))
143 /* if we have a next guy, set his prev to our prev */
145 d->next->prev = d->prev;
147 /* if we have a previous guy, set his next to our next */
149 d->prev->next = d->next;
151 /* if we have phead, track the tail and head if it points to us... */
153 if (d->owner->tail == d)
154 d->owner->tail = d->prev;
156 if (d->owner->head == d)
157 d->owner->head = d->next;
161 /* we're out of the list, we should not point anywhere any more */
168 lws_dll2_clear(struct lws_dll2 *d)
176 lws_dll2_owner_clear(struct lws_dll2_owner *d)
184 lws_dll2_add_sorted(lws_dll2_t *d, lws_dll2_owner_t *own,
185 int (*compare)(const lws_dll2_t *d, const lws_dll2_t *i))
187 lws_start_foreach_dll_safe(struct lws_dll2 *, p, tp,
188 lws_dll2_get_head(own)) {
191 if (compare(p, d) >= 0) {
192 /* drop us in before this guy */
193 lws_dll2_add_before(d, p);
195 // lws_dll2_describe(own, "post-insert");
199 } lws_end_foreach_dll_safe(p, tp);
202 * Either nobody on the list yet to compare him to, or he's the
203 * furthest away timeout... stick him at the tail end
206 lws_dll2_add_tail(d, own);
212 lws_dll2_describe(lws_dll2_owner_t *owner, const char *desc)
216 lwsl_info("%s: %s: owner %p: count %d, head %p, tail %p\n",
217 __func__, desc, owner, owner->count, owner->head, owner->tail);
219 lws_start_foreach_dll_safe(struct lws_dll2 *, p, tp,
220 lws_dll2_get_head(owner)) {
221 lwsl_info("%s: %d: %p: owner %p, prev %p, next %p\n",
222 __func__, n++, p, p->owner, p->prev, p->next);
223 } lws_end_foreach_dll_safe(p, tp);