]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_utilities/lib/fdsel.cpp
Security-57337.40.85.tar.gz
[apple/security.git] / OSX / libsecurity_utilities / lib / fdsel.cpp
1 /*
2 * Copyright (c) 2000-2001,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 #include "fdsel.h"
29
30
31 namespace Security {
32 namespace UnixPlusPlus {
33
34
35 //
36 // Throw the bitvectors away on destruction
37 //
38 FDSet::~FDSet()
39 {
40 delete mBits;
41 delete mUseBits;
42 }
43
44
45 //
46 // Given the old and desired new sizes (in fd_mask words), grow
47 // the bitvectors. New storage is zero filled. Note that we preserve
48 // the mUseBits vector, so this is safe to do during a post-select scan.
49 // This function cannot shrink the bitmaps.
50 //
51 void FDSet::grow(int oldWords, int newWords)
52 {
53 assert(oldWords < newWords);
54 grow(mBits, oldWords, newWords);
55 grow(mUseBits, oldWords, newWords);
56 }
57
58 void FDSet::grow(fd_mask * &bits, int oldWords, int newWords)
59 {
60 fd_mask *newBits = new fd_mask[newWords];
61 memcpy(newBits, bits, oldWords * sizeof(fd_mask));
62 memset(newBits + oldWords, 0, (newWords - oldWords) * sizeof(fd_mask));
63 delete [] bits;
64 bits = newBits;
65 }
66
67
68 //
69 // Set or clear a single bit in the map.
70 // No check for overflow is perfomed.
71 //
72 void FDSet::set(int fd, bool on)
73 {
74 if (on) {
75 FD_SET(fd, (fd_set *)mBits);
76 } else {
77 FD_CLR(fd, (fd_set *)mBits);
78 FD_CLR(fd, (fd_set *)mUseBits);
79 }
80 }
81
82
83 //
84 // Copy only the first words fd_mask words from mBits to mUseBits
85 // and return that for select(2) use.
86 //
87 fd_set *FDSet::make(int words)
88 {
89 //@@@ if empty -> return NULL (but check caller for [] use)
90 memcpy(mUseBits, mBits, words * sizeof(fd_mask));
91 return (fd_set *)mUseBits;
92 }
93
94
95 } // end namespace IPPlusPlus
96 } // end namespace Security