xref: /xnu-10063.101.15/libkern/os/object.h (revision 94d3b452840153a99b38a3a9659680b2a006908e)
1*94d3b452SApple OSS Distributions /*
2*94d3b452SApple OSS Distributions  * Copyright (c) 2011-2014 Apple Inc. All rights reserved.
3*94d3b452SApple OSS Distributions  *
4*94d3b452SApple OSS Distributions  * @APPLE_APACHE_LICENSE_HEADER_START@
5*94d3b452SApple OSS Distributions  *
6*94d3b452SApple OSS Distributions  * Licensed under the Apache License, Version 2.0 (the "License");
7*94d3b452SApple OSS Distributions  * you may not use this file except in compliance with the License.
8*94d3b452SApple OSS Distributions  * You may obtain a copy of the License at
9*94d3b452SApple OSS Distributions  *
10*94d3b452SApple OSS Distributions  *     http://www.apache.org/licenses/LICENSE-2.0
11*94d3b452SApple OSS Distributions  *
12*94d3b452SApple OSS Distributions  * Unless required by applicable law or agreed to in writing, software
13*94d3b452SApple OSS Distributions  * distributed under the License is distributed on an "AS IS" BASIS,
14*94d3b452SApple OSS Distributions  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15*94d3b452SApple OSS Distributions  * See the License for the specific language governing permissions and
16*94d3b452SApple OSS Distributions  * limitations under the License.
17*94d3b452SApple OSS Distributions  *
18*94d3b452SApple OSS Distributions  * @APPLE_APACHE_LICENSE_HEADER_END@
19*94d3b452SApple OSS Distributions  */
20*94d3b452SApple OSS Distributions 
21*94d3b452SApple OSS Distributions #if KERNEL
22*94d3b452SApple OSS Distributions 
23*94d3b452SApple OSS Distributions #ifndef __OS_OBJECT__
24*94d3b452SApple OSS Distributions #define __OS_OBJECT__
25*94d3b452SApple OSS Distributions 
26*94d3b452SApple OSS Distributions #ifdef __APPLE__
27*94d3b452SApple OSS Distributions #include <Availability.h>
28*94d3b452SApple OSS Distributions #endif
29*94d3b452SApple OSS Distributions #include <os/base.h>
30*94d3b452SApple OSS Distributions 
31*94d3b452SApple OSS Distributions /*!
32*94d3b452SApple OSS Distributions  * @header
33*94d3b452SApple OSS Distributions  *
34*94d3b452SApple OSS Distributions  * @preprocinfo
35*94d3b452SApple OSS Distributions  * By default, libSystem objects such as GCD and XPC objects are declared as
36*94d3b452SApple OSS Distributions  * Objective-C types when building with an Objective-C compiler. This allows
37*94d3b452SApple OSS Distributions  * them to participate in ARC, in RR management by the Blocks runtime and in
38*94d3b452SApple OSS Distributions  * leaks checking by the static analyzer, and enables them to be added to Cocoa
39*94d3b452SApple OSS Distributions  * collections.
40*94d3b452SApple OSS Distributions  *
41*94d3b452SApple OSS Distributions  * NOTE: this requires explicit cancellation of dispatch sources and xpc
42*94d3b452SApple OSS Distributions  *       connections whose handler blocks capture the source/connection object,
43*94d3b452SApple OSS Distributions  *       resp. ensuring that such captures do not form retain cycles (e.g. by
44*94d3b452SApple OSS Distributions  *       declaring the source as __weak).
45*94d3b452SApple OSS Distributions  *
46*94d3b452SApple OSS Distributions  * To opt-out of this default behavior, add -DOS_OBJECT_USE_OBJC=0 to your
47*94d3b452SApple OSS Distributions  * compiler flags.
48*94d3b452SApple OSS Distributions  *
49*94d3b452SApple OSS Distributions  * This mode requires a platform with the modern Objective-C runtime, the
50*94d3b452SApple OSS Distributions  * Objective-C GC compiler option to be disabled, and at least a Mac OS X 10.8
51*94d3b452SApple OSS Distributions  * or iOS 6.0 deployment target.
52*94d3b452SApple OSS Distributions  */
53*94d3b452SApple OSS Distributions 
54*94d3b452SApple OSS Distributions #ifndef OS_OBJECT_HAVE_OBJC_SUPPORT
55*94d3b452SApple OSS Distributions #if defined(__OBJC__) && defined(__OBJC2__) && !defined(__OBJC_GC__) && ( \
56*94d3b452SApple OSS Distributions 	__MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_8 || \
57*94d3b452SApple OSS Distributions 	__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_6_0)
58*94d3b452SApple OSS Distributions #define OS_OBJECT_HAVE_OBJC_SUPPORT 1
59*94d3b452SApple OSS Distributions #else
60*94d3b452SApple OSS Distributions #define OS_OBJECT_HAVE_OBJC_SUPPORT 0
61*94d3b452SApple OSS Distributions #endif
62*94d3b452SApple OSS Distributions #endif
63*94d3b452SApple OSS Distributions 
64*94d3b452SApple OSS Distributions #if OS_OBJECT_HAVE_OBJC_SUPPORT
65*94d3b452SApple OSS Distributions #ifndef OS_OBJECT_USE_OBJC
66*94d3b452SApple OSS Distributions #define OS_OBJECT_USE_OBJC 1
67*94d3b452SApple OSS Distributions #endif
68*94d3b452SApple OSS Distributions #elif defined(OS_OBJECT_USE_OBJC) && OS_OBJECT_USE_OBJC
69*94d3b452SApple OSS Distributions /* Unsupported platform for OS_OBJECT_USE_OBJC=1 */
70*94d3b452SApple OSS Distributions #undef OS_OBJECT_USE_OBJC
71*94d3b452SApple OSS Distributions #define OS_OBJECT_USE_OBJC 0
72*94d3b452SApple OSS Distributions #else
73*94d3b452SApple OSS Distributions #define OS_OBJECT_USE_OBJC 0
74*94d3b452SApple OSS Distributions #endif
75*94d3b452SApple OSS Distributions 
76*94d3b452SApple OSS Distributions #if OS_OBJECT_USE_OBJC
77*94d3b452SApple OSS Distributions #import <objc/NSObject.h>
78*94d3b452SApple OSS Distributions #if defined(__has_attribute)
79*94d3b452SApple OSS Distributions #if __has_attribute(objc_independent_class)
80*94d3b452SApple OSS Distributions #define OS_OBJC_INDEPENDENT_CLASS __attribute__((objc_independent_class))
81*94d3b452SApple OSS Distributions #endif
82*94d3b452SApple OSS Distributions #endif // __has_attribute(objc_independent_class)
83*94d3b452SApple OSS Distributions #ifndef OS_OBJC_INDEPENDENT_CLASS
84*94d3b452SApple OSS Distributions #define OS_OBJC_INDEPENDENT_CLASS
85*94d3b452SApple OSS Distributions #endif
86*94d3b452SApple OSS Distributions #define OS_OBJECT_CLASS(name) OS_##name
87*94d3b452SApple OSS Distributions #define OS_OBJECT_DECL_IMPL(name, ...) \
88*94d3b452SApple OSS Distributions 	        @protocol OS_OBJECT_CLASS(name) __VA_ARGS__ \
89*94d3b452SApple OSS Distributions 	        @end \
90*94d3b452SApple OSS Distributions 	        typedef NSObject<OS_OBJECT_CLASS(name)> \
91*94d3b452SApple OSS Distributions 	                        * OS_OBJC_INDEPENDENT_CLASS name##_t
92*94d3b452SApple OSS Distributions #define OS_OBJECT_DECL(name, ...) \
93*94d3b452SApple OSS Distributions 	        OS_OBJECT_DECL_IMPL(name, <NSObject>)
94*94d3b452SApple OSS Distributions #define OS_OBJECT_DECL_SUBCLASS(name, super) \
95*94d3b452SApple OSS Distributions 	        OS_OBJECT_DECL_IMPL(name, <OS_OBJECT_CLASS(super)>)
96*94d3b452SApple OSS Distributions #if defined(__has_attribute)
97*94d3b452SApple OSS Distributions #if __has_attribute(ns_returns_retained)
98*94d3b452SApple OSS Distributions #define OS_OBJECT_RETURNS_RETAINED __attribute__((__ns_returns_retained__))
99*94d3b452SApple OSS Distributions #else
100*94d3b452SApple OSS Distributions #define OS_OBJECT_RETURNS_RETAINED
101*94d3b452SApple OSS Distributions #endif
102*94d3b452SApple OSS Distributions #if __has_attribute(ns_consumed)
103*94d3b452SApple OSS Distributions #define OS_OBJECT_CONSUMED __attribute__((__ns_consumed__))
104*94d3b452SApple OSS Distributions #else
105*94d3b452SApple OSS Distributions #define OS_OBJECT_CONSUMED
106*94d3b452SApple OSS Distributions #endif
107*94d3b452SApple OSS Distributions #else
108*94d3b452SApple OSS Distributions #define OS_OBJECT_RETURNS_RETAINED
109*94d3b452SApple OSS Distributions #define OS_OBJECT_CONSUMED
110*94d3b452SApple OSS Distributions #endif
111*94d3b452SApple OSS Distributions #if defined(__has_feature)
112*94d3b452SApple OSS Distributions #if __has_feature(objc_arc)
113*94d3b452SApple OSS Distributions #define OS_OBJECT_BRIDGE __bridge
114*94d3b452SApple OSS Distributions #define OS_WARN_RESULT_NEEDS_RELEASE
115*94d3b452SApple OSS Distributions #else
116*94d3b452SApple OSS Distributions #define OS_OBJECT_BRIDGE
117*94d3b452SApple OSS Distributions #define OS_WARN_RESULT_NEEDS_RELEASE OS_WARN_RESULT
118*94d3b452SApple OSS Distributions #endif
119*94d3b452SApple OSS Distributions #else
120*94d3b452SApple OSS Distributions #define OS_OBJECT_BRIDGE
121*94d3b452SApple OSS Distributions #define OS_WARN_RESULT_NEEDS_RELEASE OS_WARN_RESULT
122*94d3b452SApple OSS Distributions #endif
123*94d3b452SApple OSS Distributions #ifndef OS_OBJECT_USE_OBJC_RETAIN_RELEASE
124*94d3b452SApple OSS Distributions #if defined(__clang_analyzer__)
125*94d3b452SApple OSS Distributions #define OS_OBJECT_USE_OBJC_RETAIN_RELEASE 1
126*94d3b452SApple OSS Distributions #elif defined(__has_feature)
127*94d3b452SApple OSS Distributions #if __has_feature(objc_arc)
128*94d3b452SApple OSS Distributions #define OS_OBJECT_USE_OBJC_RETAIN_RELEASE 1
129*94d3b452SApple OSS Distributions #else
130*94d3b452SApple OSS Distributions #define OS_OBJECT_USE_OBJC_RETAIN_RELEASE 0
131*94d3b452SApple OSS Distributions #endif
132*94d3b452SApple OSS Distributions #else
133*94d3b452SApple OSS Distributions #define OS_OBJECT_USE_OBJC_RETAIN_RELEASE 0
134*94d3b452SApple OSS Distributions #endif
135*94d3b452SApple OSS Distributions #endif
136*94d3b452SApple OSS Distributions #else
137*94d3b452SApple OSS Distributions /*! @parseOnly */
138*94d3b452SApple OSS Distributions #define OS_OBJECT_RETURNS_RETAINED
139*94d3b452SApple OSS Distributions /*! @parseOnly */
140*94d3b452SApple OSS Distributions #define OS_OBJECT_CONSUMED
141*94d3b452SApple OSS Distributions /*! @parseOnly */
142*94d3b452SApple OSS Distributions #define OS_OBJECT_BRIDGE
143*94d3b452SApple OSS Distributions /*! @parseOnly */
144*94d3b452SApple OSS Distributions #define OS_WARN_RESULT_NEEDS_RELEASE OS_WARN_RESULT
145*94d3b452SApple OSS Distributions #define OS_OBJECT_USE_OBJC_RETAIN_RELEASE 0
146*94d3b452SApple OSS Distributions #endif
147*94d3b452SApple OSS Distributions 
148*94d3b452SApple OSS Distributions #define OS_OBJECT_GLOBAL_OBJECT(type, object) ((OS_OBJECT_BRIDGE type)&(object))
149*94d3b452SApple OSS Distributions 
150*94d3b452SApple OSS Distributions __BEGIN_DECLS
151*94d3b452SApple OSS Distributions 
152*94d3b452SApple OSS Distributions /*!
153*94d3b452SApple OSS Distributions  * @function os_retain
154*94d3b452SApple OSS Distributions  *
155*94d3b452SApple OSS Distributions  * @abstract
156*94d3b452SApple OSS Distributions  * Increment the reference count of an os_object.
157*94d3b452SApple OSS Distributions  *
158*94d3b452SApple OSS Distributions  * @discussion
159*94d3b452SApple OSS Distributions  * On a platform with the modern Objective-C runtime this is exactly equivalent
160*94d3b452SApple OSS Distributions  * to sending the object the -[retain] message.
161*94d3b452SApple OSS Distributions  *
162*94d3b452SApple OSS Distributions  * @param object
163*94d3b452SApple OSS Distributions  * The object to retain.
164*94d3b452SApple OSS Distributions  *
165*94d3b452SApple OSS Distributions  * @result
166*94d3b452SApple OSS Distributions  * The retained object.
167*94d3b452SApple OSS Distributions  */
168*94d3b452SApple OSS Distributions __OSX_AVAILABLE_STARTING(__MAC_10_12, __IPHONE_10_0)
169*94d3b452SApple OSS Distributions OS_EXPORT
170*94d3b452SApple OSS Distributions void*
171*94d3b452SApple OSS Distributions os_retain(void *object);
172*94d3b452SApple OSS Distributions #if OS_OBJECT_USE_OBJC
173*94d3b452SApple OSS Distributions #undef os_retain
174*94d3b452SApple OSS Distributions #define os_retain(object) [object retain]
175*94d3b452SApple OSS Distributions #endif
176*94d3b452SApple OSS Distributions 
177*94d3b452SApple OSS Distributions /*!
178*94d3b452SApple OSS Distributions  * @function os_release
179*94d3b452SApple OSS Distributions  *
180*94d3b452SApple OSS Distributions  * @abstract
181*94d3b452SApple OSS Distributions  * Decrement the reference count of a os_object.
182*94d3b452SApple OSS Distributions  *
183*94d3b452SApple OSS Distributions  * @discussion
184*94d3b452SApple OSS Distributions  * On a platform with the modern Objective-C runtime this is exactly equivalent
185*94d3b452SApple OSS Distributions  * to sending the object the -[release] message.
186*94d3b452SApple OSS Distributions  *
187*94d3b452SApple OSS Distributions  * @param object
188*94d3b452SApple OSS Distributions  * The object to release.
189*94d3b452SApple OSS Distributions  */
190*94d3b452SApple OSS Distributions __OSX_AVAILABLE_STARTING(__MAC_10_12, __IPHONE_10_0)
191*94d3b452SApple OSS Distributions OS_EXPORT
192*94d3b452SApple OSS Distributions void
193*94d3b452SApple OSS Distributions os_release(void *object);
194*94d3b452SApple OSS Distributions #if OS_OBJECT_USE_OBJC
195*94d3b452SApple OSS Distributions #undef os_release
196*94d3b452SApple OSS Distributions #define os_release(object) [object release]
197*94d3b452SApple OSS Distributions #endif
198*94d3b452SApple OSS Distributions 
199*94d3b452SApple OSS Distributions #define fastpath(x) ((typeof(x))__builtin_expect((long)(x), ~0l))
200*94d3b452SApple OSS Distributions #define slowpath(x) ((typeof(x))__builtin_expect((long)(x), 0l))
201*94d3b452SApple OSS Distributions 
202*94d3b452SApple OSS Distributions __END_DECLS
203*94d3b452SApple OSS Distributions 
204*94d3b452SApple OSS Distributions #endif /* OS_OBJECT file guard */
205*94d3b452SApple OSS Distributions 
206*94d3b452SApple OSS Distributions #else /* KERNEL */
207*94d3b452SApple OSS Distributions 
208*94d3b452SApple OSS Distributions /* This should use the libdispatch header */
209*94d3b452SApple OSS Distributions #include_next <os/object.h>
210*94d3b452SApple OSS Distributions 
211*94d3b452SApple OSS Distributions #endif /* KERNEL */
212