2

When I am browsing Redis source on github, I found lost of static functions in source files that are not referenced by anyone within the same file at which it was defined. Since the static function can be only accessed within the same file, these functions are not used at all!

Following is a example code snippet from 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;
}

And you will find that the function aeApiAddEvent is not used locally. Such unused static functions are found in many difference files.

Why defined but not used ? Am I missing some points ?

1 Answers1

0
the static function can be only accessed within the same file

This is false. Correct is to say that they can be accessed only inside the same translation unit.

The file can be included from somewhere.

So, the reason that they include .c files is that, depending on some configuration parameter, they will choose different code to compile.

See here.

They are included from the file here.

alinsoar
  • 15,386
  • 4
  • 57
  • 74