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) {
31 list = xmalloc_and_zero(sizeof(list_t));
32 list->delete = delete;
37 void list_free(list_t *list) {
41 list_node_t *list_alloc_node(void) {
42 return xmalloc_and_zero(sizeof(list_node_t));
45 void list_free_node(list_t *list, list_node_t *node) {
46 if(node->data && list->delete) {
47 list->delete(node->data);
53 /* Insertion and deletion */
55 list_node_t *list_insert_head(list_t *list, void *data) {
58 node = list_alloc_node();
62 node->next = list->head;
66 node->next->prev = node;
76 list_node_t *list_insert_tail(list_t *list, void *data) {
79 node = list_alloc_node();
83 node->prev = list->tail;
87 node->prev->next = node;
97 void list_unlink_node(list_t *list, list_node_t *node) {
99 node->prev->next = node->next;
101 list->head = node->next;
105 node->next->prev = node->prev;
107 list->tail = node->prev;
113 void list_delete_node(list_t *list, list_node_t *node) {
114 list_unlink_node(list, node);
115 list_free_node(list, node);
118 void list_delete_head(list_t *list) {
119 list_delete_node(list, list->head);
122 void list_delete_tail(list_t *list) {
123 list_delete_node(list, list->tail);
126 /* Head/tail lookup */
128 void *list_get_head(list_t *list) {
130 return list->head->data;
136 void *list_get_tail(list_t *list) {
138 return list->tail->data;
144 /* Fast list deletion */
146 void list_delete_list(list_t *list) {
147 list_node_t *node, *next;
149 for(node = list->head; node; node = next) {
151 list_free_node(list, node);
159 void list_foreach_node(list_t *list, list_action_node_t action) {
160 list_node_t *node, *next;
162 for(node = list->head; node; node = next) {
168 void list_foreach(list_t *list, list_action_t action) {
169 list_node_t *node, *next;
171 for(node = list->head; node; node = next) {