llist.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2021, 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 https://curl.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. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #include <curl/curl.h>
  24. #include "llist.h"
  25. #include "curl_memory.h"
  26. /* this must be the last include file */
  27. #include "memdebug.h"
  28. /*
  29. * @unittest: 1300
  30. */
  31. void
  32. Curl_llist_init(struct 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. /*
  40. * Curl_llist_insert_next()
  41. *
  42. * Inserts a new list element after the given one 'e'. If the given existing
  43. * entry is NULL and the list already has elements, the new one will be
  44. * inserted first in the list.
  45. *
  46. * The 'ne' argument should be a pointer into the object to store.
  47. *
  48. * @unittest: 1300
  49. */
  50. void
  51. Curl_llist_insert_next(struct Curl_llist *list, struct Curl_llist_element *e,
  52. const void *p,
  53. struct Curl_llist_element *ne)
  54. {
  55. ne->ptr = (void *) p;
  56. if(list->size == 0) {
  57. list->head = ne;
  58. list->head->prev = NULL;
  59. list->head->next = NULL;
  60. list->tail = ne;
  61. }
  62. else {
  63. /* if 'e' is NULL here, we insert the new element first in the list */
  64. ne->next = e?e->next:list->head;
  65. ne->prev = e;
  66. if(!e) {
  67. list->head->prev = ne;
  68. list->head = ne;
  69. }
  70. else if(e->next) {
  71. e->next->prev = ne;
  72. }
  73. else {
  74. list->tail = ne;
  75. }
  76. if(e)
  77. e->next = ne;
  78. }
  79. ++list->size;
  80. }
  81. /*
  82. * @unittest: 1300
  83. */
  84. void
  85. Curl_llist_remove(struct Curl_llist *list, struct Curl_llist_element *e,
  86. void *user)
  87. {
  88. void *ptr;
  89. if(!e || list->size == 0)
  90. return;
  91. if(e == list->head) {
  92. list->head = e->next;
  93. if(!list->head)
  94. list->tail = NULL;
  95. else
  96. e->next->prev = NULL;
  97. }
  98. else {
  99. if(e->prev)
  100. e->prev->next = e->next;
  101. if(!e->next)
  102. list->tail = e->prev;
  103. else
  104. e->next->prev = e->prev;
  105. }
  106. ptr = e->ptr;
  107. e->ptr = NULL;
  108. e->prev = NULL;
  109. e->next = NULL;
  110. --list->size;
  111. /* call the dtor() last for when it actually frees the 'e' memory itself */
  112. if(list->dtor)
  113. list->dtor(user, ptr);
  114. }
  115. void
  116. Curl_llist_destroy(struct Curl_llist *list, void *user)
  117. {
  118. if(list) {
  119. while(list->size > 0)
  120. Curl_llist_remove(list, list->tail, user);
  121. }
  122. }
  123. size_t
  124. Curl_llist_count(struct Curl_llist *list)
  125. {
  126. return list->size;
  127. }