1 /* $NetBSD: list.c,v 1.3 2014/12/10 04:38:03 christos Exp $ */
4 * Automated Testing Framework (atf)
6 * Copyright (c) 2008 The NetBSD Foundation, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
19 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
27 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
29 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include "atf-c/error.h"
36 #include "atf-c/utils.h"
41 /* ---------------------------------------------------------------------
42 * Auxiliary functions.
43 * --------------------------------------------------------------------- */
46 struct list_entry
*m_prev
;
47 struct list_entry
*m_next
;
54 entry_to_citer(const atf_list_t
*l
, const struct list_entry
*le
)
56 atf_list_citer_t iter
;
64 entry_to_iter(atf_list_t
*l
, struct list_entry
*le
)
74 new_entry(void *object
, bool managed
)
76 struct list_entry
*le
;
78 le
= (struct list_entry
*)malloc(sizeof(*le
));
80 le
->m_prev
= le
->m_next
= NULL
;
81 le
->m_object
= object
;
82 le
->m_managed
= managed
;
91 delete_entry(struct list_entry
*le
)
101 new_entry_and_link(void *object
, bool managed
, struct list_entry
*prev
,
102 struct list_entry
*next
)
104 struct list_entry
*le
;
106 le
= new_entry(object
, managed
);
118 /* ---------------------------------------------------------------------
119 * The "atf_list_citer" type.
120 * --------------------------------------------------------------------- */
127 atf_list_citer_data(const atf_list_citer_t citer
)
129 const struct list_entry
*le
= citer
.m_entry
;
135 atf_list_citer_next(const atf_list_citer_t citer
)
137 const struct list_entry
*le
= citer
.m_entry
;
138 atf_list_citer_t newciter
;
143 newciter
.m_entry
= le
->m_next
;
149 atf_equal_list_citer_list_citer(const atf_list_citer_t i1
,
150 const atf_list_citer_t i2
)
152 return i1
.m_list
== i2
.m_list
&& i1
.m_entry
== i2
.m_entry
;
155 /* ---------------------------------------------------------------------
156 * The "atf_list_iter" type.
157 * --------------------------------------------------------------------- */
164 atf_list_iter_data(const atf_list_iter_t iter
)
166 const struct list_entry
*le
= iter
.m_entry
;
172 atf_list_iter_next(const atf_list_iter_t iter
)
174 const struct list_entry
*le
= iter
.m_entry
;
175 atf_list_iter_t newiter
;
180 newiter
.m_entry
= le
->m_next
;
186 atf_equal_list_iter_list_iter(const atf_list_iter_t i1
,
187 const atf_list_iter_t i2
)
189 return i1
.m_list
== i2
.m_list
&& i1
.m_entry
== i2
.m_entry
;
192 /* ---------------------------------------------------------------------
193 * The "atf_list" type.
194 * --------------------------------------------------------------------- */
197 * Constructors and destructors.
201 atf_list_init(atf_list_t
*l
)
203 struct list_entry
*lebeg
, *leend
;
205 lebeg
= new_entry(NULL
, false);
207 return atf_no_memory_error();
210 leend
= new_entry(NULL
, false);
213 return atf_no_memory_error();
216 lebeg
->m_next
= leend
;
217 lebeg
->m_prev
= NULL
;
219 leend
->m_next
= NULL
;
220 leend
->m_prev
= lebeg
;
226 return atf_no_error();
230 atf_list_fini(atf_list_t
*l
)
232 struct list_entry
*le
;
235 le
= (struct list_entry
*)l
->m_begin
;
238 struct list_entry
*lenext
;
246 INV(freed
== l
->m_size
+ 2);
254 atf_list_begin(atf_list_t
*l
)
256 struct list_entry
*le
= l
->m_begin
;
257 return entry_to_iter(l
, le
->m_next
);
261 atf_list_begin_c(const atf_list_t
*l
)
263 const struct list_entry
*le
= l
->m_begin
;
264 return entry_to_citer(l
, le
->m_next
);
268 atf_list_end(atf_list_t
*l
)
270 return entry_to_iter(l
, l
->m_end
);
274 atf_list_end_c(const atf_list_t
*l
)
276 return entry_to_citer(l
, l
->m_end
);
280 atf_list_index(atf_list_t
*list
, const size_t idx
)
282 atf_list_iter_t iter
;
284 PRE(idx
< atf_list_size(list
));
286 iter
= atf_list_begin(list
);
290 !atf_equal_list_iter_list_iter((iter
), atf_list_end(list
))) {
291 iter
= atf_list_iter_next(iter
);
295 return atf_list_iter_data(iter
);
299 atf_list_index_c(const atf_list_t
*list
, const size_t idx
)
301 atf_list_citer_t iter
;
303 PRE(idx
< atf_list_size(list
));
305 iter
= atf_list_begin_c(list
);
309 !atf_equal_list_citer_list_citer((iter
),
310 atf_list_end_c(list
))) {
311 iter
= atf_list_citer_next(iter
);
315 return atf_list_citer_data(iter
);
319 atf_list_size(const atf_list_t
*l
)
325 atf_list_to_charpp(const atf_list_t
*l
)
328 atf_list_citer_t iter
;
331 array
= malloc(sizeof(char *) * (atf_list_size(l
) + 1));
336 atf_list_for_each_c(iter
, l
) {
337 array
[i
] = strdup((const char *)atf_list_citer_data(iter
));
338 if (array
[i
] == NULL
) {
339 atf_utils_free_charpp(array
);
357 atf_list_append(atf_list_t
*l
, void *data
, bool managed
)
359 struct list_entry
*le
, *next
, *prev
;
362 next
= (struct list_entry
*)l
->m_end
;
364 le
= new_entry_and_link(data
, managed
, prev
, next
);
366 err
= atf_no_memory_error();
369 err
= atf_no_error();
376 atf_list_append_list(atf_list_t
*l
, atf_list_t
*src
)
378 struct list_entry
*e1
, *e2
, *ghost1
, *ghost2
;
380 ghost1
= (struct list_entry
*)l
->m_end
;
381 ghost2
= (struct list_entry
*)src
->m_begin
;
386 delete_entry(ghost1
);
387 delete_entry(ghost2
);
392 l
->m_end
= src
->m_end
;
393 l
->m_size
+= src
->m_size
;