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