Update documents.
[tinc] / src / list.h
1 /*
2     list.h -- linked lists
3
4     Copyright (C) 2000-2004 Ivo Timmermans <ivo@tinc-vpn.org>
5                   2000-2004 Guus Sliepen <guus@tinc-vpn.org>
6
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20
21     $Id: list.h 1374 2004-03-21 14:21:22Z guus $
22 */
23
24 #ifndef __LIST_H__
25 #define __LIST_H__
26
27 typedef struct list_node {
28         struct list_node *prev;
29         struct list_node *next;
30
31         void *data;
32 } list_node_t;
33
34 typedef void (*list_action_t)(const void *);
35 typedef void (*list_node_action_t)(const list_node_t *);
36
37 typedef struct list {
38         struct list_node *head;
39         struct list_node *tail;
40         int count;
41
42         list_action_t free;
43 } list_t;
44
45 /* (De)constructors */
46
47 extern struct list *list_new(list_action_t) __attribute__ ((__malloc__));
48 extern void list_free(struct list *);
49 extern struct list_node *list_node_new(void);
50 extern void list_node_free(struct list *, struct list_node *);
51
52 /* Insertion and deletion */
53
54 extern struct list_node *list_add_head(struct list *, void *);
55 extern struct list_node *list_add_tail(struct list *, void *);
56
57 extern void list_unlink_node(struct list *, struct list_node *);
58 extern void list_node_del(struct list *, struct list_node *);
59
60 extern void list_del_head(struct list *);
61 extern void list_del_tail(struct list *);
62
63 /* Head/tail lookup */
64
65 extern void *list_get_head(const struct list *);
66 extern void *list_get_tail(const struct list *);
67
68 /* Fast list deletion */
69
70 extern void list_del(struct list *);
71
72 /* Traversing */
73
74 #define list_foreach(list, object, action) {list_node_t *_node, *_next; \
75         for(_node = (list)->head; _node; _node = _next) { \
76                 _next = _node->next; \
77                 (object) = _node->data; \
78                 action; \
79         } \
80 }
81
82 #define list_foreach_node(list, node, action) {list_node_t *_next; \
83         for((node) = (list)->head; (node); (node) = _next) { \
84                 _next = (node)->next; \
85                 action; \
86         } \
87 }
88
89 #endif