00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "setup.h"
00025
00026 #include <string.h>
00027 #include <stdlib.h>
00028
00029 #include "llist.h"
00030 #include "memory.h"
00031
00032
00033 #include "memdebug.h"
00034
00035 void
00036 Curl_llist_init(struct curl_llist *l, curl_llist_dtor dtor)
00037 {
00038 l->size = 0;
00039 l->dtor = dtor;
00040 l->head = NULL;
00041 l->tail = NULL;
00042 }
00043
00044 struct curl_llist *
00045 Curl_llist_alloc(curl_llist_dtor dtor)
00046 {
00047 struct curl_llist *list;
00048
00049 list = (struct curl_llist *)malloc(sizeof(struct curl_llist));
00050 if(NULL == list)
00051 return NULL;
00052
00053 Curl_llist_init(list, dtor);
00054
00055 return list;
00056 }
00057
00058
00059
00060
00061 int
00062 Curl_llist_insert_next(struct curl_llist *list, struct curl_llist_element *e,
00063 const void *p)
00064 {
00065 struct curl_llist_element *ne =
00066 (struct curl_llist_element *) malloc(sizeof(struct curl_llist_element));
00067 if(!ne)
00068 return 0;
00069
00070 ne->ptr = (void *) p;
00071 if (list->size == 0) {
00072 list->head = ne;
00073 list->head->prev = NULL;
00074 list->head->next = NULL;
00075 list->tail = ne;
00076 }
00077 else {
00078 ne->next = e->next;
00079 ne->prev = e;
00080 if (e->next) {
00081 e->next->prev = ne;
00082 }
00083 else {
00084 list->tail = ne;
00085 }
00086 e->next = ne;
00087 }
00088
00089 ++list->size;
00090
00091 return 1;
00092 }
00093
00094 int
00095 Curl_llist_remove(struct curl_llist *list, struct curl_llist_element *e,
00096 void *user)
00097 {
00098 if (e == NULL || list->size == 0)
00099 return 1;
00100
00101 if (e == list->head) {
00102 list->head = e->next;
00103
00104 if (list->head == NULL)
00105 list->tail = NULL;
00106 else
00107 e->next->prev = NULL;
00108 } else {
00109 e->prev->next = e->next;
00110 if (!e->next)
00111 list->tail = e->prev;
00112 else
00113 e->next->prev = e->prev;
00114 }
00115
00116 list->dtor(user, e->ptr);
00117 free(e);
00118 --list->size;
00119
00120 return 1;
00121 }
00122
00123 void
00124 Curl_llist_destroy(struct curl_llist *list, void *user)
00125 {
00126 if(list) {
00127 while (list->size > 0)
00128 Curl_llist_remove(list, list->tail, user);
00129
00130 free(list);
00131 }
00132 }
00133
00134 size_t
00135 Curl_llist_count(struct curl_llist *list)
00136 {
00137 return list->size;
00138 }