1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
/*
* libkmod - interface to kernel module operations
*
* Copyright (C) 2011-2013 ProFUSION embedded systems
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include "libkmod.h"
#include "libkmod-internal.h"
/**
* SECTION:libkmod-list
* @short_description: general purpose list
*/
static inline struct list_node *list_node_init(struct list_node *node)
{
node->next = node;
node->prev = node;
return node;
}
static inline void list_node_append(struct list_node *list,
struct list_node *node)
{
if (list == NULL) {
list_node_init(node);
return;
}
node->prev = list->prev;
list->prev->next = node;
list->prev = node;
node->next = list;
}
static inline struct list_node *list_node_remove(struct list_node *node)
{
if (node->prev == node || node->next == node)
return NULL;
node->prev->next = node->next;
node->next->prev = node->prev;
return node->next;
}
static inline void list_node_insert_after(struct list_node *list,
struct list_node *node)
{
if (list == NULL) {
list_node_init(node);
return;
}
node->prev = list;
node->next = list->next;
list->next->prev = node;
list->next = node;
}
static inline void list_node_insert_before(struct list_node *list,
struct list_node *node)
{
if (list == NULL) {
list_node_init(node);
return;
}
node->next = list;
node->prev = list->prev;
list->prev->next = node;
list->prev = node;
}
static inline void list_node_append_list(struct list_node *list1,
struct list_node *list2)
{
struct list_node *list1_last;
if (list1 == NULL) {
list_node_init(list2);
return;
}
list1->prev->next = list2;
list2->prev->next = list1;
/* cache the last, because we will lose the pointer */
list1_last = list1->prev;
list1->prev = list2->prev;
list2->prev = list1_last;
}
struct kmod_list *kmod_list_append(struct kmod_list *list, const void *data)
{
struct kmod_list *new;
new = malloc(sizeof(*new));
if (new == NULL)
return NULL;
new->data = (void *)data;
list_node_append(list ? &list->node : NULL, &new->node);
return list ? list : new;
}
struct kmod_list *kmod_list_insert_after(struct kmod_list *list,
const void *data)
{
struct kmod_list *new;
if (list == NULL)
return kmod_list_append(list, data);
new = malloc(sizeof(*new));
if (new == NULL)
return NULL;
new->data = (void *)data;
list_node_insert_after(&list->node, &new->node);
return list;
}
struct kmod_list *kmod_list_insert_before(struct kmod_list *list,
const void *data)
{
struct kmod_list *new;
if (list == NULL)
return kmod_list_append(list, data);
new = malloc(sizeof(*new));
if (new == NULL)
return NULL;
new->data = (void *)data;
list_node_insert_before(&list->node, &new->node);
return new;
}
struct kmod_list *kmod_list_append_list(struct kmod_list *list1,
struct kmod_list *list2)
{
if (list1 == NULL)
return list2;
if (list2 == NULL)
return list1;
list_node_append_list(&list1->node, &list2->node);
return list1;
}
struct kmod_list *kmod_list_prepend(struct kmod_list *list, const void *data)
{
struct kmod_list *new;
new = malloc(sizeof(*new));
if (new == NULL)
return NULL;
new->data = (void *)data;
list_node_append(list ? &list->node : NULL, &new->node);
return new;
}
struct kmod_list *kmod_list_remove(struct kmod_list *list)
{
struct list_node *node;
if (list == NULL)
return NULL;
node = list_node_remove(&list->node);
free(list);
if (node == NULL)
return NULL;
return container_of(node, struct kmod_list, node);
}
struct kmod_list *kmod_list_remove_data(struct kmod_list *list,
const void *data)
{
struct kmod_list *itr;
struct list_node *node;
for (itr = list; itr != NULL; itr = kmod_list_next(list, itr)) {
if (itr->data == data)
break;
}
if (itr == NULL)
return list;
node = list_node_remove(&itr->node);
free(itr);
if (node == NULL)
return NULL;
return container_of(node, struct kmod_list, node);
}
/*
* n must be greater to or equal the number of elements (we don't check the
* condition)
*/
struct kmod_list *kmod_list_remove_n_latest(struct kmod_list *list,
unsigned int n)
{
struct kmod_list *l = list;
unsigned int i;
for (i = 0; i < n; i++) {
l = kmod_list_last(l);
l = kmod_list_remove(l);
}
return l;
}
/**
* kmod_list_prev:
* @list: the head of the list
* @curr: the current node in the list
*
* Get the previous node in @list relative to @curr as if @list was not a
* circular list. I.e.: the previous of the head is NULL. It can be used to
* iterate a list by checking for NULL return to know when all elements were
* iterated.
*
* Returns: node previous to @curr or NULL if either this node is the head of
* the list or the list is empty.
*/
KMOD_EXPORT struct kmod_list *kmod_list_prev(const struct kmod_list *list,
const struct kmod_list *curr)
{
if (list == NULL || curr == NULL)
return NULL;
if (list == curr)
return NULL;
return container_of(curr->node.prev, struct kmod_list, node);
}
/**
* kmod_list_next:
* @list: the head of the list
* @curr: the current node in the list
*
* Get the next node in @list relative to @curr as if @list was not a circular
* list. I.e. calling this function in the last node of the list returns
* NULL.. It can be used to iterate a list by checking for NULL return to know
* when all elements were iterated.
*
* Returns: node next to @curr or NULL if either this node is the last of or
* list is empty.
*/
KMOD_EXPORT struct kmod_list *kmod_list_next(const struct kmod_list *list,
const struct kmod_list *curr)
{
if (list == NULL || curr == NULL)
return NULL;
if (curr->node.next == &list->node)
return NULL;
return container_of(curr->node.next, struct kmod_list, node);
}
/**
* kmod_list_last:
* @list: the head of the list
*
* Get the last element of the @list. As @list is a circular list,
* this is a cheap operation O(1) with the last element being the
* previous element.
*
* If the list has a single element it will return the list itself (as
* expected, and this is what differentiates from kmod_list_prev()).
*
* Returns: last node at @list or NULL if the list is empty.
*/
KMOD_EXPORT struct kmod_list *kmod_list_last(const struct kmod_list *list)
{
if (list == NULL)
return NULL;
return container_of(list->node.prev, struct kmod_list, node);
}
|