llist.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. /*
  126. * @unittest: 1300
  127. */
  128. void
  129. Curl_node_uremove(struct Curl_llist_node *e, void *user)
  130. {
  131. void *ptr;
  132. struct Curl_llist *list;
  133. if(!e)
  134. return;
  135. list = e->_list;
  136. DEBUGASSERT(list);
  137. DEBUGASSERT(list->_init == LLISTINIT);
  138. DEBUGASSERT(list->_size);
  139. DEBUGASSERT(e->_init == NODEINIT);
  140. if(e == list->_head) {
  141. list->_head = e->_next;
  142. if(!list->_head)
  143. list->_tail = NULL;
  144. else
  145. e->_next->_prev = NULL;
  146. }
  147. else {
  148. if(e->_prev)
  149. e->_prev->_next = e->_next;
  150. if(!e->_next)
  151. list->_tail = e->_prev;
  152. else
  153. e->_next->_prev = e->_prev;
  154. }
  155. ptr = e->_ptr;
  156. e->_list = NULL;
  157. e->_ptr = NULL;
  158. e->_prev = NULL;
  159. e->_next = NULL;
  160. #ifdef DEBUGBUILD
  161. e->_init = NODEREM; /* specific pattern on remove - not zero */
  162. #endif
  163. --list->_size;
  164. /* call the dtor() last for when it actually frees the 'e' memory itself */
  165. if(list->_dtor)
  166. list->_dtor(user, ptr);
  167. }
  168. void Curl_node_remove(struct Curl_llist_node *e)
  169. {
  170. Curl_node_uremove(e, NULL);
  171. }
  172. void
  173. Curl_llist_destroy(struct Curl_llist *list, void *user)
  174. {
  175. if(list) {
  176. DEBUGASSERT(list->_init == LLISTINIT);
  177. while(list->_size > 0)
  178. Curl_node_uremove(list->_tail, user);
  179. }
  180. }
  181. /* Curl_llist_head() returns the first 'struct Curl_llist_node *', which
  182. might be NULL */
  183. struct Curl_llist_node *Curl_llist_head(struct Curl_llist *list)
  184. {
  185. DEBUGASSERT(list);
  186. DEBUGASSERT(list->_init == LLISTINIT);
  187. return VERIFYNODE(list->_head);
  188. }
  189. #ifdef UNITTESTS
  190. /* Curl_llist_tail() returns the last 'struct Curl_llist_node *', which
  191. might be NULL */
  192. struct Curl_llist_node *Curl_llist_tail(struct Curl_llist *list)
  193. {
  194. DEBUGASSERT(list);
  195. DEBUGASSERT(list->_init == LLISTINIT);
  196. return VERIFYNODE(list->_tail);
  197. }
  198. #endif
  199. /* Curl_llist_count() returns a size_t the number of nodes in the list */
  200. size_t Curl_llist_count(struct Curl_llist *list)
  201. {
  202. DEBUGASSERT(list);
  203. DEBUGASSERT(list->_init == LLISTINIT);
  204. return list->_size;
  205. }
  206. /* Curl_node_elem() returns the custom data from a Curl_llist_node */
  207. void *Curl_node_elem(struct Curl_llist_node *n)
  208. {
  209. DEBUGASSERT(n);
  210. DEBUGASSERT(n->_init == NODEINIT);
  211. return n->_ptr;
  212. }
  213. /* Curl_node_next() returns the next element in a list from a given
  214. Curl_llist_node */
  215. struct Curl_llist_node *Curl_node_next(struct Curl_llist_node *n)
  216. {
  217. DEBUGASSERT(n);
  218. DEBUGASSERT(n->_init == NODEINIT);
  219. return VERIFYNODE(n->_next);
  220. }
  221. #ifdef UNITTESTS
  222. /* Curl_node_prev() returns the previous element in a list from a given
  223. Curl_llist_node */
  224. struct Curl_llist_node *Curl_node_prev(struct Curl_llist_node *n)
  225. {
  226. DEBUGASSERT(n);
  227. DEBUGASSERT(n->_init == NODEINIT);
  228. return VERIFYNODE(n->_prev);
  229. }
  230. #endif
  231. struct Curl_llist *Curl_node_llist(struct Curl_llist_node *n)
  232. {
  233. DEBUGASSERT(n);
  234. DEBUGASSERT(!n->_list || n->_init == NODEINIT);
  235. return n->_list;
  236. }