功能原型与否?

时间:2014-03-29 10:15:35

标签: c kernel

我在原始linux内核代码中找到了以下内容。 (link

static inline _syscall0(int,fork)
static inline _syscall0(int,pause)
static inline _syscall0(int,setup)
static inline _syscall0(int,sync)

它不能是函数调用,因为static inline带有前缀。它不能是原型,因为函数不能在C中重载。最后没有分号。这是什么?

此外,它之前是此评论(如果您也可以解释这一点)

/*
 * we need this inline - forking from kernel space will result
 * in NO COPY ON WRITE (!!!), until an execve is executed. This
 * is no problem, but for the stack. This is handled by not letting
 * main() use the stack at all after fork(). Thus, no function
 * calls - which means inline code for fork too, as otherwise we
 * would use the stack upon exit from 'fork()'.
 *
 * Actually only pause and fork are needed inline, so that there
 * won't be any messing with the stack from main(), but we define
 * some others too.
 */

1 个答案:

答案 0 :(得分:4)

因为_syscall0macro(在unistd.h中):

#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 call(type,name) \
    type name(void) \
    {return 0;}

static inline call(int,func)

int main(void)
{
    return func();
} 

扩展为:

static inline int func(void) {return 0;}

int main(void)
{
    return func();
}