当我在github上浏览Redis源代码时,我发现源文件中丢失了静态函数,这些函数在定义它的同一文件中没有被任何人引用。由于静态函数只能在同一个文件中访问,所以根本不使用这些函数!
以下是src/ae_epoll.c的示例代码片段:
static int aeApiAddEvent(aeEventLoop *eventLoop, int fd, int mask) {
aeApiState *state = eventLoop->apidata;
struct epoll_event ee = {0}; /* avoid valgrind warning */
/* If the fd was already monitored for some event, we need a MOD
* operation. Otherwise we need an ADD operation. */
int op = eventLoop->events[fd].mask == AE_NONE ?
EPOLL_CTL_ADD : EPOLL_CTL_MOD;
ee.events = 0;
mask |= eventLoop->events[fd].mask; /* Merge old events */
if (mask & AE_READABLE) ee.events |= EPOLLIN;
if (mask & AE_WRITABLE) ee.events |= EPOLLOUT;
ee.data.fd = fd;
if (epoll_ctl(state->epfd,op,fd,&ee) == -1) return -1;
return 0;
}
你会发现函数aeApiAddEvent
不是在本地使用的。许多差异文件中都包含这些未使用的静态函数。
为何定义但未使用?我错过了一些观点吗?
the static function can be only accessed within the same file
这是错误的。正确的是说它们只能在同一个translation unit内访问。
该文件可以从某个地方包含。
因此,它们包含.c文件的原因是,根据一些配置参数,它们将选择不同的代码进行编译。
见here。
他们包括在the file here。