1 /* void_list.c - the opkg package management system
5 Copyright (C) 2001 University of Southern California
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2, or (at
10 your option) any later version.
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
21 #include "void_list.h"
23 int void_list_elt_init(void_list_elt_t
*elt
, void *data
)
25 INIT_LIST_HEAD(&elt
->node
);
31 void_list_elt_t
* void_list_elt_new (void *data
) {
33 /* freed in void_list_deinit */
34 elt
= calloc(1, sizeof(void_list_elt_t
));
36 fprintf(stderr
, "%s: out of memory\n", __FUNCTION__
);
39 void_list_elt_init(elt
, data
);
43 void void_list_elt_deinit(void_list_elt_t
*elt
)
45 list_del_init(&elt
->node
);
46 void_list_elt_init(elt
, NULL
);
49 int void_list_init(void_list_t
*list
)
51 INIT_LIST_HEAD(&list
->head
);
55 void void_list_deinit(void_list_t
*list
)
59 while (!void_list_empty(list
)) {
60 elt
= void_list_pop(list
);
61 void_list_elt_deinit(elt
);
62 /* malloced in void_list_append */
65 INIT_LIST_HEAD(&list
->head
);
68 int void_list_append(void_list_t
*list
, void *data
)
70 void_list_elt_t
*elt
= void_list_elt_new(data
);
72 list_add_tail(&elt
->node
, &list
->head
);
77 int void_list_push(void_list_t
*list
, void *data
)
79 void_list_elt_t
*elt
= void_list_elt_new(data
);
81 list_add(&elt
->node
, &list
->head
);
86 void_list_elt_t
*void_list_pop(void_list_t
*list
)
88 struct list_head
*node
;
90 if (void_list_empty(list
))
92 node
= list
->head
.next
;
94 return list_entry(node
, void_list_elt_t
, node
);
97 void *void_list_remove(void_list_t
*list
, void_list_elt_t
**iter
)
99 void_list_elt_t
*pos
, *n
;
100 void_list_elt_t
*old_elt
;
101 void *old_data
= NULL
;
106 old_data
= old_elt
->data
;
108 list_for_each_entry_safe(pos
, n
, &list
->head
, node
) {
112 if ( pos
!= old_elt
) {
113 fprintf(stderr
, "%s: ERROR: element not found in list\n", __FUNCTION__
);
117 *iter
= list_entry(pos
->node
.prev
, void_list_elt_t
, node
);
118 void_list_elt_deinit(pos
);
124 /* remove element containing elt data, using cmp(elt->data, target_data) == 0. */
125 void *void_list_remove_elt(void_list_t
*list
, const void *target_data
, void_list_cmp_t cmp
)
127 void_list_elt_t
*pos
, *n
;
128 void *old_data
= NULL
;
129 list_for_each_entry_safe(pos
, n
, &list
->head
, node
) {
130 if ( pos
->data
&& cmp(pos
->data
, target_data
)==0 ){
131 old_data
= pos
->data
;
132 void_list_elt_deinit(pos
);
139 void_list_elt_t
*void_list_first(void_list_t
*list
) {
140 struct list_head
*elt
;
143 elt
= list
->head
.next
;
144 if (elt
== &list
->head
) {
147 return list_entry(elt
, void_list_elt_t
, node
);
150 void_list_elt_t
*void_list_prev(void_list_t
*list
, void_list_elt_t
*node
) {
151 struct list_head
*elt
;
154 elt
= node
->node
.prev
;
155 if (elt
== &list
->head
) {
158 return list_entry(elt
, void_list_elt_t
, node
);
161 void_list_elt_t
*void_list_next(void_list_t
*list
, void_list_elt_t
*node
) {
162 struct list_head
*elt
;
165 elt
= node
->node
.next
;
166 if (elt
== &list
->head
) {
169 return list_entry(elt
, void_list_elt_t
, node
);
172 void_list_elt_t
*void_list_last(void_list_t
*list
) {
173 struct list_head
*elt
;
176 elt
= list
->head
.prev
;
177 if (elt
== &list
->head
) {
180 return list_entry(elt
, void_list_elt_t
, node
);