Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00031 #include "memdebug.h"
00032
00033
00034
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
00061
00062
00063
00064
00065
00066
00067
00068
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
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
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
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
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
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 }