最近在读 Linux 0.12 相关的书,发现源码里 /include/string.h 这样写:
#ifndef _STRING_H_
#define _STRING_H_
//省略...
extern inline char * strcpy(char * dest,const char *src)
{
__asm__("cld\n"
"1:\tlodsb\n\t"
"stosb\n\t"
"testb %%al,%%al\n\t"
"jne 1b"
::"S" (src),"D" (dest):"si","di","ax");
return dest;
}
//省略...
#endif
但在 /lib/strings.c 里,extern 和 inline 被定义成了空以后又使用了 /include/string.h :
/*
* linux/lib/strings.c
*
* (C) 1991 Linus Torvalds
*/
#ifndef __GNUC__
#error I want gcc!
#endif
#define extern
#define inline
#define __LIBRARY__
#include <string.h>
书上写,这个做法可以尽量让 /include/string.h 里的 strcpy 被替换到其他函数里 (inline 起作用),然后让那些无法替换的部分调 /lib/strings.c 里的 strcpy 。
这个分流效果 (inline 可以起作用的地方就起作用,不起作用的地方调库里的函数) 是怎么做到的?