“#define X X”是什么意思?
What does it mean by "#define X X"?
在Linux头文件epoll.h
中,我找到了以下代码:
enum EPOLL_EVENTS
{
EPOLLIN = 0x001,
#define EPOLLIN EPOLLIN
...
}
#define EPOLLIN EPOLLIN
是什么意思?
#define EPOLLIN EPOLLIN
这将预处理器宏 EPOLLIN
定义为 EPOLLIN
。
它可能是为了以后 #ifdef EPOLLIN
检查的目的而定义的。
这将创建一个名为 EPOLLIN
的宏,其替换文本也是 EPOLLIN
。
这很可能是预处理器检查哪些事件代码可用并根据需要有条件地编译代码的一种方式。如果我们转到 glibc 的 git 存储库并查看 git blame
的输出,我们会看到 enum EPOLL_EVENTS
的以下内容:
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 34) enum EPOLL_EVENTS
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 35) {
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 36) EPOLLIN = 0x001,
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 37) #define EPOLLIN EPOLLIN
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 38) EPOLLPRI = 0x002,
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 39) #define EPOLLPRI EPOLLPRI
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 40) EPOLLOUT = 0x004,
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 41) #define EPOLLOUT EPOLLOUT
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 42) EPOLLRDNORM = 0x040,
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 43) #define EPOLLRDNORM EPOLLRDNORM
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 44) EPOLLRDBAND = 0x080,
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 45) #define EPOLLRDBAND EPOLLRDBAND
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 46) EPOLLWRNORM = 0x100,
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 47) #define EPOLLWRNORM EPOLLWRNORM
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 48) EPOLLWRBAND = 0x200,
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 49) #define EPOLLWRBAND EPOLLWRBAND
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 50) EPOLLMSG = 0x400,
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 51) #define EPOLLMSG EPOLLMSG
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 52) EPOLLERR = 0x008,
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 53) #define EPOLLERR EPOLLERR
5e826ab5 (Ulrich Drepper 2003-03-25 01:14:36 +0000 54) EPOLLHUP = 0x010,
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 55) #define EPOLLHUP EPOLLHUP
94833f11 (Ulrich Drepper 2007-10-28 01:34:10 +0000 56) EPOLLRDHUP = 0x2000,
94833f11 (Ulrich Drepper 2007-10-28 01:34:10 +0000 57) #define EPOLLRDHUP EPOLLRDHUP
981569c7 (Joseph Myers 2016-03-14 19:04:53 +0000 58) EPOLLEXCLUSIVE = 1u << 28,
981569c7 (Joseph Myers 2016-03-14 19:04:53 +0000 59) #define EPOLLEXCLUSIVE EPOLLEXCLUSIVE
f8d44fdc (Andreas Jaeger 2012-07-26 13:11:33 +0200 60) EPOLLWAKEUP = 1u << 29,
f8d44fdc (Andreas Jaeger 2012-07-26 13:11:33 +0200 61) #define EPOLLWAKEUP EPOLLWAKEUP
4920765e (Ulrich Drepper 2011-12-21 22:14:05 -0500 62) EPOLLONESHOT = 1u << 30,
e11676dd (Ulrich Drepper 2004-01-21 06:23:26 +0000 63) #define EPOLLONESHOT EPOLLONESHOT
4920765e (Ulrich Drepper 2011-12-21 22:14:05 -0500 64) EPOLLET = 1u << 31
5e826ab5 (Ulrich Drepper 2003-03-25 01:14:36 +0000 65) #define EPOLLET EPOLLET
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 66) };
从这里,您可以看到这些事件中的大部分是在 2002 年创建的,但后来添加了一些其他事件。因此,这些宏允许您检查特定标志是否可用,如下所示:
#ifdef EPOLLEXCLUSIVE
/* code that can handle EPOLLEXCLUSIVE */
#else
/* code that uses an alternate event */
#endif
这样,代码可以 运行 在具有可用新事件的较新 Linux 版本或不可用的旧版本上。
在Linux头文件epoll.h
中,我找到了以下代码:
enum EPOLL_EVENTS
{
EPOLLIN = 0x001,
#define EPOLLIN EPOLLIN
...
}
#define EPOLLIN EPOLLIN
是什么意思?
#define EPOLLIN EPOLLIN
这将预处理器宏 EPOLLIN
定义为 EPOLLIN
。
它可能是为了以后 #ifdef EPOLLIN
检查的目的而定义的。
这将创建一个名为 EPOLLIN
的宏,其替换文本也是 EPOLLIN
。
这很可能是预处理器检查哪些事件代码可用并根据需要有条件地编译代码的一种方式。如果我们转到 glibc 的 git 存储库并查看 git blame
的输出,我们会看到 enum EPOLL_EVENTS
的以下内容:
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 34) enum EPOLL_EVENTS
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 35) {
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 36) EPOLLIN = 0x001,
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 37) #define EPOLLIN EPOLLIN
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 38) EPOLLPRI = 0x002,
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 39) #define EPOLLPRI EPOLLPRI
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 40) EPOLLOUT = 0x004,
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 41) #define EPOLLOUT EPOLLOUT
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 42) EPOLLRDNORM = 0x040,
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 43) #define EPOLLRDNORM EPOLLRDNORM
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 44) EPOLLRDBAND = 0x080,
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 45) #define EPOLLRDBAND EPOLLRDBAND
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 46) EPOLLWRNORM = 0x100,
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 47) #define EPOLLWRNORM EPOLLWRNORM
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 48) EPOLLWRBAND = 0x200,
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 49) #define EPOLLWRBAND EPOLLWRBAND
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 50) EPOLLMSG = 0x400,
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 51) #define EPOLLMSG EPOLLMSG
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 52) EPOLLERR = 0x008,
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 53) #define EPOLLERR EPOLLERR
5e826ab5 (Ulrich Drepper 2003-03-25 01:14:36 +0000 54) EPOLLHUP = 0x010,
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 55) #define EPOLLHUP EPOLLHUP
94833f11 (Ulrich Drepper 2007-10-28 01:34:10 +0000 56) EPOLLRDHUP = 0x2000,
94833f11 (Ulrich Drepper 2007-10-28 01:34:10 +0000 57) #define EPOLLRDHUP EPOLLRDHUP
981569c7 (Joseph Myers 2016-03-14 19:04:53 +0000 58) EPOLLEXCLUSIVE = 1u << 28,
981569c7 (Joseph Myers 2016-03-14 19:04:53 +0000 59) #define EPOLLEXCLUSIVE EPOLLEXCLUSIVE
f8d44fdc (Andreas Jaeger 2012-07-26 13:11:33 +0200 60) EPOLLWAKEUP = 1u << 29,
f8d44fdc (Andreas Jaeger 2012-07-26 13:11:33 +0200 61) #define EPOLLWAKEUP EPOLLWAKEUP
4920765e (Ulrich Drepper 2011-12-21 22:14:05 -0500 62) EPOLLONESHOT = 1u << 30,
e11676dd (Ulrich Drepper 2004-01-21 06:23:26 +0000 63) #define EPOLLONESHOT EPOLLONESHOT
4920765e (Ulrich Drepper 2011-12-21 22:14:05 -0500 64) EPOLLET = 1u << 31
5e826ab5 (Ulrich Drepper 2003-03-25 01:14:36 +0000 65) #define EPOLLET EPOLLET
ad3bf20c (Ulrich Drepper 2002-12-16 23:38:42 +0000 66) };
从这里,您可以看到这些事件中的大部分是在 2002 年创建的,但后来添加了一些其他事件。因此,这些宏允许您检查特定标志是否可用,如下所示:
#ifdef EPOLLEXCLUSIVE
/* code that can handle EPOLLEXCLUSIVE */
#else
/* code that uses an alternate event */
#endif
这样,代码可以 运行 在具有可用新事件的较新 Linux 版本或不可用的旧版本上。