|
89 | 89 | #define EXIT_SUCCESS 0 |
90 | 90 | #define EXIT_FAILURE 1 |
91 | 91 |
|
| 92 | +#define FD_SETIDXMASK (8 * sizeof(unsigned long)) |
| 93 | +#define FD_SETBITMASK (8 * sizeof(unsigned long)-1) |
| 94 | + |
92 | 95 | /* for select() */ |
93 | 96 | typedef struct { |
94 | | - uint32_t fd32[(FD_SETSIZE + 31) / 32]; |
| 97 | + unsigned long fds[(FD_SETSIZE + FD_SETBITMASK) / FD_SETIDXMASK]; |
95 | 98 | } fd_set; |
96 | 99 |
|
97 | | -#define FD_CLR(fd, set) do { \ |
98 | | - fd_set *__set = (set); \ |
99 | | - int __fd = (fd); \ |
100 | | - if (__fd >= 0) \ |
101 | | - __set->fd32[__fd / 32] &= ~(1U << (__fd & 31)); \ |
| 100 | +#define FD_CLR(fd, set) do { \ |
| 101 | + fd_set *__set = (set); \ |
| 102 | + int __fd = (fd); \ |
| 103 | + if (__fd >= 0) \ |
| 104 | + __set->fds[__fd / FD_SETIDXMASK] &= \ |
| 105 | + ~(1U << (__fd & FX_SETBITMASK)); \ |
102 | 106 | } while (0) |
103 | 107 |
|
104 | | -#define FD_SET(fd, set) do { \ |
105 | | - fd_set *__set = (set); \ |
106 | | - int __fd = (fd); \ |
107 | | - if (__fd >= 0) \ |
108 | | - __set->fd32[__fd / 32] |= 1U << (__fd & 31); \ |
| 108 | +#define FD_SET(fd, set) do { \ |
| 109 | + fd_set *__set = (set); \ |
| 110 | + int __fd = (fd); \ |
| 111 | + if (__fd >= 0) \ |
| 112 | + __set->fds[__fd / FD_SETIDXMASK] |= \ |
| 113 | + 1 << (__fd & FD_SETBITMASK); \ |
109 | 114 | } while (0) |
110 | 115 |
|
111 | | -#define FD_ISSET(fd, set) ({ \ |
112 | | - fd_set *__set = (set); \ |
113 | | - int __fd = (fd); \ |
114 | | - int __r = 0; \ |
115 | | - if (__fd >= 0) \ |
116 | | - __r = !!(__set->fd32[__fd / 32] & 1U << (__fd & 31)); \ |
117 | | - __r; \ |
| 116 | +#define FD_ISSET(fd, set) ({ \ |
| 117 | + fd_set *__set = (set); \ |
| 118 | + int __fd = (fd); \ |
| 119 | + int __r = 0; \ |
| 120 | + if (__fd >= 0) \ |
| 121 | + __r = !!(__set->fds[__fd / FD_SETIDXMASK] & \ |
| 122 | +1U << (__fd & FD_SET_BITMASK)); \ |
| 123 | + __r; \ |
118 | 124 | }) |
119 | 125 |
|
120 | | -#define FD_ZERO(set) do { \ |
121 | | - fd_set *__set = (set); \ |
122 | | - int __idx; \ |
123 | | - for (__idx = 0; __idx < (FD_SETSIZE+31) / 32; __idx ++) \ |
124 | | - __set->fd32[__idx] = 0; \ |
| 126 | +#define FD_ZERO(set) do { \ |
| 127 | + fd_set *__set = (set); \ |
| 128 | + int __idx; \ |
| 129 | + int __size = (FD_SETSIZE+FD_SETBITMASK) / FD_SETIDXMASK;\ |
| 130 | + for (__idx = 0; __idx < __size; __idx++) \ |
| 131 | + __set->fds[__idx] = 0; \ |
125 | 132 | } while (0) |
126 | 133 |
|
127 | 134 | /* for poll() */ |
|
0 commit comments