2
0

llist.c 3.2 KB

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