1 |
/* |
2 |
* ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd). |
3 |
* list.h: A header for the code in list.c. |
4 |
* |
5 |
* Copyright (C) 2002 by the past and present ircd coders, and others. |
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
20 |
* USA |
21 |
* |
22 |
* $Id$ |
23 |
*/ |
24 |
|
25 |
/* |
26 |
* double-linked-list stuff |
27 |
*/ |
28 |
typedef struct _dlink_node dlink_node; |
29 |
typedef struct _dlink_list dlink_list; |
30 |
|
31 |
#ifdef IN_MISC_C |
32 |
extern void init_dlink_nodes(void); |
33 |
#endif |
34 |
LIBIO_EXTERN void free_dlink_node(dlink_node *); |
35 |
LIBIO_EXTERN dlink_node *make_dlink_node(void); |
36 |
|
37 |
struct _dlink_node |
38 |
{ |
39 |
void *data; |
40 |
dlink_node *prev; |
41 |
dlink_node *next; |
42 |
}; |
43 |
|
44 |
struct _dlink_list |
45 |
{ |
46 |
dlink_node *head; |
47 |
dlink_node *tail; |
48 |
unsigned long length; |
49 |
}; |
50 |
|
51 |
LIBIO_EXTERN void dlinkAdd(void *, dlink_node *, dlink_list *); |
52 |
LIBIO_EXTERN void dlinkAddBefore(dlink_node *, void *, dlink_node *, dlink_list *); |
53 |
LIBIO_EXTERN void dlinkAddTail(void *, dlink_node *, dlink_list *); |
54 |
LIBIO_EXTERN void dlinkDelete(dlink_node *, dlink_list *); |
55 |
LIBIO_EXTERN void dlinkMoveList(dlink_list *, dlink_list *); |
56 |
LIBIO_EXTERN dlink_node *dlinkFind(dlink_list *, void *); |
57 |
LIBIO_EXTERN dlink_node *dlinkFindDelete(dlink_list *, void *); |
58 |
|
59 |
/* |
60 |
* These macros are basically swiped from the linux kernel |
61 |
* they are simple yet effective |
62 |
*/ |
63 |
|
64 |
/* |
65 |
* Walks forward of a list. |
66 |
* pos is your node |
67 |
* head is your list head |
68 |
*/ |
69 |
#define DLINK_FOREACH(pos, head) for (pos = (head); pos != NULL; pos = pos->next) |
70 |
|
71 |
/* |
72 |
* Walks forward of a list safely while removing nodes |
73 |
* pos is your node |
74 |
* n is another list head for temporary storage |
75 |
* head is your list head |
76 |
*/ |
77 |
#define DLINK_FOREACH_SAFE(pos, n, head) for (pos = (head), n = pos ? pos->next : NULL; pos != NULL; pos = n, n = pos ? pos->next : NULL) |
78 |
#define DLINK_FOREACH_PREV(pos, head) for (pos = (head); pos != NULL; pos = pos->prev) |
79 |
|
80 |
/* Returns the list length */ |
81 |
#define dlink_list_length(list) (list)->length |