- fix for ticker #4787
[oscam.git] / oscam-llist.h
blob2e93d692a2468ab033311835e728bb99dcc7b024
1 /* singularly linked-list */
3 #ifndef OSCAM_LLIST_H_
4 #define OSCAM_LLIST_H_
6 typedef struct llnode LL_NODE;
7 struct llnode
9 void *obj;
10 LL_NODE *nxt;
13 typedef struct llist LLIST;
14 struct llist
16 //void *obj;
17 LL_NODE *initial;
18 LL_NODE *last;
19 int32_t count;
20 CS_MUTEX_LOCK lock;
21 int32_t flag;
22 uint32_t version; // updated on every modification of the list - exception is on prepends and appends as this should not have impacts on iterations!
25 typedef struct lliter LL_ITER;
26 struct lliter
28 LLIST *l;
29 LL_NODE *cur, *prv;
30 uint32_t ll_version;
33 typedef struct llistlockiter LL_LOCKITER;
34 struct llistlockiter
36 LLIST *l;
37 int32_t writelock;
38 LL_ITER it;
41 LLIST *ll_create(const char *name); // create llist, return ptr to llist
42 void ll_destroy(LLIST **pl); // same as ll_clear_abstract() but frees up LLIST mem as well
43 void ll_destroy_data(LLIST **pl); // same as ll_clear_data() but frees up obj allocations as well
44 void ll_destroy_free_data(LLIST **pl); // same as ll_clear_data() but frees up obj allocations as well. More, really free node without use GBC
46 void ll_clear(LLIST *l); // frees up all llnodes nodes but not data held in obj ptrs
47 void ll_clear_data(LLIST *l); // same as ll_clear_data() but frees up obj allocations as well
49 void **ll_sort(const LLIST *l, void *compare, int32_t *size); // sorts the list, compare = int func(const T *a, const T *b)
50 LL_NODE *ll_append(LLIST *l, void *obj); // append obj to llist
51 LL_NODE *ll_prepend(LLIST *l, void *obj); // prepend obj to llist
53 // clones a list, duplicates data
54 LLIST *ll_clone(LLIST *l, uint32_t copysize);
56 // New type of lock, list is locked during iterate! create=lock, destroy=unlock
57 LL_LOCKITER *ll_li_create(LLIST *l, int32_t writelock);
58 void ll_li_destroy(LL_LOCKITER *li);
59 void *ll_li_next(LL_LOCKITER *li);
61 // Old Iterators:
62 LL_ITER ll_iter_create(LLIST *l); // return ptr to iterator obj
63 void *ll_iter_next(LL_ITER *it); // iterate to and return next llnode obj, returns NULL at end
64 void *ll_iter_next_remove(LL_ITER *it); // iterate to and return next llnode obj, returns NULL at end, removing it
65 void *ll_iter_peek(const LL_ITER *it, int32_t offset); // return obj at offset from iterator but do not iterate
66 void ll_iter_reset(LL_ITER *it); // reset itrerator to first llnode
67 void ll_iter_insert(LL_ITER *it, void *obj); // insert obj at iterator node
68 void *ll_iter_remove(LL_ITER *it); // remove llnode at iterator, returns ptr to the llnode obj removed
69 void ll_iter_remove_data(LL_ITER *it); // remove llnode and free llnode obj
70 void *ll_iter_move(LL_ITER *it, int32_t offset); // moves the iterator position
71 int32_t ll_iter_move_first(LL_ITER *it); // moves an entry to top
72 static inline int32_t ll_count(const LLIST *l) // return number of items in list
74 if(!l || l->flag)
75 { return 0; }
77 return l->count;
79 void *ll_has_elements(const LLIST *l); // returns first obj if has one
80 void *ll_last_element(const LLIST *l);
81 int32_t ll_contains(const LLIST *l, const void *obj);
82 const void *ll_contains_data(const LLIST *l, const void *obj, uint32_t size);
83 int32_t ll_remove(LLIST *l, const void *obj);
84 void ll_remove_data(LLIST *l, void *obj);
85 int32_t ll_remove_all(LLIST *l, const LLIST *elements_to_remove); // removes all elements from l where elements are in elements_to_remove
87 void ll_putall(LLIST *dest, LLIST *src);
89 void *ll_remove_first(LLIST *l);
90 void ll_remove_first_data(LLIST *l);
92 #endif