Created
December 4, 2010 15:06
-
-
Save neesenk/728240 to your computer and use it in GitHub Desktop.
比较有用的宏
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 编译时断言, cond为编译时常量 */ | |
#define BUILD_ASSERT(cond) do { (void) sizeof(char [1 - 2*!(cond)]); } while(0) | |
#define EXPR_BUILD_ASSERT(cond) (sizeof(char [1 - 2*!(cond)]) - 1) | |
/* 数据类型的对齐方式 == __alignof__(t) */ | |
#define ALIGNOF(t) ((char *)(&((struct { char c; t _h; } *)0)->_h) - (char *)0) | |
#define container_of(ptr, type, member) \ | |
((type *)((char *)(ptr) - (size_t)(&((type *)0)->member))) | |
#define likely(cond) __builtin_expect(!!(cond), 1) | |
#define unlikely(cond) __builtin_expect(!!(cond), 0) | |
#define __ARRAY_TYPE_CHK(a) \ | |
EXPR_BUILD_ASSERT(!__builtin_types_compatible_p(typeof(a), typeof(&(a)[0]))) | |
/* 数组的长度 */ | |
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __ARRAY_TYPE_CHK(arr)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment