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
|
/* Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include "libdnssec/list.h"
#include "libdnssec/list/ucw_clists.h"
#include "libdnssec/error.h"
#include "libdnssec/shared/shared.h"
struct dnssec_list {
clist list;
};
struct dnssec_item {
cnode node;
void *data;
};
/*!
* Allocate new list item and set item data.
*/
static dnssec_item_t *item_new(void *data)
{
dnssec_item_t *item = malloc(sizeof(*item));
if (item) {
clear_struct(item);
item->data = data;
}
return item;
}
/*!
* Wrapper around libc free with unused context.
*/
static void wrap_free(void *ptr, void *ctx _unused_)
{
free(ptr);
}
/* -- public API ----------------------------------------------------------- */
_public_
void *dnssec_item_get(const dnssec_item_t *item)
{
return item ? item->data : NULL;
}
_public_
void dnssec_item_set(dnssec_item_t *item, void *data)
{
if (item) {
item->data = data;
}
}
_public_
dnssec_list_t *dnssec_list_new(void)
{
dnssec_list_t *list = malloc(sizeof(*list));
if (list) {
clist_init(&list->list);
}
return list;
}
_public_
void dnssec_list_clear(dnssec_list_t *list)
{
if (!list) {
return;
}
dnssec_list_foreach(item, list) {
free(item);
}
}
_public_
void dnssec_list_clear_full(dnssec_list_t *list, dnssec_item_free_cb free_cb,
void *free_ctx)
{
if (!list) {
return;
}
if (!free_cb) {
free_cb = wrap_free;
}
dnssec_list_foreach(item, list) {
free_cb(item->data, free_ctx);
free(item);
}
}
_public_
void dnssec_list_free(dnssec_list_t *list)
{
if (!list) {
return;
}
dnssec_list_clear(list);
free(list);
}
_public_
void dnssec_list_free_full(dnssec_list_t *list, dnssec_item_free_cb free_cb,
void *free_ctx)
{
if (!list) {
return;
}
dnssec_list_clear_full(list, free_cb, free_ctx);
free(list);
}
_public_
dnssec_item_t *dnssec_list_head(dnssec_list_t *list)
{
if (!list) {
return NULL;
}
return clist_head(&list->list);
}
_public_
dnssec_item_t *dnssec_list_tail(dnssec_list_t *list)
{
if (!list) {
return NULL;
}
return clist_tail(&list->list);
}
_public_
dnssec_item_t *dnssec_list_next(dnssec_list_t *list, dnssec_item_t *item)
{
if (!list || !item) {
return NULL;
}
return clist_next(&list->list, &item->node);
}
_public_
dnssec_item_t *dnssec_list_prev(dnssec_list_t *list, dnssec_item_t *item)
{
if (!list || !item) {
return NULL;
}
return clist_prev(&list->list, &item->node);
}
_public_
dnssec_item_t *dnssec_list_nth(dnssec_list_t *list, size_t position)
{
size_t index = 0;
dnssec_item_t *item = dnssec_list_head(list);
while (item) {
if (index == position) {
return item;
} else {
item = dnssec_list_next(list, item);
index += 1;
}
}
return NULL;
}
_public_
bool dnssec_list_is_empty(dnssec_list_t *list)
{
return !list || clist_empty(&list->list);
}
_public_
size_t dnssec_list_size(dnssec_list_t *list)
{
return list ? clist_size(&list->list) : 0;
}
_public_
int dnssec_list_insert_before(dnssec_item_t *item, void *data)
{
if (!item) {
return DNSSEC_EINVAL;
}
dnssec_item_t *add = item_new(data);
if (!add) {
return DNSSEC_ENOMEM;
}
clist_insert_before(&add->node, &item->node);
return DNSSEC_EOK;
}
_public_
int dnssec_list_insert_after(dnssec_item_t *item, void *data)
{
if (!item) {
return DNSSEC_EINVAL;
}
dnssec_item_t *add = item_new(data);
if (!add) {
return DNSSEC_ENOMEM;
}
clist_insert_after(&add->node, &item->node);
return DNSSEC_EOK;
}
_public_
int dnssec_list_append(dnssec_list_t *list, void *data)
{
if (!list) {
return DNSSEC_EINVAL;
}
dnssec_item_t *add = item_new(data);
if (!add) {
return DNSSEC_ENOMEM;
}
clist_add_tail(&list->list , &add->node);
return DNSSEC_EOK;
}
_public_
int dnssec_list_prepend(dnssec_list_t *list, void *data)
{
if (!list) {
return DNSSEC_EINVAL;
}
dnssec_item_t *add = item_new(data);
if (!add) {
return DNSSEC_ENOMEM;
}
clist_add_head(&list->list , &add->node);
return DNSSEC_EOK;
}
_public_
void dnssec_list_remove(dnssec_item_t *item)
{
if (item) {
clist_remove(&item->node);
free(item);
}
}
_public_
dnssec_item_t *dnssec_list_search(dnssec_list_t *list, void *data)
{
dnssec_list_foreach(item, list) {
if (item->data == data) {
return item;
}
}
return NULL;
}
_public_
bool dnssec_list_contains(dnssec_list_t *list, void *data)
{
return dnssec_list_search(list, data) != NULL;
}
|