2
0

llist.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2017, 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.haxx.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. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #include <curl/curl.h>
  24. #include "llist.h"
  25. #include "curl_memory.h"
  26. /* this must be the last include file */
  27. #include "memdebug.h"
  28. /*
  29. * @unittest: 1300
  30. */
  31. void
  32. Curl_llist_init(struct curl_llist *l, curl_llist_dtor dtor)
  33. {
  34. l->size = 0;
  35. l->dtor = dtor;
  36. l->head = NULL;
  37. l->tail = NULL;
  38. }
  39. /*
  40. * Curl_llist_insert_next()
  41. *
  42. * Inserts a new list element after the given one 'e'. If the given existing
  43. * entry is NULL and the list already has elements, the new one will be
  44. * inserted first in the list.
  45. *
  46. * The 'ne' argument should be a pointer into the object to store.
  47. *
  48. * @unittest: 1300
  49. */
  50. void
  51. Curl_llist_insert_next(struct curl_llist *list, struct curl_llist_element *e,
  52. const void *p,
  53. struct curl_llist_element *ne)
  54. {
  55. ne->ptr = (void *) p;
  56. if(list->size == 0) {
  57. list->head = ne;
  58. list->head->prev = NULL;
  59. list->head->next = NULL;
  60. list->tail = ne;
  61. }
  62. else {
  63. /* if 'e' is NULL here, we insert the new element first in the list */
  64. ne->next = e?e->next:list->head;
  65. ne->prev = e;
  66. if(!e) {
  67. list->head->prev = ne;
  68. list->head = ne;
  69. }
  70. else if(e->next) {
  71. e->next->prev = ne;
  72. }
  73. else {
  74. list->tail = ne;
  75. }
  76. if(e)
  77. e->next = ne;
  78. }
  79. ++list->size;
  80. }
  81. /*
  82. * @unittest: 1300
  83. */
  84. void
  85. Curl_llist_remove(struct curl_llist *list, struct curl_llist_element *e,
  86. void *user)
  87. {
  88. void *ptr;
  89. if(e == NULL || list->size == 0)
  90. return;
  91. if(e == list->head) {
  92. list->head = e->next;
  93. if(list->head == NULL)
  94. list->tail = NULL;
  95. else
  96. e->next->prev = NULL;
  97. }
  98. else {
  99. if(!e->prev)
  100. list->head = e->next;
  101. else
  102. e->prev->next = e->next;
  103. if(!e->next)
  104. list->tail = e->prev;
  105. else
  106. e->next->prev = e->prev;
  107. }
  108. ptr = e->ptr;
  109. e->ptr = NULL;
  110. e->prev = NULL;
  111. e->next = NULL;
  112. --list->size;
  113. /* call the dtor() last for when it actually frees the 'e' memory itself */
  114. if(list->dtor)
  115. list->dtor(user, ptr);
  116. }
  117. void
  118. Curl_llist_destroy(struct curl_llist *list, void *user)
  119. {
  120. if(list) {
  121. while(list->size > 0)
  122. Curl_llist_remove(list, list->tail, user);
  123. }
  124. }
  125. size_t
  126. Curl_llist_count(struct curl_llist *list)
  127. {
  128. return list->size;
  129. }
  130. /*
  131. * @unittest: 1300
  132. */
  133. void Curl_llist_move(struct curl_llist *list, struct curl_llist_element *e,
  134. struct curl_llist *to_list,
  135. struct curl_llist_element *to_e)
  136. {
  137. /* Remove element from list */
  138. if(e == NULL || list->size == 0)
  139. return;
  140. if(e == list->head) {
  141. list->head = e->next;
  142. if(list->head == NULL)
  143. list->tail = NULL;
  144. else
  145. e->next->prev = NULL;
  146. }
  147. else {
  148. e->prev->next = e->next;
  149. if(!e->next)
  150. list->tail = e->prev;
  151. else
  152. e->next->prev = e->prev;
  153. }
  154. --list->size;
  155. /* Add element to to_list after to_e */
  156. if(to_list->size == 0) {
  157. to_list->head = e;
  158. to_list->head->prev = NULL;
  159. to_list->head->next = NULL;
  160. to_list->tail = e;
  161. }
  162. else {
  163. e->next = to_e->next;
  164. e->prev = to_e;
  165. if(to_e->next) {
  166. to_e->next->prev = e;
  167. }
  168. else {
  169. to_list->tail = e;
  170. }
  171. to_e->next = e;
  172. }
  173. ++to_list->size;
  174. }