1. hex2bin: 將字串 "F9" 轉成數值 0xF9 (取自 rtmpdump)
#define HEX2BIN(a) (((a)&0x40) ? ((a)&0xf) + 9: ((a)&0xf)) //這個轉換方式超棒
int hex2bin(char *str, char **hex)
{
char *ptr;
int i, l = strlen(str);
if (l & 1)
return 0;
*hex = malloc(l/2);
ptr = *hex;
if (!ptr)
return 0;
for (i=0; i*ptr++ = (HEX2BIN(str[i]) << 4) | HEX2BIN(str[i+1]);
return l/2;
}
2. linker script 內的 ALIGN 作法
#define ALIGN(exp) (. + exp - 1) & ~(exp - 1)3. linux kernel.h 內的幾個 Macro
// Return the result of the current location counter (.
) aligned to the next exp boundary. exp must be an expression whose value is a power of two.
#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0]))
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
#define min(x, y) ({ \
typeof(x) _min1 = (x); \
typeof(y) _min2 = (y); \
(void) (&_min1 == &_min2); \ /* type check*/
_min1 < _min2 ? _min1 : _min2; })
此處可參考 http://stackoverflow.com/questions/5595593/min-macro-in-kernel-h
4. 建議配置記憶體的方法 p = kmalloc(sizeof(*p), ...); ,此作法好處是好處是萬一pointer p的型別改變時,不會因為忘記同步更該型別的名稱而出錯。
5. TBD