llist.c
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                  _   _ ____  _
00003  *  Project                     ___| | | |  _ \| |
00004  *                             / __| | | | |_) | |
00005  *                            | (__| |_| |  _ <| |___
00006  *                             \___|\___/|_| \_\_____|
00007  *
00008  * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
00009  *
00010  * This software is licensed as described in the file COPYING, which
00011  * you should have received as part of this distribution. The terms
00012  * are also available at https://curl.haxx.se/docs/copyright.html.
00013  *
00014  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
00015  * copies of the Software, and permit persons to whom the Software is
00016  * furnished to do so, under the terms of the COPYING file.
00017  *
00018  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
00019  * KIND, either express or implied.
00020  *
00021  ***************************************************************************/
00022 
00023 #include "curl_setup.h"
00024 
00025 #include <curl/curl.h>
00026 
00027 #include "llist.h"
00028 #include "curl_memory.h"
00029 
00030 /* this must be the last include file */
00031 #include "memdebug.h"
00032 
00033 /*
00034  * @unittest: 1300
00035  */
00036 static void
00037 llist_init(struct curl_llist *l, curl_llist_dtor dtor)
00038 {
00039   l->size = 0;
00040   l->dtor = dtor;
00041   l->head = NULL;
00042   l->tail = NULL;
00043 }
00044 
00045 struct curl_llist *
00046 Curl_llist_alloc(curl_llist_dtor dtor)
00047 {
00048   struct curl_llist *list;
00049 
00050   list = malloc(sizeof(struct curl_llist));
00051   if(!list)
00052     return NULL;
00053 
00054   llist_init(list, dtor);
00055 
00056   return list;
00057 }
00058 
00059 /*
00060  * Curl_llist_insert_next()
00061  *
00062  * Inserts a new list element after the given one 'e'. If the given existing
00063  * entry is NULL and the list already has elements, the new one will be
00064  * inserted first in the list.
00065  *
00066  * Returns: 1 on success and 0 on failure.
00067  *
00068  * @unittest: 1300
00069  */
00070 int
00071 Curl_llist_insert_next(struct curl_llist *list, struct curl_llist_element *e,
00072                        const void *p)
00073 {
00074   struct curl_llist_element *ne = malloc(sizeof(struct curl_llist_element));
00075   if(!ne)
00076     return 0;
00077 
00078   ne->ptr = (void *) p;
00079   if(list->size == 0) {
00080     list->head = ne;
00081     list->head->prev = NULL;
00082     list->head->next = NULL;
00083     list->tail = ne;
00084   }
00085   else {
00086     /* if 'e' is NULL here, we insert the new element first in the list */
00087     ne->next = e?e->next:list->head;
00088     ne->prev = e;
00089     if(!e) {
00090       list->head->prev = ne;
00091       list->head = ne;
00092     }
00093     else if(e->next) {
00094       e->next->prev = ne;
00095     }
00096     else {
00097       list->tail = ne;
00098     }
00099     if(e)
00100       e->next = ne;
00101   }
00102 
00103   ++list->size;
00104 
00105   return 1;
00106 }
00107 
00108 /*
00109  * @unittest: 1300
00110  */
00111 int
00112 Curl_llist_remove(struct curl_llist *list, struct curl_llist_element *e,
00113                   void *user)
00114 {
00115   if(e == NULL || list->size == 0)
00116     return 1;
00117 
00118   if(e == list->head) {
00119     list->head = e->next;
00120 
00121     if(list->head == NULL)
00122       list->tail = NULL;
00123     else
00124       e->next->prev = NULL;
00125   }
00126   else {
00127     e->prev->next = e->next;
00128     if(!e->next)
00129       list->tail = e->prev;
00130     else
00131       e->next->prev = e->prev;
00132   }
00133 
00134   list->dtor(user, e->ptr);
00135 
00136   e->ptr  = NULL;
00137   e->prev = NULL;
00138   e->next = NULL;
00139 
00140   free(e);
00141   --list->size;
00142 
00143   return 1;
00144 }
00145 
00146 void
00147 Curl_llist_destroy(struct curl_llist *list, void *user)
00148 {
00149   if(list) {
00150     while(list->size > 0)
00151       Curl_llist_remove(list, list->tail, user);
00152 
00153     free(list);
00154   }
00155 }
00156 
00157 size_t
00158 Curl_llist_count(struct curl_llist *list)
00159 {
00160   return list->size;
00161 }
00162 
00163 /*
00164  * @unittest: 1300
00165  */
00166 int Curl_llist_move(struct curl_llist *list, struct curl_llist_element *e,
00167                     struct curl_llist *to_list,
00168                     struct curl_llist_element *to_e)
00169 {
00170   /* Remove element from list */
00171   if(e == NULL || list->size == 0)
00172     return 0;
00173 
00174   if(e == list->head) {
00175     list->head = e->next;
00176 
00177     if(list->head == NULL)
00178       list->tail = NULL;
00179     else
00180       e->next->prev = NULL;
00181   }
00182   else {
00183     e->prev->next = e->next;
00184     if(!e->next)
00185       list->tail = e->prev;
00186     else
00187       e->next->prev = e->prev;
00188   }
00189 
00190   --list->size;
00191 
00192   /* Add element to to_list after to_e */
00193   if(to_list->size == 0) {
00194     to_list->head = e;
00195     to_list->head->prev = NULL;
00196     to_list->head->next = NULL;
00197     to_list->tail = e;
00198   }
00199   else {
00200     e->next = to_e->next;
00201     e->prev = to_e;
00202     if(to_e->next) {
00203       to_e->next->prev = e;
00204     }
00205     else {
00206       to_list->tail = e;
00207     }
00208     to_e->next = e;
00209   }
00210 
00211   ++to_list->size;
00212 
00213   return 1;
00214 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:05