2 * Automated Testing Framework (atf)
4 * Copyright (c) 2008 The NetBSD Foundation, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "atf-c/error.h"
34 #include "atf-c/utils.h"
39 /* ---------------------------------------------------------------------
40 * Auxiliary functions.
41 * --------------------------------------------------------------------- */
44 struct list_entry *m_prev;
45 struct list_entry *m_next;
52 entry_to_citer(const atf_list_t *l, const struct list_entry *le)
54 atf_list_citer_t iter;
62 entry_to_iter(atf_list_t *l, struct list_entry *le)
72 new_entry(void *object, bool managed)
74 struct list_entry *le;
76 le = (struct list_entry *)malloc(sizeof(*le));
78 le->m_prev = le->m_next = NULL;
79 le->m_object = object;
80 le->m_managed = managed;
89 delete_entry(struct list_entry *le)
99 new_entry_and_link(void *object, bool managed, struct list_entry *prev,
100 struct list_entry *next)
102 struct list_entry *le;
104 le = new_entry(object, managed);
116 /* ---------------------------------------------------------------------
117 * The "atf_list_citer" type.
118 * --------------------------------------------------------------------- */
125 atf_list_citer_data(const atf_list_citer_t citer)
127 const struct list_entry *le = citer.m_entry;
133 atf_list_citer_next(const atf_list_citer_t citer)
135 const struct list_entry *le = citer.m_entry;
136 atf_list_citer_t newciter;
141 newciter.m_entry = le->m_next;
147 atf_equal_list_citer_list_citer(const atf_list_citer_t i1,
148 const atf_list_citer_t i2)
150 return i1.m_list == i2.m_list && i1.m_entry == i2.m_entry;
153 /* ---------------------------------------------------------------------
154 * The "atf_list_iter" type.
155 * --------------------------------------------------------------------- */
162 atf_list_iter_data(const atf_list_iter_t iter)
164 const struct list_entry *le = iter.m_entry;
170 atf_list_iter_next(const atf_list_iter_t iter)
172 const struct list_entry *le = iter.m_entry;
173 atf_list_iter_t newiter;
178 newiter.m_entry = le->m_next;
184 atf_equal_list_iter_list_iter(const atf_list_iter_t i1,
185 const atf_list_iter_t i2)
187 return i1.m_list == i2.m_list && i1.m_entry == i2.m_entry;
190 /* ---------------------------------------------------------------------
191 * The "atf_list" type.
192 * --------------------------------------------------------------------- */
195 * Constructors and destructors.
199 atf_list_init(atf_list_t *l)
201 struct list_entry *lebeg, *leend;
203 lebeg = new_entry(NULL, false);
205 return atf_no_memory_error();
208 leend = new_entry(NULL, false);
211 return atf_no_memory_error();
214 lebeg->m_next = leend;
215 lebeg->m_prev = NULL;
217 leend->m_next = NULL;
218 leend->m_prev = lebeg;
224 return atf_no_error();
228 atf_list_fini(atf_list_t *l)
230 struct list_entry *le;
233 le = (struct list_entry *)l->m_begin;
236 struct list_entry *lenext;
244 INV(freed == l->m_size + 2);
252 atf_list_begin(atf_list_t *l)
254 struct list_entry *le = l->m_begin;
255 return entry_to_iter(l, le->m_next);
259 atf_list_begin_c(const atf_list_t *l)
261 const struct list_entry *le = l->m_begin;
262 return entry_to_citer(l, le->m_next);
266 atf_list_end(atf_list_t *l)
268 return entry_to_iter(l, l->m_end);
272 atf_list_end_c(const atf_list_t *l)
274 return entry_to_citer(l, l->m_end);
278 atf_list_index(atf_list_t *list, const size_t idx)
280 atf_list_iter_t iter;
282 PRE(idx < atf_list_size(list));
284 iter = atf_list_begin(list);
288 !atf_equal_list_iter_list_iter((iter), atf_list_end(list))) {
289 iter = atf_list_iter_next(iter);
293 return atf_list_iter_data(iter);
297 atf_list_index_c(const atf_list_t *list, const size_t idx)
299 atf_list_citer_t iter;
301 PRE(idx < atf_list_size(list));
303 iter = atf_list_begin_c(list);
307 !atf_equal_list_citer_list_citer((iter),
308 atf_list_end_c(list))) {
309 iter = atf_list_citer_next(iter);
313 return atf_list_citer_data(iter);
317 atf_list_size(const atf_list_t *l)
323 atf_list_to_charpp(const atf_list_t *l)
326 atf_list_citer_t iter;
329 array = malloc(sizeof(char *) * (atf_list_size(l) + 1));
334 atf_list_for_each_c(iter, l) {
335 array[i] = strdup((const char *)atf_list_citer_data(iter));
336 if (array[i] == NULL) {
337 atf_utils_free_charpp(array);
355 atf_list_append(atf_list_t *l, void *data, bool managed)
357 struct list_entry *le, *next, *prev;
360 next = (struct list_entry *)l->m_end;
362 le = new_entry_and_link(data, managed, prev, next);
364 err = atf_no_memory_error();
367 err = atf_no_error();
374 atf_list_append_list(atf_list_t *l, atf_list_t *src)
376 struct list_entry *e1, *e2, *ghost1, *ghost2;
378 ghost1 = (struct list_entry *)l->m_end;
379 ghost2 = (struct list_entry *)src->m_begin;
384 delete_entry(ghost1);
385 delete_entry(ghost2);
390 l->m_end = src->m_end;
391 l->m_size += src->m_size;