llist.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #include <curl/curl.h>
  26. #include "llist.h"
  27. #include "curl_memory.h"
  28. /* this must be the last include file */
  29. #include "memdebug.h"
  30. #ifdef DEBUGBUILD
  31. #define LLISTINIT 0x100cc001 /* random pattern */
  32. #define NODEINIT 0x12344321 /* random pattern */
  33. #define NODEREM 0x54321012 /* random pattern */
  34. #define VERIFYNODE(x) verifynode(x)
  35. static struct Curl_llist_node *verifynode(struct Curl_llist_node *n)
  36. {
  37. DEBUGASSERT(!n || (n->_init == NODEINIT));
  38. return n;
  39. }
  40. #else
  41. #define VERIFYNODE(x) x
  42. #endif
  43. /*
  44. * @unittest: 1300
  45. */
  46. void
  47. Curl_llist_init(struct Curl_llist *l, Curl_llist_dtor dtor)
  48. {
  49. l->_size = 0;
  50. l->_dtor = dtor;
  51. l->_head = NULL;
  52. l->_tail = NULL;
  53. #ifdef DEBUGBUILD
  54. l->_init = LLISTINIT;
  55. #endif
  56. }
  57. /*
  58. * Curl_llist_insert_next()
  59. *
  60. * Inserts a new list element after the given one 'e'. If the given existing
  61. * entry is NULL and the list already has elements, the new one will be
  62. * inserted first in the list.
  63. *
  64. * The 'ne' argument should be a pointer into the object to store.
  65. *
  66. * @unittest: 1300
  67. */
  68. void
  69. Curl_llist_insert_next(struct Curl_llist *list,
  70. struct Curl_llist_node *e, /* may be NULL */
  71. const void *p,
  72. struct Curl_llist_node *ne)
  73. {
  74. DEBUGASSERT(list);
  75. DEBUGASSERT(list->_init == LLISTINIT);
  76. DEBUGASSERT(ne);
  77. #ifdef DEBUGBUILD
  78. ne->_init = NODEINIT;
  79. #endif
  80. ne->_ptr = CURL_UNCONST(p);
  81. ne->_list = list;
  82. if(list->_size == 0) {
  83. list->_head = ne;
  84. list->_head->_prev = NULL;
  85. list->_head->_next = NULL;
  86. list->_tail = ne;
  87. }
  88. else {
  89. /* if 'e' is NULL here, we insert the new element first in the list */
  90. ne->_next = e ? e->_next : list->_head;
  91. ne->_prev = e;
  92. if(!e) {
  93. list->_head->_prev = ne;
  94. list->_head = ne;
  95. }
  96. else if(e->_next) {
  97. e->_next->_prev = ne;
  98. }
  99. else {
  100. list->_tail = ne;
  101. }
  102. if(e)
  103. e->_next = ne;
  104. }
  105. ++list->_size;
  106. }
  107. /*
  108. * Curl_llist_append()
  109. *
  110. * Adds a new list element to the end of the list.
  111. *
  112. * The 'ne' argument should be a pointer into the object to store.
  113. *
  114. * @unittest: 1300
  115. */
  116. void
  117. Curl_llist_append(struct Curl_llist *list, const void *p,
  118. struct Curl_llist_node *ne)
  119. {
  120. DEBUGASSERT(list);
  121. DEBUGASSERT(list->_init == LLISTINIT);
  122. DEBUGASSERT(ne);
  123. Curl_llist_insert_next(list, list->_tail, p, ne);
  124. }
  125. void *Curl_node_take_elem(struct Curl_llist_node *e)
  126. {
  127. void *ptr;
  128. struct Curl_llist *list;
  129. if(!e)
  130. return NULL;
  131. list = e->_list;
  132. DEBUGASSERT(list);
  133. DEBUGASSERT(list->_init == LLISTINIT);
  134. DEBUGASSERT(list->_size);
  135. DEBUGASSERT(e->_init == NODEINIT);
  136. if(list) {
  137. if(e == list->_head) {
  138. list->_head = e->_next;
  139. if(!list->_head)
  140. list->_tail = NULL;
  141. else
  142. e->_next->_prev = NULL;
  143. }
  144. else {
  145. if(e->_prev)
  146. e->_prev->_next = e->_next;
  147. if(!e->_next)
  148. list->_tail = e->_prev;
  149. else
  150. e->_next->_prev = e->_prev;
  151. }
  152. --list->_size;
  153. }
  154. ptr = e->_ptr;
  155. e->_list = NULL;
  156. e->_ptr = NULL;
  157. e->_prev = NULL;
  158. e->_next = NULL;
  159. #ifdef DEBUGBUILD
  160. e->_init = NODEREM; /* specific pattern on remove - not zero */
  161. #endif
  162. return ptr;
  163. }
  164. /*
  165. * @unittest: 1300
  166. */
  167. UNITTEST void Curl_node_uremove(struct Curl_llist_node *, void *);
  168. UNITTEST void
  169. Curl_node_uremove(struct Curl_llist_node *e, void *user)
  170. {
  171. struct Curl_llist *list;
  172. void *ptr;
  173. if(!e)
  174. return;
  175. list = e->_list;
  176. DEBUGASSERT(list);
  177. if(list) {
  178. ptr = Curl_node_take_elem(e);
  179. if(list->_dtor)
  180. list->_dtor(user, ptr);
  181. }
  182. }
  183. void Curl_node_remove(struct Curl_llist_node *e)
  184. {
  185. Curl_node_uremove(e, NULL);
  186. }
  187. void
  188. Curl_llist_destroy(struct Curl_llist *list, void *user)
  189. {
  190. if(list) {
  191. DEBUGASSERT(list->_init == LLISTINIT);
  192. while(list->_size > 0)
  193. Curl_node_uremove(list->_tail, user);
  194. }
  195. }
  196. /* Curl_llist_head() returns the first 'struct Curl_llist_node *', which
  197. might be NULL */
  198. struct Curl_llist_node *Curl_llist_head(struct Curl_llist *list)
  199. {
  200. DEBUGASSERT(list);
  201. DEBUGASSERT(list->_init == LLISTINIT);
  202. return VERIFYNODE(list->_head);
  203. }
  204. #ifdef UNITTESTS
  205. /* Curl_llist_tail() returns the last 'struct Curl_llist_node *', which
  206. might be NULL */
  207. struct Curl_llist_node *Curl_llist_tail(struct Curl_llist *list)
  208. {
  209. DEBUGASSERT(list);
  210. DEBUGASSERT(list->_init == LLISTINIT);
  211. return VERIFYNODE(list->_tail);
  212. }
  213. #endif
  214. /* Curl_llist_count() returns a size_t the number of nodes in the list */
  215. size_t Curl_llist_count(struct Curl_llist *list)
  216. {
  217. DEBUGASSERT(list);
  218. DEBUGASSERT(list->_init == LLISTINIT);
  219. return list->_size;
  220. }
  221. /* Curl_node_elem() returns the custom data from a Curl_llist_node */
  222. void *Curl_node_elem(struct Curl_llist_node *n)
  223. {
  224. DEBUGASSERT(n);
  225. DEBUGASSERT(n->_init == NODEINIT);
  226. return n->_ptr;
  227. }
  228. /* Curl_node_next() returns the next element in a list from a given
  229. Curl_llist_node */
  230. struct Curl_llist_node *Curl_node_next(struct Curl_llist_node *n)
  231. {
  232. DEBUGASSERT(n);
  233. DEBUGASSERT(n->_init == NODEINIT);
  234. return VERIFYNODE(n->_next);
  235. }
  236. #ifdef UNITTESTS
  237. /* Curl_node_prev() returns the previous element in a list from a given
  238. Curl_llist_node */
  239. struct Curl_llist_node *Curl_node_prev(struct Curl_llist_node *n)
  240. {
  241. DEBUGASSERT(n);
  242. DEBUGASSERT(n->_init == NODEINIT);
  243. return VERIFYNODE(n->_prev);
  244. }
  245. #endif
  246. struct Curl_llist *Curl_node_llist(struct Curl_llist_node *n)
  247. {
  248. DEBUGASSERT(n);
  249. DEBUGASSERT(!n->_list || n->_init == NODEINIT);
  250. return n->_list;
  251. }