113498266Sopenharmony_ci/*************************************************************************** 213498266Sopenharmony_ci * _ _ ____ _ 313498266Sopenharmony_ci * Project ___| | | | _ \| | 413498266Sopenharmony_ci * / __| | | | |_) | | 513498266Sopenharmony_ci * | (__| |_| | _ <| |___ 613498266Sopenharmony_ci * \___|\___/|_| \_\_____| 713498266Sopenharmony_ci * 813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 913498266Sopenharmony_ci * 1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which 1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms 1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html. 1313498266Sopenharmony_ci * 1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell 1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is 1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file. 1713498266Sopenharmony_ci * 1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 1913498266Sopenharmony_ci * KIND, either express or implied. 2013498266Sopenharmony_ci * 2113498266Sopenharmony_ci * SPDX-License-Identifier: curl 2213498266Sopenharmony_ci * 2313498266Sopenharmony_ci ***************************************************************************/ 2413498266Sopenharmony_ci 2513498266Sopenharmony_ci#include "curl_setup.h" 2613498266Sopenharmony_ci 2713498266Sopenharmony_ci#include <curl/curl.h> 2813498266Sopenharmony_ci 2913498266Sopenharmony_ci#include "llist.h" 3013498266Sopenharmony_ci#include "curl_memory.h" 3113498266Sopenharmony_ci 3213498266Sopenharmony_ci/* this must be the last include file */ 3313498266Sopenharmony_ci#include "memdebug.h" 3413498266Sopenharmony_ci 3513498266Sopenharmony_ci/* 3613498266Sopenharmony_ci * @unittest: 1300 3713498266Sopenharmony_ci */ 3813498266Sopenharmony_civoid 3913498266Sopenharmony_ciCurl_llist_init(struct Curl_llist *l, Curl_llist_dtor dtor) 4013498266Sopenharmony_ci{ 4113498266Sopenharmony_ci l->size = 0; 4213498266Sopenharmony_ci l->dtor = dtor; 4313498266Sopenharmony_ci l->head = NULL; 4413498266Sopenharmony_ci l->tail = NULL; 4513498266Sopenharmony_ci} 4613498266Sopenharmony_ci 4713498266Sopenharmony_ci/* 4813498266Sopenharmony_ci * Curl_llist_insert_next() 4913498266Sopenharmony_ci * 5013498266Sopenharmony_ci * Inserts a new list element after the given one 'e'. If the given existing 5113498266Sopenharmony_ci * entry is NULL and the list already has elements, the new one will be 5213498266Sopenharmony_ci * inserted first in the list. 5313498266Sopenharmony_ci * 5413498266Sopenharmony_ci * The 'ne' argument should be a pointer into the object to store. 5513498266Sopenharmony_ci * 5613498266Sopenharmony_ci * @unittest: 1300 5713498266Sopenharmony_ci */ 5813498266Sopenharmony_civoid 5913498266Sopenharmony_ciCurl_llist_insert_next(struct Curl_llist *list, struct Curl_llist_element *e, 6013498266Sopenharmony_ci const void *p, 6113498266Sopenharmony_ci struct Curl_llist_element *ne) 6213498266Sopenharmony_ci{ 6313498266Sopenharmony_ci ne->ptr = (void *) p; 6413498266Sopenharmony_ci if(list->size == 0) { 6513498266Sopenharmony_ci list->head = ne; 6613498266Sopenharmony_ci list->head->prev = NULL; 6713498266Sopenharmony_ci list->head->next = NULL; 6813498266Sopenharmony_ci list->tail = ne; 6913498266Sopenharmony_ci } 7013498266Sopenharmony_ci else { 7113498266Sopenharmony_ci /* if 'e' is NULL here, we insert the new element first in the list */ 7213498266Sopenharmony_ci ne->next = e?e->next:list->head; 7313498266Sopenharmony_ci ne->prev = e; 7413498266Sopenharmony_ci if(!e) { 7513498266Sopenharmony_ci list->head->prev = ne; 7613498266Sopenharmony_ci list->head = ne; 7713498266Sopenharmony_ci } 7813498266Sopenharmony_ci else if(e->next) { 7913498266Sopenharmony_ci e->next->prev = ne; 8013498266Sopenharmony_ci } 8113498266Sopenharmony_ci else { 8213498266Sopenharmony_ci list->tail = ne; 8313498266Sopenharmony_ci } 8413498266Sopenharmony_ci if(e) 8513498266Sopenharmony_ci e->next = ne; 8613498266Sopenharmony_ci } 8713498266Sopenharmony_ci 8813498266Sopenharmony_ci ++list->size; 8913498266Sopenharmony_ci} 9013498266Sopenharmony_ci 9113498266Sopenharmony_ci/* 9213498266Sopenharmony_ci * @unittest: 1300 9313498266Sopenharmony_ci */ 9413498266Sopenharmony_civoid 9513498266Sopenharmony_ciCurl_llist_remove(struct Curl_llist *list, struct Curl_llist_element *e, 9613498266Sopenharmony_ci void *user) 9713498266Sopenharmony_ci{ 9813498266Sopenharmony_ci void *ptr; 9913498266Sopenharmony_ci if(!e || list->size == 0) 10013498266Sopenharmony_ci return; 10113498266Sopenharmony_ci 10213498266Sopenharmony_ci if(e == list->head) { 10313498266Sopenharmony_ci list->head = e->next; 10413498266Sopenharmony_ci 10513498266Sopenharmony_ci if(!list->head) 10613498266Sopenharmony_ci list->tail = NULL; 10713498266Sopenharmony_ci else 10813498266Sopenharmony_ci e->next->prev = NULL; 10913498266Sopenharmony_ci } 11013498266Sopenharmony_ci else { 11113498266Sopenharmony_ci if(e->prev) 11213498266Sopenharmony_ci e->prev->next = e->next; 11313498266Sopenharmony_ci 11413498266Sopenharmony_ci if(!e->next) 11513498266Sopenharmony_ci list->tail = e->prev; 11613498266Sopenharmony_ci else 11713498266Sopenharmony_ci e->next->prev = e->prev; 11813498266Sopenharmony_ci } 11913498266Sopenharmony_ci 12013498266Sopenharmony_ci ptr = e->ptr; 12113498266Sopenharmony_ci 12213498266Sopenharmony_ci e->ptr = NULL; 12313498266Sopenharmony_ci e->prev = NULL; 12413498266Sopenharmony_ci e->next = NULL; 12513498266Sopenharmony_ci 12613498266Sopenharmony_ci --list->size; 12713498266Sopenharmony_ci 12813498266Sopenharmony_ci /* call the dtor() last for when it actually frees the 'e' memory itself */ 12913498266Sopenharmony_ci if(list->dtor) 13013498266Sopenharmony_ci list->dtor(user, ptr); 13113498266Sopenharmony_ci} 13213498266Sopenharmony_ci 13313498266Sopenharmony_civoid 13413498266Sopenharmony_ciCurl_llist_destroy(struct Curl_llist *list, void *user) 13513498266Sopenharmony_ci{ 13613498266Sopenharmony_ci if(list) { 13713498266Sopenharmony_ci while(list->size > 0) 13813498266Sopenharmony_ci Curl_llist_remove(list, list->tail, user); 13913498266Sopenharmony_ci } 14013498266Sopenharmony_ci} 14113498266Sopenharmony_ci 14213498266Sopenharmony_cisize_t 14313498266Sopenharmony_ciCurl_llist_count(struct Curl_llist *list) 14413498266Sopenharmony_ci{ 14513498266Sopenharmony_ci return list->size; 14613498266Sopenharmony_ci} 147