1 | //FIXME Not checked on threadsafety yet; after checking please remove this line
|
---|
2 | /*
|
---|
3 | * module-obj-llist.c
|
---|
4 | *
|
---|
5 | * Created on: 23.04.2010
|
---|
6 | * Author: alno
|
---|
7 | */
|
---|
8 |
|
---|
9 | #include <string.h>
|
---|
10 | #include <stdlib.h>
|
---|
11 | #include "module-obj-llist.h"
|
---|
12 |
|
---|
13 | LLIST *llist_create(void)
|
---|
14 | {
|
---|
15 | LLIST *l = malloc(sizeof(LLIST));
|
---|
16 | if (!l)
|
---|
17 | return NULL;
|
---|
18 | memset(l, 0, sizeof(LLIST));
|
---|
19 |
|
---|
20 | pthread_mutex_init(&l->lock, NULL);
|
---|
21 |
|
---|
22 | l->items = 0;
|
---|
23 |
|
---|
24 | return l;
|
---|
25 | }
|
---|
26 |
|
---|
27 | void llist_destroy(LLIST *l)
|
---|
28 | {
|
---|
29 | LLIST_ITR itr;
|
---|
30 | if (!l)
|
---|
31 | return;
|
---|
32 | void *o = llist_itr_init(l, &itr);
|
---|
33 | while (o) {
|
---|
34 | free(o);
|
---|
35 | o = llist_itr_remove(&itr);
|
---|
36 | }
|
---|
37 | pthread_mutex_destroy(&l->lock);
|
---|
38 | free(l);
|
---|
39 | // llist_itr_release(&itr);
|
---|
40 | }
|
---|
41 |
|
---|
42 | void *llist_append(LLIST *l, void *o)
|
---|
43 | {
|
---|
44 | if (!l)
|
---|
45 | return NULL;
|
---|
46 | pthread_mutex_lock(&l->lock);
|
---|
47 | if (o) {
|
---|
48 | struct llist_node *ln = malloc(sizeof(struct llist_node));
|
---|
49 | if (!ln) {
|
---|
50 | pthread_mutex_unlock(&l->lock);
|
---|
51 | return NULL;
|
---|
52 | }
|
---|
53 |
|
---|
54 | memset(ln, 0, sizeof(struct llist_node));
|
---|
55 | ln->obj = o;
|
---|
56 |
|
---|
57 | if (l->last) {
|
---|
58 | ln->prv = l->last;
|
---|
59 | ln->prv->nxt = ln;
|
---|
60 | } else {
|
---|
61 | l->first = ln;
|
---|
62 | }
|
---|
63 | l->last = ln;
|
---|
64 |
|
---|
65 | l->items++;
|
---|
66 | }
|
---|
67 | pthread_mutex_unlock(&l->lock);
|
---|
68 |
|
---|
69 | return o;
|
---|
70 | }
|
---|
71 |
|
---|
72 | void *llist_insert_first(LLIST *l, void *o)
|
---|
73 | {
|
---|
74 | if (!l)
|
---|
75 | return NULL;
|
---|
76 | pthread_mutex_lock(&l->lock);
|
---|
77 | if (o) {
|
---|
78 | struct llist_node *ln = malloc(sizeof(struct llist_node));
|
---|
79 | if (!ln) {
|
---|
80 | pthread_mutex_unlock(&l->lock);
|
---|
81 | return NULL;
|
---|
82 | }
|
---|
83 |
|
---|
84 | memset(ln, 0, sizeof(struct llist_node));
|
---|
85 | ln->obj = o;
|
---|
86 |
|
---|
87 | if (l->first) {
|
---|
88 | ln->nxt = l->first;
|
---|
89 | ln->nxt->prv = ln;
|
---|
90 | } else {
|
---|
91 | l->last = ln;
|
---|
92 | }
|
---|
93 | l->first = ln;
|
---|
94 |
|
---|
95 | l->items++;
|
---|
96 | }
|
---|
97 | pthread_mutex_unlock(&l->lock);
|
---|
98 |
|
---|
99 | return o;
|
---|
100 | }
|
---|
101 |
|
---|
102 | void *llist_itr_init(LLIST *l, LLIST_ITR *itr)
|
---|
103 | {
|
---|
104 | if (!l || !itr)
|
---|
105 | return NULL;
|
---|
106 | // pthread_mutex_lock(&l->lock);
|
---|
107 | if (l->first) {
|
---|
108 |
|
---|
109 | memset(itr, 0, sizeof(LLIST_ITR));
|
---|
110 | itr->cur = l->first;
|
---|
111 | itr->l = l;
|
---|
112 |
|
---|
113 | return itr->cur->obj;
|
---|
114 | }
|
---|
115 |
|
---|
116 | return NULL;
|
---|
117 | }
|
---|
118 | /*
|
---|
119 | void llist_itr_release(LLIST_ITR *itr)
|
---|
120 | {
|
---|
121 | // pthread_mutex_unlock(&itr->l->lock);
|
---|
122 | }
|
---|
123 | */
|
---|
124 | void *llist_itr_next(LLIST_ITR *itr)
|
---|
125 | {
|
---|
126 | if (itr->cur->nxt) {
|
---|
127 | itr->cur = itr->cur->nxt;
|
---|
128 | return itr->cur->obj;
|
---|
129 | }
|
---|
130 |
|
---|
131 | return NULL;
|
---|
132 | }
|
---|
133 |
|
---|
134 | void *llist_itr_remove(LLIST_ITR *itr) // this needs cleaning - I was lazy
|
---|
135 | {
|
---|
136 | if (!itr || !itr->l || itr->l->items == 0)
|
---|
137 | return NULL;
|
---|
138 | itr->l->items--;
|
---|
139 | if ((itr->cur == itr->l->first) && (itr->cur == itr->l->last)) {
|
---|
140 | NULLFREE(itr->cur);
|
---|
141 | itr->l->first = NULL;
|
---|
142 | itr->l->last = NULL;
|
---|
143 | return NULL;
|
---|
144 | } else if (itr->cur == itr->l->first) {
|
---|
145 | struct llist_node *nxt = itr->cur->nxt;
|
---|
146 | NULLFREE(itr->cur);
|
---|
147 | nxt->prv = NULL;
|
---|
148 | itr->l->first = nxt;
|
---|
149 | itr->cur = nxt;
|
---|
150 | } else if (itr->cur == itr->l->last) {
|
---|
151 | itr->l->last = itr->cur->prv;
|
---|
152 | itr->l->last->nxt = NULL;
|
---|
153 | NULLFREE(itr->cur);
|
---|
154 | return NULL;
|
---|
155 | } else {
|
---|
156 | struct llist_node *nxt = itr->cur->nxt;
|
---|
157 | itr->cur->prv->nxt = itr->cur->nxt;
|
---|
158 | itr->cur->nxt->prv = itr->cur->prv;
|
---|
159 | NULLFREE(itr->cur);
|
---|
160 | itr->cur = nxt;
|
---|
161 | }
|
---|
162 |
|
---|
163 | return itr->cur->obj;
|
---|
164 | }
|
---|
165 |
|
---|
166 | int llist_count(LLIST *l)
|
---|
167 | {
|
---|
168 | return l->items;
|
---|
169 | }
|
---|
170 |
|
---|