Merge do_prune() with build_fdset(). Probably fixes the invalid filedescriptor error.
[tinc] / lib / list.c
1 /*
2     list.c -- functions to deal with double linked lists
3     Copyright (C) 2000,2001 Ivo Timmermans <itimmermans@bigfoot.com>
4                   2000,2001 Guus Sliepen <guus@sliepen.warande.net>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20     $Id: list.c,v 1.1.2.9 2001/02/25 15:34:50 guus Exp $
21 */
22
23 #include "config.h"
24
25 #include <stdlib.h>
26
27 #include <xalloc.h>
28 #include <system.h>
29
30 #include "list.h"
31
32 /* (De)constructors */
33
34 list_t *list_alloc(list_action_t delete)
35 {
36   list_t *list;
37
38   list = xmalloc_and_zero(sizeof(list_t));
39   list->delete = delete;
40
41   return list;
42 }
43
44 void list_free(list_t *list)
45 {
46   free(list);
47 }
48
49 list_node_t *list_alloc_node(void)
50 {
51   list_node_t *node;
52   
53   node = xmalloc_and_zero(sizeof(list_node_t));
54   
55   return node;
56 }
57
58 void list_free_node(list_t *list, list_node_t *node)
59 {
60   if(node->data && list->delete)
61     list->delete(node->data);
62   
63   free(node);
64 }
65
66 /* Insertion and deletion */
67
68 list_node_t *list_insert_head(list_t *list, void *data)
69 {
70   list_node_t *node;
71   
72   node = list_alloc_node();
73   
74   node->data = data;
75   node->prev = NULL;
76   node->next = list->head;
77   list->head = node;
78   
79   if(node->next)
80     node->next->prev = node;
81   else
82     list->tail = node;
83
84   return node;
85 }
86
87 list_node_t *list_insert_tail(list_t *list, void *data)
88 {
89   list_node_t *node;
90   
91   node = list_alloc_node();
92   
93   node->data = data;
94   node->next = NULL;
95   node->prev = list->tail;
96   list->tail = node;
97   
98   if(node->prev)
99     node->prev->next = node;
100   else
101     list->head = node;
102
103   return node;
104 }
105
106 void list_unlink_node(list_t *list, list_node_t *node)
107 {
108   if(node->prev)
109     node->prev->next = node->next;
110   else
111     list->head = node->next;
112     
113   if(node->next)
114     node->next->prev = node->prev;
115   else
116     list->tail = node->prev;
117 }
118
119 void list_delete_node(list_t *list, list_node_t *node)
120 {
121   list_unlink_node(list, node);
122   list_free_node(list, node);
123 }
124
125 void list_delete_head(list_t *list)
126 {
127   list_delete_node(list, list->head);
128 }
129
130 void list_delete_tail(list_t *list)
131 {
132   list_delete_node(list, list->tail);
133 }
134
135 /* Head/tail lookup */
136
137 void *list_get_head(list_t *list)
138 {
139   if(list->head)
140     return list->head->data;
141   else
142     return NULL;
143 }
144
145 void *list_get_tail(list_t *list)
146 {
147   if(list->tail)
148     return list->tail->data;
149   else
150     return NULL;
151 }
152
153 /* Fast list deletion */
154
155 void list_delete_list(list_t *list)
156 {
157   list_node_t *node, *next;
158   
159   for(node = list->head; node; node = next)
160     {
161       next = node->next;
162       list_free_node(list, node);
163     }
164
165   list_free(list);
166 }
167
168 /* Traversing */
169
170 void list_foreach_node(list_t *list, list_action_node_t action)
171 {
172   list_node_t *node, *next;
173
174   for(node = list->head; node; node = next)
175     {
176       next = node->next;
177       action(node);
178     }
179 }
180
181 void list_foreach(list_t *list, list_action_t action)
182 {
183   list_node_t *node, *next;
184
185   for(node = list->head; node; node = next)
186     {
187       next = node->next;
188       if(node->data)
189         action(node->data);
190     }
191 }