| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#include "curl_setup.h" |
| 26 |
|
| 27 |
#include <curl/curl.h> |
| 28 |
|
| 29 |
#include "llist.h" |
| 30 |
#include "curl_memory.h" |
| 31 |
|
| 32 |
|
| 33 |
#include "memdebug.h" |
| 34 |
|
| 35 |
#ifdef DEBUGBUILD |
| 36 |
#define LLISTINIT 0x100cc001 |
| 37 |
#define NODEINIT 0x12344321 |
| 38 |
#define NODEREM 0x54321012 |
| 39 |
|
| 40 |
#define VERIFYNODE(x) verifynode(x) |
| 41 |
static struct Curl_llist_node *verifynode(struct Curl_llist_node *n) |
| 42 |
{ |
| 43 |
DEBUGASSERT(!n || (n->_init == NODEINIT)); |
| 44 |
return n; |
| 45 |
} |
| 46 |
#else |
| 47 |
#define VERIFYNODE(x) x |
| 48 |
#endif |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
void |
| 53 |
Curl_llist_init(struct Curl_llist *l, Curl_llist_dtor dtor) |
| 54 |
{ |
| 55 |
l->_size = 0; |
| 56 |
l->_dtor = dtor; |
| 57 |
l->_head = NULL; |
| 58 |
l->_tail = NULL; |
| 59 |
#ifdef DEBUGBUILD |
| 60 |
l->_init = LLISTINIT; |
| 61 |
#endif |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
void |
| 76 |
Curl_llist_insert_next(struct Curl_llist *list, |
| 77 |
struct Curl_llist_node *e, |
| 78 |
const void *p, |
| 79 |
struct Curl_llist_node *ne) |
| 80 |
{ |
| 81 |
DEBUGASSERT(list); |
| 82 |
DEBUGASSERT(list->_init == LLISTINIT); |
| 83 |
DEBUGASSERT(ne); |
| 84 |
|
| 85 |
#ifdef DEBUGBUILD |
| 86 |
ne->_init = NODEINIT; |
| 87 |
#endif |
| 88 |
ne->_ptr = CURL_UNCONST(p); |
| 89 |
ne->_list = list; |
| 90 |
if(list->_size == 0) { |
| 91 |
list->_head = ne; |
| 92 |
list->_head->_prev = NULL; |
| 93 |
list->_head->_next = NULL; |
| 94 |
list->_tail = ne; |
| 95 |
} |
| 96 |
else { |
| 97 |
|
| 98 |
ne->_next = e ? e->_next : list->_head; |
| 99 |
ne->_prev = e; |
| 100 |
if(!e) { |
| 101 |
list->_head->_prev = ne; |
| 102 |
list->_head = ne; |
| 103 |
} |
| 104 |
else if(e->_next) { |
| 105 |
e->_next->_prev = ne; |
| 106 |
} |
| 107 |
else { |
| 108 |
list->_tail = ne; |
| 109 |
} |
| 110 |
if(e) |
| 111 |
e->_next = ne; |
| 112 |
} |
| 113 |
|
| 114 |
++list->_size; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
void |
| 127 |
Curl_llist_append(struct Curl_llist *list, const void *p, |
| 128 |
struct Curl_llist_node *ne) |
| 129 |
{ |
| 130 |
DEBUGASSERT(list); |
| 131 |
DEBUGASSERT(list->_init == LLISTINIT); |
| 132 |
DEBUGASSERT(ne); |
| 133 |
Curl_llist_insert_next(list, list->_tail, p, ne); |
| 134 |
} |
| 135 |
|
| 136 |
void *Curl_node_take_elem(struct Curl_llist_node *e) |
| 137 |
{ |
| 138 |
void *ptr; |
| 139 |
struct Curl_llist *list; |
| 140 |
if(!e) |
| 141 |
return NULL; |
| 142 |
|
| 143 |
list = e->_list; |
| 144 |
DEBUGASSERT(list); |
| 145 |
DEBUGASSERT(list->_init == LLISTINIT); |
| 146 |
DEBUGASSERT(list->_size); |
| 147 |
DEBUGASSERT(e->_init == NODEINIT); |
| 148 |
if(list) { |
| 149 |
if(e == list->_head) { |
| 150 |
list->_head = e->_next; |
| 151 |
|
| 152 |
if(!list->_head) |
| 153 |
list->_tail = NULL; |
| 154 |
else |
| 155 |
e->_next->_prev = NULL; |
| 156 |
} |
| 157 |
else { |
| 158 |
if(e->_prev) |
| 159 |
e->_prev->_next = e->_next; |
| 160 |
|
| 161 |
if(!e->_next) |
| 162 |
list->_tail = e->_prev; |
| 163 |
else |
| 164 |
e->_next->_prev = e->_prev; |
| 165 |
} |
| 166 |
--list->_size; |
| 167 |
} |
| 168 |
ptr = e->_ptr; |
| 169 |
|
| 170 |
e->_list = NULL; |
| 171 |
e->_ptr = NULL; |
| 172 |
e->_prev = NULL; |
| 173 |
e->_next = NULL; |
| 174 |
#ifdef DEBUGBUILD |
| 175 |
e->_init = NODEREM; |
| 176 |
#endif |
| 177 |
|
| 178 |
return ptr; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
void |
| 185 |
Curl_node_uremove(struct Curl_llist_node *e, void *user) |
| 186 |
{ |
| 187 |
struct Curl_llist *list; |
| 188 |
void *ptr; |
| 189 |
if(!e) |
| 190 |
return; |
| 191 |
|
| 192 |
list = e->_list; |
| 193 |
DEBUGASSERT(list); |
| 194 |
if(list) { |
| 195 |
ptr = Curl_node_take_elem(e); |
| 196 |
if(list->_dtor) |
| 197 |
list->_dtor(user, ptr); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
void Curl_node_remove(struct Curl_llist_node *e) |
| 202 |
{ |
| 203 |
Curl_node_uremove(e, NULL); |
| 204 |
} |
| 205 |
|
| 206 |
void |
| 207 |
Curl_llist_destroy(struct Curl_llist *list, void *user) |
| 208 |
{ |
| 209 |
if(list) { |
| 210 |
DEBUGASSERT(list->_init == LLISTINIT); |
| 211 |
while(list->_size > 0) |
| 212 |
Curl_node_uremove(list->_tail, user); |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
struct Curl_llist_node *Curl_llist_head(struct Curl_llist *list) |
| 219 |
{ |
| 220 |
DEBUGASSERT(list); |
| 221 |
DEBUGASSERT(list->_init == LLISTINIT); |
| 222 |
return VERIFYNODE(list->_head); |
| 223 |
} |
| 224 |
|
| 225 |
#ifdef UNITTESTS |
| 226 |
|
| 227 |
|
| 228 |
struct Curl_llist_node *Curl_llist_tail(struct Curl_llist *list) |
| 229 |
{ |
| 230 |
DEBUGASSERT(list); |
| 231 |
DEBUGASSERT(list->_init == LLISTINIT); |
| 232 |
return VERIFYNODE(list->_tail); |
| 233 |
} |
| 234 |
#endif |
| 235 |
|
| 236 |
|
| 237 |
size_t Curl_llist_count(struct Curl_llist *list) |
| 238 |
{ |
| 239 |
DEBUGASSERT(list); |
| 240 |
DEBUGASSERT(list->_init == LLISTINIT); |
| 241 |
return list->_size; |
| 242 |
} |
| 243 |
|
| 244 |
|
| 245 |
void *Curl_node_elem(struct Curl_llist_node *n) |
| 246 |
{ |
| 247 |
DEBUGASSERT(n); |
| 248 |
DEBUGASSERT(n->_init == NODEINIT); |
| 249 |
return n->_ptr; |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
struct Curl_llist_node *Curl_node_next(struct Curl_llist_node *n) |
| 255 |
{ |
| 256 |
DEBUGASSERT(n); |
| 257 |
DEBUGASSERT(n->_init == NODEINIT); |
| 258 |
return VERIFYNODE(n->_next); |
| 259 |
} |
| 260 |
|
| 261 |
#ifdef UNITTESTS |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
struct Curl_llist_node *Curl_node_prev(struct Curl_llist_node *n) |
| 266 |
{ |
| 267 |
DEBUGASSERT(n); |
| 268 |
DEBUGASSERT(n->_init == NODEINIT); |
| 269 |
return VERIFYNODE(n->_prev); |
| 270 |
} |
| 271 |
|
| 272 |
#endif |
| 273 |
|
| 274 |
struct Curl_llist *Curl_node_llist(struct Curl_llist_node *n) |
| 275 |
{ |
| 276 |
DEBUGASSERT(n); |
| 277 |
DEBUGASSERT(!n->_list || n->_init == NODEINIT); |
| 278 |
return n->_list; |
| 279 |
} |