lws_dll2.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
  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. #include "private-lib-core.h"
  25. #ifdef LWS_HAVE_SYS_TYPES_H
  26. #include <sys/types.h>
  27. #endif
  28. int
  29. lws_dll2_foreach_safe(struct lws_dll2_owner *owner, void *user,
  30. int (*cb)(struct lws_dll2 *d, void *user))
  31. {
  32. lws_start_foreach_dll_safe(struct lws_dll2 *, p, tp, owner->head) {
  33. if (cb(p, user))
  34. return 1;
  35. } lws_end_foreach_dll_safe(p, tp);
  36. return 0;
  37. }
  38. void
  39. lws_dll2_add_head(struct lws_dll2 *d, struct lws_dll2_owner *owner)
  40. {
  41. if (!lws_dll2_is_detached(d)) {
  42. assert(0); /* only wholly detached things can be added */
  43. return;
  44. }
  45. /* our next guy is current first guy, if any */
  46. if (owner->head != d)
  47. d->next = owner->head;
  48. /* if there is a next guy, set his prev ptr to our next ptr */
  49. if (d->next)
  50. d->next->prev = d;
  51. /* there is nobody previous to us, we are the head */
  52. d->prev = NULL;
  53. /* set the first guy to be us */
  54. owner->head = d;
  55. if (!owner->tail)
  56. owner->tail = d;
  57. d->owner = owner;
  58. owner->count++;
  59. }
  60. /*
  61. * add us to the list that 'after' is in, just before him
  62. */
  63. void
  64. lws_dll2_add_before(struct lws_dll2 *d, struct lws_dll2 *after)
  65. {
  66. lws_dll2_owner_t *owner = after->owner;
  67. if (!lws_dll2_is_detached(d)) {
  68. assert(0); /* only wholly detached things can be added */
  69. return;
  70. }
  71. if (lws_dll2_is_detached(after)) {
  72. assert(0); /* can't add after something detached */
  73. return;
  74. }
  75. d->owner = owner;
  76. /* we need to point forward to after */
  77. d->next = after;
  78. /* we need to point back to after->prev */
  79. d->prev = after->prev;
  80. /* guy that used to point to after, needs to point to us */
  81. if (after->prev)
  82. after->prev->next = d;
  83. else
  84. owner->head = d;
  85. /* then after needs to point back to us */
  86. after->prev = d;
  87. owner->count++;
  88. }
  89. void
  90. lws_dll2_add_tail(struct lws_dll2 *d, struct lws_dll2_owner *owner)
  91. {
  92. if (!lws_dll2_is_detached(d)) {
  93. assert(0); /* only wholly detached things can be added */
  94. return;
  95. }
  96. /* our previous guy is current last guy */
  97. d->prev = owner->tail;
  98. /* if there is a prev guy, set his next ptr to our prev ptr */
  99. if (d->prev)
  100. d->prev->next = d;
  101. /* our next ptr is NULL */
  102. d->next = NULL;
  103. /* set the last guy to be us */
  104. owner->tail = d;
  105. /* list head is also us if we're the first */
  106. if (!owner->head)
  107. owner->head = d;
  108. d->owner = owner;
  109. owner->count++;
  110. }
  111. void
  112. lws_dll2_remove(struct lws_dll2 *d)
  113. {
  114. if (lws_dll2_is_detached(d))
  115. return;
  116. /* if we have a next guy, set his prev to our prev */
  117. if (d->next)
  118. d->next->prev = d->prev;
  119. /* if we have a previous guy, set his next to our next */
  120. if (d->prev)
  121. d->prev->next = d->next;
  122. /* if we have phead, track the tail and head if it points to us... */
  123. if (d->owner->tail == d)
  124. d->owner->tail = d->prev;
  125. if (d->owner->head == d)
  126. d->owner->head = d->next;
  127. d->owner->count--;
  128. /* we're out of the list, we should not point anywhere any more */
  129. d->owner = NULL;
  130. d->prev = NULL;
  131. d->next = NULL;
  132. }
  133. void
  134. lws_dll2_clear(struct lws_dll2 *d)
  135. {
  136. d->owner = NULL;
  137. d->prev = NULL;
  138. d->next = NULL;
  139. }
  140. void
  141. lws_dll2_owner_clear(struct lws_dll2_owner *d)
  142. {
  143. d->head = NULL;
  144. d->tail = NULL;
  145. d->count = 0;
  146. }
  147. void
  148. lws_dll2_add_sorted(lws_dll2_t *d, lws_dll2_owner_t *own,
  149. int (*compare)(const lws_dll2_t *d, const lws_dll2_t *i))
  150. {
  151. lws_start_foreach_dll_safe(struct lws_dll2 *, p, tp,
  152. lws_dll2_get_head(own)) {
  153. assert(p != d);
  154. if (compare(p, d) >= 0) {
  155. /* drop us in before this guy */
  156. lws_dll2_add_before(d, p);
  157. // lws_dll2_describe(own, "post-insert");
  158. return;
  159. }
  160. } lws_end_foreach_dll_safe(p, tp);
  161. /*
  162. * Either nobody on the list yet to compare him to, or he's the
  163. * furthest away timeout... stick him at the tail end
  164. */
  165. lws_dll2_add_tail(d, own);
  166. }
  167. void *
  168. _lws_dll2_search_sz_pl(lws_dll2_owner_t *own, const char *name, size_t namelen,
  169. size_t dll2_ofs, size_t ptr_ofs)
  170. {
  171. lws_start_foreach_dll(struct lws_dll2 *, p, lws_dll2_get_head(own)) {
  172. uint8_t *ref = ((uint8_t *)p) - dll2_ofs;
  173. /*
  174. * We have to read the const char * at the computed place and
  175. * the string is where that points
  176. */
  177. const char *str = *((const char **)(ref + ptr_ofs));
  178. if (str && !strncmp(str, name, namelen) && !str[namelen])
  179. return (void *)ref;
  180. } lws_end_foreach_dll(p);
  181. return NULL;
  182. }
  183. #if defined(_DEBUG)
  184. void
  185. lws_dll2_describe(lws_dll2_owner_t *owner, const char *desc)
  186. {
  187. #if _LWS_ENABLED_LOGS & LLL_INFO
  188. int n = 1;
  189. lwsl_info("%s: %s: owner %p: count %d, head %p, tail %p\n",
  190. __func__, desc, owner, (int)owner->count, owner->head, owner->tail);
  191. lws_start_foreach_dll_safe(struct lws_dll2 *, p, tp,
  192. lws_dll2_get_head(owner)) {
  193. lwsl_info("%s: %d: %p: owner %p, prev %p, next %p\n",
  194. __func__, n++, p, p->owner, p->prev, p->next);
  195. } lws_end_foreach_dll_safe(p, tp);
  196. #endif
  197. }
  198. #endif