llist.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2004, 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: llist.c,v 1.12 2004/01/07 09:19:35 bagder Exp $
  22. ***************************************************************************/
  23. #include "setup.h"
  24. #include <string.h>
  25. #include <stdlib.h>
  26. #include "llist.h"
  27. #ifdef CURLDEBUG
  28. /* this must be the last include file */
  29. #include "memdebug.h"
  30. #endif
  31. void
  32. Curl_llist_init(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. curl_llist *
  40. Curl_llist_alloc(curl_llist_dtor dtor)
  41. {
  42. curl_llist *list;
  43. list = (curl_llist *)malloc(sizeof(curl_llist));
  44. if(NULL == list)
  45. return NULL;
  46. Curl_llist_init(list, dtor);
  47. return list;
  48. }
  49. int
  50. Curl_llist_insert_next(curl_llist *list, curl_llist_element *e, const void *p)
  51. {
  52. curl_llist_element *ne;
  53. ne = (curl_llist_element *) malloc(sizeof(curl_llist_element));
  54. ne->ptr = (void *) p;
  55. if (list->size == 0) {
  56. list->head = ne;
  57. list->head->prev = NULL;
  58. list->head->next = NULL;
  59. list->tail = ne;
  60. } else {
  61. ne->next = e->next;
  62. ne->prev = e;
  63. if (e->next) {
  64. e->next->prev = ne;
  65. } else {
  66. list->tail = ne;
  67. }
  68. e->next = ne;
  69. }
  70. ++list->size;
  71. return 1;
  72. }
  73. #if 0
  74. int
  75. Curl_llist_insert_prev(curl_llist *list, curl_llist_element *e, const void *p)
  76. {
  77. curl_llist_element *ne;
  78. ne = (curl_llist_element *) malloc(sizeof(curl_llist_element));
  79. ne->ptr = (void *) p;
  80. if (list->size == 0) {
  81. list->head = ne;
  82. list->head->prev = NULL;
  83. list->head->next = NULL;
  84. list->tail = ne;
  85. } else {
  86. ne->next = e;
  87. ne->prev = e->prev;
  88. if (e->prev)
  89. e->prev->next = ne;
  90. else
  91. list->head = ne;
  92. e->prev = ne;
  93. }
  94. ++list->size;
  95. return 1;
  96. }
  97. #endif
  98. int
  99. Curl_llist_remove(curl_llist *list, curl_llist_element *e, 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. } else {
  110. e->prev->next = e->next;
  111. if (!e->next)
  112. list->tail = e->prev;
  113. else
  114. e->next->prev = e->prev;
  115. }
  116. list->dtor(user, e->ptr);
  117. free(e);
  118. --list->size;
  119. return 1;
  120. }
  121. #if 0
  122. int
  123. Curl_llist_remove_next(curl_llist *list, curl_llist_element *e, void *user)
  124. {
  125. return Curl_llist_remove(list, e->next, user);
  126. }
  127. int
  128. Curl_llist_remove_prev(curl_llist *list, curl_llist_element *e, void *user)
  129. {
  130. return Curl_llist_remove(list, e->prev, user);
  131. }
  132. size_t
  133. Curl_llist_count(curl_llist *list)
  134. {
  135. return list->size;
  136. }
  137. #endif
  138. void
  139. Curl_llist_destroy(curl_llist *list, void *user)
  140. {
  141. if(list) {
  142. while (list->size > 0)
  143. Curl_llist_remove(list, list->tail, user);
  144. free(list);
  145. }
  146. }