Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:14:34

0001 /*
0002 
0003 Copyright 1994, 1998  The Open Group
0004 
0005 Permission to use, copy, modify, distribute, and sell this software and its
0006 documentation for any purpose is hereby granted without fee, provided that
0007 the above copyright notice appear in all copies and that both that
0008 copyright notice and this permission notice appear in supporting
0009 documentation.
0010 
0011 The above copyright notice and this permission notice shall be included
0012 in all copies or substantial portions of the Software.
0013 
0014 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
0015 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
0016 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
0017 IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
0018 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
0019 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
0020 OTHER DEALINGS IN THE SOFTWARE.
0021 
0022 Except as contained in this notice, the name of The Open Group shall
0023 not be used in advertising or otherwise to promote the sale, use or
0024 other dealings in this Software without prior written authorization
0025 from The Open Group.
0026 
0027 */
0028 
0029 /*
0030  * Copyright © 2005 Daniel Stone
0031  * 
0032  * Permission to use, copy, modify, distribute, and sell this software and its
0033  * documentation for any purpose is hereby granted without fee, provided that
0034  * the above copyright notice appear in all copies and that both that
0035  * copyright notice and this permission notice appear in supporting
0036  * documentation, and that the name of Daniel Stone not be used in advertising
0037  * or publicity pertaining to distribution of the software without specific,
0038  * written prior permission.  Daniel Stone makes no representations about the
0039  * suitability of this software for any purpose.  It is provided "as is"
0040  * without express or implied warranty.
0041  *
0042  * DANIEL STONE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
0043  * ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
0044  * DANIEL STONE BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
0045  * ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
0046  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
0047  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
0048 */
0049 
0050 #ifndef _XPOLL_H_
0051 #define _XPOLL_H_
0052 
0053 #if !defined(WIN32) || defined(__CYGWIN__)
0054 
0055 #ifndef USE_POLL
0056 
0057 #include <X11/Xos.h>
0058 
0059 #include <sys/select.h>  /* Get the FD_* macros. */
0060 
0061 #include <X11/Xmd.h>
0062 
0063 #ifdef CSRG_BASED
0064 #include <sys/param.h>
0065 # if BSD < 199103
0066 typedef long fd_mask;
0067 # endif
0068 #endif
0069 
0070 #if defined(FD_SETSIZE) && FD_SETSIZE < 512
0071 # define XFD_SETSIZE    FD_SETSIZE
0072 #else
0073 # define XFD_SETSIZE    512
0074 # ifndef FD_SETSIZE
0075 #  define FD_SETSIZE    XFD_SETSIZE
0076 # endif
0077 #endif
0078 
0079 #ifndef NBBY
0080 #define NBBY    8       /* number of bits in a byte */
0081 #endif
0082 
0083 #ifndef NFDBITS
0084 #define NFDBITS (sizeof(fd_mask) * NBBY)    /* bits per mask */
0085 #endif
0086 
0087 #ifndef howmany
0088 #define howmany(x,y)    (((x)+((y)-1))/(y))
0089 #endif
0090 
0091 #if defined(BSD) && BSD < 198911 
0092 typedef struct fd_set {
0093     fd_mask fds_bits[howmany(FD_SETSIZE, NFDBITS)];
0094 } fd_set;
0095 #endif
0096 
0097 # define Select(n,r,w,e,t) select(n,(fd_set*)r,(fd_set*)w,(fd_set*)e,(struct timeval*)t)
0098 
0099 #define __X_FDS_BITS __fds_bits
0100 
0101 #ifndef __FDS_BITS
0102 # define __FDS_BITS(p)  ((p)->__X_FDS_BITS)
0103 #endif
0104 
0105 #define __XFDS_BITS(p, n) (__FDS_BITS(p))[n]
0106 
0107 #ifndef FD_SET
0108 #define FD_SET(n, p)    (__XFDS_BITS(p, ((n)/NFDBITS)) |= ((fd_mask)1 << ((n) % NFDBITS)))
0109 #endif
0110 #ifndef FD_CLR
0111 #define FD_CLR(n, p)    (__XFDS_BITS((p), ((n)/NFDBITS)) &= ~((fd_mask)1 << ((n) % NFDBITS)))
0112 #endif
0113 #ifndef FD_ISSET
0114 #define FD_ISSET(n, p)  ((__XFDS_BITS((p), ((n)/NFDBITS))) & ((fd_mask)1 << ((n) % NFDBITS)))
0115 #endif
0116 #ifndef FD_ZERO
0117 #define FD_ZERO(p)      bzero((char *)(p), sizeof(*(p)))
0118 #endif
0119 
0120 /*
0121  * The howmany(FD_SETSIZE, NFDBITS) computes the number of elements in the
0122  * array. before accessing an element in the array we check it exists.
0123  * If it does not exist then the compiler discards the code to access it. 
0124  */
0125 #define XFD_ANYSET(p) \
0126         ((howmany(FD_SETSIZE, NFDBITS) > 0 && (__XFDS_BITS(p, 0))) || \
0127         (howmany(FD_SETSIZE, NFDBITS) > 1 && (__XFDS_BITS(p, 1))) || \
0128         (howmany(FD_SETSIZE, NFDBITS) > 2 && (__XFDS_BITS(p, 2))) || \
0129         (howmany(FD_SETSIZE, NFDBITS) > 3 && (__XFDS_BITS(p, 3))) || \
0130         (howmany(FD_SETSIZE, NFDBITS) > 4 && (__XFDS_BITS(p, 4))) || \
0131         (howmany(FD_SETSIZE, NFDBITS) > 5 && (__XFDS_BITS(p, 5))) || \
0132         (howmany(FD_SETSIZE, NFDBITS) > 6 && (__XFDS_BITS(p, 6))) || \
0133         (howmany(FD_SETSIZE, NFDBITS) > 7 && (__XFDS_BITS(p, 7))) || \
0134         (howmany(FD_SETSIZE, NFDBITS) > 8 && (__XFDS_BITS(p, 8))) || \
0135         (howmany(FD_SETSIZE, NFDBITS) > 9 && (__XFDS_BITS(p, 9))) || \
0136         (howmany(FD_SETSIZE, NFDBITS) > 10 && (__XFDS_BITS(p, 10))) || \
0137         (howmany(FD_SETSIZE, NFDBITS) > 11 && (__XFDS_BITS(p, 11))) || \
0138         (howmany(FD_SETSIZE, NFDBITS) > 12 && (__XFDS_BITS(p, 12))) || \
0139         (howmany(FD_SETSIZE, NFDBITS) > 13 && (__XFDS_BITS(p, 13))) || \
0140         (howmany(FD_SETSIZE, NFDBITS) > 14 && (__XFDS_BITS(p, 14))) || \
0141         (howmany(FD_SETSIZE, NFDBITS) > 15 && (__XFDS_BITS(p, 15))))
0142 
0143 
0144 #define XFD_COPYSET(src,dst) { \
0145         int __i__; \
0146         for (__i__ = 0; __i__ < howmany(FD_SETSIZE, NFDBITS); __i__++) \
0147             __XFDS_BITS((dst), __i__) = __XFDS_BITS((src), __i__); \
0148         }
0149 #define XFD_ANDSET(dst,b1,b2) { \
0150         int __i__; \
0151         for (__i__ = 0; __i__ < howmany(FD_SETSIZE, NFDBITS); __i__++) \
0152             __XFDS_BITS((dst), __i__) = ((__XFDS_BITS((b1), __i__)) & (__XFDS_BITS((b2), __i__))); \
0153         }
0154 #define XFD_ORSET(dst,b1,b2) { \
0155         int __i__; \
0156         for (__i__ = 0; __i__ < howmany(FD_SETSIZE, NFDBITS); __i__++) \
0157         __XFDS_BITS((dst), __i__) = ((__XFDS_BITS((b1), __i__)) | (__XFDS_BITS((b2), __i__))); \
0158         }        
0159 #define XFD_UNSET(dst,b1) { \
0160         int __i__; \
0161         for (__i__ = 0; __i__ < howmany(FD_SETSIZE, NFDBITS); __i__++) \
0162             __XFDS_BITS((dst), __i__) &= ~(__XFDS_BITS((b1), __i__)); \
0163         }
0164 
0165 #else /* USE_POLL */
0166 #include <sys/poll.h>
0167 #endif /* USE_POLL */
0168 
0169 #else /* WIN32 */
0170 
0171 #define XFD_SETSIZE 512
0172 #ifndef FD_SETSIZE
0173 #define FD_SETSIZE  XFD_SETSIZE
0174 #endif
0175 #include <X11/Xwinsock.h>
0176 
0177 #define Select(n,r,w,e,t) select(0,(fd_set*)r,(fd_set*)w,(fd_set*)e,(struct timeval*)t)
0178 
0179 #define XFD_SETCOUNT(p) (((fd_set FAR *)(p))->fd_count)
0180 #define XFD_FD(p,i) (((fd_set FAR *)(p))->fd_array[i])
0181 #define XFD_ANYSET(p)   XFD_SETCOUNT(p)
0182 
0183 #define XFD_COPYSET(src,dst) { \
0184     u_int __i; \
0185     FD_ZERO(dst); \
0186     for (__i = 0; __i < XFD_SETCOUNT(src) ; __i++) { \
0187         XFD_FD(dst,__i) = XFD_FD(src,__i); \
0188     } \
0189     XFD_SETCOUNT(dst) = XFD_SETCOUNT(src); \
0190 }
0191 
0192 #define XFD_ANDSET(dst,b1,b2) { \
0193     u_int __i; \
0194     FD_ZERO(dst); \
0195     for (__i = 0; __i < XFD_SETCOUNT(b1) ; __i++) { \
0196         if (FD_ISSET(XFD_FD(b1,__i), b2)) \
0197        FD_SET(XFD_FD(b1,__i), dst); \
0198     } \
0199 }
0200 
0201 #define XFD_ORSET(dst,b1,b2) { \
0202     u_int __i; \
0203     if (dst != b1) XFD_COPYSET(b1,dst); \
0204     for (__i = 0; __i < XFD_SETCOUNT(b2) ; __i++) { \
0205         if (!FD_ISSET(XFD_FD(b2,__i), dst)) \
0206        FD_SET(XFD_FD(b2,__i), dst); \
0207     } \
0208 }
0209 
0210 /* this one is really sub-optimal */
0211 #define XFD_UNSET(dst,b1) { \
0212     u_int __i; \
0213     for (__i = 0; __i < XFD_SETCOUNT(b1) ; __i++) { \
0214     FD_CLR(XFD_FD(b1,__i), dst); \
0215     } \
0216 }
0217 
0218 /* we have to pay the price of having an array here, unlike with bitmasks
0219    calling twice FD_SET with the same fd is not transparent, so be careful */
0220 #undef FD_SET
0221 #define FD_SET(fd,set) do { \
0222     if (XFD_SETCOUNT(set) < FD_SETSIZE && !FD_ISSET(fd,set)) \
0223         XFD_FD(set,XFD_SETCOUNT(set)++)=(fd); \
0224 } while(0)
0225 
0226 #define getdtablesize() FD_SETSIZE 
0227 
0228 #endif /* WIN32 */
0229 
0230 #endif /* _XPOLL_H_ */