xref: /haiku/src/add-ons/kernel/bus_managers/scsi/dl_list.h (revision 4bd0c1066b227cec4b79883bdef697c7a27f2e90)
1 /*
2 ** Copyright 2002/03, Thomas Kurschel. All rights reserved.
3 ** Distributed under the terms of the MIT License.
4 */
5 
6 /*
7 	Macros for double linked lists
8 */
9 
10 #ifndef _DL_LIST_H
11 #define _DL_LIST_H
12 
13 #define REMOVE_DL_LIST( item, head, prefix ) \
14 	do { \
15 		if( item->prefix##prev ) \
16 			item->prefix##prev->prefix##next = item->prefix##next; \
17 		else \
18 			head = item->prefix##next; \
19 \
20 		if( item->prefix##next ) \
21 			item->prefix##next->prefix##prev = item->prefix##prev; \
22 	} while( 0 )
23 
24 #define ADD_DL_LIST_HEAD( item, head, prefix ) \
25 	do { \
26 		item->prefix##next = head; \
27 		item->prefix##prev = NULL; \
28 \
29 		if( (head) ) \
30 			(head)->prefix##prev = item; \
31 \
32 		(head) = item; \
33 	} while( 0 )
34 
35 #define REMOVE_CDL_LIST( item, head, prefix ) \
36 	do { \
37 		item->prefix##next->prefix##prev = item->prefix##prev; \
38 		item->prefix##prev->prefix##next = item->prefix##next; \
39  \
40 		if( item == (head) ) { \
41 			if( item->prefix##next != item ) \
42 				(head) = item->prefix##next; \
43 			else \
44 				(head) = NULL; \
45 		} \
46 	} while( 0 )
47 
48 #define ADD_CDL_LIST_TAIL( item, type, head, prefix ) \
49 	do { \
50 		type *old_head = head; \
51  \
52 		if( old_head ) { \
53 			type *first, *last; \
54  \
55 			first = old_head; \
56 			last = first->prefix##prev; \
57  \
58 			item->prefix##next = first; \
59 			item->prefix##prev = last; \
60 			first->prefix##prev = item; \
61 			last->prefix##next = item; \
62 		} else { \
63 			head = item; \
64 			item->prefix##next = item->prefix##prev = item; \
65 		} \
66 	} while( 0 )
67 
68 #define ADD_CDL_LIST_HEAD( item, type, head, prefix ) \
69 	do { \
70 		type *old_head = head; \
71  \
72  		head = item; \
73 		if( old_head ) { \
74 			type *first, *last; \
75  \
76 			first = old_head; \
77 			last = first->prefix##prev; \
78  \
79 			item->prefix##next = first; \
80 			item->prefix##prev = last; \
81 			first->prefix##prev = item; \
82 			last->prefix##next = item; \
83 		} else { \
84 			item->prefix##next = item->prefix##prev = item; \
85 		} \
86 	} while( 0 )
87 
88 #endif
89