/*----- doubly linked lists -----*/
-#define ISNODE(T) struct { T *succ, *pred; } node /* must be at start */
-#define DEFLIST(T) typedef struct { T *hd, *tl, *tp; int count; } T##List
+#define ISNODE(T) struct node list_node
+#define DEFLIST(T) \
+ typedef struct { \
+ union { struct list li; T *for_type; } u; \
+ int count; \
+ } T##List
-#define NODE(n) (assert((void*)&(n)->node == &(n)), \
- (struct node*)&(n)->node)
+#define NODE(n) (assert((void*)&(n)->list_node == &(n)), &(n)->list_node)
#define LIST_CHECKCANHAVENODE(l,n) \
- ((void)((n) == ((l).hd))) /* just for the type check */
+ ((void)((n) == ((l).u.for_type))) /* just for the type check */
-#define LIST_ADDSOMEHOW(l,n,list_addsomehow) \
- ( LIST_CHECKCANHAVENODE(l,n), \
- list_addsomehow((struct list*)&(l), NODE((n))), \
- (void)(l).count++ \
+#define LIST_ADDSOMEHOW(l,n,list_addsomehow) \
+ ( LIST_CHECKCANHAVENODE(l,n), \
+ list_addsomehow(&(l).u.li, NODE((n))), \
+ (void)(l).count++ \
)
#define LIST_REMSOMEHOW(l,list_remsomehow) \
- ( (typeof((l).hd)) \
+ ( (typeof((l).u.for_type)) \
( (l).count \
? ( (l).count--, \
- list_remsomehow((struct list*)&(l)) ) \
+ list_remsomehow(&(l).u.li) ) \
: 0 \
) \
)
#define LIST_REMHEAD(l) LIST_REMSOMEHOW((l),list_remhead)
#define LIST_REMTAIL(l) LIST_REMSOMEHOW((l),list_remtail)
-#define LIST_INIT(l) ((l).hd, list_new((struct list*)&(l)))
-#define LIST_HEAD(l) ((typeof((l).hd))(list_head((struct list*)&(l))))
+#define LIST_INIT(l) (list_new(&(l).u.li))
+#define LIST_HEAD(l) ((typeof((l).u.for_type))(list_head((struct list*)&(l))))
#define LIST_NEXT(n) ((typeof(n))list_succ(NODE((n))))
#define LIST_BACK(n) ((typeof(n))list_pred(NODE((n))))