linux核心關於字元判斷的巨集:
用幾個巨集表示字母、數字、空白、控制符等,再組合為字母或數字等。
4/*
5* note! this ctype does not handle eof like the standard c
6* library is required to.
7*/
89
#define
_u0x01
/* upper */
10#define
_l0x02
/* lower */
11#define
_d0x04
/* digit */
12#define
_c0x08
/* cntrl */
13#define
_p0x10
/* punct */
14#define
_s0x20
/* white space (space/lf/tab) */
15#define
_x0x40
/* hex digit */
16#define
_sp0x80
/* hard space (0x20) */
1718
extern
unsigned
char
_ctype;19
20#define
__ismask(x
)(_ctype
[(int
)(unsigned
char)(x
)])21
22#define
isalnum(c
)((__ismask(c
)&(_u|_l
|_d))!=0)
23#define
isalpha(c
)((__ismask(c
)&(_u|_l
))!=0)
24#define
iscntrl(c
)((__ismask(c
)&(_c
))!=0)
25#define
isdigit(c
)((__ismask(c
)&(_d
))!=0)
26#define
isgraph(c
)((__ismask(c
)&(_p|_u
|_l|_d
))!=0)
27#define
islower(c
)((__ismask(c
)&(_l
))!=0)
28#define
isprint(c
)((__ismask(c
)&(_p|_u
|_l|_d
|_sp
))!=0)
29#define
ispunct(c
)((__ismask(c
)&(_p
))!=0)
30#define
isspace(c
)((__ismask(c
)&(_s
))!=0)
31#define
isupper(c
)((__ismask(c
)&(_u
))!=0)
32#define
isxdigit(c
)((__ismask(c
)&(_d|_x
))!=0)
3334
#define
isascii(c
)(((
unsigned
char)(c
))<=
0x7f)35
#define
toascii(c
)(((
unsigned
char)(c
))&0x7f
)
知道編碼規則,將符號放入陣列相應位置,如在65-70之間放入_u和_x,兩者是求或放入的,所以保留了兩者資訊,所以a-f既會被判斷為大寫字母,又會被判斷為十六進製制。
1/*
2* linux/lib/ctype.c
3*
45
*/
67
#include
<
linux/ctype.h
>
8#include
<
linux/module.h
>910
unsigned
char
_ctype
=;/* 240-255 */
3536
export_symbol
(_ctype
);37
例如,乙個字元『a』,首先被轉換為unsigned char為65,再轉換為int65,然後查詢_ctype陣列的第65個元素,為_u|_x,為0x41,為二進位制的0100 0001,在呼叫isupper()巨集的時候與_u相與,即與0000 0001相與,保留了最低位1,非零,巨集值為1,所以『a』判斷為是大寫字母。
Linux系統核心中判斷大小的巨集
min和max巨集 min max macros that also do strict type checking.see the unnecessary pointer comparison.define min x,y define max x,y and if you can t take ...
Linux核心中的list for each
在linux核心原始碼中,經常要對鍊錶進行操作,其中乙個很重要的巨集是list for each entry 意思大體如下 假設只有兩個結點,則第乙個member代表head,list for each entry的作用就是迴圈遍歷每乙個pos中的member子項。巨集list for each e...
Linux核心中的Namespace
linux核心中的namespace提供了乙個輕量級的基於系統呼叫層面的虛擬化解決方案。相比傳統的使用 vmware,qemu,xen,kvm,hurd的虛擬 圖1所示 基於namespace的輕量級虛擬具有易使用,易管理,無需硬體虛擬化支援,低 成本等優點。圖 1.namespace又稱conta...