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