1
0

llist.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2004, 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 "curl_memory.h"
  28. /* this must be the last include file */
  29. #include "memdebug.h"
  30. void
  31. Curl_llist_init(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. curl_llist *
  39. Curl_llist_alloc(curl_llist_dtor dtor)
  40. {
  41. curl_llist *list;
  42. list = (curl_llist *)malloc(sizeof(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(curl_llist *list, curl_llist_element *e, const void *p)
  53. {
  54. curl_llist_element *ne =
  55. (curl_llist_element *) malloc(sizeof(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(curl_llist *list, curl_llist_element *e, void *user)
  81. {
  82. if (e == NULL || list->size == 0)
  83. return 1;
  84. if (e == list->head) {
  85. list->head = e->next;
  86. if (list->head == NULL)
  87. list->tail = NULL;
  88. else
  89. e->next->prev = NULL;
  90. } else {
  91. e->prev->next = e->next;
  92. if (!e->next)
  93. list->tail = e->prev;
  94. else
  95. e->next->prev = e->prev;
  96. }
  97. list->dtor(user, e->ptr);
  98. free(e);
  99. --list->size;
  100. return 1;
  101. }
  102. void
  103. Curl_llist_destroy(curl_llist *list, void *user)
  104. {
  105. if(list) {
  106. while (list->size > 0)
  107. Curl_llist_remove(list, list->tail, user);
  108. free(list);
  109. }
  110. }