llist.c 4.1 KB

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