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_destroy(&l->lock);
|
---|
23 | add_garbage(l);
|
---|
24 | }
|
---|
25 | }
|
---|
26 |
|
---|
27 | LLIST *ll_create()
|
---|
28 | {
|
---|
29 | LLIST *l = calloc(1, sizeof(LLIST));
|
---|
30 | pthread_mutex_init(&l->lock, NULL);
|
---|
31 | return l;
|
---|
32 | }
|
---|
33 |
|
---|
34 | void ll_lock(LLIST *l)
|
---|
35 | {
|
---|
36 | if (l && !l->flag)
|
---|
37 | pthread_mutex_lock(&l->lock);
|
---|
38 | }
|
---|
39 |
|
---|
40 | void ll_unlock(LLIST *l)
|
---|
41 | {
|
---|
42 | if (l && !l->flag)
|
---|
43 | pthread_mutex_unlock(&l->lock);
|
---|
44 | }
|
---|
45 |
|
---|
46 | void ll_destroy(LLIST *l)
|
---|
47 | {
|
---|
48 | if (!l) return;
|
---|
49 | ll_clear(l);
|
---|
50 |
|
---|
51 | _destroy(l);
|
---|
52 | }
|
---|
53 |
|
---|
54 | void ll_destroy_data(LLIST *l)
|
---|
55 | {
|
---|
56 | if (!l) return;
|
---|
57 | ll_clear_data(l);
|
---|
58 |
|
---|
59 | _destroy(l);
|
---|
60 | }
|
---|
61 |
|
---|
62 | void *ll_iter_next_nolock(LL_ITER *it)
|
---|
63 | {
|
---|
64 | if (it && it->l) {
|
---|
65 | if (it->cur) {
|
---|
66 | it->prv = it->cur;
|
---|
67 | it->cur = it->cur->nxt;
|
---|
68 | } else if (it->l->initial && !it->prv)
|
---|
69 | it->cur = it->l->initial;
|
---|
70 |
|
---|
71 | if (it->cur)
|
---|
72 | return it->cur->obj;
|
---|
73 | }
|
---|
74 |
|
---|
75 | return NULL;
|
---|
76 | }
|
---|
77 |
|
---|
78 | static void ll_clear_int(LLIST *l, int clear_data)
|
---|
79 | {
|
---|
80 | if (!l) return;
|
---|
81 |
|
---|
82 | ll_lock(l);
|
---|
83 | LL_ITER *it = ll_iter_create(l);
|
---|
84 | while (ll_iter_next_nolock(it)) {
|
---|
85 | if (it->cur && !it->cur->flag++) {
|
---|
86 | if (clear_data)
|
---|
87 | add_garbage(it->cur->obj);
|
---|
88 | add_garbage(it->cur);
|
---|
89 | }
|
---|
90 | }
|
---|
91 | ll_iter_release(it);
|
---|
92 | l->count = 0;
|
---|
93 | l->initial = 0;
|
---|
94 | ll_unlock(l);
|
---|
95 | }
|
---|
96 |
|
---|
97 | void ll_clear(LLIST *l)
|
---|
98 | {
|
---|
99 | ll_clear_int(l, 0);
|
---|
100 | }
|
---|
101 |
|
---|
102 |
|
---|
103 | void ll_clear_data(LLIST *l)
|
---|
104 | {
|
---|
105 | ll_clear_int(l, 1);
|
---|
106 | }
|
---|
107 |
|
---|
108 | LL_NODE* ll_append_nolock(LLIST *l, void *obj)
|
---|
109 | {
|
---|
110 | if (l && obj) {
|
---|
111 | LL_NODE *new = calloc(1, sizeof(LL_NODE));
|
---|
112 | LL_NODE *n = l->initial;
|
---|
113 |
|
---|
114 | new->obj = obj;
|
---|
115 |
|
---|
116 | if (n) {
|
---|
117 | while (n->nxt) n = n->nxt;
|
---|
118 | n->nxt = new;
|
---|
119 | } else
|
---|
120 | l->initial = new;
|
---|
121 |
|
---|
122 | l->count++;
|
---|
123 | return new;
|
---|
124 | }
|
---|
125 |
|
---|
126 | return NULL;
|
---|
127 | }
|
---|
128 |
|
---|
129 | LL_NODE* ll_append(LLIST *l, void *obj)
|
---|
130 | {
|
---|
131 | if (l && obj) {
|
---|
132 | ll_lock(l);
|
---|
133 | LL_NODE *n = ll_append_nolock(l, obj);
|
---|
134 | ll_unlock(l);
|
---|
135 | return n;
|
---|
136 | }
|
---|
137 | return NULL;
|
---|
138 | }
|
---|
139 |
|
---|
140 | LL_NODE *ll_prepend(LLIST *l, void *obj)
|
---|
141 | {
|
---|
142 | if (l && obj) {
|
---|
143 | LL_NODE *new = calloc(1, sizeof(LL_NODE));
|
---|
144 |
|
---|
145 | ll_lock(l);
|
---|
146 | new->obj = obj;
|
---|
147 | new->nxt = l->initial;
|
---|
148 |
|
---|
149 | l->initial = new;
|
---|
150 | l->count++;
|
---|
151 | ll_unlock(l);
|
---|
152 |
|
---|
153 | return new;
|
---|
154 | }
|
---|
155 |
|
---|
156 | return NULL;
|
---|
157 | }
|
---|
158 |
|
---|
159 | LL_ITER *ll_iter_create(LLIST *l)
|
---|
160 | {
|
---|
161 | if (!l) return NULL;
|
---|
162 | LL_ITER *it;
|
---|
163 | struct s_client *cl = cur_client();
|
---|
164 | if(cl && !cl->itused){
|
---|
165 | it = &(cl->it);
|
---|
166 | it->prv = NULL;
|
---|
167 | it->cur = NULL;
|
---|
168 | cl->itused = 1;
|
---|
169 | } else
|
---|
170 | if(!cs_malloc(&it, sizeof(LL_ITER), -1)) return NULL;
|
---|
171 | it->l = l;
|
---|
172 | return it;
|
---|
173 | }
|
---|
174 |
|
---|
175 | LL_ITER *ll_iter_create_s(LLIST *l, LL_ITER *it)
|
---|
176 | {
|
---|
177 | if (!l) return NULL;
|
---|
178 | it->l = l;
|
---|
179 | return it;
|
---|
180 | }
|
---|
181 |
|
---|
182 | void ll_iter_release(LL_ITER *it)
|
---|
183 | {
|
---|
184 | struct s_client *cl = cur_client();
|
---|
185 | if(cl && cl->itused && it == &(cl->it)){
|
---|
186 | cl->itused = 0;
|
---|
187 | // We don't need add_garbage here as iterators aren't shared across threads
|
---|
188 | } else free(it);
|
---|
189 | }
|
---|
190 |
|
---|
191 | void *ll_iter_next(LL_ITER *it)
|
---|
192 | {
|
---|
193 | if (it && it->l) {
|
---|
194 | ll_lock(it->l);
|
---|
195 | void *res = ll_iter_next_nolock(it);
|
---|
196 | ll_unlock(it->l);
|
---|
197 | return res;
|
---|
198 | }
|
---|
199 | return NULL;
|
---|
200 | }
|
---|
201 |
|
---|
202 | void *ll_iter_move(LL_ITER *it, int32_t offset)
|
---|
203 | {
|
---|
204 | if (it && it->l) {
|
---|
205 | ll_lock(it->l);
|
---|
206 | int32_t i;
|
---|
207 | void *res = NULL;
|
---|
208 | for (i=0; i<offset; i++) {
|
---|
209 | res = ll_iter_next_nolock(it);
|
---|
210 | if (!res) break;
|
---|
211 | }
|
---|
212 | ll_unlock(it->l);
|
---|
213 | return res;
|
---|
214 | }
|
---|
215 | return NULL;
|
---|
216 | }
|
---|
217 |
|
---|
218 | void *ll_iter_peek(LL_ITER *it, int32_t offset)
|
---|
219 | {
|
---|
220 | if (it && it->l) {
|
---|
221 | ll_lock(it->l);
|
---|
222 | LL_NODE *n = it->cur;
|
---|
223 | int32_t i;
|
---|
224 |
|
---|
225 | for (i = 0; i < offset; i++) {
|
---|
226 | if (n)
|
---|
227 | n = n->nxt;
|
---|
228 | else
|
---|
229 | break;
|
---|
230 | }
|
---|
231 | ll_unlock(it->l);
|
---|
232 |
|
---|
233 | if (!n)
|
---|
234 | return NULL;
|
---|
235 | return n->obj;
|
---|
236 | }
|
---|
237 | return NULL;
|
---|
238 | }
|
---|
239 |
|
---|
240 | void ll_iter_reset(LL_ITER *it)
|
---|
241 | {
|
---|
242 | if (it) {
|
---|
243 | it->prv = NULL;
|
---|
244 | it->cur = NULL;
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | void ll_iter_insert(LL_ITER *it, void *obj)
|
---|
249 | {
|
---|
250 | if (it && obj) {
|
---|
251 | ll_lock(it->l);
|
---|
252 | if (!it->cur || !it->cur->nxt)
|
---|
253 | ll_append_nolock(it->l, obj);
|
---|
254 | else {
|
---|
255 | LL_NODE *n = calloc(1, sizeof(LL_NODE));
|
---|
256 |
|
---|
257 | n->obj = obj;
|
---|
258 | n->nxt = it->cur->nxt;
|
---|
259 | it->cur->nxt = n;
|
---|
260 |
|
---|
261 | it->l->count++;
|
---|
262 | }
|
---|
263 | ll_unlock(it->l);
|
---|
264 | }
|
---|
265 | }
|
---|
266 |
|
---|
267 | void *ll_iter_remove(LL_ITER *it)
|
---|
268 | {
|
---|
269 | void *obj = NULL;
|
---|
270 | if (it) {
|
---|
271 | ll_lock(it->l);
|
---|
272 | LL_NODE *del = it->cur;
|
---|
273 | if (del && !del->flag++) { //preventing duplicate free because of multiple threads
|
---|
274 | obj = del->obj;
|
---|
275 | LL_NODE *prv = it->prv;
|
---|
276 |
|
---|
277 | if (prv)
|
---|
278 | prv->nxt = del->nxt;
|
---|
279 | else
|
---|
280 | it->l->initial = del->nxt;
|
---|
281 | it->l->count--;
|
---|
282 |
|
---|
283 | ll_iter_reset(it);
|
---|
284 | while (prv && ll_iter_next_nolock(it))
|
---|
285 | if (it->cur == prv)
|
---|
286 | break;
|
---|
287 |
|
---|
288 | add_garbage(del);
|
---|
289 | }
|
---|
290 | ll_unlock(it->l);
|
---|
291 | }
|
---|
292 |
|
---|
293 | return obj;
|
---|
294 | }
|
---|
295 |
|
---|
296 | int ll_iter_move_first(LL_ITER *it)
|
---|
297 | {
|
---|
298 | int moved = 0;
|
---|
299 | if (it) {
|
---|
300 | ll_lock(it->l);
|
---|
301 | LL_NODE *move = it->cur;
|
---|
302 | if (move && !move->flag++) { //preventing duplicate free because of multiple threads
|
---|
303 | LL_NODE *prv = it->prv;
|
---|
304 |
|
---|
305 | if (prv)
|
---|
306 | prv->nxt = move->nxt;
|
---|
307 | else
|
---|
308 | it->l->initial = move->nxt;
|
---|
309 |
|
---|
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 | ll_iter_release(it);
|
---|
352 | return (data==obj);
|
---|
353 | }
|
---|
354 |
|
---|
355 | int32_t ll_remove(LLIST *l, void *obj)
|
---|
356 | {
|
---|
357 | int n = 0;
|
---|
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(it);
|
---|
363 | n++;
|
---|
364 | }
|
---|
365 | }
|
---|
366 | ll_iter_release(it);
|
---|
367 | return n;
|
---|
368 | }
|
---|
369 |
|
---|
370 | void ll_remove_data(LLIST *l, void *obj)
|
---|
371 | {
|
---|
372 | LL_ITER *it = ll_iter_create(l);
|
---|
373 | void *data;
|
---|
374 | while ((data=ll_iter_next(it))) {
|
---|
375 | if (data==obj)
|
---|
376 | ll_iter_remove_data(it);
|
---|
377 | }
|
---|
378 | ll_iter_release(it);
|
---|
379 | }
|
---|
380 |
|
---|
381 | // removes all elements from l where elements are in elements_to_remove
|
---|
382 | int32_t ll_remove_all(LLIST *l, LLIST *elements_to_remove)
|
---|
383 | {
|
---|
384 | int32_t count = 0;
|
---|
385 | LL_ITER *it1 = ll_iter_create(l);
|
---|
386 | LL_ITER *it2 = ll_iter_create(elements_to_remove);
|
---|
387 |
|
---|
388 | void *data1, *data2;
|
---|
389 | while ((data1=ll_iter_next(it1))) {
|
---|
390 | ll_iter_reset(it2);
|
---|
391 | while ((data2=ll_iter_next(it2))) {
|
---|
392 | if (data1 == data2) {
|
---|
393 | ll_iter_remove(it1);
|
---|
394 | count++;
|
---|
395 | break;
|
---|
396 | }
|
---|
397 | }
|
---|
398 | }
|
---|
399 |
|
---|
400 | ll_iter_release(it2);
|
---|
401 | ll_iter_release(it1);
|
---|
402 |
|
---|
403 | return count;
|
---|
404 | }
|
---|