source: trunk/module-obj-llist.c@ 3039

Last change on this file since 3039 was 3039, checked in by schlocke, 10 years ago

cccam: UA/SA for EMM adjusted
cccam: ccckeepalive parameter for readers inserted
cccam: minimizecards parameter for server
cccam: New feature: card-cash for faster switching

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