Package version up
[platform/upstream/xproto.git] / Xpoll.h.in
1 /*
2
3 Copyright 1994, 1998  The Open Group
4
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
9 documentation.
10
11 The above copyright notice and this permission notice shall be included
12 in all copies or substantial portions of the Software.
13
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.
21
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
25 from The Open Group.
26
27 */
28
29 /*
30  * Copyright © 2005 Daniel Stone
31  * 
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.
41  *
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.
48 */
49
50 #ifndef _XPOLL_H_
51 #define _XPOLL_H_
52
53 #if !defined(WIN32) || defined(__CYGWIN__)
54
55 #ifndef USE_POLL
56
57 #include <X11/Xos.h>
58
59 #include <sys/select.h>  /* Get the FD_* macros. */
60
61 #include <X11/Xmd.h>
62
63 #ifdef CSRG_BASED
64 #include <sys/param.h>
65 # if BSD < 199103
66 typedef long fd_mask;
67 # endif
68 #endif
69
70 #ifndef FD_SETSIZE
71 #define XFD_SETSIZE     256
72 #define FD_SETSIZE      XFD_SETSIZE
73 #else
74 #define XFD_SETSIZE     FD_SETSIZE
75 #endif
76
77 #ifndef NBBY
78 #define NBBY    8               /* number of bits in a byte */
79 #endif
80
81 #ifndef NFDBITS
82 #define NFDBITS (sizeof(fd_mask) * NBBY)        /* bits per mask */
83 #endif
84
85 #ifndef howmany
86 #define howmany(x,y)    (((x)+((y)-1))/(y))
87 #endif
88
89 #if defined(BSD) && BSD < 198911 
90 typedef struct fd_set {
91         fd_mask fds_bits[howmany(FD_SETSIZE, NFDBITS)];
92 } fd_set;
93 #endif
94
95 # define Select(n,r,w,e,t) select(n,(fd_set*)r,(fd_set*)w,(fd_set*)e,(struct timeval*)t)
96
97 #define __X_FDS_BITS @USE_FDS_BITS@
98
99 #ifndef __FDS_BITS
100 # define __FDS_BITS(p)  ((p)->__X_FDS_BITS)
101 #endif
102
103 #define __XFDS_BITS(p, n) (__FDS_BITS(p))[n]
104
105 #ifndef FD_SET
106 #define FD_SET(n, p)    (__XFDS_BITS(p, ((n)/NFDBITS)) |= ((fd_mask)1 << ((n) % NFDBITS)))
107 #endif
108 #ifndef FD_CLR
109 #define FD_CLR(n, p)    (__XFDS_BITS((p), ((n)/NFDBITS)) &= ~((fd_mask)1 << ((n) % NFDBITS)))
110 #endif
111 #ifndef FD_ISSET
112 #define FD_ISSET(n, p)  ((__XFDS_BITS((p), ((n)/NFDBITS))) & ((fd_mask)1 << ((n) % NFDBITS)))
113 #endif
114 #ifndef FD_ZERO
115 #define FD_ZERO(p)      bzero((char *)(p), sizeof(*(p)))
116 #endif
117
118 /*
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. 
122  */
123 static inline int
124 xfd_anyset(fd_set* p)
125 {
126     int i;
127     for(i=0; i < howmany(FD_SETSIZE, NFDBITS); i++)
128     {
129         if(__XFDS_BITS(p, i)) return 1;
130     }
131     return 0;
132 }
133 #define XFD_ANYSET(p)  (xfd_anyset(p))
134
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))))
145 */
146
147 #define XFD_COPYSET(src,dst) { \
148         int __i__; \
149                 for (__i__ = 0; __i__ < howmany(FD_SETSIZE, NFDBITS); __i__++) \
150             __XFDS_BITS((dst), __i__) = __XFDS_BITS((src), __i__); \
151         }
152 #define XFD_ANDSET(dst,b1,b2) { \
153         int __i__; \
154         for (__i__ = 0; __i__ < howmany(FD_SETSIZE, NFDBITS); __i__++) \
155             __XFDS_BITS((dst), __i__) = ((__XFDS_BITS((b1), __i__)) & (__XFDS_BITS((b2), __i__))); \
156         }
157 #define XFD_ORSET(dst,b1,b2) { \
158         int __i__; \
159         for (__i__ = 0; __i__ < howmany(FD_SETSIZE, NFDBITS); __i__++) \
160                 __XFDS_BITS((dst), __i__) = ((__XFDS_BITS((b1), __i__)) | (__XFDS_BITS((b2), __i__))); \
161         }        
162 #define XFD_UNSET(dst,b1) { \
163         int __i__; \
164         for (__i__ = 0; __i__ < howmany(FD_SETSIZE, NFDBITS); __i__++) \
165                 __XFDS_BITS((dst), __i__) &= ~(__XFDS_BITS((b1), __i__)); \
166         }
167
168 #else /* USE_POLL */
169 #include <sys/poll.h>
170 #endif /* USE_POLL */
171
172 #else /* WIN32 */
173
174 #define XFD_SETSIZE     256
175 #ifndef FD_SETSIZE
176 #define FD_SETSIZE      XFD_SETSIZE
177 #endif
178 #include <X11/Xwinsock.h>
179
180 #define Select(n,r,w,e,t) select(0,(fd_set*)r,(fd_set*)w,(fd_set*)e,(struct timeval*)t)
181
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)
185
186 #define XFD_COPYSET(src,dst) { \
187     u_int __i; \
188     FD_ZERO(dst); \
189     for (__i = 0; __i < XFD_SETCOUNT(src) ; __i++) { \
190         XFD_FD(dst,__i) = XFD_FD(src,__i); \
191     } \
192     XFD_SETCOUNT(dst) = XFD_SETCOUNT(src); \
193 }
194
195 #define XFD_ANDSET(dst,b1,b2) { \
196     u_int __i; \
197     FD_ZERO(dst); \
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); \
201     } \
202 }
203
204 #define XFD_ORSET(dst,b1,b2) { \
205     u_int __i; \
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); \
210     } \
211 }
212
213 /* this one is really sub-optimal */
214 #define XFD_UNSET(dst,b1) { \
215     u_int __i; \
216     for (__i = 0; __i < XFD_SETCOUNT(b1) ; __i++) { \
217         FD_CLR(XFD_FD(b1,__i), dst); \
218     } \
219 }
220
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 */
223 #undef FD_SET
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); \
227 } while(0)
228
229 #define getdtablesize() FD_SETSIZE 
230
231 #endif /* WIN32 */
232
233 #endif /* _XPOLL_H_ */