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->l && it->l->lock)
|
---|
129 | pthread_mutex_unlock(it->l->lock);
|
---|
130 |
|
---|
131 | add_garbage(it);
|
---|
132 | }
|
---|
133 |
|
---|
134 | void *ll_iter_next(LL_ITER *it)
|
---|
135 | {
|
---|
136 | if (it) {
|
---|
137 | if (it->cur) {
|
---|
138 | void *obj = it->cur->obj;
|
---|
139 |
|
---|
140 | it->cur = it->cur->nxt;
|
---|
141 |
|
---|
142 | return obj;
|
---|
143 | }
|
---|
144 | }
|
---|
145 |
|
---|
146 | return NULL;
|
---|
147 | }
|
---|
148 |
|
---|
149 | void *ll_iter_peek(LL_ITER *it, int offset)
|
---|
150 | {
|
---|
151 | LL_NODE *n = it->cur;
|
---|
152 | int i;
|
---|
153 |
|
---|
154 | for (i = 0; i < offset; i++)
|
---|
155 | if (n)
|
---|
156 | n = n->nxt;
|
---|
157 | else
|
---|
158 | return NULL;
|
---|
159 |
|
---|
160 | if (!n)
|
---|
161 | return NULL;
|
---|
162 |
|
---|
163 | return n->obj;
|
---|
164 | }
|
---|
165 |
|
---|
166 | void ll_iter_reset(LL_ITER *it)
|
---|
167 | {
|
---|
168 | if (it && it->l)
|
---|
169 | it->cur = it->l->initial;
|
---|
170 | }
|
---|
171 |
|
---|
172 | void ll_iter_insert(LL_ITER *it, void *obj)
|
---|
173 | {
|
---|
174 | if(it && obj) {
|
---|
175 | if (!it->cur)
|
---|
176 | ll_append_nolock(it->l, obj);
|
---|
177 | else {
|
---|
178 | LL_NODE *n = calloc(1, sizeof(LL_NODE));
|
---|
179 | n->obj = obj;
|
---|
180 | n->nxt = it->cur;
|
---|
181 | n->prv = it->cur->prv;
|
---|
182 |
|
---|
183 | it->cur->prv->nxt = n;
|
---|
184 | it->cur->prv = n;
|
---|
185 | it->l->count++;
|
---|
186 | }
|
---|
187 | }
|
---|
188 | }
|
---|
189 |
|
---|
190 | void *ll_iter_remove(LL_ITER *it)
|
---|
191 | {
|
---|
192 | if (it && it->l) {
|
---|
193 | LL_NODE *n;
|
---|
194 |
|
---|
195 | // if is last node, handle differently
|
---|
196 | if (!it->cur && it->l->initial) {
|
---|
197 | n = it->l->initial;
|
---|
198 | while (n->nxt) n = n->nxt;
|
---|
199 | } else
|
---|
200 | n = it->cur->prv;
|
---|
201 |
|
---|
202 | if (n) {
|
---|
203 | void *obj = n->obj;
|
---|
204 | if (n->nxt) n->nxt->prv = n->prv;
|
---|
205 | if (n->prv) n->prv->nxt = n->nxt;
|
---|
206 | else it->l->initial = n->nxt;
|
---|
207 |
|
---|
208 | it->l->count--;
|
---|
209 | add_garbage(n);
|
---|
210 | return obj;
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | return NULL;
|
---|
215 | }
|
---|
216 |
|
---|
217 | void ll_iter_remove_data(LL_ITER *it)
|
---|
218 | {
|
---|
219 | void *obj = ll_iter_remove(it);
|
---|
220 | add_garbage(obj);
|
---|
221 | }
|
---|
222 |
|
---|
223 | int ll_count(LLIST *l)
|
---|
224 | {
|
---|
225 | if (!l)
|
---|
226 | return 0;
|
---|
227 |
|
---|
228 | return l->count;
|
---|
229 | }
|
---|
230 |
|
---|
231 | void ll_insert_at_nolock(LLIST *l, void *obj, int pos)
|
---|
232 | {
|
---|
233 | if (!l || !obj)
|
---|
234 | return;
|
---|
235 |
|
---|
236 | LL_NODE *new = calloc(1, sizeof(LL_NODE));
|
---|
237 | LL_NODE *n = l->initial;
|
---|
238 | int i;
|
---|
239 |
|
---|
240 | for (i = 0; i < pos; i++)
|
---|
241 | if (n)
|
---|
242 | n = n->nxt;
|
---|
243 | else
|
---|
244 | break;
|
---|
245 |
|
---|
246 | new->obj = obj;
|
---|
247 | new->nxt = n;
|
---|
248 |
|
---|
249 | if (n && n->prv) n->prv->nxt = new;
|
---|
250 | else l->initial = new;
|
---|
251 | l->count++;
|
---|
252 |
|
---|
253 | if (n) n->prv = new;
|
---|
254 | }
|
---|
255 |
|
---|
256 | void ll_insert_at(LLIST *l, void *obj, int pos)
|
---|
257 | {
|
---|
258 | if (!l || !obj)
|
---|
259 | return;
|
---|
260 |
|
---|
261 | if (l->lock)
|
---|
262 | pthread_mutex_lock(l->lock);
|
---|
263 | ll_insert_at_nolock(l, obj, pos);
|
---|
264 | if (l->lock)
|
---|
265 | pthread_mutex_unlock(l->lock);
|
---|
266 | }
|
---|
267 |
|
---|
268 | //Returns first object if there is one
|
---|
269 | void *ll_has_elements(LLIST *l) {
|
---|
270 | if (!l || !l->initial)
|
---|
271 | return NULL;
|
---|
272 | return l->initial->obj;
|
---|
273 | }
|
---|
274 |
|
---|