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