llist.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. /*
  31. * @unittest: 1300
  32. */
  33. void
  34. Curl_llist_init(struct Curl_llist *l, Curl_llist_dtor dtor)
  35. {
  36. l->size = 0;
  37. l->dtor = dtor;
  38. l->head = NULL;
  39. l->tail = NULL;
  40. }
  41. /*
  42. * Curl_llist_insert_next()
  43. *
  44. * Inserts a new list element after the given one 'e'. If the given existing
  45. * entry is NULL and the list already has elements, the new one will be
  46. * inserted first in the list.
  47. *
  48. * The 'ne' argument should be a pointer into the object to store.
  49. *
  50. * @unittest: 1300
  51. */
  52. void
  53. Curl_llist_insert_next(struct Curl_llist *list, struct Curl_llist_element *e,
  54. const void *p,
  55. struct Curl_llist_element *ne)
  56. {
  57. ne->ptr = (void *) p;
  58. if(list->size == 0) {
  59. list->head = ne;
  60. list->head->prev = NULL;
  61. list->head->next = NULL;
  62. list->tail = ne;
  63. }
  64. else {
  65. /* if 'e' is NULL here, we insert the new element first in the list */
  66. ne->next = e?e->next:list->head;
  67. ne->prev = e;
  68. if(!e) {
  69. list->head->prev = ne;
  70. list->head = ne;
  71. }
  72. else if(e->next) {
  73. e->next->prev = ne;
  74. }
  75. else {
  76. list->tail = ne;
  77. }
  78. if(e)
  79. e->next = ne;
  80. }
  81. ++list->size;
  82. }
  83. /*
  84. * Curl_llist_append()
  85. *
  86. * Adds a new list element to the end of the list.
  87. *
  88. * The 'ne' argument should be a pointer into the object to store.
  89. *
  90. * @unittest: 1300
  91. */
  92. void
  93. Curl_llist_append(struct Curl_llist *list, const void *p,
  94. struct Curl_llist_element *ne)
  95. {
  96. Curl_llist_insert_next(list, list->tail, p, ne);
  97. }
  98. /*
  99. * @unittest: 1300
  100. */
  101. void
  102. Curl_llist_remove(struct Curl_llist *list, struct Curl_llist_element *e,
  103. void *user)
  104. {
  105. void *ptr;
  106. if(!e || list->size == 0)
  107. return;
  108. if(e == list->head) {
  109. list->head = e->next;
  110. if(!list->head)
  111. list->tail = NULL;
  112. else
  113. e->next->prev = NULL;
  114. }
  115. else {
  116. if(e->prev)
  117. e->prev->next = e->next;
  118. if(!e->next)
  119. list->tail = e->prev;
  120. else
  121. e->next->prev = e->prev;
  122. }
  123. ptr = e->ptr;
  124. e->ptr = NULL;
  125. e->prev = NULL;
  126. e->next = NULL;
  127. --list->size;
  128. /* call the dtor() last for when it actually frees the 'e' memory itself */
  129. if(list->dtor)
  130. list->dtor(user, ptr);
  131. }
  132. void
  133. Curl_llist_destroy(struct Curl_llist *list, void *user)
  134. {
  135. if(list) {
  136. while(list->size > 0)
  137. Curl_llist_remove(list, list->tail, user);
  138. }
  139. }
  140. size_t
  141. Curl_llist_count(struct Curl_llist *list)
  142. {
  143. return list->size;
  144. }