llist.c 3.8 KB

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