的確,和真正的程序員相比,我差得太遠,但和同齡人,同水平的人來說,我還能算點什么的..
如果不是有感來研究Linux的源碼,肯定不會發現...原來,竟有這么漂亮的函數定義方式.
#define _syscall0(type,name) \
type name(void) \
{ \
type __res; \
__asm__ volatile ("int $0x80" \
: "=a" (__res) \
: "0" (__NR_##name)); \
if (__res >= 0) \
return __res; \
errno = -__res; \
return -1; \
}
#define _syscall1(type,name,atype,a) \
type name(atype a) \
{ \
type __res; \
__asm__ volatile ("int $0x80" \
: "=a" (__res) \
: "0" (__NR_##name),"b" (a)); \
if (__res >= 0) \
return __res; \
errno = -__res; \
return -1; \
}
#define _syscall2(type,name,atype,a,btype,b) \
type name(atype a,btype b) \
{ \
type __res; \
__asm__ volatile ("int $0x80" \
: "=a" (__res) \
: "0" (__NR_##name),"b" (a),"c" (b)); \
if (__res >= 0) \
return __res; \
errno = -__res; \
return -1; \
}
#define _syscall3(type,name,atype,a,btype,b,ctype,c) \
type name(atype a,btype b,ctype c) \
{ \
type __res; \
__asm__ volatile ("int $0x80" \
: "=a" (__res) \
: "0" (__NR_##name),"b" (a),"c" (b),"d" (c)); \
if (__res<0) \
errno=-__res , __res = -1; \
return __res;\
}
如果能讀懂的話,應該理解是什么意思.在unistd.h文件中這幾行代碼,竟將67個系統函數給概括了(Linux0.01為67,0.95增加為89.)..
我沒見過多少比這更精簡的代碼,我只能說,我的感覺,這不能是一般的漂亮,而是精簡到極致的華麗..
....打心底佩服Linus大俠!!