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 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 | int ll_lock(LLIST *l)
|
---|
37 | {
|
---|
38 | int 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, int clear_data)
|
---|
85 | {
|
---|
86 | if (!l) return;
|
---|
87 |
|
---|
88 | if (!ll_lock(l)) return;
|
---|
89 |
|
---|
90 | LL_ITER *it = ll_iter_create(l);
|
---|
91 | while (ll_iter_next_nolock(it)) {
|
---|
92 | if (it->cur && !it->cur->flag++) {
|
---|
93 | if (clear_data)
|
---|
94 | add_garbage(it->cur->obj);
|
---|
95 | add_garbage(it->cur);
|
---|
96 | }
|
---|
97 | }
|
---|
98 | ll_iter_release(it);
|
---|
99 | l->count = 0;
|
---|
100 | l->initial = 0;
|
---|
101 | ll_unlock(l);
|
---|
102 | }
|
---|
103 |
|
---|
104 | void ll_clear(LLIST *l)
|
---|
105 | {
|
---|
106 | ll_clear_int(l, 0);
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | void ll_clear_data(LLIST *l)
|
---|
111 | {
|
---|
112 | ll_clear_int(l, 1);
|
---|
113 | }
|
---|
114 |
|
---|
115 | LL_NODE* ll_append_nolock(LLIST *l, void *obj)
|
---|
116 | {
|
---|
117 | if (l && obj) {
|
---|
118 | LL_NODE *new = calloc(1, sizeof(LL_NODE));
|
---|
119 | LL_NODE *n = l->initial;
|
---|
120 |
|
---|
121 | new->obj = obj;
|
---|
122 |
|
---|
123 | if (n) {
|
---|
124 | while (n->nxt) n = n->nxt;
|
---|
125 | n->nxt = new;
|
---|
126 | } else
|
---|
127 | l->initial = new;
|
---|
128 |
|
---|
129 | l->count++;
|
---|
130 | return new;
|
---|
131 | }
|
---|
132 |
|
---|
133 | return NULL;
|
---|
134 | }
|
---|
135 |
|
---|
136 | LL_NODE* ll_append(LLIST *l, void *obj)
|
---|
137 | {
|
---|
138 | if (l && obj) {
|
---|
139 | if (!ll_lock(l)) return NULL;
|
---|
140 |
|
---|
141 | LL_NODE *n = ll_append_nolock(l, obj);
|
---|
142 | ll_unlock(l);
|
---|
143 | return n;
|
---|
144 | }
|
---|
145 | return NULL;
|
---|
146 | }
|
---|
147 |
|
---|
148 | LL_NODE *ll_prepend(LLIST *l, void *obj)
|
---|
149 | {
|
---|
150 | if (l && obj) {
|
---|
151 | if (!ll_lock(l)) return NULL;
|
---|
152 |
|
---|
153 | LL_NODE *new = calloc(1, sizeof(LL_NODE));
|
---|
154 |
|
---|
155 | new->obj = obj;
|
---|
156 | new->nxt = l->initial;
|
---|
157 |
|
---|
158 | l->initial = new;
|
---|
159 | l->count++;
|
---|
160 | ll_unlock(l);
|
---|
161 |
|
---|
162 | return new;
|
---|
163 | }
|
---|
164 |
|
---|
165 | return NULL;
|
---|
166 | }
|
---|
167 |
|
---|
168 | LL_ITER *ll_iter_create(LLIST *l)
|
---|
169 | {
|
---|
170 | if (!l) return NULL;
|
---|
171 | LL_ITER *it;
|
---|
172 | struct s_client *cl = cur_client();
|
---|
173 | if(cl && !cl->itused){
|
---|
174 | it = &(cl->it);
|
---|
175 | it->prv = NULL;
|
---|
176 | it->cur = NULL;
|
---|
177 | cl->itused = 1;
|
---|
178 | } else
|
---|
179 | if(!cs_malloc(&it, sizeof(LL_ITER), -1)) return NULL;
|
---|
180 | it->l = l;
|
---|
181 | return it;
|
---|
182 | }
|
---|
183 |
|
---|
184 | LL_ITER *ll_iter_create_s(LLIST *l, LL_ITER *it)
|
---|
185 | {
|
---|
186 | if (!l) return NULL;
|
---|
187 | it->l = l;
|
---|
188 | return it;
|
---|
189 | }
|
---|
190 |
|
---|
191 | void ll_iter_release(LL_ITER *it)
|
---|
192 | {
|
---|
193 | struct s_client *cl = cur_client();
|
---|
194 | if(cl && cl->itused && it == &(cl->it)){
|
---|
195 | cl->itused = 0;
|
---|
196 | // We don't need add_garbage here as iterators aren't shared across threads
|
---|
197 | } else free(it);
|
---|
198 | }
|
---|
199 |
|
---|
200 | void *ll_iter_next(LL_ITER *it)
|
---|
201 | {
|
---|
202 | if (it && it->l) {
|
---|
203 | if (!ll_lock(it->l)) return NULL;
|
---|
204 | void *res = ll_iter_next_nolock(it);
|
---|
205 | ll_unlock(it->l);
|
---|
206 | return res;
|
---|
207 | }
|
---|
208 | return NULL;
|
---|
209 | }
|
---|
210 |
|
---|
211 | void *ll_iter_move(LL_ITER *it, int32_t offset)
|
---|
212 | {
|
---|
213 | if (it && it->l) {
|
---|
214 | if (!ll_lock(it->l)) return NULL;
|
---|
215 | int32_t i;
|
---|
216 | void *res = NULL;
|
---|
217 | for (i=0; i<offset; i++) {
|
---|
218 | res = ll_iter_next_nolock(it);
|
---|
219 | if (!res) break;
|
---|
220 | }
|
---|
221 | ll_unlock(it->l);
|
---|
222 | return res;
|
---|
223 | }
|
---|
224 | return NULL;
|
---|
225 | }
|
---|
226 |
|
---|
227 | void *ll_iter_peek(LL_ITER *it, int32_t offset)
|
---|
228 | {
|
---|
229 | if (it && it->l) {
|
---|
230 | if (!ll_lock(it->l)) return NULL;
|
---|
231 |
|
---|
232 | LL_NODE *n = it->cur;
|
---|
233 | int32_t i;
|
---|
234 |
|
---|
235 | for (i = 0; i < offset; i++) {
|
---|
236 | if (n)
|
---|
237 | n = n->nxt;
|
---|
238 | else
|
---|
239 | break;
|
---|
240 | }
|
---|
241 | ll_unlock(it->l);
|
---|
242 |
|
---|
243 | if (!n)
|
---|
244 | return NULL;
|
---|
245 | return n->obj;
|
---|
246 | }
|
---|
247 | return NULL;
|
---|
248 | }
|
---|
249 |
|
---|
250 | void ll_iter_reset(LL_ITER *it)
|
---|
251 | {
|
---|
252 | if (it) {
|
---|
253 | it->prv = NULL;
|
---|
254 | it->cur = NULL;
|
---|
255 | }
|
---|
256 | }
|
---|
257 |
|
---|
258 | void ll_iter_insert(LL_ITER *it, void *obj)
|
---|
259 | {
|
---|
260 | if (it && obj) {
|
---|
261 | if (!ll_lock(it->l)) return;
|
---|
262 |
|
---|
263 | if (!it->cur || !it->cur->nxt)
|
---|
264 | ll_append_nolock(it->l, obj);
|
---|
265 | else {
|
---|
266 | LL_NODE *n = calloc(1, sizeof(LL_NODE));
|
---|
267 |
|
---|
268 | n->obj = obj;
|
---|
269 | n->nxt = it->cur->nxt;
|
---|
270 | it->cur->nxt = n;
|
---|
271 |
|
---|
272 | it->l->count++;
|
---|
273 | }
|
---|
274 | ll_unlock(it->l);
|
---|
275 | }
|
---|
276 | }
|
---|
277 |
|
---|
278 | void *ll_iter_remove(LL_ITER *it)
|
---|
279 | {
|
---|
280 | void *obj = NULL;
|
---|
281 | if (it) {
|
---|
282 | if (!ll_lock(it->l)) return NULL;
|
---|
283 | LL_NODE *del = it->cur;
|
---|
284 | if (del && !del->flag++) { //preventing duplicate free because of multiple threads
|
---|
285 | obj = del->obj;
|
---|
286 | LL_NODE *prv = it->prv;
|
---|
287 |
|
---|
288 | if (prv)
|
---|
289 | prv->nxt = del->nxt;
|
---|
290 | else
|
---|
291 | it->l->initial = del->nxt;
|
---|
292 | it->l->count--;
|
---|
293 |
|
---|
294 | ll_iter_reset(it);
|
---|
295 | while (prv && ll_iter_next_nolock(it))
|
---|
296 | if (it->cur == prv)
|
---|
297 | break;
|
---|
298 |
|
---|
299 | add_garbage(del);
|
---|
300 | }
|
---|
301 | ll_unlock(it->l);
|
---|
302 | }
|
---|
303 |
|
---|
304 | return obj;
|
---|
305 | }
|
---|
306 |
|
---|
307 | int ll_iter_move_first(LL_ITER *it)
|
---|
308 | {
|
---|
309 | int moved = 0;
|
---|
310 | if (it) {
|
---|
311 | if (!ll_lock(it->l)) return moved;
|
---|
312 |
|
---|
313 | LL_NODE *move = it->cur;
|
---|
314 | if (move && !move->flag++) { //preventing duplicate free because of multiple threads
|
---|
315 | LL_NODE *prv = it->prv;
|
---|
316 |
|
---|
317 | if (prv)
|
---|
318 | prv->nxt = move->nxt;
|
---|
319 | else
|
---|
320 | it->l->initial = move->nxt;
|
---|
321 |
|
---|
322 | move->nxt = it->l->initial;
|
---|
323 | it->l->initial = move;
|
---|
324 | moved = 1;
|
---|
325 |
|
---|
326 | ll_iter_reset(it);
|
---|
327 | }
|
---|
328 | ll_unlock(it->l);
|
---|
329 | }
|
---|
330 | return moved;
|
---|
331 | }
|
---|
332 |
|
---|
333 | void ll_iter_remove_data(LL_ITER *it)
|
---|
334 | {
|
---|
335 | void *obj = ll_iter_remove(it);
|
---|
336 | add_garbage(obj);
|
---|
337 | }
|
---|
338 |
|
---|
339 | int32_t ll_count(LLIST *l)
|
---|
340 | {
|
---|
341 | if (!l)
|
---|
342 | return 0;
|
---|
343 |
|
---|
344 | return l->count;
|
---|
345 | }
|
---|
346 |
|
---|
347 | void *ll_has_elements(LLIST *l) {
|
---|
348 | if (!l || !l->initial)
|
---|
349 | return NULL;
|
---|
350 | return l->initial->obj;
|
---|
351 | }
|
---|
352 |
|
---|
353 | int32_t ll_contains(LLIST *l, void *obj)
|
---|
354 | {
|
---|
355 | if (!l || !obj)
|
---|
356 | return 0;
|
---|
357 | LL_ITER *it = ll_iter_create(l);
|
---|
358 | void *data;
|
---|
359 | while ((data=ll_iter_next(it))) {
|
---|
360 | if (data==obj)
|
---|
361 | break;
|
---|
362 | }
|
---|
363 | ll_iter_release(it);
|
---|
364 | return (data==obj);
|
---|
365 | }
|
---|
366 |
|
---|
367 | int32_t ll_remove(LLIST *l, void *obj)
|
---|
368 | {
|
---|
369 | int 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 | ll_iter_release(it);
|
---|
379 | return n;
|
---|
380 | }
|
---|
381 |
|
---|
382 | void ll_remove_data(LLIST *l, void *obj)
|
---|
383 | {
|
---|
384 | LL_ITER *it = ll_iter_create(l);
|
---|
385 | void *data;
|
---|
386 | while ((data=ll_iter_next(it))) {
|
---|
387 | if (data==obj)
|
---|
388 | ll_iter_remove_data(it);
|
---|
389 | }
|
---|
390 | ll_iter_release(it);
|
---|
391 | }
|
---|
392 |
|
---|
393 | // removes all elements from l where elements are in elements_to_remove
|
---|
394 | int32_t ll_remove_all(LLIST *l, LLIST *elements_to_remove)
|
---|
395 | {
|
---|
396 | int32_t count = 0;
|
---|
397 | LL_ITER *it1 = ll_iter_create(l);
|
---|
398 | LL_ITER *it2 = ll_iter_create(elements_to_remove);
|
---|
399 |
|
---|
400 | void *data1, *data2;
|
---|
401 | while ((data1=ll_iter_next(it1))) {
|
---|
402 | ll_iter_reset(it2);
|
---|
403 | while ((data2=ll_iter_next(it2))) {
|
---|
404 | if (data1 == data2) {
|
---|
405 | ll_iter_remove(it1);
|
---|
406 | count++;
|
---|
407 | break;
|
---|
408 | }
|
---|
409 | }
|
---|
410 | }
|
---|
411 |
|
---|
412 | ll_iter_release(it2);
|
---|
413 | ll_iter_release(it1);
|
---|
414 |
|
---|
415 | return count;
|
---|
416 | }
|
---|