s/malloc/xmalloc/ s/calloc/xcalloc/ s/realloc/realloc/
[oweals/opkg-lede.git] / libopkg / void_list.c
1 /* void_list.c - the opkg package management system
2
3    Carl D. Worth
4
5    Copyright (C) 2001 University of Southern California
6
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.
11
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.
16 */
17
18 #include "includes.h"
19 #include <errno.h>
20
21 #include "void_list.h"
22 #include "libbb/libbb.h"
23
24 int void_list_elt_init(void_list_elt_t *elt, void *data)
25 {
26     INIT_LIST_HEAD(&elt->node);
27     elt->data = data;
28
29     return 0;
30 }
31
32 void_list_elt_t * void_list_elt_new (void *data) {
33     void_list_elt_t *elt;
34     /* freed in void_list_elt_deinit */
35     elt = xcalloc(1, sizeof(void_list_elt_t));
36     void_list_elt_init(elt, data);
37     return elt;
38 }
39
40 void void_list_elt_deinit(void_list_elt_t *elt)
41 {
42     list_del_init(&elt->node);
43     void_list_elt_init(elt, NULL);
44     free(elt);
45 }
46
47 int void_list_init(void_list_t *list)
48 {
49     INIT_LIST_HEAD(&list->head);
50     return 0;
51 }
52
53 void void_list_deinit(void_list_t *list)
54 {
55     void_list_elt_t *elt;
56
57     while (!void_list_empty(list)) {
58         elt = void_list_pop(list);
59         void_list_elt_deinit(elt);
60     }
61     INIT_LIST_HEAD(&list->head);
62 }
63
64 int void_list_append(void_list_t *list, void *data)
65 {
66     void_list_elt_t *elt = void_list_elt_new(data);
67      
68     list_add_tail(&elt->node, &list->head);
69
70     return 0;
71 }
72
73 int void_list_push(void_list_t *list, void *data)
74 {
75     void_list_elt_t *elt = void_list_elt_new(data);
76
77     list_add(&elt->node, &list->head);
78
79     return 0;
80 }
81
82 void_list_elt_t *void_list_pop(void_list_t *list)
83 {
84     struct list_head *node;
85
86     if (void_list_empty(list)) 
87         return NULL;
88     node = list->head.next;
89     list_del_init(node);
90     return list_entry(node, void_list_elt_t, node);
91 }
92
93 void *void_list_remove(void_list_t *list, void_list_elt_t **iter)
94 {
95     void_list_elt_t *pos, *n;
96     void_list_elt_t *old_elt;
97     void *old_data = NULL;
98
99     old_elt = *iter;
100     if (!old_elt)
101         return old_data;
102     old_data = old_elt->data;
103
104     list_for_each_entry_safe(pos, n, &list->head, node) {
105         if (pos == old_elt)
106             break;
107     }
108     if ( pos != old_elt) {
109         fprintf(stderr, "%s: ERROR: element not found in list\n", __FUNCTION__);
110         return NULL;
111     }
112
113     *iter = list_entry(pos->node.prev, void_list_elt_t, node);
114     void_list_elt_deinit(pos);
115
116     return old_data;
117 }
118
119 /* remove element containing elt data, using cmp(elt->data, target_data) == 0. */
120 void *void_list_remove_elt(void_list_t *list, const void *target_data, void_list_cmp_t cmp)
121 {
122     void_list_elt_t *pos, *n;
123     void *old_data = NULL;
124     list_for_each_entry_safe(pos, n, &list->head, node) {
125         if ( pos->data && cmp(pos->data, target_data)==0 ){
126             old_data = pos->data;
127             void_list_elt_deinit(pos);
128             break;
129         }
130     }
131     return old_data;
132 }
133
134 void_list_elt_t *void_list_first(void_list_t *list) {
135     struct list_head *elt;
136     if (!list)
137         return NULL;
138     elt = list->head.next;
139     if (elt == &list->head ) {
140         return NULL;
141     }
142     return list_entry(elt, void_list_elt_t, node);
143 }
144
145 void_list_elt_t *void_list_prev(void_list_t *list, void_list_elt_t *node) {
146     struct list_head *elt;
147     if (!list || !node)
148         return NULL;
149     elt = node->node.prev;
150     if (elt == &list->head ) {
151         return NULL;
152     }
153     return list_entry(elt, void_list_elt_t, node);
154 }
155
156 void_list_elt_t *void_list_next(void_list_t *list, void_list_elt_t *node) {
157     struct list_head *elt;
158     if (!list || !node)
159         return NULL;
160     elt = node->node.next;
161     if (elt == &list->head ) {
162         return NULL;
163     }
164     return list_entry(elt, void_list_elt_t, node);
165 }
166
167 void_list_elt_t *void_list_last(void_list_t *list) {
168     struct list_head *elt;
169     if (!list)
170         return NULL;
171     elt = list->head.prev;
172     if (elt == &list->head ) {
173         return NULL;
174     }
175     return list_entry(elt, void_list_elt_t, node);
176 }
177