9e54ac081810f4d85f93ae73e675e50155b16b2a
[tinc] / support / list.c
1 /*
2     list.c -- linked lists
3     Copyright (C) 2000-2004 Ivo Timmermans <ivo@tinc-vpn.org>
4                   2000-2004 Guus Sliepen <guus@tinc-vpn.org>
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 1374 2004-03-21 14:21:22Z guus $
21 */
22
23 #include "system.h"
24
25 #include "support/list.h"
26 #include "support/xalloc.h"
27
28 list_t *list_new(list_action_t free) {
29         list_t *list;
30
31         clear(new(list));
32         list->free = free;
33
34         return list;
35 }
36
37 void list_free(list_t *list) {
38         free(list);
39 }
40
41 list_node_t *list_node_new(void) {
42         list_node_t *node;
43
44         return clear(new(node));
45 }
46
47 void list_node_free(list_t *list, list_node_t *node) {
48         if(node->data && list->free)
49                 list->free(node->data);
50
51         free(node);
52 }
53
54 list_node_t *list_add_head(list_t *list, void *data) {
55         list_node_t *node;
56
57         node = list_node_new();
58
59         node->data = data;
60         node->prev = NULL;
61         node->next = list->head;
62         list->head = node;
63
64         if(node->next)
65                 node->next->prev = node;
66         else
67                 list->tail = node;
68
69         list->count++;
70
71         return node;
72 }
73
74 list_node_t *list_add_tail(list_t *list, void *data) {
75         list_node_t *node;
76
77         node = list_node_new();
78
79         node->data = data;
80         node->next = NULL;
81         node->prev = list->tail;
82         list->tail = node;
83
84         if(node->prev)
85                 node->prev->next = node;
86         else
87                 list->head = node;
88
89         list->count++;
90
91         return node;
92 }
93
94 void list_unlink_node(list_t *list, list_node_t *node) {
95         if(node->prev)
96                 node->prev->next = node->next;
97         else
98                 list->head = node->next;
99
100         if(node->next)
101                 node->next->prev = node->prev;
102         else
103                 list->tail = node->prev;
104
105         list->count--;
106 }
107
108 void list_del_node(list_t *list, list_node_t *node) {
109         list_unlink_node(list, node);
110         list_node_free(list, node);
111 }
112
113 void list_del_head(list_t *list) {
114         list_del_node(list, list->head);
115 }
116
117 void list_del_tail(list_t *list) {
118         list_del_node(list, list->tail);
119 }
120
121 void *list_get_head(const list_t *list) {
122         if(list->head)
123                 return list->head->data;
124         else
125                 return NULL;
126 }
127
128 void *list_get_tail(const list_t *list) {
129         if(list->tail)
130                 return list->tail->data;
131         else
132                 return NULL;
133 }
134
135 void list_del(list_t *list) {
136         list_node_t *node;
137
138         list_foreach_node(list, node, list_node_free(list, node));
139         list_free(list);
140 }