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