3 Copyright (C) 2000-2004 Ivo Timmermans <ivo@tinc-vpn.org>
4 2000-2004 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.
20 $Id: list.c 1374 2004-03-21 14:21:22Z guus $
25 #include "support/list.h"
26 #include "support/xalloc.h"
28 list_t *list_new(list_action_t free) {
37 void list_free(list_t *list) {
41 list_node_t *list_node_new(void) {
44 return clear(new(node));
47 void list_node_free(list_t *list, list_node_t *node) {
48 if(node->data && list->free)
49 list->free(node->data);
54 list_node_t *list_add_head(list_t *list, void *data) {
57 node = list_node_new();
61 node->next = list->head;
65 node->next->prev = node;
74 list_node_t *list_add_tail(list_t *list, void *data) {
77 node = list_node_new();
81 node->prev = list->tail;
85 node->prev->next = node;
94 void list_unlink_node(list_t *list, list_node_t *node) {
96 node->prev->next = node->next;
98 list->head = node->next;
101 node->next->prev = node->prev;
103 list->tail = node->prev;
108 void list_del_node(list_t *list, list_node_t *node) {
109 list_unlink_node(list, node);
110 list_node_free(list, node);
113 void list_del_head(list_t *list) {
114 list_del_node(list, list->head);
117 void list_del_tail(list_t *list) {
118 list_del_node(list, list->tail);
121 void *list_get_head(const list_t *list) {
123 return list->head->data;
128 void *list_get_tail(const list_t *list) {
130 return list->tail->data;
135 void list_del(list_t *list) {
138 list_foreach_node(list, node, list_node_free(list, node));