1/*
2 * Copyright (c) 2003-2012 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28#ifndef _FD_SET
29#define _FD_SET
30
31#include <machine/types.h> /* __int32_t and uintptr_t */
32#if !KERNEL
33#include <Availability.h>
34#endif
35
36/*
37 * Select uses bit masks of file descriptors in longs. These macros
38 * manipulate such bit fields (the filesystem macros use chars). The
39 * extra protection here is to permit application redefinition above
40 * the default size.
41 */
42#ifdef FD_SETSIZE
43#define __DARWIN_FD_SETSIZE FD_SETSIZE
44#else /* !FD_SETSIZE */
45#define __DARWIN_FD_SETSIZE 1024
46#endif /* FD_SETSIZE */
47#define __DARWIN_NBBY 8 /* bits in a byte */
48#define __DARWIN_NFDBITS (sizeof(__int32_t) * __DARWIN_NBBY) /* bits per mask */
49#define __DARWIN_howmany(x, y) ((((x) % (y)) == 0) ? ((x) / (y)) : (((x) / (y)) + 1)) /* # y's == x bits? */
50
51__BEGIN_DECLS
52typedef struct fd_set {
53 __int32_t fds_bits[__DARWIN_howmany(__DARWIN_FD_SETSIZE, __DARWIN_NFDBITS)];
54} fd_set;
55
56#if !KERNEL
57int __darwin_check_fd_set_overflow(int, const void *, int) __API_AVAILABLE(macosx(10.16), ios(14.0), tvos(14.0), watchos(7.0));
58#endif
59__END_DECLS
60
61#if !KERNEL
62__header_always_inline int
63__darwin_check_fd_set(int _a, const void *_b)
64{
65#ifdef __clang__
66#pragma clang diagnostic push
67#pragma clang diagnostic ignored "-Wunguarded-availability-new"
68#endif
69 if ((uintptr_t)&__darwin_check_fd_set_overflow != (uintptr_t) 0) {
70#if defined(_DARWIN_UNLIMITED_SELECT) || defined(_DARWIN_C_SOURCE)
71 return __darwin_check_fd_set_overflow(_a, _b, 1);
72#else
73 return __darwin_check_fd_set_overflow(_a, _b, 0);
74#endif
75 } else {
76 return 1;
77 }
78#ifdef __clang__
79#pragma clang diagnostic pop
80#endif
81}
82
83/* This inline avoids argument side-effect issues with FD_ISSET() */
84__header_always_inline int
85__darwin_fd_isset(int _fd, const struct fd_set *_p)
86{
87 if (__darwin_check_fd_set(_fd, (const void *) _p)) {
88 return _p->fds_bits[(unsigned long)_fd / __DARWIN_NFDBITS] & ((__int32_t)(((unsigned long)1) << ((unsigned long)_fd % __DARWIN_NFDBITS)));
89 }
90
91 return 0;
92}
93
94__header_always_inline void
95__darwin_fd_set(int _fd, struct fd_set *const _p)
96{
97 if (__darwin_check_fd_set(_fd, (const void *) _p)) {
98 (_p->fds_bits[(unsigned long)_fd / __DARWIN_NFDBITS] |= ((__int32_t)(((unsigned long)1) << ((unsigned long)_fd % __DARWIN_NFDBITS))));
99 }
100}
101
102__header_always_inline void
103__darwin_fd_clr(int _fd, struct fd_set *const _p)
104{
105 if (__darwin_check_fd_set(_fd, (const void *) _p)) {
106 (_p->fds_bits[(unsigned long)_fd / __DARWIN_NFDBITS] &= ~((__int32_t)(((unsigned long)1) << ((unsigned long)_fd % __DARWIN_NFDBITS))));
107 }
108}
109
110#else /* KERNEL */
111
112__header_always_inline int
113__darwin_fd_isset(int _fd, const struct fd_set *_p)
114{
115 return _p->fds_bits[(unsigned long)_fd / __DARWIN_NFDBITS] & ((__int32_t)(((unsigned long)1) << ((unsigned long)_fd % __DARWIN_NFDBITS)));
116}
117
118__header_always_inline void
119__darwin_fd_set(int _fd, struct fd_set *const _p)
120{
121 (_p->fds_bits[(unsigned long)_fd / __DARWIN_NFDBITS] |= ((__int32_t)(((unsigned long)1) << ((unsigned long)_fd % __DARWIN_NFDBITS))));
122}
123
124__header_always_inline void
125__darwin_fd_clr(int _fd, struct fd_set *const _p)
126{
127 (_p->fds_bits[(unsigned long)_fd / __DARWIN_NFDBITS] &= ~((__int32_t)(((unsigned long)1) << ((unsigned long)_fd % __DARWIN_NFDBITS))));
128}
129#endif /* KERNEL */
130
131#define __DARWIN_FD_SET(n, p) __darwin_fd_set((n), (p))
132#define __DARWIN_FD_CLR(n, p) __darwin_fd_clr((n), (p))
133#define __DARWIN_FD_ISSET(n, p) __darwin_fd_isset((n), (p))
134
135#if __GNUC__ > 3 || __GNUC__ == 3 && __GNUC_MINOR__ >= 3
136/*
137 * Use the built-in bzero function instead of the library version so that
138 * we do not pollute the namespace or introduce prototype warnings.
139 */
140#define __DARWIN_FD_ZERO(p) __builtin_bzero(p, sizeof(*(p)))
141#else
142#define __DARWIN_FD_ZERO(p) bzero(p, sizeof(*(p)))
143#endif
144
145#define __DARWIN_FD_COPY(f, t) bcopy(f, t, sizeof(*(f)))
146#endif /* _FD_SET */
147