1
0

llist.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2006, 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. #include "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 = (struct curl_llist *)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 =
  56. (struct curl_llist_element *) malloc(sizeof(struct curl_llist_element));
  57. if(!ne)
  58. return 0;
  59. ne->ptr = (void *) p;
  60. if (list->size == 0) {
  61. list->head = ne;
  62. list->head->prev = NULL;
  63. list->head->next = NULL;
  64. list->tail = ne;
  65. }
  66. else {
  67. ne->next = e->next;
  68. ne->prev = e;
  69. if (e->next) {
  70. e->next->prev = ne;
  71. }
  72. else {
  73. list->tail = ne;
  74. }
  75. e->next = ne;
  76. }
  77. ++list->size;
  78. return 1;
  79. }
  80. int
  81. Curl_llist_remove(struct curl_llist *list, struct curl_llist_element *e,
  82. void *user)
  83. {
  84. if (e == NULL || list->size == 0)
  85. return 1;
  86. if (e == list->head) {
  87. list->head = e->next;
  88. if (list->head == NULL)
  89. list->tail = NULL;
  90. else
  91. e->next->prev = NULL;
  92. } else {
  93. e->prev->next = e->next;
  94. if (!e->next)
  95. list->tail = e->prev;
  96. else
  97. e->next->prev = e->prev;
  98. }
  99. list->dtor(user, e->ptr);
  100. free(e);
  101. --list->size;
  102. return 1;
  103. }
  104. void
  105. Curl_llist_destroy(struct curl_llist *list, void *user)
  106. {
  107. if(list) {
  108. while (list->size > 0)
  109. Curl_llist_remove(list, list->tail, user);
  110. free(list);
  111. }
  112. }
  113. size_t
  114. Curl_llist_count(struct curl_llist *list)
  115. {
  116. return list->size;
  117. }