]> git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/fdsel.h
Security-179.tar.gz
[apple/security.git] / cdsa / cdsa_utilities / fdsel.h
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
8 * using this file.
9 *
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
16 */
17
18
19 //
20 // fdsel - select-style file descriptor set management
21 //
22 #ifndef _H_FDSEL
23 #define _H_FDSEL
24
25 #include <Security/utilities.h>
26 #include <sys/types.h>
27 #include <Security/debugging.h>
28
29
30 namespace Security {
31 namespace UnixPlusPlus {
32
33
34 //
35 // An FDSet object maintains a single select(2) compatible bitmap.
36 // Size is implicitly kept by the caller (who needs to call grow() as
37 // needed, starting at zero). As long as this is done correctly, we are
38 // not bound by the FD_SETSIZE limit.
39 // An FDSet can self-copy for select(2) use; after that, the [] operator
40 // investigates the copy.
41 //
42 // Why are we using the FD_* macros even though we know these
43 // are fd_mask arrays? Some implementations use optimized assembly
44 // for these operations, and we want to pick those up.
45 //
46 // This whole mess is completely UNIX specific. If your system has
47 // the poll(2) system call, ditch this and use it.
48 //
49 class FDSet {
50 public:
51 FDSet() : mBits(NULL), mUseBits(NULL) { }
52 ~FDSet();
53
54 void grow(int oldWords, int newWords);
55 void set(int fd, bool on);
56
57 fd_set *make(int words);
58 bool operator [] (int fd) const { return FD_ISSET(fd, (fd_set *)mUseBits); }
59
60 inline static int words(int fd) { return (fd - 1) / NFDBITS + 1; }
61
62 private:
63 fd_mask *mBits; // base bits
64 fd_mask *mUseBits; // mutable copy for select(2)
65
66 void grow(fd_mask * &bits, int oldWords, int newWords);
67 };
68
69
70 } // end namespace UnixPlusPlus
71 } // end namespace Security
72
73
74 #endif //_H_FDSEL