2 * Automated Testing Framework (atf)
4 * Copyright (c) 2008, 2009 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.
32 #include "atf-c/error.h"
33 #include "atf-c/list.h"
34 #include "atf-c/sanity.h"
36 /* ---------------------------------------------------------------------
37 * Auxiliary functions.
38 * --------------------------------------------------------------------- */
41 struct list_entry
*m_prev
;
42 struct list_entry
*m_next
;
49 entry_to_citer(const atf_list_t
*l
, const struct list_entry
*le
)
51 atf_list_citer_t iter
;
59 entry_to_iter(atf_list_t
*l
, struct list_entry
*le
)
69 new_entry(void *object
, bool managed
)
71 struct list_entry
*le
;
73 le
= (struct list_entry
*)malloc(sizeof(*le
));
75 le
->m_prev
= le
->m_next
= NULL
;
76 le
->m_object
= object
;
77 le
->m_managed
= managed
;
86 delete_entry(struct list_entry
*le
)
96 new_entry_and_link(void *object
, bool managed
, struct list_entry
*prev
,
97 struct list_entry
*next
)
99 struct list_entry
*le
;
101 le
= new_entry(object
, managed
);
113 /* ---------------------------------------------------------------------
114 * The "atf_list_citer" type.
115 * --------------------------------------------------------------------- */
122 atf_list_citer_data(const atf_list_citer_t citer
)
124 const struct list_entry
*le
= citer
.m_entry
;
130 atf_list_citer_next(const atf_list_citer_t citer
)
132 const struct list_entry
*le
= citer
.m_entry
;
133 atf_list_citer_t newciter
;
138 newciter
.m_entry
= le
->m_next
;
144 atf_equal_list_citer_list_citer(const atf_list_citer_t i1
,
145 const atf_list_citer_t i2
)
147 return i1
.m_list
== i2
.m_list
&& i1
.m_entry
== i2
.m_entry
;
150 /* ---------------------------------------------------------------------
151 * The "atf_list_iter" type.
152 * --------------------------------------------------------------------- */
159 atf_list_iter_data(const atf_list_iter_t iter
)
161 const struct list_entry
*le
= iter
.m_entry
;
167 atf_list_iter_next(const atf_list_iter_t iter
)
169 const struct list_entry
*le
= iter
.m_entry
;
170 atf_list_iter_t newiter
;
175 newiter
.m_entry
= le
->m_next
;
181 atf_equal_list_iter_list_iter(const atf_list_iter_t i1
,
182 const atf_list_iter_t i2
)
184 return i1
.m_list
== i2
.m_list
&& i1
.m_entry
== i2
.m_entry
;
187 /* ---------------------------------------------------------------------
188 * The "atf_list" type.
189 * --------------------------------------------------------------------- */
192 * Constructors and destructors.
196 atf_list_init(atf_list_t
*l
)
198 struct list_entry
*lebeg
, *leend
;
200 lebeg
= new_entry(NULL
, false);
202 return atf_no_memory_error();
205 leend
= new_entry(NULL
, false);
208 return atf_no_memory_error();
211 lebeg
->m_next
= leend
;
212 lebeg
->m_prev
= NULL
;
214 leend
->m_next
= NULL
;
215 leend
->m_prev
= lebeg
;
221 atf_object_init(&l
->m_object
);
223 return atf_no_error();
227 atf_list_fini(atf_list_t
*l
)
229 struct list_entry
*le
;
232 le
= (struct list_entry
*)l
->m_begin
;
235 struct list_entry
*lenext
;
243 INV(freed
== l
->m_size
+ 2);
245 atf_object_fini(&l
->m_object
);
253 atf_list_begin(atf_list_t
*l
)
255 struct list_entry
*le
= l
->m_begin
;
256 return entry_to_iter(l
, le
->m_next
);
260 atf_list_begin_c(const atf_list_t
*l
)
262 const struct list_entry
*le
= l
->m_begin
;
263 return entry_to_citer(l
, le
->m_next
);
267 atf_list_end(atf_list_t
*l
)
269 return entry_to_iter(l
, l
->m_end
);
273 atf_list_end_c(const atf_list_t
*l
)
275 return entry_to_citer(l
, l
->m_end
);
279 atf_list_index(atf_list_t
*list
, const size_t idx
)
281 atf_list_iter_t iter
;
283 PRE(idx
< atf_list_size(list
));
285 iter
= atf_list_begin(list
);
289 !atf_equal_list_iter_list_iter((iter
), atf_list_end(list
))) {
290 iter
= atf_list_iter_next(iter
);
294 return atf_list_iter_data(iter
);
298 atf_list_index_c(const atf_list_t
*list
, const size_t idx
)
300 atf_list_citer_t iter
;
302 PRE(idx
< atf_list_size(list
));
304 iter
= atf_list_begin_c(list
);
308 !atf_equal_list_citer_list_citer((iter
),
309 atf_list_end_c(list
))) {
310 iter
= atf_list_citer_next(iter
);
314 return atf_list_citer_data(iter
);
318 atf_list_size(const atf_list_t
*l
)
328 atf_list_append(atf_list_t
*l
, void *data
, bool managed
)
330 struct list_entry
*le
, *next
, *prev
;
333 next
= (struct list_entry
*)l
->m_end
;
335 le
= new_entry_and_link(data
, managed
, prev
, next
);
337 err
= atf_no_memory_error();
340 err
= atf_no_error();
347 atf_list_append_list(atf_list_t
*l
, atf_list_t
*src
)
349 struct list_entry
*e1
, *e2
, *ghost1
, *ghost2
;
351 ghost1
= (struct list_entry
*)l
->m_end
;
352 ghost2
= (struct list_entry
*)src
->m_begin
;
357 delete_entry(ghost1
);
358 delete_entry(ghost2
);
363 l
->m_end
= src
->m_end
;
364 l
->m_size
+= src
->m_size
;
366 atf_object_fini(&src
->m_object
);