1 |
|
---|
2 | /* singularly linked-list */
|
---|
3 |
|
---|
4 | #include <stdlib.h>
|
---|
5 |
|
---|
6 | #include "globals.h"
|
---|
7 | #include "module-datastruct-llist.h"
|
---|
8 |
|
---|
9 | /*
|
---|
10 | Locking rules:
|
---|
11 |
|
---|
12 | mutex lock is needed when...
|
---|
13 | 1. l->initial + l->last is modified/accessed
|
---|
14 | 2. LL_NODE nxt modified/accessed
|
---|
15 |
|
---|
16 |
|
---|
17 | */
|
---|
18 | static void _destroy(LLIST *l)
|
---|
19 | {
|
---|
20 | if (!l) return;
|
---|
21 | if (!l->flag++) {
|
---|
22 | pthread_mutex_trylock(&l->lock);
|
---|
23 | pthread_mutex_unlock(&l->lock);
|
---|
24 | pthread_mutex_destroy(&l->lock);
|
---|
25 | add_garbage(l);
|
---|
26 | }
|
---|
27 | }
|
---|
28 |
|
---|
29 | LLIST *ll_create()
|
---|
30 | {
|
---|
31 | LLIST *l = cs_malloc(&l, sizeof(LLIST), 0);
|
---|
32 | pthread_mutex_init(&l->lock, NULL);
|
---|
33 | return l;
|
---|
34 | }
|
---|
35 |
|
---|
36 | int32_t ll_lock(LLIST *l)
|
---|
37 | {
|
---|
38 | int32_t res = 1;
|
---|
39 | while (l && !l->flag && (res=pthread_mutex_trylock(&l->lock))) {
|
---|
40 | cs_debug_mask(D_TRACE, "trylock ll_lock wait");
|
---|
41 | cs_sleepms(50);
|
---|
42 | }
|
---|
43 | return !res;
|
---|
44 | }
|
---|
45 |
|
---|
46 | void ll_unlock(LLIST *l)
|
---|
47 | {
|
---|
48 | if (l && !l->flag)
|
---|
49 | pthread_mutex_unlock(&l->lock);
|
---|
50 | }
|
---|
51 |
|
---|
52 | void ll_destroy(LLIST *l)
|
---|
53 | {
|
---|
54 | if (!l) return;
|
---|
55 | ll_clear(l);
|
---|
56 |
|
---|
57 | _destroy(l);
|
---|
58 | }
|
---|
59 |
|
---|
60 | void ll_destroy_data(LLIST *l)
|
---|
61 | {
|
---|
62 | if (!l) return;
|
---|
63 | ll_clear_data(l);
|
---|
64 |
|
---|
65 | _destroy(l);
|
---|
66 | }
|
---|
67 |
|
---|
68 | void *ll_iter_next_nolock(LL_ITER *it)
|
---|
69 | {
|
---|
70 | if (it && it->l) {
|
---|
71 | if (it->cur) {
|
---|
72 | it->prv = it->cur;
|
---|
73 | it->cur = it->cur->nxt;
|
---|
74 | } else if (it->l->initial && !it->prv)
|
---|
75 | it->cur = it->l->initial;
|
---|
76 |
|
---|
77 | if (it->cur)
|
---|
78 | return it->cur->obj;
|
---|
79 | }
|
---|
80 |
|
---|
81 | return NULL;
|
---|
82 | }
|
---|
83 |
|
---|
84 | static void ll_clear_int(LLIST *l, int32_t clear_data)
|
---|
85 | {
|
---|
86 | if (!l) return;
|
---|
87 |
|
---|
88 | if (!ll_lock(l)) return;
|
---|
89 |
|
---|
90 | LL_NODE *n=l->initial, *nxt;
|
---|
91 | while (n) {
|
---|
92 | nxt = n->nxt;
|
---|
93 | if (n && !n->flag++) {
|
---|
94 | if (clear_data)
|
---|
95 | add_garbage(n->obj);
|
---|
96 | add_garbage(n);
|
---|
97 | }
|
---|
98 | n = nxt;
|
---|
99 | }
|
---|
100 | l->count = 0;
|
---|
101 | l->initial = 0;
|
---|
102 | l->last = 0;
|
---|
103 | ll_unlock(l);
|
---|
104 | }
|
---|
105 |
|
---|
106 | void ll_clear(LLIST *l)
|
---|
107 | {
|
---|
108 | ll_clear_int(l, 0);
|
---|
109 | }
|
---|
110 |
|
---|
111 |
|
---|
112 | void ll_clear_data(LLIST *l)
|
---|
113 | {
|
---|
114 | ll_clear_int(l, 1);
|
---|
115 | }
|
---|
116 |
|
---|
117 | LL_NODE* ll_append_nolock(LLIST *l, void *obj)
|
---|
118 | {
|
---|
119 | if (l && obj) {
|
---|
120 | LL_NODE *new;
|
---|
121 | if(!cs_malloc(&new,sizeof(LL_NODE), -1)) return NULL;
|
---|
122 | new->obj = obj;
|
---|
123 |
|
---|
124 | if (l->last)
|
---|
125 | l->last->nxt = new;
|
---|
126 | else
|
---|
127 | l->initial = new;
|
---|
128 | l->last = new;
|
---|
129 |
|
---|
130 | l->count++;
|
---|
131 | return new;
|
---|
132 | }
|
---|
133 |
|
---|
134 | return NULL;
|
---|
135 | }
|
---|
136 |
|
---|
137 | LL_NODE* ll_append(LLIST *l, void *obj)
|
---|
138 | {
|
---|
139 | if (l && obj) {
|
---|
140 | if (!ll_lock(l)) return NULL;
|
---|
141 |
|
---|
142 | LL_NODE *n = ll_append_nolock(l, obj);
|
---|
143 | ll_unlock(l);
|
---|
144 | return n;
|
---|
145 | }
|
---|
146 | return NULL;
|
---|
147 | }
|
---|
148 |
|
---|
149 | LL_NODE *ll_prepend(LLIST *l, void *obj)
|
---|
150 | {
|
---|
151 | if (l && obj) {
|
---|
152 | if (!ll_lock(l)) return NULL;
|
---|
153 |
|
---|
154 | LL_NODE *new;
|
---|
155 | if(!cs_malloc(&new,sizeof(LL_NODE), -1)) return NULL;
|
---|
156 |
|
---|
157 | new->obj = obj;
|
---|
158 | new->nxt = l->initial;
|
---|
159 |
|
---|
160 | l->initial = new;
|
---|
161 | if (!l->last)
|
---|
162 | l->last = l->initial;
|
---|
163 | l->count++;
|
---|
164 | ll_unlock(l);
|
---|
165 |
|
---|
166 | return new;
|
---|
167 | }
|
---|
168 |
|
---|
169 | return NULL;
|
---|
170 | }
|
---|
171 |
|
---|
172 | LL_ITER ll_iter_create(LLIST *l)
|
---|
173 | {
|
---|
174 | LL_ITER it;
|
---|
175 | memset(&it, 0, sizeof(it));
|
---|
176 | it.l = l;
|
---|
177 | return it;
|
---|
178 | }
|
---|
179 |
|
---|
180 |
|
---|
181 | void *ll_iter_next(LL_ITER *it)
|
---|
182 | {
|
---|
183 | if (it && it->l) {
|
---|
184 | if (!ll_lock(it->l)) return NULL;
|
---|
185 | void *res = ll_iter_next_nolock(it);
|
---|
186 | ll_unlock(it->l);
|
---|
187 | return res;
|
---|
188 | }
|
---|
189 | return NULL;
|
---|
190 | }
|
---|
191 |
|
---|
192 | void *ll_iter_move(LL_ITER *it, int32_t offset)
|
---|
193 | {
|
---|
194 | if (it && it->l) {
|
---|
195 | if (!ll_lock(it->l)) return NULL;
|
---|
196 | int32_t i;
|
---|
197 | void *res = NULL;
|
---|
198 | for (i=0; i<offset; i++) {
|
---|
199 | res = ll_iter_next_nolock(it);
|
---|
200 | if (!res) break;
|
---|
201 | }
|
---|
202 | ll_unlock(it->l);
|
---|
203 | return res;
|
---|
204 | }
|
---|
205 | return NULL;
|
---|
206 | }
|
---|
207 |
|
---|
208 | void *ll_iter_peek(LL_ITER *it, int32_t offset)
|
---|
209 | {
|
---|
210 | if (it && it->l) {
|
---|
211 | if (!ll_lock(it->l)) return NULL;
|
---|
212 |
|
---|
213 | LL_NODE *n = it->cur;
|
---|
214 | int32_t i;
|
---|
215 |
|
---|
216 | for (i = 0; i < offset; i++) {
|
---|
217 | if (n)
|
---|
218 | n = n->nxt;
|
---|
219 | else
|
---|
220 | break;
|
---|
221 | }
|
---|
222 | ll_unlock(it->l);
|
---|
223 |
|
---|
224 | if (!n)
|
---|
225 | return NULL;
|
---|
226 | return n->obj;
|
---|
227 | }
|
---|
228 | return NULL;
|
---|
229 | }
|
---|
230 |
|
---|
231 | void ll_iter_reset(LL_ITER *it)
|
---|
232 | {
|
---|
233 | if (it) {
|
---|
234 | it->prv = NULL;
|
---|
235 | it->cur = NULL;
|
---|
236 | }
|
---|
237 | }
|
---|
238 |
|
---|
239 | void ll_iter_insert(LL_ITER *it, void *obj)
|
---|
240 | {
|
---|
241 | if (it && obj) {
|
---|
242 | if (!ll_lock(it->l)) return;
|
---|
243 |
|
---|
244 | if (!it->cur || !it->cur->nxt)
|
---|
245 | ll_append_nolock(it->l, obj);
|
---|
246 | else {
|
---|
247 | LL_NODE *n;
|
---|
248 | if(!cs_malloc(&n,sizeof(LL_NODE), -1)) return;
|
---|
249 |
|
---|
250 | n->obj = obj;
|
---|
251 | n->nxt = it->cur->nxt;
|
---|
252 | it->cur->nxt = n;
|
---|
253 |
|
---|
254 | it->l->count++;
|
---|
255 | }
|
---|
256 | ll_unlock(it->l);
|
---|
257 | }
|
---|
258 | }
|
---|
259 |
|
---|
260 | void *ll_iter_remove(LL_ITER *it)
|
---|
261 | {
|
---|
262 | void *obj = NULL;
|
---|
263 | if (it) {
|
---|
264 | if (!ll_lock(it->l)) return NULL;
|
---|
265 | LL_NODE *del = it->cur;
|
---|
266 | if (del && !del->flag++) { //preventing duplicate free because of multiple threads
|
---|
267 | obj = del->obj;
|
---|
268 | LL_NODE *prv = it->prv;
|
---|
269 |
|
---|
270 | if (prv)
|
---|
271 | prv->nxt = del->nxt;
|
---|
272 | else
|
---|
273 | it->l->initial = del->nxt;
|
---|
274 | if (!it->l->initial)
|
---|
275 | it->l->last = NULL;
|
---|
276 | else if (del == it->l->last)
|
---|
277 | it->l->last = prv;
|
---|
278 |
|
---|
279 | it->l->count--;
|
---|
280 | ll_iter_reset(it);
|
---|
281 | while (prv && ll_iter_next_nolock(it))
|
---|
282 | if (it->cur == prv)
|
---|
283 | break;
|
---|
284 |
|
---|
285 | add_garbage(del);
|
---|
286 | }
|
---|
287 | ll_unlock(it->l);
|
---|
288 | }
|
---|
289 |
|
---|
290 | return obj;
|
---|
291 | }
|
---|
292 |
|
---|
293 | int32_t ll_iter_move_first(LL_ITER *it)
|
---|
294 | {
|
---|
295 | int32_t moved = 0;
|
---|
296 | if (it) {
|
---|
297 | if (!ll_lock(it->l)) return moved;
|
---|
298 |
|
---|
299 | LL_NODE *move = it->cur;
|
---|
300 | if (move && !move->flag++) { //preventing duplicate free because of multiple threads
|
---|
301 | LL_NODE *prv = it->prv;
|
---|
302 |
|
---|
303 | if (prv)
|
---|
304 | prv->nxt = move->nxt;
|
---|
305 | else
|
---|
306 | it->l->initial = move->nxt;
|
---|
307 |
|
---|
308 | if (prv && it->l->last == move)
|
---|
309 | it->l->last = prv;
|
---|
310 | move->nxt = it->l->initial;
|
---|
311 | it->l->initial = move;
|
---|
312 | moved = 1;
|
---|
313 |
|
---|
314 | ll_iter_reset(it);
|
---|
315 | }
|
---|
316 | ll_unlock(it->l);
|
---|
317 | }
|
---|
318 | return moved;
|
---|
319 | }
|
---|
320 |
|
---|
321 | void ll_iter_remove_data(LL_ITER *it)
|
---|
322 | {
|
---|
323 | void *obj = ll_iter_remove(it);
|
---|
324 | add_garbage(obj);
|
---|
325 | }
|
---|
326 |
|
---|
327 | int32_t ll_count(LLIST *l)
|
---|
328 | {
|
---|
329 | if (!l)
|
---|
330 | return 0;
|
---|
331 |
|
---|
332 | return l->count;
|
---|
333 | }
|
---|
334 |
|
---|
335 | void *ll_has_elements(LLIST *l) {
|
---|
336 | if (!l || !l->initial)
|
---|
337 | return NULL;
|
---|
338 | return l->initial->obj;
|
---|
339 | }
|
---|
340 |
|
---|
341 | int32_t ll_contains(LLIST *l, void *obj)
|
---|
342 | {
|
---|
343 | if (!l || !obj)
|
---|
344 | return 0;
|
---|
345 | LL_ITER it = ll_iter_create(l);
|
---|
346 | void *data;
|
---|
347 | while ((data=ll_iter_next(&it))) {
|
---|
348 | if (data==obj)
|
---|
349 | break;
|
---|
350 | }
|
---|
351 | return (data==obj);
|
---|
352 | }
|
---|
353 |
|
---|
354 | void *ll_contains_data(LLIST *l, void *obj, uint32_t size)
|
---|
355 | {
|
---|
356 | if (!l || !obj)
|
---|
357 | return NULL;
|
---|
358 | LL_ITER it = ll_iter_create(l);
|
---|
359 | void *data;
|
---|
360 | while ((data=ll_iter_next(&it))) {
|
---|
361 | if (!memcmp(data,obj,size))
|
---|
362 | break;
|
---|
363 | }
|
---|
364 | return data;
|
---|
365 | }
|
---|
366 |
|
---|
367 | int32_t ll_remove(LLIST *l, void *obj)
|
---|
368 | {
|
---|
369 | int32_t n = 0;
|
---|
370 | LL_ITER it = ll_iter_create(l);
|
---|
371 | void *data;
|
---|
372 | while ((data=ll_iter_next(&it))) {
|
---|
373 | if (data==obj) {
|
---|
374 | ll_iter_remove(&it);
|
---|
375 | n++;
|
---|
376 | }
|
---|
377 | }
|
---|
378 | return n;
|
---|
379 | }
|
---|
380 |
|
---|
381 | void ll_remove_data(LLIST *l, void *obj)
|
---|
382 | {
|
---|
383 | LL_ITER it = ll_iter_create(l);
|
---|
384 | void *data;
|
---|
385 | while ((data=ll_iter_next(&it))) {
|
---|
386 | if (data==obj)
|
---|
387 | ll_iter_remove_data(&it);
|
---|
388 | }
|
---|
389 | }
|
---|
390 |
|
---|
391 | // removes all elements from l where elements are in elements_to_remove
|
---|
392 | int32_t ll_remove_all(LLIST *l, LLIST *elements_to_remove)
|
---|
393 | {
|
---|
394 | int32_t count = 0;
|
---|
395 | LL_ITER it1 = ll_iter_create(l);
|
---|
396 | LL_ITER it2 = ll_iter_create(elements_to_remove);
|
---|
397 |
|
---|
398 | void *data1, *data2;
|
---|
399 | while ((data1=ll_iter_next(&it1))) {
|
---|
400 | ll_iter_reset(&it2);
|
---|
401 | while ((data2=ll_iter_next(&it2))) {
|
---|
402 | if (data1 == data2) {
|
---|
403 | ll_iter_remove(&it1);
|
---|
404 | count++;
|
---|
405 | break;
|
---|
406 | }
|
---|
407 | }
|
---|
408 | }
|
---|
409 |
|
---|
410 | return count;
|
---|
411 | }
|
---|
412 |
|
---|
413 | void ll_sort(LLIST *l, void *compare)
|
---|
414 | {
|
---|
415 | if (!l || !l->initial || !compare) return;
|
---|
416 |
|
---|
417 | if (!ll_lock(l)) return;
|
---|
418 |
|
---|
419 | //Because this list has no prv pointer, we can not do qsort, so
|
---|
420 | //copy to a flat array, sort them, and then copy back
|
---|
421 | void **p = cs_malloc(&p, l->count*sizeof(p[0]), 0);
|
---|
422 | LL_NODE *n = l->initial;
|
---|
423 | int32_t i=0;
|
---|
424 | while (n) {
|
---|
425 | p[i++] = n->obj;
|
---|
426 | n=n->nxt;
|
---|
427 | }
|
---|
428 |
|
---|
429 | cs_debug_mask(D_TRACE, "sort: count %d size %d", l->count, sizeof(p[0]));
|
---|
430 |
|
---|
431 | qsort(p, l->count, sizeof(p[0]), compare);
|
---|
432 |
|
---|
433 | n = l->initial;
|
---|
434 | i = 0;
|
---|
435 | while (n) {
|
---|
436 | n->obj = p[i++];
|
---|
437 | n=n->nxt;
|
---|
438 | }
|
---|
439 |
|
---|
440 | free(p);
|
---|
441 |
|
---|
442 | ll_unlock(l);
|
---|
443 | }
|
---|