2 list.c -- functions to deal with double linked lists
3 Copyright (C) 2000-2005 Ivo Timmermans
4 2000-2006 Guus Sliepen <guus@tinc-vpn.org>
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.
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.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 /* (De)constructors */
28 list_t *list_alloc(list_action_t delete) {
29 list_t *list = xmalloc_and_zero(sizeof(list_t));
30 list->delete = delete;
35 void list_free(list_t *list) {
39 list_node_t *list_alloc_node(void) {
40 return xmalloc_and_zero(sizeof(list_node_t));
43 void list_free_node(list_t *list, list_node_t *node) {
44 if(node->data && list->delete)
45 list->delete(node->data);
50 /* Insertion and deletion */
52 list_node_t *list_insert_head(list_t *list, void *data) {
53 list_node_t *node = list_alloc_node();
57 node->next = list->head;
61 node->next->prev = node;
70 list_node_t *list_insert_tail(list_t *list, void *data) {
71 list_node_t *node = list_alloc_node();
75 node->prev = list->tail;
79 node->prev->next = node;
88 list_node_t *list_insert_after(list_t *list, list_node_t *after, void *data) {
89 list_node_t *node = list_alloc_node();
92 node->next = after->next;
97 node->next->prev = node;
106 list_node_t *list_insert_before(list_t *list, list_node_t *before, void *data) {
109 node = list_alloc_node();
113 node->prev = before->prev;
117 node->prev->next = node;
126 void list_unlink_node(list_t *list, list_node_t *node) {
128 node->prev->next = node->next;
130 list->head = node->next;
133 node->next->prev = node->prev;
135 list->tail = node->prev;
140 void list_delete_node(list_t *list, list_node_t *node) {
141 list_unlink_node(list, node);
142 list_free_node(list, node);
145 void list_delete_head(list_t *list) {
146 list_delete_node(list, list->head);
149 void list_delete_tail(list_t *list) {
150 list_delete_node(list, list->tail);
153 void list_delete(list_t *list, const void *data) {
154 for(list_node_t *node = list->head, *next; next = node ? node->next : NULL, node; node = next)
155 if(node->data == data)
156 list_delete_node(list, node);
159 /* Head/tail lookup */
161 void *list_get_head(list_t *list) {
163 return list->head->data;
168 void *list_get_tail(list_t *list) {
170 return list->tail->data;
175 /* Fast list deletion */
177 void list_delete_list(list_t *list) {
178 for(list_node_t *node = list->head, *next; next = node ? node->next : NULL, node; node = next)
179 list_free_node(list, node);
186 void list_foreach_node(list_t *list, list_action_node_t action) {
187 for(list_node_t *node = list->head, *next; next = node ? node->next : NULL, node; node = next)
191 void list_foreach(list_t *list, list_action_t action) {
192 for(list_node_t *node = list->head, *next; next = node ? node->next : NULL, node; node = next)