最近在读 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 可以起作用的地方就起作用,不起作用的地方调库里的函数) 是怎么做到的?
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.