llist.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, 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.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. static void
  32. 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. struct curl_llist *
  40. Curl_llist_alloc(curl_llist_dtor dtor)
  41. {
  42. struct curl_llist *list;
  43. list = malloc(sizeof(struct curl_llist));
  44. if(!list)
  45. return NULL;
  46. llist_init(list, dtor);
  47. return list;
  48. }
  49. /*
  50. * Curl_llist_insert_next()
  51. *
  52. * Inserts a new list element after the given one 'e'. If the given existing
  53. * entry is NULL and the list already has elements, the new one will be
  54. * inserted first in the list.
  55. *
  56. * Returns: 1 on success and 0 on failure.
  57. *
  58. * @unittest: 1300
  59. */
  60. int
  61. Curl_llist_insert_next(struct curl_llist *list, struct curl_llist_element *e,
  62. const void *p)
  63. {
  64. struct curl_llist_element *ne = malloc(sizeof(struct curl_llist_element));
  65. if(!ne)
  66. return 0;
  67. ne->ptr = (void *) p;
  68. if(list->size == 0) {
  69. list->head = ne;
  70. list->head->prev = NULL;
  71. list->head->next = NULL;
  72. list->tail = ne;
  73. }
  74. else {
  75. /* if 'e' is NULL here, we insert the new element first in the list */
  76. ne->next = e?e->next:list->head;
  77. ne->prev = e;
  78. if(!e) {
  79. list->head->prev = ne;
  80. list->head = ne;
  81. }
  82. else if(e->next) {
  83. e->next->prev = ne;
  84. }
  85. else {
  86. list->tail = ne;
  87. }
  88. if(e)
  89. e->next = ne;
  90. }
  91. ++list->size;
  92. return 1;
  93. }
  94. /*
  95. * @unittest: 1300
  96. */
  97. int
  98. Curl_llist_remove(struct curl_llist *list, struct curl_llist_element *e,
  99. void *user)
  100. {
  101. if(e == NULL || list->size == 0)
  102. return 1;
  103. if(e == list->head) {
  104. list->head = e->next;
  105. if(list->head == NULL)
  106. list->tail = NULL;
  107. else
  108. e->next->prev = NULL;
  109. }
  110. else {
  111. e->prev->next = e->next;
  112. if(!e->next)
  113. list->tail = e->prev;
  114. else
  115. e->next->prev = e->prev;
  116. }
  117. list->dtor(user, e->ptr);
  118. e->ptr = NULL;
  119. e->prev = NULL;
  120. e->next = NULL;
  121. free(e);
  122. --list->size;
  123. return 1;
  124. }
  125. void
  126. Curl_llist_destroy(struct curl_llist *list, void *user)
  127. {
  128. if(list) {
  129. while(list->size > 0)
  130. Curl_llist_remove(list, list->tail, user);
  131. free(list);
  132. }
  133. }
  134. size_t
  135. Curl_llist_count(struct curl_llist *list)
  136. {
  137. return list->size;
  138. }
  139. /*
  140. * @unittest: 1300
  141. */
  142. int Curl_llist_move(struct curl_llist *list, struct curl_llist_element *e,
  143. struct curl_llist *to_list,
  144. struct curl_llist_element *to_e)
  145. {
  146. /* Remove element from list */
  147. if(e == NULL || list->size == 0)
  148. return 0;
  149. if(e == list->head) {
  150. list->head = e->next;
  151. if(list->head == NULL)
  152. list->tail = NULL;
  153. else
  154. e->next->prev = NULL;
  155. }
  156. else {
  157. e->prev->next = e->next;
  158. if(!e->next)
  159. list->tail = e->prev;
  160. else
  161. e->next->prev = e->prev;
  162. }
  163. --list->size;
  164. /* Add element to to_list after to_e */
  165. if(to_list->size == 0) {
  166. to_list->head = e;
  167. to_list->head->prev = NULL;
  168. to_list->head->next = NULL;
  169. to_list->tail = e;
  170. }
  171. else {
  172. e->next = to_e->next;
  173. e->prev = to_e;
  174. if(to_e->next) {
  175. to_e->next->prev = e;
  176. }
  177. else {
  178. to_list->tail = e;
  179. }
  180. to_e->next = e;
  181. }
  182. ++to_list->size;
  183. return 1;
  184. }