llist.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2002, 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 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. #ifdef MALLOCDEBUG
  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. int
  74. Curl_llist_insert_prev(curl_llist *list, curl_llist_element *e, const void *p)
  75. {
  76. curl_llist_element *ne;
  77. ne = (curl_llist_element *) malloc(sizeof(curl_llist_element));
  78. ne->ptr = (void *) p;
  79. if (list->size == 0) {
  80. list->head = ne;
  81. list->head->prev = NULL;
  82. list->head->next = NULL;
  83. list->tail = ne;
  84. } else {
  85. ne->next = e;
  86. ne->prev = e->prev;
  87. if (e->prev)
  88. e->prev->next = ne;
  89. else
  90. list->head = ne;
  91. e->prev = ne;
  92. }
  93. ++list->size;
  94. return 1;
  95. }
  96. int
  97. Curl_llist_remove(curl_llist *list, curl_llist_element *e, void *user)
  98. {
  99. if (e == NULL || list->size == 0)
  100. return 1;
  101. if (e == list->head) {
  102. list->head = e->next;
  103. if (list->head == NULL)
  104. list->tail = NULL;
  105. else
  106. e->next->prev = NULL;
  107. } else {
  108. e->prev->next = e->next;
  109. if (!e->next)
  110. list->tail = e->prev;
  111. else
  112. e->next->prev = e->prev;
  113. }
  114. list->dtor(user, e->ptr);
  115. free(e);
  116. --list->size;
  117. return 1;
  118. }
  119. int
  120. Curl_llist_remove_next(curl_llist *list, curl_llist_element *e, void *user)
  121. {
  122. return Curl_llist_remove(list, e->next, user);
  123. }
  124. int
  125. Curl_llist_remove_prev(curl_llist *list, curl_llist_element *e, void *user)
  126. {
  127. return Curl_llist_remove(list, e->prev, user);
  128. }
  129. size_t
  130. Curl_llist_count(curl_llist *list)
  131. {
  132. return list->size;
  133. }
  134. void
  135. Curl_llist_destroy(curl_llist *list, void *user)
  136. {
  137. while (list->size > 0) {
  138. Curl_llist_remove(list, CURL_LLIST_TAIL(list), user);
  139. }
  140. free(list);
  141. list = NULL;
  142. }