2 * Copyright (c) 2011-2014 Apple Inc. All rights reserved.
4 * @APPLE_APACHE_LICENSE_HEADER_START@
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * @APPLE_APACHE_LICENSE_HEADER_END@
25 #include <Availability.h>
26 #include <TargetConditionals.h>
31 #include <os/linux_base.h>
38 * By default, libSystem objects such as GCD and XPC objects are declared as
39 * Objective-C types when building with an Objective-C compiler. This allows
40 * them to participate in ARC, in RR management by the Blocks runtime and in
41 * leaks checking by the static analyzer, and enables them to be added to Cocoa
44 * NOTE: this requires explicit cancellation of dispatch sources and xpc
45 * connections whose handler blocks capture the source/connection object,
46 * resp. ensuring that such captures do not form retain cycles (e.g. by
47 * declaring the source as __weak).
49 * To opt-out of this default behavior, add -DOS_OBJECT_USE_OBJC=0 to your
52 * This mode requires a platform with the modern Objective-C runtime, the
53 * Objective-C GC compiler option to be disabled, and at least a Mac OS X 10.8
54 * or iOS 6.0 deployment target.
57 #ifndef OS_OBJECT_HAVE_OBJC_SUPPORT
58 #if !defined(__OBJC__) || defined(__OBJC_GC__)
59 # define OS_OBJECT_HAVE_OBJC_SUPPORT 0
60 #elif !defined(TARGET_OS_MAC) || !TARGET_OS_MAC
61 # define OS_OBJECT_HAVE_OBJC_SUPPORT 0
62 #elif TARGET_OS_IOS && __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0
63 # define OS_OBJECT_HAVE_OBJC_SUPPORT 0
64 #elif TARGET_OS_MAC && !TARGET_OS_IPHONE
65 # if __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_8
66 # define OS_OBJECT_HAVE_OBJC_SUPPORT 0
67 # elif defined(__i386__) && __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_12
68 # define OS_OBJECT_HAVE_OBJC_SUPPORT 0
70 # define OS_OBJECT_HAVE_OBJC_SUPPORT 1
73 # define OS_OBJECT_HAVE_OBJC_SUPPORT 1
75 #endif // OS_OBJECT_HAVE_OBJC_SUPPORT
77 #if OS_OBJECT_HAVE_OBJC_SUPPORT
78 #ifndef OS_OBJECT_USE_OBJC
79 #define OS_OBJECT_USE_OBJC 1
81 #elif defined(OS_OBJECT_USE_OBJC) && OS_OBJECT_USE_OBJC
82 /* Unsupported platform for OS_OBJECT_USE_OBJC=1 */
83 #undef OS_OBJECT_USE_OBJC
84 #define OS_OBJECT_USE_OBJC 0
86 #define OS_OBJECT_USE_OBJC 0
89 #ifndef OS_OBJECT_SWIFT3
90 #if defined(SWIFT_SDK_OVERLAY_DISPATCH_EPOCH) && \
91 SWIFT_SDK_OVERLAY_DISPATCH_EPOCH >= 2
92 #define OS_OBJECT_SWIFT3 1
94 #define OS_OBJECT_SWIFT3 0
95 #endif // SWIFT_SDK_OVERLAY_DISPATCH_EPOCH >= 2
96 #endif // OS_OBJECT_SWIFT3
98 #if OS_OBJECT_USE_OBJC
99 #import <objc/NSObject.h>
100 #if __has_attribute(objc_independent_class)
101 #define OS_OBJC_INDEPENDENT_CLASS __attribute__((objc_independent_class))
102 #endif // __has_attribute(objc_independent_class)
103 #ifndef OS_OBJC_INDEPENDENT_CLASS
104 #define OS_OBJC_INDEPENDENT_CLASS
106 #define OS_OBJECT_CLASS(name) OS_##name
107 #define OS_OBJECT_DECL_PROTOCOL(name, ...) \
108 @protocol OS_OBJECT_CLASS(name) __VA_ARGS__ \
110 #define OS_OBJECT_CLASS_IMPLEMENTS_PROTOCOL_IMPL(name, proto) \
111 @interface name () <proto> \
113 #define OS_OBJECT_CLASS_IMPLEMENTS_PROTOCOL(name, proto) \
114 OS_OBJECT_CLASS_IMPLEMENTS_PROTOCOL_IMPL( \
115 OS_OBJECT_CLASS(name), OS_OBJECT_CLASS(proto))
116 #define OS_OBJECT_DECL_IMPL(name, ...) \
117 OS_OBJECT_DECL_PROTOCOL(name, __VA_ARGS__) \
118 typedef NSObject<OS_OBJECT_CLASS(name)> \
119 * OS_OBJC_INDEPENDENT_CLASS name##_t
120 #define OS_OBJECT_DECL_BASE(name, ...) \
121 @interface OS_OBJECT_CLASS(name) : __VA_ARGS__ \
122 - (instancetype)init OS_SWIFT_UNAVAILABLE("Unavailable in Swift"); \
124 #define OS_OBJECT_DECL_IMPL_CLASS(name, ...) \
125 OS_OBJECT_DECL_BASE(name, ## __VA_ARGS__) \
126 typedef OS_OBJECT_CLASS(name) \
127 * OS_OBJC_INDEPENDENT_CLASS name##_t
128 #define OS_OBJECT_DECL(name, ...) \
129 OS_OBJECT_DECL_IMPL(name, <NSObject>)
130 #define OS_OBJECT_DECL_SUBCLASS(name, super) \
131 OS_OBJECT_DECL_IMPL(name, <OS_OBJECT_CLASS(super)>)
132 #if __has_attribute(ns_returns_retained)
133 #define OS_OBJECT_RETURNS_RETAINED __attribute__((__ns_returns_retained__))
135 #define OS_OBJECT_RETURNS_RETAINED
137 #if __has_attribute(ns_consumed)
138 #define OS_OBJECT_CONSUMED __attribute__((__ns_consumed__))
140 #define OS_OBJECT_CONSUMED
142 #if __has_feature(objc_arc)
143 #define OS_OBJECT_BRIDGE __bridge
144 #define OS_WARN_RESULT_NEEDS_RELEASE
146 #define OS_OBJECT_BRIDGE
147 #define OS_WARN_RESULT_NEEDS_RELEASE OS_WARN_RESULT
149 #if __has_attribute(objc_runtime_visible) && \
150 ((defined(__MAC_OS_X_VERSION_MIN_REQUIRED) && \
151 __MAC_OS_X_VERSION_MIN_REQUIRED < __MAC_10_12) || \
152 (defined(__IPHONE_OS_VERSION_MIN_REQUIRED) && \
153 !defined(__TV_OS_VERSION_MIN_REQUIRED) && \
154 !defined(__WATCH_OS_VERSION_MIN_REQUIRED) && \
155 __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_10_0) || \
156 (defined(__TV_OS_VERSION_MIN_REQUIRED) && \
157 __TV_OS_VERSION_MIN_REQUIRED < __TVOS_10_0) || \
158 (defined(__WATCH_OS_VERSION_MIN_REQUIRED) && \
159 __WATCH_OS_VERSION_MIN_REQUIRED < __WATCHOS_3_0))
161 * To provide backward deployment of ObjC objects in Swift on pre-10.12
162 * SDKs, OS_object classes can be marked as OS_OBJECT_OBJC_RUNTIME_VISIBLE.
163 * When compiling with a deployment target earlier than OS X 10.12 (iOS 10.0,
164 * tvOS 10.0, watchOS 3.0) the Swift compiler will only refer to this type at
165 * runtime (using the ObjC runtime).
167 #define OS_OBJECT_OBJC_RUNTIME_VISIBLE __attribute__((objc_runtime_visible))
169 #define OS_OBJECT_OBJC_RUNTIME_VISIBLE
171 #ifndef OS_OBJECT_USE_OBJC_RETAIN_RELEASE
172 #if defined(__clang_analyzer__)
173 #define OS_OBJECT_USE_OBJC_RETAIN_RELEASE 1
174 #elif __has_feature(objc_arc) && !OS_OBJECT_SWIFT3
175 #define OS_OBJECT_USE_OBJC_RETAIN_RELEASE 1
177 #define OS_OBJECT_USE_OBJC_RETAIN_RELEASE 0
181 #define OS_OBJECT_DECL_SWIFT(name) \
182 OS_EXPORT OS_OBJECT_OBJC_RUNTIME_VISIBLE \
183 OS_OBJECT_DECL_IMPL_CLASS(name, NSObject)
184 #define OS_OBJECT_DECL_SUBCLASS_SWIFT(name, super) \
185 OS_EXPORT OS_OBJECT_OBJC_RUNTIME_VISIBLE \
186 OS_OBJECT_DECL_IMPL_CLASS(name, OS_OBJECT_CLASS(super))
187 OS_EXPORT OS_OBJECT_OBJC_RUNTIME_VISIBLE
188 OS_OBJECT_DECL_BASE(object
, NSObject
);
189 #endif // OS_OBJECT_SWIFT3
192 #define OS_OBJECT_RETURNS_RETAINED
194 #define OS_OBJECT_CONSUMED
196 #define OS_OBJECT_BRIDGE
198 #define OS_WARN_RESULT_NEEDS_RELEASE OS_WARN_RESULT
200 #define OS_OBJECT_OBJC_RUNTIME_VISIBLE
201 #define OS_OBJECT_USE_OBJC_RETAIN_RELEASE 0
205 #define OS_OBJECT_DECL_CLASS(name) \
206 OS_OBJECT_DECL_SUBCLASS_SWIFT(name, object)
207 #elif OS_OBJECT_USE_OBJC
208 #define OS_OBJECT_DECL_CLASS(name) \
211 #define OS_OBJECT_DECL_CLASS(name) \
212 typedef struct name##_s *name##_t
215 #define OS_OBJECT_GLOBAL_OBJECT(type, object) ((OS_OBJECT_BRIDGE type)&(object))
220 * @function os_retain
223 * Increment the reference count of an os_object.
226 * On a platform with the modern Objective-C runtime this is exactly equivalent
227 * to sending the object the -[retain] message.
230 * The object to retain.
233 * The retained object.
235 __OSX_AVAILABLE_STARTING(__MAC_10_10
,__IPHONE_8_0
)
236 OS_EXPORT
OS_SWIFT_UNAVAILABLE("Can't be used with ARC")
238 os_retain(void *object
);
239 #if OS_OBJECT_USE_OBJC
241 #define os_retain(object) [object retain]
245 * @function os_release
248 * Decrement the reference count of a os_object.
251 * On a platform with the modern Objective-C runtime this is exactly equivalent
252 * to sending the object the -[release] message.
255 * The object to release.
257 __OSX_AVAILABLE_STARTING(__MAC_10_10
,__IPHONE_8_0
)
259 void OS_SWIFT_UNAVAILABLE("Can't be used with ARC")
260 os_release(void *object
);
261 #if OS_OBJECT_USE_OBJC
263 #define os_release(object) [object release]