1 /* $Xorg: Xpoll.h,v 1.4 2001/02/09 02:03:23 xorgcvs Exp $ */ 2 3 /* 4 5 Copyright 1994, 1998 The Open Group 6 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 11 documentation. 12 13 The above copyright notice and this permission notice shall be included 14 in all copies or substantial portions of the Software. 15 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. 23 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 27 from The Open Group. 28 29 */ 30 31 /* 32 * Copyright 2005 Daniel Stone 33 * 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. 43 * 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. 50 */ 51 52 /* $XFree86: xc/include/Xpoll.h,v 3.8 2001/01/17 17:53:11 dawes Exp $ */ 53 54 #ifndef _XPOLL_H_ 55 #define _XPOLL_H_ 56 57 #ifndef WIN32 58 59 #ifndef USE_POLL 60 61 #include <X11/Xos.h> 62 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. */ 66 #if !defined(DGUX) 67 # if (defined(SVR4) || defined(CRAY) || defined(AIXV3)) && !defined(FD_SETSIZE) 68 # include <sys/select.h> 69 # ifdef luna 70 # include <sysent.h> 71 # endif 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> 76 # endif 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 80 # endif 81 # include <sys/select.h> 82 # endif 83 # endif 84 #else /* DGUX -- No sys/select in Intel DG/ux */ 85 # include <sys/time.h> 86 # include <sys/types.h> 87 # include <unistd.h> 88 #endif 89 90 #include <X11/Xmd.h> 91 92 #ifdef CSRG_BASED 93 #include <sys/param.h> 94 # if BSD < 199103 95 typedef long fd_mask; 96 # endif 97 #endif 98 99 #define XFD_SETSIZE 256 100 101 #ifndef FD_SETSIZE 102 #define FD_SETSIZE XFD_SETSIZE 103 #endif 104 105 #ifndef NBBY 106 #define NBBY 8 /* number of bits in a byte */ 107 #endif 108 109 #ifndef NFDBITS 110 #define NFDBITS (sizeof(fd_mask) * NBBY) /* bits per mask */ 111 #endif 112 113 #ifndef howmany 114 #define howmany(x,y) (((x)+((y)-1))/(y)) 115 #endif 116 117 #if defined(BSD) && BSD < 198911 && !defined(luna) 118 typedef struct fd_set { 119 fd_mask fds_bits[howmany(FD_SETSIZE, NFDBITS)]; 120 } fd_set; 121 #endif 122 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) 125 #else 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) 128 # else 129 # define Select(n,r,w,e,t) select(n,(fd_set*)r,(fd_set*)w,(fd_set*)e,(struct timeval*)t) 130 # endif 131 #endif 132 133 #define __X_FDS_BITS __fds_bits 134 135 #ifndef __FDS_BITS 136 # define __FDS_BITS(p) ((p)->__X_FDS_BITS) 137 #endif 138 139 #define __XFDS_BITS(p, n) (__FDS_BITS(p))[n] 140 141 #ifndef FD_SET 142 #define FD_SET(n, p) (__XFDS_BITS(p, ((n)/NFDBITS)) |= ((fd_mask)1 << ((n) % NFDBITS))) 143 #endif 144 #ifndef FD_CLR 145 #define FD_CLR(n, p) (__XFDS_BITS((p), ((n)/NFDBITS)) &= ~((fd_mask)1 << ((n) % NFDBITS))) 146 #endif 147 #ifndef FD_ISSET 148 #define FD_ISSET(n, p) ((__XFDS_BITS((p), ((n)/NFDBITS))) & ((fd_mask)1 << ((n) % NFDBITS))) 149 #endif 150 #ifndef FD_ZERO 151 #define FD_ZERO(p) bzero((char *)(p), sizeof(*(p))) 152 #endif 153 154 /* 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. 158 */ 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)))) 168 169 #define XFD_COPYSET(src,dst) { \ 170 int __i__; \ 171 for (__i__ = 0; __i__ < howmany(FD_SETSIZE, NFDBITS); __i__++) \ 172 __XFDS_BITS((dst), __i__) = __XFDS_BITS((src), __i__); \ 173 } 174 #define XFD_ANDSET(dst,b1,b2) { \ 175 int __i__; \ 176 for (__i__ = 0; __i__ < howmany(FD_SETSIZE, NFDBITS); __i__++) \ 177 __XFDS_BITS((dst), __i__) = ((__XFDS_BITS((b1), __i__)) & (__XFDS_BITS((b2), __i__))); \ 178 } 179 #define XFD_ORSET(dst,b1,b2) { \ 180 int __i__; \ 181 for (__i__ = 0; __i__ < howmany(FD_SETSIZE, NFDBITS); __i__++) \ 182 __XFDS_BITS((dst), __i__) = ((__XFDS_BITS((b1), __i__)) | (__XFDS_BITS((b2), __i__))); \ 183 } 184 #define XFD_UNSET(dst,b1) { \ 185 int __i__; \ 186 for (__i__ = 0; __i__ < howmany(FD_SETSIZE, NFDBITS); __i__++) \ 187 __XFDS_BITS((dst), __i__) &= ~(__XFDS_BITS((b1), __i__)); \ 188 } 189 190 #else /* USE_POLL */ 191 #include <sys/poll.h> 192 #endif /* USE_POLL */ 193 194 #else /* WIN32 */ 195 196 #define XFD_SETSIZE 256 197 #ifndef FD_SETSIZE 198 #define FD_SETSIZE XFD_SETSIZE 199 #endif 200 #include <X11/Xwinsock.h> 201 202 #define Select(n,r,w,e,t) select(0,(fd_set*)r,(fd_set*)w,(fd_set*)e,(struct timeval*)t) 203 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) 207 208 #define XFD_COPYSET(src,dst) { \ 209 u_int __i; \ 210 FD_ZERO(dst); \ 211 for (__i = 0; __i < XFD_SETCOUNT(src) ; __i++) { \ 212 XFD_FD(dst,__i) = XFD_FD(src,__i); \ 213 } \ 214 XFD_SETCOUNT(dst) = XFD_SETCOUNT(src); \ 215 } 216 217 #define XFD_ANDSET(dst,b1,b2) { \ 218 u_int __i; \ 219 FD_ZERO(dst); \ 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); \ 223 } \ 224 } 225 226 #define XFD_ORSET(dst,b1,b2) { \ 227 u_int __i; \ 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); \ 232 } \ 233 } 234 235 /* this one is really sub-optimal */ 236 #define XFD_UNSET(dst,b1) { \ 237 u_int __i; \ 238 for (__i = 0; __i < XFD_SETCOUNT(b1) ; __i++) { \ 239 FD_CLR(XFD_FD(b1,__i), dst); \ 240 } \ 241 } 242 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 */ 245 #undef FD_SET 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); \ 249 } while(0) 250 251 #define getdtablesize() FD_SETSIZE 252 253 #endif /* WIN32 */ 254 255 #endif /* _XPOLL_H_ */ 256