llist.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, 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.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. ***************************************************************************/
  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 == NULL || list->size == 0)
  90. return;
  91. if(e == list->head) {
  92. list->head = e->next;
  93. if(list->head == NULL)
  94. list->tail = NULL;
  95. else
  96. e->next->prev = NULL;
  97. }
  98. else {
  99. if(!e->prev)
  100. list->head = e->next;
  101. else
  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. }