1 |
|
---|
2 | /* singularly linked-list */
|
---|
3 |
|
---|
4 | #include <stdlib.h>
|
---|
5 |
|
---|
6 | #include "globals.h"
|
---|
7 | #include "module-datastruct-llist.h"
|
---|
8 |
|
---|
9 | static void _destroy(LLIST *l)
|
---|
10 | {
|
---|
11 | if (!l) return;
|
---|
12 |
|
---|
13 | if (l->lock) {
|
---|
14 | pthread_mutex_unlock(l->lock);
|
---|
15 | pthread_mutex_destroy(l->lock);
|
---|
16 | add_garbage(l->lock);
|
---|
17 | }
|
---|
18 |
|
---|
19 | add_garbage(l);
|
---|
20 | }
|
---|
21 |
|
---|
22 | LLIST *ll_create()
|
---|
23 | {
|
---|
24 | LLIST *l = calloc(1, sizeof(LLIST));
|
---|
25 |
|
---|
26 | l->lock = malloc(sizeof(pthread_mutex_t));
|
---|
27 | pthread_mutex_init(l->lock, NULL);
|
---|
28 |
|
---|
29 | return l;
|
---|
30 | }
|
---|
31 |
|
---|
32 | LLIST *ll_create_nolock()
|
---|
33 | {
|
---|
34 | LLIST *l = calloc(1, sizeof(LLIST));
|
---|
35 |
|
---|
36 | return l;
|
---|
37 | }
|
---|
38 |
|
---|
39 | void ll_destroy(LLIST *l)
|
---|
40 | {
|
---|
41 | if (!l) return;
|
---|
42 | ll_clear(l);
|
---|
43 |
|
---|
44 | _destroy(l);
|
---|
45 | }
|
---|
46 |
|
---|
47 | void ll_destroy_data(LLIST *l)
|
---|
48 | {
|
---|
49 | if (!l) return;
|
---|
50 | ll_clear_data(l);
|
---|
51 |
|
---|
52 | _destroy(l);
|
---|
53 | }
|
---|
54 |
|
---|
55 | void ll_clear(LLIST *l)
|
---|
56 | {
|
---|
57 | if (!l) return;
|
---|
58 | void *obj;
|
---|
59 |
|
---|
60 | LL_ITER *it = ll_iter_create(l);
|
---|
61 | while ((obj = ll_iter_next(it)))
|
---|
62 | ll_iter_remove(it);
|
---|
63 | l->count = 0;
|
---|
64 | ll_iter_release(it);
|
---|
65 | }
|
---|
66 |
|
---|
67 | void ll_clear_data(LLIST *l)
|
---|
68 | {
|
---|
69 | if (!l) return;
|
---|
70 | void *obj;
|
---|
71 |
|
---|
72 | LL_ITER *it = ll_iter_create(l);
|
---|
73 | while ((obj = ll_iter_next(it)))
|
---|
74 | ll_iter_remove_data(it);
|
---|
75 | l->count = 0;
|
---|
76 | ll_iter_release(it);
|
---|
77 | }
|
---|
78 |
|
---|
79 | LL_NODE * ll_append_nolock(LLIST *l, void *obj)
|
---|
80 | {
|
---|
81 | if (l && obj) {
|
---|
82 | LL_NODE *new = calloc(1, sizeof(LL_NODE));
|
---|
83 | LL_NODE *n = l->initial;
|
---|
84 |
|
---|
85 | new->obj = obj;
|
---|
86 |
|
---|
87 | if (n) {
|
---|
88 | while (n->nxt) n = n->nxt;
|
---|
89 | n->nxt = new;
|
---|
90 | new->prv = n;
|
---|
91 | } else
|
---|
92 | l->initial = new;
|
---|
93 | l->count++;
|
---|
94 | return new;
|
---|
95 | }
|
---|
96 | return NULL;
|
---|
97 | }
|
---|
98 |
|
---|
99 | void ll_append(LLIST *l, void *obj)
|
---|
100 | {
|
---|
101 | if (l && obj) {
|
---|
102 | if (l->lock)
|
---|
103 | pthread_mutex_lock(l->lock);
|
---|
104 | ll_append_nolock(l, obj);
|
---|
105 | if (l->lock)
|
---|
106 | pthread_mutex_unlock(l->lock);
|
---|
107 | }
|
---|
108 | }
|
---|
109 |
|
---|
110 | LL_ITER *ll_iter_create(LLIST *l)
|
---|
111 | {
|
---|
112 | LL_ITER *it = malloc(sizeof(LL_ITER));
|
---|
113 |
|
---|
114 | it->l = l;
|
---|
115 | if (l) {
|
---|
116 | if (l->lock)
|
---|
117 | pthread_mutex_lock(l->lock);
|
---|
118 | it->cur = l->initial;
|
---|
119 | }
|
---|
120 | else
|
---|
121 | it->cur = NULL;
|
---|
122 |
|
---|
123 | return it;
|
---|
124 | }
|
---|
125 |
|
---|
126 | void ll_iter_release(LL_ITER *it)
|
---|
127 | {
|
---|
128 | if (it) {
|
---|
129 | if (it->l && it->l->lock)
|
---|
130 | pthread_mutex_unlock(it->l->lock);
|
---|
131 |
|
---|
132 | add_garbage(it);
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | void *ll_iter_next(LL_ITER *it)
|
---|
137 | {
|
---|
138 | if (it) {
|
---|
139 | if (it->cur) {
|
---|
140 | void *obj = it->cur->obj;
|
---|
141 |
|
---|
142 | it->cur = it->cur->nxt;
|
---|
143 |
|
---|
144 | return obj;
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | return NULL;
|
---|
149 | }
|
---|
150 |
|
---|
151 | void *ll_iter_peek(LL_ITER *it, int offset)
|
---|
152 | {
|
---|
153 | LL_NODE *n = it->cur;
|
---|
154 | int i;
|
---|
155 |
|
---|
156 | for (i = 0; i < offset; i++)
|
---|
157 | if (n)
|
---|
158 | n = n->nxt;
|
---|
159 | else
|
---|
160 | return NULL;
|
---|
161 |
|
---|
162 | if (!n)
|
---|
163 | return NULL;
|
---|
164 |
|
---|
165 | return n->obj;
|
---|
166 | }
|
---|
167 |
|
---|
168 | void ll_iter_reset(LL_ITER *it)
|
---|
169 | {
|
---|
170 | if (it && it->l)
|
---|
171 | it->cur = it->l->initial;
|
---|
172 | }
|
---|
173 |
|
---|
174 | void ll_iter_insert(LL_ITER *it, void *obj)
|
---|
175 | {
|
---|
176 | if(it && obj) {
|
---|
177 | if (!it->cur)
|
---|
178 | ll_append_nolock(it->l, obj);
|
---|
179 | else {
|
---|
180 | LL_NODE *n = calloc(1, sizeof(LL_NODE));
|
---|
181 | n->obj = obj;
|
---|
182 | n->nxt = it->cur;
|
---|
183 | n->prv = it->cur->prv;
|
---|
184 |
|
---|
185 | it->cur->prv->nxt = n;
|
---|
186 | it->cur->prv = n;
|
---|
187 | it->l->count++;
|
---|
188 | }
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | void *ll_iter_remove(LL_ITER *it)
|
---|
193 | {
|
---|
194 | if (it && it->l) {
|
---|
195 | LL_NODE *n;
|
---|
196 |
|
---|
197 | // if is last node, handle differently
|
---|
198 | if (!it->cur && it->l->initial) {
|
---|
199 | n = it->l->initial;
|
---|
200 | while (n->nxt) n = n->nxt;
|
---|
201 | } else
|
---|
202 | n = it->cur->prv;
|
---|
203 |
|
---|
204 | if (n) {
|
---|
205 | void *obj = n->obj;
|
---|
206 | if (n->nxt) n->nxt->prv = n->prv;
|
---|
207 | if (n->prv) n->prv->nxt = n->nxt;
|
---|
208 | else it->l->initial = n->nxt;
|
---|
209 |
|
---|
210 | it->l->count--;
|
---|
211 | add_garbage(n);
|
---|
212 | return obj;
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | return NULL;
|
---|
217 | }
|
---|
218 |
|
---|
219 | void ll_iter_remove_data(LL_ITER *it)
|
---|
220 | {
|
---|
221 | void *obj = ll_iter_remove(it);
|
---|
222 | add_garbage(obj);
|
---|
223 | }
|
---|
224 |
|
---|
225 | int ll_count(LLIST *l)
|
---|
226 | {
|
---|
227 | if (!l)
|
---|
228 | return 0;
|
---|
229 |
|
---|
230 | return l->count;
|
---|
231 | }
|
---|
232 |
|
---|
233 | void ll_insert_at_nolock(LLIST *l, void *obj, int pos)
|
---|
234 | {
|
---|
235 | if (!l || !obj)
|
---|
236 | return;
|
---|
237 |
|
---|
238 | LL_NODE *new = calloc(1, sizeof(LL_NODE));
|
---|
239 | LL_NODE *n = l->initial;
|
---|
240 | int i;
|
---|
241 |
|
---|
242 | for (i = 0; i < pos; i++)
|
---|
243 | if (n)
|
---|
244 | n = n->nxt;
|
---|
245 | else
|
---|
246 | break;
|
---|
247 |
|
---|
248 | new->obj = obj;
|
---|
249 | new->nxt = n;
|
---|
250 |
|
---|
251 | if (n && n->prv) n->prv->nxt = new;
|
---|
252 | else l->initial = new;
|
---|
253 | l->count++;
|
---|
254 |
|
---|
255 | if (n) n->prv = new;
|
---|
256 | }
|
---|
257 |
|
---|
258 | void ll_insert_at(LLIST *l, void *obj, int pos)
|
---|
259 | {
|
---|
260 | if (!l || !obj)
|
---|
261 | return;
|
---|
262 |
|
---|
263 | if (l->lock)
|
---|
264 | pthread_mutex_lock(l->lock);
|
---|
265 | ll_insert_at_nolock(l, obj, pos);
|
---|
266 | if (l->lock)
|
---|
267 | pthread_mutex_unlock(l->lock);
|
---|
268 | }
|
---|
269 |
|
---|
270 | //Returns first object if there is one
|
---|
271 | void *ll_has_elements(LLIST *l) {
|
---|
272 | if (!l || !l->initial)
|
---|
273 | return NULL;
|
---|
274 | return l->initial->obj;
|
---|
275 | }
|
---|
276 |
|
---|
277 | int ll_contains(LLIST *l, void *obj)
|
---|
278 | {
|
---|
279 | LL_ITER *it = ll_iter_create(l);
|
---|
280 | void *data;
|
---|
281 | while ((data=ll_iter_next(it))) {
|
---|
282 | if (data==obj)
|
---|
283 | break;
|
---|
284 | }
|
---|
285 | ll_iter_release(it);
|
---|
286 | return (data==obj);
|
---|
287 | }
|
---|
288 |
|
---|
289 | void ll_remove(LLIST *l, void *obj)
|
---|
290 | {
|
---|
291 | LL_ITER *it = ll_iter_create(l);
|
---|
292 | void *data;
|
---|
293 | while ((data=ll_iter_next(it))) {
|
---|
294 | if (data==obj)
|
---|
295 | ll_iter_remove(it);
|
---|
296 | }
|
---|
297 | ll_iter_release(it);
|
---|
298 | }
|
---|
299 |
|
---|
300 | void ll_remove_data(LLIST *l, void *obj)
|
---|
301 | {
|
---|
302 | LL_ITER *it = ll_iter_create(l);
|
---|
303 | void *data;
|
---|
304 | while ((data=ll_iter_next(it))) {
|
---|
305 | if (data==obj)
|
---|
306 | ll_iter_remove_data(it);
|
---|
307 | }
|
---|
308 | ll_iter_release(it);
|
---|
309 | }
|
---|