llist.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, 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. #define LLISTINIT 0x100cc001 /* random pattern */
  31. #define NODEINIT 0x12344321 /* random pattern */
  32. #define NODEREM 0x54321012 /* random pattern */
  33. #ifdef DEBUGBUILD
  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 = (void *) 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. void
  168. Curl_node_uremove(struct Curl_llist_node *e, void *user)
  169. {
  170. struct Curl_llist *list;
  171. void *ptr;
  172. if(!e)
  173. return;
  174. list = e->_list;
  175. DEBUGASSERT(list);
  176. if(list) {
  177. ptr = Curl_node_take_elem(e);
  178. if(list->_dtor)
  179. list->_dtor(user, ptr);
  180. }
  181. }
  182. void Curl_node_remove(struct Curl_llist_node *e)
  183. {
  184. Curl_node_uremove(e, NULL);
  185. }
  186. void
  187. Curl_llist_destroy(struct Curl_llist *list, void *user)
  188. {
  189. if(list) {
  190. DEBUGASSERT(list->_init == LLISTINIT);
  191. while(list->_size > 0)
  192. Curl_node_uremove(list->_tail, user);
  193. }
  194. }
  195. /* Curl_llist_head() returns the first 'struct Curl_llist_node *', which
  196. might be NULL */
  197. struct Curl_llist_node *Curl_llist_head(struct Curl_llist *list)
  198. {
  199. DEBUGASSERT(list);
  200. DEBUGASSERT(list->_init == LLISTINIT);
  201. return VERIFYNODE(list->_head);
  202. }
  203. #ifdef UNITTESTS
  204. /* Curl_llist_tail() returns the last 'struct Curl_llist_node *', which
  205. might be NULL */
  206. struct Curl_llist_node *Curl_llist_tail(struct Curl_llist *list)
  207. {
  208. DEBUGASSERT(list);
  209. DEBUGASSERT(list->_init == LLISTINIT);
  210. return VERIFYNODE(list->_tail);
  211. }
  212. #endif
  213. /* Curl_llist_count() returns a size_t the number of nodes in the list */
  214. size_t Curl_llist_count(struct Curl_llist *list)
  215. {
  216. DEBUGASSERT(list);
  217. DEBUGASSERT(list->_init == LLISTINIT);
  218. return list->_size;
  219. }
  220. /* Curl_node_elem() returns the custom data from a Curl_llist_node */
  221. void *Curl_node_elem(struct Curl_llist_node *n)
  222. {
  223. DEBUGASSERT(n);
  224. DEBUGASSERT(n->_init == NODEINIT);
  225. return n->_ptr;
  226. }
  227. /* Curl_node_next() returns the next element in a list from a given
  228. Curl_llist_node */
  229. struct Curl_llist_node *Curl_node_next(struct Curl_llist_node *n)
  230. {
  231. DEBUGASSERT(n);
  232. DEBUGASSERT(n->_init == NODEINIT);
  233. return VERIFYNODE(n->_next);
  234. }
  235. #ifdef UNITTESTS
  236. /* Curl_node_prev() returns the previous element in a list from a given
  237. Curl_llist_node */
  238. struct Curl_llist_node *Curl_node_prev(struct Curl_llist_node *n)
  239. {
  240. DEBUGASSERT(n);
  241. DEBUGASSERT(n->_init == NODEINIT);
  242. return VERIFYNODE(n->_prev);
  243. }
  244. #endif
  245. struct Curl_llist *Curl_node_llist(struct Curl_llist_node *n)
  246. {
  247. DEBUGASSERT(n);
  248. DEBUGASSERT(!n->_list || n->_init == NODEINIT);
  249. return n->_list;
  250. }