forked from nx111/oscam
-
Notifications
You must be signed in to change notification settings - Fork 1
/
oscam-llist.c
521 lines (444 loc) · 9.93 KB
/
oscam-llist.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
/* singularly linked-list */
#include "globals.h"
/*
Locking rules:
mutex lock is needed when...
1. l->initial + l->last is modified/accessed
2. LL_NODE nxt modified/accessed
*/
#ifdef WITH_DEBUG
static int8_t chk_debuglog(LLIST *l)
{
return (l && l->lock.name != LOG_LIST);
}
#endif
static void _destroy(LLIST *l)
{
if (!l) return;
if (!l->flag++) {
cs_lock_destroy(&l->lock);
add_garbage(l);
}
}
LLIST *ll_create(const char *name)
{
LLIST *l = cs_malloc(&l, sizeof(LLIST), 0);
cs_lock_create(&l->lock, 5, name);
return l;
}
void ll_destroy(LLIST *l)
{
if (!l) return;
ll_clear(l);
_destroy(l);
}
void ll_destroy_data(LLIST *l)
{
if (!l) return;
ll_clear_data(l);
_destroy(l);
}
/* Internal iteration function. Make sure that you don't have a lock and that it and it->l are set. */
static void *ll_iter_next_nolock(LL_ITER *it)
{
if (it->l->version != it->ll_version) {
#ifdef WITH_DEBUG
if (chk_debuglog(it->l))
cs_debug_mask_nolock(D_TRACE, "list changed, searching new position");
#endif
LL_NODE *ptr;
//cs_readlock(&it->l->lock);
if (!it->cur && !it->prv) {
it->cur = it->l->initial;
} else {
for (ptr = it->l->initial; ptr; ptr = ptr->nxt) {
if (ptr == it->cur) {
it->prv = ptr;
it->cur = ptr->nxt;
break;
}
}
if (!ptr) {
ll_iter_reset(it); // restart iteration
it->cur = it->l->initial;
}
}
it->ll_version = it->l->version;
//cs_readunlock(&it->l->lock);
if (it->cur)
return it->cur->obj;
} else {
if (it->cur) {
it->prv = it->cur;
it->cur = it->cur->nxt;
} else if (it->l->initial && !it->prv)
it->cur = it->l->initial;
if (it->cur)
return it->cur->obj;
}
return NULL;
}
static void ll_clear_int(LLIST *l, int32_t clear_data)
{
if (!l) return;
cs_writelock(&l->lock);
LL_NODE *n=l->initial, *nxt;
while (n) {
nxt = n->nxt;
if (clear_data)
add_garbage(n->obj);
add_garbage(n);
n = nxt;
}
l->version++;
l->count = 0;
l->initial = 0;
l->last = 0;
cs_writeunlock(&l->lock);
}
void ll_clear(LLIST *l)
{
ll_clear_int(l, 0);
}
void ll_clear_data(LLIST *l)
{
ll_clear_int(l, 1);
}
/* Appends to the list. Do not call this from outside without having a lock! */
static LL_NODE* ll_append_nolock(LLIST *l, void *obj)
{
if (l && obj) {
LL_NODE *new;
if(!cs_malloc(&new,sizeof(LL_NODE), -1)) return NULL;
new->obj = obj;
if (l->last)
l->last->nxt = new;
else
l->initial = new;
l->last = new;
l->count++;
return new;
}
return NULL;
}
LL_NODE* ll_append(LLIST *l, void *obj)
{
if (l && obj) {
cs_writelock(&l->lock);
LL_NODE *n = ll_append_nolock(l, obj);
cs_writeunlock(&l->lock);
return n;
}
return NULL;
}
LL_NODE *ll_prepend(LLIST *l, void *obj)
{
if (l && obj) {
LL_NODE *new;
if(!cs_malloc(&new,sizeof(LL_NODE), -1)) return NULL;
new->obj = obj;
cs_writelock(&l->lock);
new->nxt = l->initial;
l->initial = new;
if (!l->last)
l->last = l->initial;
l->count++;
cs_writeunlock(&l->lock);
return new;
}
return NULL;
}
LL_ITER ll_iter_create(LLIST *l)
{
LL_ITER it;
memset(&it, 0, sizeof(it));
it.l = l;
if (it.l)
it.ll_version = it.l->version;
return it;
}
void *ll_iter_next(LL_ITER *it)
{
if (it && it->l) {
cs_readlock(&it->l->lock);
void *res = ll_iter_next_nolock(it);
cs_readunlock(&it->l->lock);
return res;
}
return NULL;
}
void *ll_iter_remove_nolock(LL_ITER *it)
{
void *obj = NULL;
if (it) {
LL_NODE *del = it->cur;
if (del) {
obj = del->obj;
LL_NODE *prv = it->prv;
if (it->ll_version != it->l->version || !prv) { // List has been modified so it->prv might be wrong!
LL_NODE *n = it->l->initial;
prv = NULL;
while (n && n != del) {
prv = n;
n = n->nxt;
}
if (n != del)
return NULL;
}
if (prv)
prv->nxt = del->nxt;
else
it->l->initial = del->nxt;
if (!it->l->initial)
it->l->last = NULL;
else if (del == it->l->last)
it->l->last = prv;
it->cur = it->l->initial;
it->prv = NULL;
if (prv != NULL) {
while (it->cur && it->cur != prv) {
it->prv = it->cur;
it->cur = it->cur->nxt;
}
} else
it->cur = NULL;
it->l->count--;
it->ll_version = ++it->l->version;
add_garbage(del);
}
}
return obj;
}
void *ll_iter_next_remove(LL_ITER *it)
{
if (it && it->l) {
cs_writelock(&it->l->lock);
void *res = ll_iter_next_nolock(it);
ll_iter_remove_nolock(it);
cs_writeunlock(&it->l->lock);
return res;
}
return NULL;
}
void *ll_iter_move(LL_ITER *it, int32_t offset)
{
if (it && it->l) {
int32_t i;
void *res = NULL;
for (i=0; i<offset; i++) {
res = ll_iter_next_nolock(it);
if (!res) break;
}
return res;
}
return NULL;
}
void *ll_iter_peek(const LL_ITER *it, int32_t offset)
{
if (it && it->l) {
cs_readlock(&((LL_ITER*)it)->l->lock);
LL_NODE *n = it->cur;
int32_t i;
for (i = 0; i < offset; i++) {
if (n)
n = n->nxt;
else
break;
}
cs_readunlock(&((LL_ITER*)it)->l->lock);
if (!n)
return NULL;
return n->obj;
}
return NULL;
}
void ll_iter_reset(LL_ITER *it)
{
if (it) {
it->prv = NULL;
it->cur = NULL;
}
}
void ll_iter_insert(LL_ITER *it, void *obj)
{
if (it && obj) {
cs_writelock(&it->l->lock);
if (!it->cur || !it->cur->nxt)
ll_append_nolock(it->l, obj);
else {
LL_NODE *n;
if(!cs_malloc(&n,sizeof(LL_NODE), -1)) { cs_writeunlock(&it->l->lock); return; }
n->obj = obj;
n->nxt = it->cur->nxt;
it->cur->nxt = n;
it->l->count++;
it->ll_version = ++it->l->version;
}
cs_writeunlock(&it->l->lock);
}
}
/* Removes the element to which the iterator currently points. */
void *ll_iter_remove(LL_ITER *it)
{
void *obj = NULL;
if (it) {
LL_NODE *del = it->cur;
if (del) {
cs_writelock(&it->l->lock);
obj = ll_iter_remove_nolock(it);
cs_writeunlock(&it->l->lock);
}
}
return obj;
}
/* Moves the element which is currently pointed to by the iterator to the head of the list.*/
int32_t ll_iter_move_first(LL_ITER *it)
{
int32_t moved = 0;
if (it) {
LL_NODE *move = it->cur;
if (move) {
if (move == it->l->initial) //Can't move self to first
return 1;
LL_NODE *prv = it->prv;
cs_writelock(&it->l->lock);
if(it->ll_version != it->l->version || !prv){ // List has been modified so it->prv might be wrong!
LL_NODE *n = it->l->initial;
prv = NULL;
while(n && n != move){
prv = n;
n = n->nxt;
}
if(n != move) {
cs_writeunlock(&it->l->lock);
return moved;
}
}
if (prv)
prv->nxt = move->nxt;
else
it->l->initial = move->nxt;
if (prv && it->l->last == move)
it->l->last = prv;
move->nxt = it->l->initial;
it->l->initial = move;
it->ll_version = ++it->l->version;
it->prv = NULL;
cs_writeunlock(&it->l->lock);
moved = 1;
}
}
return moved;
}
void ll_iter_remove_data(LL_ITER *it)
{
void *obj = ll_iter_remove(it);
add_garbage(obj);
}
int32_t ll_count(const LLIST *l)
{
if (!l)
return 0;
return l->count;
}
void *ll_has_elements(const LLIST *l) {
if (!l || !l->initial)
return NULL;
return l->initial->obj;
}
int32_t ll_contains(const LLIST *l, const void *obj)
{
if (!l || !obj)
return 0;
LL_ITER it = ll_iter_create((LLIST *) l);
const void *data;
while ((data=ll_iter_next(&it))) {
if (data==obj)
break;
}
return (data==obj);
}
const void *ll_contains_data(const LLIST *l, const void *obj, uint32_t size) {
if (!l || !obj)
return NULL;
LL_ITER it = ll_iter_create((LLIST*) l);
const void *data;
while ((data=ll_iter_next(&it))) {
if (!memcmp(data,obj,size))
break;
}
return data;
}
int32_t ll_remove(LLIST *l, const void *obj)
{
int32_t n = 0;
LL_ITER it = ll_iter_create(l);
void *data;
while ((data=ll_iter_next(&it))) {
if (data==obj) {
ll_iter_remove(&it);
n++;
}
}
return n;
}
void ll_remove_data(LLIST *l, void *obj)
{
LL_ITER it = ll_iter_create(l);
void *data;
while ((data=ll_iter_next(&it))) {
if (data==obj)
ll_iter_remove_data(&it);
}
}
// removes all elements from l where elements are in elements_to_remove
int32_t ll_remove_all(LLIST *l, const LLIST *elements_to_remove)
{
int32_t count = 0;
LL_ITER it1 = ll_iter_create(l);
LL_ITER it2 = ll_iter_create((LLIST*) elements_to_remove);
const void *data1, *data2;
while ((data1=ll_iter_next(&it1))) {
ll_iter_reset(&it2);
while ((data2=ll_iter_next(&it2))) {
if (data1 == data2) {
ll_iter_remove(&it1);
count++;
break;
}
}
}
return count;
}
/* Returns an array with all elements sorted, the amount of elements is stored in size. We do not sort the original linked list
as this might harm running iterations. Furthermore, we need the array anyway for qsort() to work. Remember to free() the result. */
void **ll_sort(const LLIST *l, void *compare, int32_t *size)
{
if (!l || !l->initial || !compare){
*size = 0;
return NULL;
}
int32_t i=0;
LL_NODE *n;
cs_readlock(&((LLIST*)l)->lock);
*size = l->count;
void **p = cs_malloc(&p, l->count*sizeof(p[0]), 0);
for (n = l->initial; n; n = n->nxt) {
p[i++] = n->obj;
}
cs_readunlock(&((LLIST*)l)->lock);
#ifdef WITH_DEBUG
// if (chk_debugLog(it->l))
//cs_debug_mask(D_TRACE, "sort: count %d size %d", l->count, sizeof(p[0]));
#endif
qsort(p, l->count, sizeof(p[0]), compare);
return p;
}
void ll_putall(LLIST *dest, LLIST *src)
{
LL_ITER it = ll_iter_create(src);
void *data;
while ((data=ll_iter_next(&it))) {
ll_append(dest, data);
}
}