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
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 /* (De)constructors */
30 list_t *list_alloc(list_action_t delete) {
33 list = xmalloc_and_zero(sizeof(list_t));
34 list->delete = delete;
39 void list_free(list_t *list) {
43 list_node_t *list_alloc_node(void) {
44 return xmalloc_and_zero(sizeof(list_node_t));
47 void list_free_node(list_t *list, list_node_t *node) {
48 if(node->data && list->delete)
49 list->delete(node->data);
54 /* Insertion and deletion */
56 list_node_t *list_insert_head(list_t *list, void *data) {
59 node = list_alloc_node();
63 node->next = list->head;
67 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;
96 void list_unlink_node(list_t *list, list_node_t *node) {
98 node->prev->next = node->next;
100 list->head = node->next;
103 node->next->prev = node->prev;
105 list->tail = node->prev;
110 void list_delete_node(list_t *list, list_node_t *node) {
111 list_unlink_node(list, node);
112 list_free_node(list, node);
115 void list_delete_head(list_t *list) {
116 list_delete_node(list, list->head);
119 void list_delete_tail(list_t *list) {
120 list_delete_node(list, list->tail);
123 /* Head/tail lookup */
125 void *list_get_head(list_t *list) {
127 return list->head->data;
132 void *list_get_tail(list_t *list) {
134 return list->tail->data;
139 /* Fast list deletion */
141 void list_delete_list(list_t *list) {
142 list_node_t *node, *next;
144 for(node = list->head; node; node = next) {
146 list_free_node(list, node);
154 void list_foreach_node(list_t *list, list_action_node_t action) {
155 list_node_t *node, *next;
157 for(node = list->head; node; node = next) {
163 void list_foreach(list_t *list, list_action_t action) {
164 list_node_t *node, *next;
166 for(node = list->head; node; node = next) {