1 /* $XConsortium: Xpoll.h /main/6 1996/12/02 10:25:52 lehors $ */
5 Copyright (c) 1994 X Consortium
7 Permission is hereby granted, free of charge, to any person obtaining
8 a copy of this software and associated documentation files (the
9 "Software"), to deal in the Software without restriction, including
10 without limitation the rights to use, copy, modify, merge, publish,
11 distribute, sublicense, and/or sell copies of the Software, and to
12 permit persons to whom the Software is furnished to do so, subject to
13 the following conditions:
15 The above copyright notice and this permission notice shall be included
16 in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
22 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
23 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 OTHER DEALINGS IN THE SOFTWARE.
26 Except as contained in this notice, the name of the X Consortium shall
27 not be used in advertising or otherwise to promote the sale, use or
28 other dealings in this Software without prior written authorization
29 from the X Consortium.
41 #if (defined(SVR4) || defined(CRAY) || defined(AIXV3)) && !defined(FD_SETSIZE)
42 #include <sys/select.h>
47 /* AIX 4.2 fubar-ed <sys/select.h>, so go to heroic measures to get it */
48 #if defined(AIXV4) && !defined(NFDBITS)
49 #include <sys/select.h>
53 #include <sys/param.h>
59 #define XFD_SETSIZE 256
61 #define FD_SETSIZE XFD_SETSIZE
65 #define NBBY 8 /* number of bits in a byte */
69 #define NFDBITS (sizeof(fd_mask) * NBBY) /* bits per mask */
73 #define howmany(x,y) (((x)+((y)-1))/(y))
77 # if BSD < 198911 /* 198911 == OSF/1, 199103 == CSRG_BASED */
78 # ifndef luna /* and even though on LUNA BSD == 43, it has it */
79 typedef struct fd_set {
80 fd_mask fds_bits[howmany(FD_SETSIZE, NFDBITS)];
86 #ifdef hpux /* and perhaps old BSD ??? */
87 #define Select(n,r,w,e,t) select(n,(int*)r,(int*)w,(int*)e,(struct timeval*)t)
89 #define Select(n,r,w,e,t) select(n,(fd_set*)r,(fd_set*)w,(fd_set*)e,(struct timeval*)t)
93 #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
96 #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
99 #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
102 #define FD_ZERO(p) bzero((char *)(p), sizeof(*(p)))
106 * The following macros are used by the servers only. There is an
107 * explicit assumption that the bit array in the fd_set is at least
108 * 256 bits long (8 32-bit words). This is true on most modern POSIX
109 * systems. Note that this is merely an optimization for the servers
110 * based on assumptions about the way that file descripters are
111 * allocated on POSIX systems.
113 * When porting X to new systems it is important to adjust these
114 * macros if the system has fewer than 256 bits in the fd_set bit
117 #define XFD_ANYSET(p) \
118 ((p)->fds_bits[0] || (p)->fds_bits[1] || \
119 (p)->fds_bits[2] || (p)->fds_bits[3] || \
120 (p)->fds_bits[4] || (p)->fds_bits[5] || \
121 (p)->fds_bits[6] || (p)->fds_bits[7])
123 #define XFD_COPYSET(src,dst) \
124 (dst)->fds_bits[0] = (src)->fds_bits[0]; \
125 (dst)->fds_bits[1] = (src)->fds_bits[1]; \
126 (dst)->fds_bits[2] = (src)->fds_bits[2]; \
127 (dst)->fds_bits[3] = (src)->fds_bits[3]; \
128 (dst)->fds_bits[4] = (src)->fds_bits[4]; \
129 (dst)->fds_bits[5] = (src)->fds_bits[5]; \
130 (dst)->fds_bits[6] = (src)->fds_bits[6]; \
131 (dst)->fds_bits[7] = (src)->fds_bits[7];
133 #define XFD_ANDSET(dst,b1,b2) \
134 (dst)->fds_bits[0] = ((b1)->fds_bits[0] & (b2)->fds_bits[0]); \
135 (dst)->fds_bits[1] = ((b1)->fds_bits[1] & (b2)->fds_bits[1]); \
136 (dst)->fds_bits[2] = ((b1)->fds_bits[2] & (b2)->fds_bits[2]); \
137 (dst)->fds_bits[3] = ((b1)->fds_bits[3] & (b2)->fds_bits[3]); \
138 (dst)->fds_bits[4] = ((b1)->fds_bits[4] & (b2)->fds_bits[4]); \
139 (dst)->fds_bits[5] = ((b1)->fds_bits[5] & (b2)->fds_bits[5]); \
140 (dst)->fds_bits[6] = ((b1)->fds_bits[6] & (b2)->fds_bits[6]); \
141 (dst)->fds_bits[7] = ((b1)->fds_bits[7] & (b2)->fds_bits[7]);
143 #define XFD_ORSET(dst,b1,b2) \
144 (dst)->fds_bits[0] = ((b1)->fds_bits[0] | (b2)->fds_bits[0]); \
145 (dst)->fds_bits[1] = ((b1)->fds_bits[1] | (b2)->fds_bits[1]); \
146 (dst)->fds_bits[2] = ((b1)->fds_bits[2] | (b2)->fds_bits[2]); \
147 (dst)->fds_bits[3] = ((b1)->fds_bits[3] | (b2)->fds_bits[3]); \
148 (dst)->fds_bits[4] = ((b1)->fds_bits[4] | (b2)->fds_bits[4]); \
149 (dst)->fds_bits[5] = ((b1)->fds_bits[5] | (b2)->fds_bits[5]); \
150 (dst)->fds_bits[6] = ((b1)->fds_bits[6] | (b2)->fds_bits[6]); \
151 (dst)->fds_bits[7] = ((b1)->fds_bits[7] | (b2)->fds_bits[7]);
153 #define XFD_UNSET(dst,b1) \
154 (dst)->fds_bits[0] &= ~((b1)->fds_bits[0]); \
155 (dst)->fds_bits[1] &= ~((b1)->fds_bits[1]); \
156 (dst)->fds_bits[2] &= ~((b1)->fds_bits[2]); \
157 (dst)->fds_bits[3] &= ~((b1)->fds_bits[3]); \
158 (dst)->fds_bits[4] &= ~((b1)->fds_bits[4]); \
159 (dst)->fds_bits[5] &= ~((b1)->fds_bits[5]); \
160 (dst)->fds_bits[6] &= ~((b1)->fds_bits[6]); \
161 (dst)->fds_bits[7] &= ~((b1)->fds_bits[7]);
164 #include <sys/poll.h>
165 #endif /* USE_POLL */
169 #define XFD_SETSIZE 256
171 #define FD_SETSIZE XFD_SETSIZE
173 #include <X11/Xwinsock.h>
175 #define Select(n,r,w,e,t) select(0,(fd_set*)r,(fd_set*)w,(fd_set*)e,(struct timeval*)t)
177 #define XFD_SETCOUNT(p) (((fd_set FAR *)(p))->fd_count)
178 #define XFD_FD(p,i) (((fd_set FAR *)(p))->fd_array[i])
179 #define XFD_ANYSET(p) XFD_SETCOUNT(p)
181 #define XFD_COPYSET(src,dst) { \
184 for (__i = 0; __i < XFD_SETCOUNT(src) ; __i++) { \
185 XFD_FD(dst,__i) = XFD_FD(src,__i); \
187 XFD_SETCOUNT(dst) = XFD_SETCOUNT(src); \
190 #define XFD_ANDSET(dst,b1,b2) { \
193 for (__i = 0; __i < XFD_SETCOUNT(b1) ; __i++) { \
194 if (FD_ISSET(XFD_FD(b1,__i), b2)) \
195 FD_SET(XFD_FD(b1,__i), dst); \
199 #define XFD_ORSET(dst,b1,b2) { \
201 XFD_COPYSET(b1,dst); \
202 for (__i = 0; __i < XFD_SETCOUNT(b2) ; __i++) { \
203 if (!FD_ISSET(XFD_FD(b2,__i), dst)) \
204 FD_SET(XFD_FD(b2,__i), dst); \
208 /* this one is really sub-optimal */
209 #define XFD_UNSET(dst,b1) { \
211 for (__i = 0; __i < XFD_SETCOUNT(b1) ; __i++) { \
212 FD_CLR(XFD_FD(b1,__i), dst); \
216 /* we have to pay the price of having an array here, unlike with bitmasks
217 calling twice FD_SET with the same fd is not transparent, so be careful */
219 #define FD_SET(fd,set) do { \
220 if (XFD_SETCOUNT(set) < FD_SETSIZE && !FD_ISSET(fd,set)) \
221 XFD_FD(set,XFD_SETCOUNT(set)++)=(fd); \
224 #define getdtablesize() FD_SETSIZE
228 #endif /* _XPOLL_H_ */