3 Copyright 1994, 1998 The Open Group
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
11 The above copyright notice and this permission notice shall be included
12 in all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 OTHER DEALINGS IN THE SOFTWARE.
22 Except as contained in this notice, the name of The Open Group shall
23 not be used in advertising or otherwise to promote the sale, use or
24 other dealings in this Software without prior written authorization
30 * Copyright © 2005 Daniel Stone
32 * Permission to use, copy, modify, distribute, and sell this software and its
33 * documentation for any purpose is hereby granted without fee, provided that
34 * the above copyright notice appear in all copies and that both that
35 * copyright notice and this permission notice appear in supporting
36 * documentation, and that the name of Daniel Stone not be used in advertising
37 * or publicity pertaining to distribution of the software without specific,
38 * written prior permission. Daniel Stone makes no representations about the
39 * suitability of this software for any purpose. It is provided "as is"
40 * without express or implied warranty.
42 * DANIEL STONE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
43 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
44 * DANIEL STONE BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
45 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
46 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
47 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
53 #if !defined(WIN32) || defined(__CYGWIN__)
59 #include <sys/select.h> /* Get the FD_* macros. */
64 #include <sys/param.h>
71 #define XFD_SETSIZE 256
72 #define FD_SETSIZE XFD_SETSIZE
74 #define XFD_SETSIZE FD_SETSIZE
78 #define NBBY 8 /* number of bits in a byte */
82 #define NFDBITS (sizeof(fd_mask) * NBBY) /* bits per mask */
86 #define howmany(x,y) (((x)+((y)-1))/(y))
89 #if defined(BSD) && BSD < 198911
90 typedef struct fd_set {
91 fd_mask fds_bits[howmany(FD_SETSIZE, NFDBITS)];
95 # define Select(n,r,w,e,t) select(n,(fd_set*)r,(fd_set*)w,(fd_set*)e,(struct timeval*)t)
97 #define __X_FDS_BITS @USE_FDS_BITS@
100 # define __FDS_BITS(p) ((p)->__X_FDS_BITS)
103 #define __XFDS_BITS(p, n) (__FDS_BITS(p))[n]
106 #define FD_SET(n, p) (__XFDS_BITS(p, ((n)/NFDBITS)) |= ((fd_mask)1 << ((n) % NFDBITS)))
109 #define FD_CLR(n, p) (__XFDS_BITS((p), ((n)/NFDBITS)) &= ~((fd_mask)1 << ((n) % NFDBITS)))
112 #define FD_ISSET(n, p) ((__XFDS_BITS((p), ((n)/NFDBITS))) & ((fd_mask)1 << ((n) % NFDBITS)))
115 #define FD_ZERO(p) bzero((char *)(p), sizeof(*(p)))
119 * The howmany(FD_SETSIZE, NFDBITS) computes the number of elements in the
120 * array. before accessing an element in the array we check it exists.
121 * If it does not exist then the compiler discards the code to access it.
124 xfd_anyset(fd_set* p)
127 for(i=0; i < howmany(FD_SETSIZE, NFDBITS); i++)
129 if(__XFDS_BITS(p, i)) return 1;
133 #define XFD_ANYSET(p) (xfd_anyset(p))
135 /* Change Inline Function
136 #define XFD_ANYSET(p) \
137 ((howmany(FD_SETSIZE, NFDBITS) > 0 && (__XFDS_BITS(p, 0))) || \
138 (howmany(FD_SETSIZE, NFDBITS) > 1 && (__XFDS_BITS(p, 1))) || \
139 (howmany(FD_SETSIZE, NFDBITS) > 2 && (__XFDS_BITS(p, 2))) || \
140 (howmany(FD_SETSIZE, NFDBITS) > 3 && (__XFDS_BITS(p, 3))) || \
141 (howmany(FD_SETSIZE, NFDBITS) > 4 && (__XFDS_BITS(p, 4))) || \
142 (howmany(FD_SETSIZE, NFDBITS) > 5 && (__XFDS_BITS(p, 5))) || \
143 (howmany(FD_SETSIZE, NFDBITS) > 6 && (__XFDS_BITS(p, 6))) || \
144 (howmany(FD_SETSIZE, NFDBITS) > 7 && (__XFDS_BITS(p, 7))))
147 #define XFD_COPYSET(src,dst) { \
149 for (__i__ = 0; __i__ < howmany(FD_SETSIZE, NFDBITS); __i__++) \
150 __XFDS_BITS((dst), __i__) = __XFDS_BITS((src), __i__); \
152 #define XFD_ANDSET(dst,b1,b2) { \
154 for (__i__ = 0; __i__ < howmany(FD_SETSIZE, NFDBITS); __i__++) \
155 __XFDS_BITS((dst), __i__) = ((__XFDS_BITS((b1), __i__)) & (__XFDS_BITS((b2), __i__))); \
157 #define XFD_ORSET(dst,b1,b2) { \
159 for (__i__ = 0; __i__ < howmany(FD_SETSIZE, NFDBITS); __i__++) \
160 __XFDS_BITS((dst), __i__) = ((__XFDS_BITS((b1), __i__)) | (__XFDS_BITS((b2), __i__))); \
162 #define XFD_UNSET(dst,b1) { \
164 for (__i__ = 0; __i__ < howmany(FD_SETSIZE, NFDBITS); __i__++) \
165 __XFDS_BITS((dst), __i__) &= ~(__XFDS_BITS((b1), __i__)); \
169 #include <sys/poll.h>
170 #endif /* USE_POLL */
174 #define XFD_SETSIZE 256
176 #define FD_SETSIZE XFD_SETSIZE
178 #include <X11/Xwinsock.h>
180 #define Select(n,r,w,e,t) select(0,(fd_set*)r,(fd_set*)w,(fd_set*)e,(struct timeval*)t)
182 #define XFD_SETCOUNT(p) (((fd_set FAR *)(p))->fd_count)
183 #define XFD_FD(p,i) (((fd_set FAR *)(p))->fd_array[i])
184 #define XFD_ANYSET(p) XFD_SETCOUNT(p)
186 #define XFD_COPYSET(src,dst) { \
189 for (__i = 0; __i < XFD_SETCOUNT(src) ; __i++) { \
190 XFD_FD(dst,__i) = XFD_FD(src,__i); \
192 XFD_SETCOUNT(dst) = XFD_SETCOUNT(src); \
195 #define XFD_ANDSET(dst,b1,b2) { \
198 for (__i = 0; __i < XFD_SETCOUNT(b1) ; __i++) { \
199 if (FD_ISSET(XFD_FD(b1,__i), b2)) \
200 FD_SET(XFD_FD(b1,__i), dst); \
204 #define XFD_ORSET(dst,b1,b2) { \
206 if (dst != b1) XFD_COPYSET(b1,dst); \
207 for (__i = 0; __i < XFD_SETCOUNT(b2) ; __i++) { \
208 if (!FD_ISSET(XFD_FD(b2,__i), dst)) \
209 FD_SET(XFD_FD(b2,__i), dst); \
213 /* this one is really sub-optimal */
214 #define XFD_UNSET(dst,b1) { \
216 for (__i = 0; __i < XFD_SETCOUNT(b1) ; __i++) { \
217 FD_CLR(XFD_FD(b1,__i), dst); \
221 /* we have to pay the price of having an array here, unlike with bitmasks
222 calling twice FD_SET with the same fd is not transparent, so be careful */
224 #define FD_SET(fd,set) do { \
225 if (XFD_SETCOUNT(set) < FD_SETSIZE && !FD_ISSET(fd,set)) \
226 XFD_FD(set,XFD_SETCOUNT(set)++)=(fd); \
229 #define getdtablesize() FD_SETSIZE
233 #endif /* _XPOLL_H_ */