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_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 | ll_unlock(l);
|
---|
103 | }
|
---|
104 |
|
---|
105 | void ll_clear(LLIST *l)
|
---|
106 | {
|
---|
107 | ll_clear_int(l, 0);
|
---|
108 | }
|
---|
109 |
|
---|
110 |
|
---|
111 | void ll_clear_data(LLIST *l)
|
---|
112 | {
|
---|
113 | ll_clear_int(l, 1);
|
---|
114 | }
|
---|
115 |
|
---|
116 | LL_NODE* ll_append_nolock(LLIST *l, void *obj)
|
---|
117 | {
|
---|
118 | if (l && obj) {
|
---|
119 | LL_NODE *new = calloc(1, sizeof(LL_NODE));
|
---|
120 | LL_NODE *n = l->initial;
|
---|
121 |
|
---|
122 | new->obj = obj;
|
---|
123 |
|
---|
124 | if (n) {
|
---|
125 | while (n->nxt) n = n->nxt;
|
---|
126 | n->nxt = new;
|
---|
127 | } else
|
---|
128 | l->initial = 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 = calloc(1, sizeof(LL_NODE));
|
---|
155 |
|
---|
156 | new->obj = obj;
|
---|
157 | new->nxt = l->initial;
|
---|
158 |
|
---|
159 | l->initial = new;
|
---|
160 | l->count++;
|
---|
161 | ll_unlock(l);
|
---|
162 |
|
---|
163 | return new;
|
---|
164 | }
|
---|
165 |
|
---|
166 | return NULL;
|
---|
167 | }
|
---|
168 |
|
---|
169 | LL_ITER ll_iter_create(LLIST *l)
|
---|
170 | {
|
---|
171 | LL_ITER it;
|
---|
172 | memset(&it, 0, sizeof(it));
|
---|
173 | it.l = l;
|
---|
174 | return it;
|
---|
175 | }
|
---|
176 |
|
---|
177 |
|
---|
178 | void *ll_iter_next(LL_ITER *it)
|
---|
179 | {
|
---|
180 | if (it && it->l) {
|
---|
181 | if (!ll_lock(it->l)) return NULL;
|
---|
182 | void *res = ll_iter_next_nolock(it);
|
---|
183 | ll_unlock(it->l);
|
---|
184 | return res;
|
---|
185 | }
|
---|
186 | return NULL;
|
---|
187 | }
|
---|
188 |
|
---|
189 | void *ll_iter_move(LL_ITER *it, int32_t offset)
|
---|
190 | {
|
---|
191 | if (it && it->l) {
|
---|
192 | if (!ll_lock(it->l)) return NULL;
|
---|
193 | int32_t i;
|
---|
194 | void *res = NULL;
|
---|
195 | for (i=0; i<offset; i++) {
|
---|
196 | res = ll_iter_next_nolock(it);
|
---|
197 | if (!res) break;
|
---|
198 | }
|
---|
199 | ll_unlock(it->l);
|
---|
200 | return res;
|
---|
201 | }
|
---|
202 | return NULL;
|
---|
203 | }
|
---|
204 |
|
---|
205 | void *ll_iter_peek(LL_ITER *it, int32_t offset)
|
---|
206 | {
|
---|
207 | if (it && it->l) {
|
---|
208 | if (!ll_lock(it->l)) return NULL;
|
---|
209 |
|
---|
210 | LL_NODE *n = it->cur;
|
---|
211 | int32_t i;
|
---|
212 |
|
---|
213 | for (i = 0; i < offset; i++) {
|
---|
214 | if (n)
|
---|
215 | n = n->nxt;
|
---|
216 | else
|
---|
217 | break;
|
---|
218 | }
|
---|
219 | ll_unlock(it->l);
|
---|
220 |
|
---|
221 | if (!n)
|
---|
222 | return NULL;
|
---|
223 | return n->obj;
|
---|
224 | }
|
---|
225 | return NULL;
|
---|
226 | }
|
---|
227 |
|
---|
228 | void ll_iter_reset(LL_ITER *it)
|
---|
229 | {
|
---|
230 | if (it) {
|
---|
231 | it->prv = NULL;
|
---|
232 | it->cur = NULL;
|
---|
233 | }
|
---|
234 | }
|
---|
235 |
|
---|
236 | void ll_iter_insert(LL_ITER *it, void *obj)
|
---|
237 | {
|
---|
238 | if (it && obj) {
|
---|
239 | if (!ll_lock(it->l)) return;
|
---|
240 |
|
---|
241 | if (!it->cur || !it->cur->nxt)
|
---|
242 | ll_append_nolock(it->l, obj);
|
---|
243 | else {
|
---|
244 | LL_NODE *n = calloc(1, sizeof(LL_NODE));
|
---|
245 |
|
---|
246 | n->obj = obj;
|
---|
247 | n->nxt = it->cur->nxt;
|
---|
248 | it->cur->nxt = n;
|
---|
249 |
|
---|
250 | it->l->count++;
|
---|
251 | }
|
---|
252 | ll_unlock(it->l);
|
---|
253 | }
|
---|
254 | }
|
---|
255 |
|
---|
256 | void *ll_iter_remove(LL_ITER *it)
|
---|
257 | {
|
---|
258 | void *obj = NULL;
|
---|
259 | if (it) {
|
---|
260 | if (!ll_lock(it->l)) return NULL;
|
---|
261 | LL_NODE *del = it->cur;
|
---|
262 | if (del && !del->flag++) { //preventing duplicate free because of multiple threads
|
---|
263 | obj = del->obj;
|
---|
264 | LL_NODE *prv = it->prv;
|
---|
265 |
|
---|
266 | if (prv)
|
---|
267 | prv->nxt = del->nxt;
|
---|
268 | else
|
---|
269 | it->l->initial = del->nxt;
|
---|
270 | it->l->count--;
|
---|
271 |
|
---|
272 | ll_iter_reset(it);
|
---|
273 | while (prv && ll_iter_next_nolock(it))
|
---|
274 | if (it->cur == prv)
|
---|
275 | break;
|
---|
276 |
|
---|
277 | add_garbage(del);
|
---|
278 | }
|
---|
279 | ll_unlock(it->l);
|
---|
280 | }
|
---|
281 |
|
---|
282 | return obj;
|
---|
283 | }
|
---|
284 |
|
---|
285 | int ll_iter_move_first(LL_ITER *it)
|
---|
286 | {
|
---|
287 | int moved = 0;
|
---|
288 | if (it) {
|
---|
289 | if (!ll_lock(it->l)) return moved;
|
---|
290 |
|
---|
291 | LL_NODE *move = it->cur;
|
---|
292 | if (move && !move->flag++) { //preventing duplicate free because of multiple threads
|
---|
293 | LL_NODE *prv = it->prv;
|
---|
294 |
|
---|
295 | if (prv)
|
---|
296 | prv->nxt = move->nxt;
|
---|
297 | else
|
---|
298 | it->l->initial = move->nxt;
|
---|
299 |
|
---|
300 | move->nxt = it->l->initial;
|
---|
301 | it->l->initial = move;
|
---|
302 | moved = 1;
|
---|
303 |
|
---|
304 | ll_iter_reset(it);
|
---|
305 | }
|
---|
306 | ll_unlock(it->l);
|
---|
307 | }
|
---|
308 | return moved;
|
---|
309 | }
|
---|
310 |
|
---|
311 | void ll_iter_remove_data(LL_ITER *it)
|
---|
312 | {
|
---|
313 | void *obj = ll_iter_remove(it);
|
---|
314 | add_garbage(obj);
|
---|
315 | }
|
---|
316 |
|
---|
317 | int32_t ll_count(LLIST *l)
|
---|
318 | {
|
---|
319 | if (!l)
|
---|
320 | return 0;
|
---|
321 |
|
---|
322 | return l->count;
|
---|
323 | }
|
---|
324 |
|
---|
325 | void *ll_has_elements(LLIST *l) {
|
---|
326 | if (!l || !l->initial)
|
---|
327 | return NULL;
|
---|
328 | return l->initial->obj;
|
---|
329 | }
|
---|
330 |
|
---|
331 | int32_t ll_contains(LLIST *l, void *obj)
|
---|
332 | {
|
---|
333 | if (!l || !obj)
|
---|
334 | return 0;
|
---|
335 | LL_ITER it = ll_iter_create(l);
|
---|
336 | void *data;
|
---|
337 | while ((data=ll_iter_next(&it))) {
|
---|
338 | if (data==obj)
|
---|
339 | break;
|
---|
340 | }
|
---|
341 | return (data==obj);
|
---|
342 | }
|
---|
343 |
|
---|
344 | int32_t ll_remove(LLIST *l, void *obj)
|
---|
345 | {
|
---|
346 | int n = 0;
|
---|
347 | LL_ITER it = ll_iter_create(l);
|
---|
348 | void *data;
|
---|
349 | while ((data=ll_iter_next(&it))) {
|
---|
350 | if (data==obj) {
|
---|
351 | ll_iter_remove(&it);
|
---|
352 | n++;
|
---|
353 | }
|
---|
354 | }
|
---|
355 | return n;
|
---|
356 | }
|
---|
357 |
|
---|
358 | void ll_remove_data(LLIST *l, void *obj)
|
---|
359 | {
|
---|
360 | LL_ITER it = ll_iter_create(l);
|
---|
361 | void *data;
|
---|
362 | while ((data=ll_iter_next(&it))) {
|
---|
363 | if (data==obj)
|
---|
364 | ll_iter_remove_data(&it);
|
---|
365 | }
|
---|
366 | }
|
---|
367 |
|
---|
368 | // removes all elements from l where elements are in elements_to_remove
|
---|
369 | int32_t ll_remove_all(LLIST *l, LLIST *elements_to_remove)
|
---|
370 | {
|
---|
371 | int32_t count = 0;
|
---|
372 | LL_ITER it1 = ll_iter_create(l);
|
---|
373 | LL_ITER it2 = ll_iter_create(elements_to_remove);
|
---|
374 |
|
---|
375 | void *data1, *data2;
|
---|
376 | while ((data1=ll_iter_next(&it1))) {
|
---|
377 | ll_iter_reset(&it2);
|
---|
378 | while ((data2=ll_iter_next(&it2))) {
|
---|
379 | if (data1 == data2) {
|
---|
380 | count++;
|
---|
381 | break;
|
---|
382 | }
|
---|
383 | }
|
---|
384 | }
|
---|
385 |
|
---|
386 | return count;
|
---|
387 | }
|
---|