2 list.c -- functions to deal with double linked lists
3 Copyright (C) 2000-2005 Ivo Timmermans
4 2000-2013 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 = xzalloc(sizeof(list_t));
30 list->delete = delete;
35 void list_free(list_t *list) {
39 list_node_t *list_alloc_node(void) {
40 return xzalloc(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);
51 /* Insertion and deletion */
53 list_node_t *list_insert_head(list_t *list, void *data) {
54 list_node_t *node = list_alloc_node();
58 node->next = list->head;
62 node->next->prev = node;
72 list_node_t *list_insert_tail(list_t *list, void *data) {
73 list_node_t *node = list_alloc_node();
77 node->prev = list->tail;
81 node->prev->next = node;
91 list_node_t *list_insert_after(list_t *list, list_node_t *after, void *data) {
92 list_node_t *node = list_alloc_node();
95 node->next = after->next;
100 node->next->prev = node;
110 list_node_t *list_insert_before(list_t *list, list_node_t *before, void *data) {
113 node = list_alloc_node();
117 node->prev = before->prev;
121 node->prev->next = node;
131 void list_unlink_node(list_t *list, list_node_t *node) {
133 node->prev->next = node->next;
135 list->head = node->next;
139 node->next->prev = node->prev;
141 list->tail = node->prev;
147 void list_delete_node(list_t *list, list_node_t *node) {
148 list_unlink_node(list, node);
149 list_free_node(list, node);
152 void list_delete_head(list_t *list) {
153 list_delete_node(list, list->head);
156 void list_delete_tail(list_t *list) {
157 list_delete_node(list, list->tail);
160 void list_delete(list_t *list, const void *data) {
161 for(list_node_t *node = list->head, *next; next = node ? node->next : NULL, node; node = next)
162 if(node->data == data) {
163 list_delete_node(list, node);
167 /* Head/tail lookup */
169 void *list_get_head(list_t *list) {
171 return list->head->data;
177 void *list_get_tail(list_t *list) {
179 return list->tail->data;
185 /* Fast list deletion */
187 void list_empty_list(list_t *list) {
188 for(list_node_t *node = list->head, *next; next = node ? node->next : NULL, node; node = next) {
189 list_free_node(list, node);
197 void list_delete_list(list_t *list) {
198 list_empty_list(list);
204 void list_foreach_node(list_t *list, list_action_node_t action) {
205 for(list_node_t *node = list->head, *next; next = node ? node->next : NULL, node; node = next) {
210 void list_foreach(list_t *list, list_action_t action) {
211 for(list_node_t *node = list->head, *next; next = node ? node->next : NULL, node; node = next)