1 |
|
---|
2 | /* singularly linked-list */
|
---|
3 |
|
---|
4 | #include "globals.h"
|
---|
5 | #include "oscam-garbage.h"
|
---|
6 | #include "oscam-lock.h"
|
---|
7 | #include "oscam-string.h"
|
---|
8 |
|
---|
9 | extern char *LOG_LIST;
|
---|
10 |
|
---|
11 | /*
|
---|
12 | Locking rules:
|
---|
13 |
|
---|
14 | mutex lock is needed when...
|
---|
15 | 1. l->initial + l->last is modified/accessed
|
---|
16 | 2. LL_NODE nxt modified/accessed
|
---|
17 |
|
---|
18 |
|
---|
19 | */
|
---|
20 |
|
---|
21 | #ifdef WITH_DEBUG
|
---|
22 | static int8_t chk_debuglog(LLIST *l)
|
---|
23 | {
|
---|
24 | return (l && l->lock.name != LOG_LIST);
|
---|
25 | }
|
---|
26 | #endif
|
---|
27 |
|
---|
28 | static void _destroy(LLIST *l)
|
---|
29 | {
|
---|
30 | if (!l) return;
|
---|
31 | if (!l->flag++) {
|
---|
32 | cs_writelock(&l->lock); //just getting sure noone is using it
|
---|
33 | cs_writeunlock(&l->lock);
|
---|
34 |
|
---|
35 | cs_lock_destroy(&l->lock);
|
---|
36 | add_garbage(l);
|
---|
37 | }
|
---|
38 | }
|
---|
39 |
|
---|
40 | LLIST *ll_create(const char *name)
|
---|
41 | {
|
---|
42 | LLIST *l;
|
---|
43 | if (!cs_malloc(&l, sizeof(LLIST)))
|
---|
44 | return NULL;
|
---|
45 | cs_lock_create(&l->lock, 5, name);
|
---|
46 | return l;
|
---|
47 | }
|
---|
48 |
|
---|
49 | void ll_destroy(LLIST *l)
|
---|
50 | {
|
---|
51 | if (!l || l->flag) return;
|
---|
52 | ll_clear(l);
|
---|
53 |
|
---|
54 | _destroy(l);
|
---|
55 | }
|
---|
56 |
|
---|
57 | void ll_destroy_data(LLIST *l)
|
---|
58 | {
|
---|
59 | if (!l) return;
|
---|
60 | ll_clear_data(l);
|
---|
61 |
|
---|
62 | _destroy(l);
|
---|
63 | }
|
---|
64 |
|
---|
65 | /* Internal iteration function. Make sure that you don't have a lock and that it and it->l are set. */
|
---|
66 | static void *ll_iter_next_nolock(LL_ITER *it)
|
---|
67 | {
|
---|
68 | if (it->l->version != it->ll_version) {
|
---|
69 | #ifdef WITH_DEBUG
|
---|
70 | if (chk_debuglog(it->l))
|
---|
71 | cs_debug_mask_nolock(D_TRACE, "list changed, searching new position");
|
---|
72 | #endif
|
---|
73 |
|
---|
74 | LL_NODE *ptr;
|
---|
75 | //cs_readlock(&it->l->lock);
|
---|
76 | if (!it->cur && !it->prv) {
|
---|
77 | it->cur = it->l->initial;
|
---|
78 | } else {
|
---|
79 | for (ptr = it->l->initial; ptr; ptr = ptr->nxt) {
|
---|
80 | if (ptr == it->cur) {
|
---|
81 | it->prv = ptr;
|
---|
82 | it->cur = ptr->nxt;
|
---|
83 | break;
|
---|
84 | }
|
---|
85 | }
|
---|
86 | if (!ptr) {
|
---|
87 | ll_iter_reset(it); // restart iteration
|
---|
88 | it->cur = it->l->initial;
|
---|
89 | }
|
---|
90 | }
|
---|
91 | it->ll_version = it->l->version;
|
---|
92 | //cs_readunlock(&it->l->lock);
|
---|
93 |
|
---|
94 | if (it->cur)
|
---|
95 | return it->cur->obj;
|
---|
96 |
|
---|
97 | } else {
|
---|
98 | if (it->cur) {
|
---|
99 | it->prv = it->cur;
|
---|
100 | it->cur = it->cur->nxt;
|
---|
101 | } else if (it->l->initial && !it->prv)
|
---|
102 | it->cur = it->l->initial;
|
---|
103 |
|
---|
104 | if (it->cur)
|
---|
105 | return it->cur->obj;
|
---|
106 | }
|
---|
107 | return NULL;
|
---|
108 | }
|
---|
109 |
|
---|
110 | static void ll_clear_int(LLIST *l, int32_t clear_data)
|
---|
111 | {
|
---|
112 | if (!l||l->flag) return;
|
---|
113 |
|
---|
114 | cs_writelock(&l->lock);
|
---|
115 |
|
---|
116 | LL_NODE *n=l->initial, *nxt;
|
---|
117 | while (n) {
|
---|
118 | nxt = n->nxt;
|
---|
119 | if (clear_data)
|
---|
120 | add_garbage(n->obj);
|
---|
121 | add_garbage(n);
|
---|
122 | n = nxt;
|
---|
123 | }
|
---|
124 | l->version++;
|
---|
125 | l->count = 0;
|
---|
126 | l->initial = 0;
|
---|
127 | l->last = 0;
|
---|
128 | cs_writeunlock(&l->lock);
|
---|
129 | }
|
---|
130 |
|
---|
131 | void ll_clear(LLIST *l)
|
---|
132 | {
|
---|
133 | ll_clear_int(l, 0);
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 | void ll_clear_data(LLIST *l)
|
---|
138 | {
|
---|
139 | ll_clear_int(l, 1);
|
---|
140 | }
|
---|
141 |
|
---|
142 | /* Appends to the list. Do not call this from outside without having a lock! */
|
---|
143 | static LL_NODE* ll_append_nolock(LLIST *l, void *obj)
|
---|
144 | {
|
---|
145 | if (l && obj && !l->flag) {
|
---|
146 | LL_NODE *new;
|
---|
147 | if (!cs_malloc(&new, sizeof(LL_NODE)))
|
---|
148 | return NULL;
|
---|
149 | new->obj = obj;
|
---|
150 |
|
---|
151 | if (l->last)
|
---|
152 | l->last->nxt = new;
|
---|
153 | else
|
---|
154 | l->initial = new;
|
---|
155 | l->last = new;
|
---|
156 |
|
---|
157 | l->count++;
|
---|
158 | return new;
|
---|
159 | }
|
---|
160 |
|
---|
161 | return NULL;
|
---|
162 | }
|
---|
163 |
|
---|
164 | LL_NODE* ll_append(LLIST *l, void *obj)
|
---|
165 | {
|
---|
166 | if (l && obj && !l->flag) {
|
---|
167 | cs_writelock(&l->lock);
|
---|
168 |
|
---|
169 | LL_NODE *n = ll_append_nolock(l, obj);
|
---|
170 | cs_writeunlock(&l->lock);
|
---|
171 | return n;
|
---|
172 | }
|
---|
173 | return NULL;
|
---|
174 | }
|
---|
175 |
|
---|
176 | LL_NODE *ll_prepend(LLIST *l, void *obj)
|
---|
177 | {
|
---|
178 | if (l && obj && !l->flag) {
|
---|
179 | LL_NODE *new;
|
---|
180 | if (!cs_malloc(&new, sizeof(LL_NODE)))
|
---|
181 | return NULL;
|
---|
182 | new->obj = obj;
|
---|
183 |
|
---|
184 | cs_writelock(&l->lock);
|
---|
185 |
|
---|
186 | new->nxt = l->initial;
|
---|
187 |
|
---|
188 | l->initial = new;
|
---|
189 | if (!l->last)
|
---|
190 | l->last = l->initial;
|
---|
191 | l->count++;
|
---|
192 | cs_writeunlock(&l->lock);
|
---|
193 |
|
---|
194 | return new;
|
---|
195 | }
|
---|
196 |
|
---|
197 | return NULL;
|
---|
198 | }
|
---|
199 |
|
---|
200 | LL_ITER ll_iter_create(LLIST *l)
|
---|
201 | {
|
---|
202 | LL_ITER it;
|
---|
203 | memset(&it, 0, sizeof(it));
|
---|
204 | it.l = l;
|
---|
205 | if (it.l)
|
---|
206 | it.ll_version = it.l->version;
|
---|
207 | return it;
|
---|
208 | }
|
---|
209 |
|
---|
210 |
|
---|
211 | void *ll_iter_next(LL_ITER *it)
|
---|
212 | {
|
---|
213 | if (it && it->l && !it->l->flag) {
|
---|
214 | cs_readlock(&it->l->lock);
|
---|
215 | void *res = ll_iter_next_nolock(it);
|
---|
216 | cs_readunlock(&it->l->lock);
|
---|
217 | return res;
|
---|
218 | }
|
---|
219 | return NULL;
|
---|
220 | }
|
---|
221 |
|
---|
222 | void *ll_iter_remove_nolock(LL_ITER *it)
|
---|
223 | {
|
---|
224 | void *obj = NULL;
|
---|
225 | if (it) {
|
---|
226 | LL_NODE *del = it->cur;
|
---|
227 | if (del) {
|
---|
228 | obj = del->obj;
|
---|
229 | LL_NODE *prv = it->prv;
|
---|
230 | if (it->ll_version != it->l->version || !prv) { // List has been modified so it->prv might be wrong!
|
---|
231 | LL_NODE *n = it->l->initial;
|
---|
232 | prv = NULL;
|
---|
233 | while (n && n != del) {
|
---|
234 | prv = n;
|
---|
235 | n = n->nxt;
|
---|
236 | }
|
---|
237 | if (n != del)
|
---|
238 | return NULL;
|
---|
239 | }
|
---|
240 |
|
---|
241 | if (prv)
|
---|
242 | prv->nxt = del->nxt;
|
---|
243 | else
|
---|
244 | it->l->initial = del->nxt;
|
---|
245 | if (!it->l->initial)
|
---|
246 | it->l->last = NULL;
|
---|
247 | else if (del == it->l->last)
|
---|
248 | it->l->last = prv;
|
---|
249 |
|
---|
250 | it->cur = it->l->initial;
|
---|
251 | it->prv = NULL;
|
---|
252 | if (prv != NULL) {
|
---|
253 | while (it->cur && it->cur != prv) {
|
---|
254 | it->prv = it->cur;
|
---|
255 | it->cur = it->cur->nxt;
|
---|
256 | }
|
---|
257 | } else
|
---|
258 | it->cur = NULL;
|
---|
259 | it->l->count--;
|
---|
260 | it->ll_version = ++it->l->version;
|
---|
261 |
|
---|
262 | add_garbage(del);
|
---|
263 | }
|
---|
264 | }
|
---|
265 | return obj;
|
---|
266 | }
|
---|
267 |
|
---|
268 | void *ll_iter_next_remove(LL_ITER *it)
|
---|
269 | {
|
---|
270 | if (it && it->l && !it->l->flag) {
|
---|
271 | cs_writelock(&it->l->lock);
|
---|
272 | void *res = ll_iter_next_nolock(it);
|
---|
273 | ll_iter_remove_nolock(it);
|
---|
274 | cs_writeunlock(&it->l->lock);
|
---|
275 | return res;
|
---|
276 | }
|
---|
277 | return NULL;
|
---|
278 | }
|
---|
279 |
|
---|
280 | void *ll_iter_move(LL_ITER *it, int32_t offset)
|
---|
281 | {
|
---|
282 | if (it && it->l && !it->l->flag) {
|
---|
283 | int32_t i;
|
---|
284 | void *res = NULL;
|
---|
285 | for (i=0; i<offset; i++) {
|
---|
286 | res = ll_iter_next_nolock(it);
|
---|
287 | if (!res) break;
|
---|
288 | }
|
---|
289 |
|
---|
290 | return res;
|
---|
291 | }
|
---|
292 | return NULL;
|
---|
293 | }
|
---|
294 |
|
---|
295 | void *ll_iter_peek(const LL_ITER *it, int32_t offset)
|
---|
296 | {
|
---|
297 | if (it && it->l && !it->l->flag) {
|
---|
298 | cs_readlock(&((LL_ITER*)it)->l->lock);
|
---|
299 |
|
---|
300 | LL_NODE *n = it->cur;
|
---|
301 | int32_t i;
|
---|
302 |
|
---|
303 | for (i = 0; i < offset; i++) {
|
---|
304 | if (n)
|
---|
305 | n = n->nxt;
|
---|
306 | else
|
---|
307 | break;
|
---|
308 | }
|
---|
309 | cs_readunlock(&((LL_ITER*)it)->l->lock);
|
---|
310 |
|
---|
311 | if (!n)
|
---|
312 | return NULL;
|
---|
313 | return n->obj;
|
---|
314 | }
|
---|
315 | return NULL;
|
---|
316 | }
|
---|
317 |
|
---|
318 | void ll_iter_reset(LL_ITER *it)
|
---|
319 | {
|
---|
320 | if (it) {
|
---|
321 | it->prv = NULL;
|
---|
322 | it->cur = NULL;
|
---|
323 | }
|
---|
324 | }
|
---|
325 |
|
---|
326 | void ll_iter_insert(LL_ITER *it, void *obj)
|
---|
327 | {
|
---|
328 | if (it && obj && !it->l->flag) {
|
---|
329 | cs_writelock(&it->l->lock);
|
---|
330 |
|
---|
331 | if (!it->cur || !it->cur->nxt)
|
---|
332 | ll_append_nolock(it->l, obj);
|
---|
333 | else {
|
---|
334 | LL_NODE *n;
|
---|
335 | if (!cs_malloc(&n, sizeof(LL_NODE))) {
|
---|
336 | cs_writeunlock(&it->l->lock);
|
---|
337 | return;
|
---|
338 | }
|
---|
339 |
|
---|
340 | n->obj = obj;
|
---|
341 | n->nxt = it->cur->nxt;
|
---|
342 | it->cur->nxt = n;
|
---|
343 |
|
---|
344 | it->l->count++;
|
---|
345 | it->ll_version = ++it->l->version;
|
---|
346 | }
|
---|
347 | cs_writeunlock(&it->l->lock);
|
---|
348 | }
|
---|
349 | }
|
---|
350 |
|
---|
351 | /* Removes the element to which the iterator currently points. */
|
---|
352 | void *ll_iter_remove(LL_ITER *it)
|
---|
353 | {
|
---|
354 | void *obj = NULL;
|
---|
355 | if (it && it->l && !it->l->flag) {
|
---|
356 | LL_NODE *del = it->cur;
|
---|
357 | if (del) {
|
---|
358 | cs_writelock(&it->l->lock);
|
---|
359 | obj = ll_iter_remove_nolock(it);
|
---|
360 | cs_writeunlock(&it->l->lock);
|
---|
361 | }
|
---|
362 | }
|
---|
363 |
|
---|
364 | return obj;
|
---|
365 | }
|
---|
366 |
|
---|
367 | /* Moves the element which is currently pointed to by the iterator to the head of the list.*/
|
---|
368 | int32_t ll_iter_move_first(LL_ITER *it)
|
---|
369 | {
|
---|
370 | int32_t moved = 0;
|
---|
371 | if (it && it->l && !it->l->flag) {
|
---|
372 | LL_NODE *move = it->cur;
|
---|
373 | if (move) {
|
---|
374 | if (move == it->l->initial) //Can't move self to first
|
---|
375 | return 1;
|
---|
376 |
|
---|
377 | LL_NODE *prv = it->prv;
|
---|
378 | cs_writelock(&it->l->lock);
|
---|
379 | if(it->ll_version != it->l->version || !prv){ // List has been modified so it->prv might be wrong!
|
---|
380 | LL_NODE *n = it->l->initial;
|
---|
381 | prv = NULL;
|
---|
382 | while(n && n != move){
|
---|
383 | prv = n;
|
---|
384 | n = n->nxt;
|
---|
385 | }
|
---|
386 | if(n != move) {
|
---|
387 | cs_writeunlock(&it->l->lock);
|
---|
388 | return moved;
|
---|
389 | }
|
---|
390 | }
|
---|
391 |
|
---|
392 | if (prv)
|
---|
393 | prv->nxt = move->nxt;
|
---|
394 | else
|
---|
395 | it->l->initial = move->nxt;
|
---|
396 |
|
---|
397 | if (prv && it->l->last == move)
|
---|
398 | it->l->last = prv;
|
---|
399 | move->nxt = it->l->initial;
|
---|
400 | it->l->initial = move;
|
---|
401 |
|
---|
402 | it->ll_version = ++it->l->version;
|
---|
403 | it->prv = NULL;
|
---|
404 | cs_writeunlock(&it->l->lock);
|
---|
405 | moved = 1;
|
---|
406 | }
|
---|
407 | }
|
---|
408 | return moved;
|
---|
409 | }
|
---|
410 |
|
---|
411 | void ll_iter_remove_data(LL_ITER *it)
|
---|
412 | {
|
---|
413 | void *obj = ll_iter_remove(it);
|
---|
414 | add_garbage(obj);
|
---|
415 | }
|
---|
416 |
|
---|
417 | void *ll_has_elements(const LLIST *l) {
|
---|
418 | if (!l || !l->initial || l->flag)
|
---|
419 | return NULL;
|
---|
420 | return l->initial->obj;
|
---|
421 | }
|
---|
422 |
|
---|
423 | void *ll_last_element(const LLIST *l) {
|
---|
424 | if (!l || !l->last || l->flag)
|
---|
425 | return NULL;
|
---|
426 | return l->last->obj;
|
---|
427 | }
|
---|
428 |
|
---|
429 | int32_t ll_contains(const LLIST *l, const void *obj)
|
---|
430 | {
|
---|
431 | if (!l || !obj || l->flag)
|
---|
432 | return 0;
|
---|
433 | LL_ITER it = ll_iter_create((LLIST *) l);
|
---|
434 | const void *data;
|
---|
435 | while ((data=ll_iter_next(&it))) {
|
---|
436 | if (data==obj)
|
---|
437 | break;
|
---|
438 | }
|
---|
439 | return (data==obj);
|
---|
440 | }
|
---|
441 |
|
---|
442 | const void *ll_contains_data(const LLIST *l, const void *obj, uint32_t size) {
|
---|
443 | if (!l || !obj || l->flag)
|
---|
444 | return NULL;
|
---|
445 | LL_ITER it = ll_iter_create((LLIST*) l);
|
---|
446 | const void *data;
|
---|
447 | while ((data=ll_iter_next(&it))) {
|
---|
448 | if (!memcmp(data,obj,size))
|
---|
449 | break;
|
---|
450 | }
|
---|
451 | return data;
|
---|
452 | }
|
---|
453 |
|
---|
454 | int32_t ll_remove(LLIST *l, const void *obj)
|
---|
455 | {
|
---|
456 | int32_t n = 0;
|
---|
457 | LL_ITER it = ll_iter_create(l);
|
---|
458 | void *data;
|
---|
459 | while ((data=ll_iter_next(&it))) {
|
---|
460 | if (data==obj) {
|
---|
461 | ll_iter_remove(&it);
|
---|
462 | n++;
|
---|
463 | }
|
---|
464 | }
|
---|
465 | return n;
|
---|
466 | }
|
---|
467 |
|
---|
468 | void ll_remove_data(LLIST *l, void *obj)
|
---|
469 | {
|
---|
470 | LL_ITER it = ll_iter_create(l);
|
---|
471 | void *data;
|
---|
472 | while ((data=ll_iter_next(&it))) {
|
---|
473 | if (data==obj)
|
---|
474 | ll_iter_remove_data(&it);
|
---|
475 | }
|
---|
476 | }
|
---|
477 |
|
---|
478 | // removes all elements from l where elements are in elements_to_remove
|
---|
479 | int32_t ll_remove_all(LLIST *l, const LLIST *elements_to_remove)
|
---|
480 | {
|
---|
481 | int32_t count = 0;
|
---|
482 | LL_ITER it1 = ll_iter_create(l);
|
---|
483 | LL_ITER it2 = ll_iter_create((LLIST*) elements_to_remove);
|
---|
484 |
|
---|
485 | const void *data1, *data2;
|
---|
486 | while ((data1=ll_iter_next(&it1))) {
|
---|
487 | ll_iter_reset(&it2);
|
---|
488 | while ((data2=ll_iter_next(&it2))) {
|
---|
489 | if (data1 == data2) {
|
---|
490 | ll_iter_remove(&it1);
|
---|
491 | count++;
|
---|
492 | break;
|
---|
493 | }
|
---|
494 | }
|
---|
495 | }
|
---|
496 |
|
---|
497 | return count;
|
---|
498 | }
|
---|
499 |
|
---|
500 | /* Returns an array with all elements sorted, the amount of elements is stored in size. We do not sort the original linked list
|
---|
501 | as this might harm running iterations. Furthermore, we need the array anyway for qsort() to work. Remember to free() the result. */
|
---|
502 | void **ll_sort(const LLIST *l, void *compare, int32_t *size)
|
---|
503 | {
|
---|
504 | if (!l || !l->initial || !compare){
|
---|
505 | *size = 0;
|
---|
506 | return NULL;
|
---|
507 | }
|
---|
508 | int32_t i=0;
|
---|
509 | LL_NODE *n;
|
---|
510 |
|
---|
511 | cs_readlock(&((LLIST*)l)->lock);
|
---|
512 | *size = l->count;
|
---|
513 | void **p;
|
---|
514 | if (!cs_malloc(&p, l->count * sizeof(p[0]))) {
|
---|
515 | cs_readunlock(&((LLIST*)l)->lock);
|
---|
516 | return NULL;
|
---|
517 | }
|
---|
518 | for (n = l->initial; n; n = n->nxt) {
|
---|
519 | p[i++] = n->obj;
|
---|
520 | }
|
---|
521 | cs_readunlock(&((LLIST*)l)->lock);
|
---|
522 | #ifdef WITH_DEBUG
|
---|
523 | // if (chk_debugLog(it->l))
|
---|
524 | //cs_debug_mask(D_TRACE, "sort: count %d size %d", l->count, sizeof(p[0]));
|
---|
525 | #endif
|
---|
526 | qsort(p, l->count, sizeof(p[0]), compare);
|
---|
527 |
|
---|
528 | return p;
|
---|
529 | }
|
---|
530 |
|
---|
531 | void ll_putall(LLIST *dest, LLIST *src)
|
---|
532 | {
|
---|
533 | LL_ITER it = ll_iter_create(src);
|
---|
534 | void *data;
|
---|
535 | while ((data=ll_iter_next(&it))) {
|
---|
536 | ll_append(dest, data);
|
---|
537 | }
|
---|
538 | }
|
---|
539 |
|
---|
540 | //New Iterator:
|
---|
541 | LL_LOCKITER *ll_li_create(LLIST *l, int32_t writelock)
|
---|
542 | {
|
---|
543 | if (!l||l->flag) return NULL;
|
---|
544 |
|
---|
545 | LL_LOCKITER *li;
|
---|
546 | if (!cs_malloc(&li, sizeof(LL_LOCKITER)))
|
---|
547 | return NULL;
|
---|
548 |
|
---|
549 | li->l = l;
|
---|
550 | li->writelock = writelock;
|
---|
551 | if (writelock)
|
---|
552 | cs_writelock(&l->lock);
|
---|
553 | else
|
---|
554 | cs_readlock(&l->lock);
|
---|
555 | li->it = ll_iter_create(l);
|
---|
556 | return li;
|
---|
557 | }
|
---|
558 |
|
---|
559 | void ll_li_destroy(LL_LOCKITER *li)
|
---|
560 | {
|
---|
561 | if (li && li->l) {
|
---|
562 | if (li->writelock)
|
---|
563 | cs_writeunlock(&li->l->lock);
|
---|
564 | else
|
---|
565 | cs_readunlock(&li->l->lock);
|
---|
566 | li->l = NULL;
|
---|
567 | add_garbage(li);
|
---|
568 | }
|
---|
569 | }
|
---|
570 |
|
---|
571 | void *ll_li_next(LL_LOCKITER *li)
|
---|
572 | {
|
---|
573 | if (li && li->l) {
|
---|
574 | return ll_iter_next_nolock(&li->it);
|
---|
575 | }
|
---|
576 | return NULL;
|
---|
577 | }
|
---|
578 |
|
---|
579 | LLIST *ll_clone(LLIST *l, uint32_t copysize)
|
---|
580 | {
|
---|
581 | if (!l||l->flag) return NULL;
|
---|
582 |
|
---|
583 | LLIST *cloned = ll_create(l->lock.name);
|
---|
584 | LL_LOCKITER *li = ll_li_create(l, 0);
|
---|
585 | void *data;
|
---|
586 | while ((data=ll_li_next(li))) {
|
---|
587 | void *new_data;
|
---|
588 | if (!cs_malloc(&new_data, copysize))
|
---|
589 | break;
|
---|
590 | memcpy(new_data, data, copysize);
|
---|
591 | ll_append_nolock(cloned, new_data);
|
---|
592 | }
|
---|
593 | ll_li_destroy(li);
|
---|
594 | return cloned;
|
---|
595 | }
|
---|
596 |
|
---|
597 | void *ll_remove_first(LLIST *l) {
|
---|
598 | if (l && !l->flag) {
|
---|
599 | LL_ITER it = ll_iter_create(l);
|
---|
600 | void *data = ll_iter_next(&it);
|
---|
601 | if (data) ll_iter_remove(&it);
|
---|
602 | return data;
|
---|
603 | }
|
---|
604 | return NULL;
|
---|
605 | }
|
---|
606 |
|
---|
607 | void ll_remove_first_data(LLIST *l) {
|
---|
608 | void *data = ll_remove_first(l);
|
---|
609 | if (data) free(data);
|
---|
610 | }
|
---|