Home | History | Annotate | Download | only in src
      1 /*
      2  * This file is not copyrighted.
      3  */
      4 
      5 #ifndef _WSBM_UTIL_H_
      6 #define _WSBM_UTIL_H_
      7 
      8 #include <stddef.h>
      9 
     10 #ifndef containerOf
     11 #define containerOf(__item, __type, __field)				\
     12       ((__type *)(((char *) (__item)) - offsetof(__type, __field)))
     13 #endif
     14 
     15 struct _WsbmListHead
     16 {
     17     struct _WsbmListHead *prev;
     18     struct _WsbmListHead *next;
     19 };
     20 
     21 #define WSBMINITLISTHEAD(__item)		       \
     22     do{						       \
     23 	(__item)->prev = (__item);		       \
     24 	(__item)->next = (__item);		       \
     25     } while (0)
     26 
     27 #define WSBMLISTADD(__item, __list)		\
     28   do {						\
     29     (__item)->prev = (__list);			\
     30     (__item)->next = (__list)->next;		\
     31     (__list)->next->prev = (__item);		\
     32     (__list)->next = (__item);			\
     33   } while (0)
     34 
     35 #define WSBMLISTADDTAIL(__item, __list)		\
     36   do {						\
     37     (__item)->next = (__list);			\
     38     (__item)->prev = (__list)->prev;		\
     39     (__list)->prev->next = (__item);		\
     40     (__list)->prev = (__item);			\
     41   } while(0)
     42 
     43 #define WSBMLISTDEL(__item)			\
     44   do {						\
     45     (__item)->prev->next = (__item)->next;	\
     46     (__item)->next->prev = (__item)->prev;	\
     47   } while(0)
     48 
     49 #define WSBMLISTDELINIT(__item)			\
     50   do {						\
     51     (__item)->prev->next = (__item)->next;	\
     52     (__item)->next->prev = (__item)->prev;	\
     53     (__item)->next = (__item);			\
     54     (__item)->prev = (__item);			\
     55   } while(0)
     56 
     57 #define WSBMLISTFOREACH(__item, __list) \
     58     for((__item) = (__list)->next; (__item) != (__list); (__item) = (__item)->next)
     59 
     60 #define WSBMLISTFOREACHPREV(__item, __list) \
     61     for((__item) = (__list)->prev; (__item) != (__list); (__item) = (__item)->prev)
     62 
     63 #define WSBMLISTFOREACHSAFE(__item, __next, __list)		\
     64         for((__item) = (__list)->next, (__next) = (__item)->next;	\
     65 	(__item) != (__list);					\
     66 	(__item) = (__next), (__next) = (__item)->next)
     67 
     68 #define WSBMLISTFOREACHPREVSAFE(__item, __prev, __list)		\
     69     for((__item) = (__list)->prev, (__prev) = (__item->prev);	\
     70 	(__item) != (__list);					\
     71 	(__item) = (__prev), (__prev) = (__item)->prev)
     72 
     73 #define WSBMLISTENTRY(__item, __type, __field)			\
     74   containerOf(__item, __type, __field)
     75 
     76 #endif
     77