#define ARRAY_SIZE(X) ((sizeof((X)))/(sizeof((X)[0])))
#define ALIGN(x,a) (((x)+(a)-1) & (~((a)-1)))
#define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
#define abs(x) ({ \
int __x = (x); \
(__x < 0) ? -__x : __x; \
})
//太有用的宏了, 一定要掌握
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
//也非常有用
/**
* container_of - cast a member of a structure out to the containing structure
* @ptr: the pointer to the member.
* @type: the type of the container struct this is embedded in.
* @member: the name of the member within the struct.
*
*/
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
//#define container_of(ptr,type,member)
//({ \
// const typeof(((type *)0)->member) *__mptr = (ptr); \ //仅仅计算一次
// (type *)((char *)__mptr - offsetof(type,member)) ; \
//})
/**
* list_entry - get the struct for this entry
* @ptr: the &struct list_head pointer.
* @type: the type of the struct this is embedded in.
* @member: the name of the list_struct within the struct.
*/
#define list_entry(ptr, type, member) \
container_of(ptr, type, member)
//#define abs(x) ((x) > 0)?(x):(-(x))
/*
* ..and if you can't take the strict
* types, you can specify one yourself.
*
* Or not use min/max at all, of course.
*/
#define MIN_T(type,x,y) \
({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
#define MAX_T(type,x,y) \
({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
#define MAX(x,y) ( (x)>(y)?(x):(y) )
#define MAX_CHECK(x,y) \
({ \
typeof(x) __x = (x); \
typeof(y) __y = (y); \
(void) (&__x == &__y); \
(__x>__y) ?__x:__y ;\
})
|