1 /*
2  * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
3  * All rights reserved
4  *
5  * "THE BEER-WARE LICENSE" (Revision 42):
6  * Sergey Lyubka wrote this file.  As long as you retain this notice you
7  * can do whatever you want with this stuff. If we meet some day, and you think
8  * this stuff is worth it, you can buy me a beer in return.
9  */
10 
11 #ifndef LLIST_HEADER_INCLUDED
12 #define	LLIST_HEADER_INCLUDED
13 
14 /*
15  * Linked list macros.
16  */
17 struct llhead {
18 	struct llhead	*prev;
19 	struct llhead	*next;
20 };
21 
22 #define	LL_INIT(N)	((N)->next = (N)->prev = (N))
23 
24 #define LL_HEAD(H)	struct llhead H = { &H, &H }
25 
26 #define LL_ENTRY(P,T,N) ((T *)((char *)(P) - offsetof(T, N)))
27 
28 #define	LL_ADD(H, N)							\
29 	do {								\
30 		((H)->next)->prev = (N);				\
31 		(N)->next = ((H)->next);				\
32 		(N)->prev = (H);					\
33 		(H)->next = (N);					\
34 	} while (0)
35 
36 #define	LL_TAIL(H, N)							\
37 	do {								\
38 		((H)->prev)->next = (N);				\
39 		(N)->prev = ((H)->prev);				\
40 		(N)->next = (H);					\
41 		(H)->prev = (N);					\
42 	} while (0)
43 
44 #define	LL_DEL(N)							\
45 	do {								\
46 		((N)->next)->prev = ((N)->prev);			\
47 		((N)->prev)->next = ((N)->next);			\
48 		LL_INIT(N);						\
49 	} while (0)
50 
51 #define	LL_EMPTY(N)	((N)->next == (N))
52 
53 #define	LL_FOREACH(H,N)	for (N = (H)->next; N != (H); N = (N)->next)
54 
55 #define LL_FOREACH_SAFE(H,N,T)						\
56 	for (N = (H)->next, T = (N)->next; N != (H);			\
57 			N = (T), T = (N)->next)
58 
59 #endif /* LLIST_HEADER_INCLUDED */
60