1 /* $Xorg: Xpoll.h,v 1.4 2001/02/09 02:03:23 xorgcvs Exp $ */
5 Copyright 1994, 1998 The Open Group
7 Permission to use, copy, modify, distribute, and sell this software and its
8 documentation for any purpose is hereby granted without fee, provided that
9 the above copyright notice appear in all copies and that both that
10 copyright notice and this permission notice appear in supporting
13 The above copyright notice and this permission notice shall be included
14 in all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 OTHER DEALINGS IN THE SOFTWARE.
24 Except as contained in this notice, the name of The Open Group shall
25 not be used in advertising or otherwise to promote the sale, use or
26 other dealings in this Software without prior written authorization
32 * Copyright © 2005 Daniel Stone
34 * Permission to use, copy, modify, distribute, and sell this software and its
35 * documentation for any purpose is hereby granted without fee, provided that
36 * the above copyright notice appear in all copies and that both that
37 * copyright notice and this permission notice appear in supporting
38 * documentation, and that the name of Daniel Stone not be used in advertising
39 * or publicity pertaining to distribution of the software without specific,
40 * written prior permission. Daniel Stone makes no representations about the
41 * suitability of this software for any purpose. It is provided "as is"
42 * without express or implied warranty.
44 * DANIEL STONE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
45 * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
46 * DANIEL STONE BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
47 * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
48 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
49 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
52 /* $XFree86: xc/include/Xpoll.h,v 3.8 2001/01/17 17:53:11 dawes Exp $ */
63 /* Below is the monster branch from hell. Basically, most systems will drop to
64 * 'the branch below is the fallthrough for halfway modern systems', and include
65 * <sys/select.h>, so we get the FD_* macros. */
67 # if (defined(SVR4) || defined(CRAY) || defined(AIXV3)) && !defined(FD_SETSIZE)
68 # include <sys/select.h>
72 # else /* not SVR4/CRAY/AIXv3 */
73 # if defined(AIXV4) /* AIX 4.2 fubar-ed <sys/select.h>, so try really hard. */
74 # if !defined(NFDBITS)
75 # include <sys/select.h>
77 # else /* the branch below is the fallthrough for halfway modern systems */
78 # ifdef __QNX__ /* Make sure we get 256 bit select masks */
79 # define FD_SETSIZE 256
81 # include <sys/select.h>
84 #else /* DGUX -- No sys/select in Intel DG/ux */
85 # include <sys/time.h>
86 # include <sys/types.h>
93 #include <sys/param.h>
99 #define XFD_SETSIZE 256
102 #define FD_SETSIZE XFD_SETSIZE
106 #define NBBY 8 /* number of bits in a byte */
110 #define NFDBITS (sizeof(fd_mask) * NBBY) /* bits per mask */
114 #define howmany(x,y) (((x)+((y)-1))/(y))
117 #if defined(BSD) && BSD < 198911 && !defined(luna)
118 typedef struct fd_set
{
119 fd_mask fds_bits
[howmany(FD_SETSIZE
, NFDBITS
)];
123 #ifndef hpux /* and perhaps old BSD ??? */
124 # define Select(n,r,w,e,t) select(n,(fd_set*)r,(fd_set*)w,(fd_set*)e,(struct timeval*)t)
126 # ifndef _XPG4_EXTENDED /* HPUX 9.x and earlier */
127 # define Select(n,r,w,e,t) select(n,(int*)r,(int*)w,(int*)e,(struct timeval*)t)
129 # define Select(n,r,w,e,t) select(n,(fd_set*)r,(fd_set*)w,(fd_set*)e,(struct timeval*)t)
133 #define __X_FDS_BITS __fds_bits
136 # define __FDS_BITS(p) ((p)->__X_FDS_BITS)
139 #define __XFDS_BITS(p, n) (__FDS_BITS(p))[n]
142 #define FD_SET(n, p) (__XFDS_BITS(p, ((n)/NFDBITS)) |= ((fd_mask)1 << ((n) % NFDBITS)))
145 #define FD_CLR(n, p) (__XFDS_BITS((p), ((n)/NFDBITS)) &= ~((fd_mask)1 << ((n) % NFDBITS)))
148 #define FD_ISSET(n, p) ((__XFDS_BITS((p), ((n)/NFDBITS))) & ((fd_mask)1 << ((n) % NFDBITS)))
151 #define FD_ZERO(p) bzero((char *)(p), sizeof(*(p)))
155 * The howmany(FD_SETSIZE, NFDBITS) computes the number of elements in the
156 * array. before accessing an element in the array we check it exists.
157 * If it does not exist then the compiler discards the code to access it.
159 #define XFD_ANYSET(p) \
160 ((howmany(FD_SETSIZE, NFDBITS) > 0 && (__XFDS_BITS(p, 0))) || \
161 (howmany(FD_SETSIZE, NFDBITS) > 1 && (__XFDS_BITS(p, 1))) || \
162 (howmany(FD_SETSIZE, NFDBITS) > 2 && (__XFDS_BITS(p, 2))) || \
163 (howmany(FD_SETSIZE, NFDBITS) > 3 && (__XFDS_BITS(p, 3))) || \
164 (howmany(FD_SETSIZE, NFDBITS) > 4 && (__XFDS_BITS(p, 4))) || \
165 (howmany(FD_SETSIZE, NFDBITS) > 5 && (__XFDS_BITS(p, 5))) || \
166 (howmany(FD_SETSIZE, NFDBITS) > 6 && (__XFDS_BITS(p, 6))) || \
167 (howmany(FD_SETSIZE, NFDBITS) > 7 && (__XFDS_BITS(p, 7))))
169 #define XFD_COPYSET(src,dst) { \
171 for (__i__ = 0; __i__ < howmany(FD_SETSIZE, NFDBITS); __i__++) \
172 __XFDS_BITS((dst), __i__) = __XFDS_BITS((src), __i__); \
174 #define XFD_ANDSET(dst,b1,b2) { \
176 for (__i__ = 0; __i__ < howmany(FD_SETSIZE, NFDBITS); __i__++) \
177 __XFDS_BITS((dst), __i__) = ((__XFDS_BITS((b1), __i__)) & (__XFDS_BITS((b2), __i__))); \
179 #define XFD_ORSET(dst,b1,b2) { \
181 for (__i__ = 0; __i__ < howmany(FD_SETSIZE, NFDBITS); __i__++) \
182 __XFDS_BITS((dst), __i__) = ((__XFDS_BITS((b1), __i__)) | (__XFDS_BITS((b2), __i__))); \
184 #define XFD_UNSET(dst,b1) { \
186 for (__i__ = 0; __i__ < howmany(FD_SETSIZE, NFDBITS); __i__++) \
187 __XFDS_BITS((dst), __i__) &= ~(__XFDS_BITS((b1), __i__)); \
191 #include <sys/poll.h>
192 #endif /* USE_POLL */
196 #define XFD_SETSIZE 256
198 #define FD_SETSIZE XFD_SETSIZE
200 #include <X11/Xwinsock.h>
202 #define Select(n,r,w,e,t) select(0,(fd_set*)r,(fd_set*)w,(fd_set*)e,(struct timeval*)t)
204 #define XFD_SETCOUNT(p) (((fd_set FAR *)(p))->fd_count)
205 #define XFD_FD(p,i) (((fd_set FAR *)(p))->fd_array[i])
206 #define XFD_ANYSET(p) XFD_SETCOUNT(p)
208 #define XFD_COPYSET(src,dst) { \
211 for (__i = 0; __i < XFD_SETCOUNT(src) ; __i++) { \
212 XFD_FD(dst,__i) = XFD_FD(src,__i); \
214 XFD_SETCOUNT(dst) = XFD_SETCOUNT(src); \
217 #define XFD_ANDSET(dst,b1,b2) { \
220 for (__i = 0; __i < XFD_SETCOUNT(b1) ; __i++) { \
221 if (FD_ISSET(XFD_FD(b1,__i), b2)) \
222 FD_SET(XFD_FD(b1,__i), dst); \
226 #define XFD_ORSET(dst,b1,b2) { \
228 if (dst != b1) XFD_COPYSET(b1,dst); \
229 for (__i = 0; __i < XFD_SETCOUNT(b2) ; __i++) { \
230 if (!FD_ISSET(XFD_FD(b2,__i), dst)) \
231 FD_SET(XFD_FD(b2,__i), dst); \
235 /* this one is really sub-optimal */
236 #define XFD_UNSET(dst,b1) { \
238 for (__i = 0; __i < XFD_SETCOUNT(b1) ; __i++) { \
239 FD_CLR(XFD_FD(b1,__i), dst); \
243 /* we have to pay the price of having an array here, unlike with bitmasks
244 calling twice FD_SET with the same fd is not transparent, so be careful */
246 #define FD_SET(fd,set) do { \
247 if (XFD_SETCOUNT(set) < FD_SETSIZE && !FD_ISSET(fd,set)) \
248 XFD_FD(set,XFD_SETCOUNT(set)++)=(fd); \
251 #define getdtablesize() FD_SETSIZE
255 #endif /* _XPOLL_H_ */