objc-auto.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright (c) 2004-2007 Apple Inc. All rights reserved.
  3. *
  4. * @APPLE_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. Please obtain a copy of the License at
  10. * http://www.opensource.apple.com/apsl/ and read it before using this
  11. * file.
  12. *
  13. * The Original Code and all software distributed under the License are
  14. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  15. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  16. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  18. * Please see the License for the specific language governing rights and
  19. * limitations under the License.
  20. *
  21. * @APPLE_LICENSE_HEADER_END@
  22. */
  23. #ifndef _OBJC_AUTO_H_
  24. #define _OBJC_AUTO_H_
  25. #include <objc/objc.h>
  26. #include <malloc/malloc.h>
  27. #include <stdint.h>
  28. #include <stddef.h>
  29. #include <string.h>
  30. #include <Availability.h>
  31. #include <TargetConditionals.h>
  32. #include <sys/types.h>
  33. #include <libkern/OSAtomic.h>
  34. // Define OBJC_SILENCE_GC_DEPRECATIONS=1 to temporarily
  35. // silence deprecation warnings for GC functions.
  36. #if OBJC_SILENCE_GC_DEPRECATIONS
  37. # define OBJC_GC_DEPRECATED(message)
  38. #elif __has_extension(attribute_deprecated_with_message)
  39. # define OBJC_GC_DEPRECATED(message) __attribute__((deprecated(message ". Define OBJC_SILENCE_GC_DEPRECATIONS=1 to temporarily silence this diagnostic.")))
  40. #else
  41. # define OBJC_GC_DEPRECATED(message) __attribute__((deprecated))
  42. #endif
  43. enum {
  44. OBJC_RATIO_COLLECTION = (0 << 0),
  45. OBJC_GENERATIONAL_COLLECTION = (1 << 0),
  46. OBJC_FULL_COLLECTION = (2 << 0),
  47. OBJC_EXHAUSTIVE_COLLECTION = (3 << 0),
  48. OBJC_COLLECT_IF_NEEDED = (1 << 3),
  49. OBJC_WAIT_UNTIL_DONE = (1 << 4)
  50. };
  51. enum {
  52. OBJC_CLEAR_RESIDENT_STACK = (1 << 0)
  53. };
  54. #if !defined(OBJC_NO_GC) || \
  55. (OBJC_DECLARE_SYMBOLS && !defined(OBJC_NO_GC_API))
  56. /* Out-of-line declarations */
  57. OBJC_EXPORT void objc_collect(unsigned long options)
  58. __OSX_DEPRECATED(10.6, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE;
  59. OBJC_EXPORT BOOL objc_collectingEnabled(void)
  60. __OSX_DEPRECATED(10.5, 10.8, "it always returns NO") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE;
  61. OBJC_EXPORT malloc_zone_t *objc_collectableZone(void)
  62. __OSX_DEPRECATED(10.7, 10.8, "it always returns nil") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE;
  63. OBJC_EXPORT void objc_setCollectionThreshold(size_t threshold)
  64. __OSX_DEPRECATED(10.5, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE;
  65. OBJC_EXPORT void objc_setCollectionRatio(size_t ratio)
  66. __OSX_DEPRECATED(10.5, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE;
  67. OBJC_EXPORT BOOL objc_atomicCompareAndSwapPtr(id predicate, id replacement, volatile id *objectLocation)
  68. __OSX_DEPRECATED(10.6, 10.8, "use OSAtomicCompareAndSwapPtr instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE OBJC_ARC_UNAVAILABLE;
  69. OBJC_EXPORT BOOL objc_atomicCompareAndSwapPtrBarrier(id predicate, id replacement, volatile id *objectLocation)
  70. __OSX_DEPRECATED(10.6, 10.8, "use OSAtomicCompareAndSwapPtrBarrier instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE OBJC_ARC_UNAVAILABLE;
  71. OBJC_EXPORT BOOL objc_atomicCompareAndSwapGlobal(id predicate, id replacement, volatile id *objectLocation)
  72. __OSX_DEPRECATED(10.6, 10.8, "use OSAtomicCompareAndSwapPtr instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE OBJC_ARC_UNAVAILABLE;
  73. OBJC_EXPORT BOOL objc_atomicCompareAndSwapGlobalBarrier(id predicate, id replacement, volatile id *objectLocation)
  74. __OSX_DEPRECATED(10.6, 10.8, "use OSAtomicCompareAndSwapPtrBarrier instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE OBJC_ARC_UNAVAILABLE;
  75. OBJC_EXPORT BOOL objc_atomicCompareAndSwapInstanceVariable(id predicate, id replacement, volatile id *objectLocation)
  76. __OSX_DEPRECATED(10.6, 10.8, "use OSAtomicCompareAndSwapPtr instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE OBJC_ARC_UNAVAILABLE;
  77. OBJC_EXPORT BOOL objc_atomicCompareAndSwapInstanceVariableBarrier(id predicate, id replacement, volatile id *objectLocation)
  78. __OSX_DEPRECATED(10.6, 10.8, "use OSAtomicCompareAndSwapPtrBarrier instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE OBJC_ARC_UNAVAILABLE;
  79. OBJC_EXPORT id objc_assign_strongCast(id val, id *dest)
  80. __OSX_DEPRECATED(10.4, 10.8, "use a simple assignment instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE;
  81. OBJC_EXPORT id objc_assign_global(id val, id *dest)
  82. __OSX_DEPRECATED(10.4, 10.8, "use a simple assignment instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE;
  83. OBJC_EXPORT id objc_assign_threadlocal(id val, id *dest)
  84. __OSX_DEPRECATED(10.7, 10.8, "use a simple assignment instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE;
  85. OBJC_EXPORT id objc_assign_ivar(id value, id dest, ptrdiff_t offset)
  86. __OSX_DEPRECATED(10.4, 10.8, "use a simple assignment instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE;
  87. OBJC_EXPORT void *objc_memmove_collectable(void *dst, const void *src, size_t size)
  88. __OSX_DEPRECATED(10.4, 10.8, "use memmove instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE;
  89. OBJC_EXPORT id objc_read_weak(id *location)
  90. __OSX_DEPRECATED(10.5, 10.8, "use a simple read instead, or convert to zeroing __weak") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE;
  91. OBJC_EXPORT id objc_assign_weak(id value, id *location)
  92. __OSX_DEPRECATED(10.5, 10.8, "use a simple assignment instead, or convert to zeroing __weak") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE;
  93. OBJC_EXPORT void objc_registerThreadWithCollector(void)
  94. __OSX_DEPRECATED(10.6, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE;
  95. OBJC_EXPORT void objc_unregisterThreadWithCollector(void)
  96. __OSX_DEPRECATED(10.6, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE;
  97. OBJC_EXPORT void objc_assertRegisteredThreadWithCollector(void)
  98. __OSX_DEPRECATED(10.6, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE;
  99. OBJC_EXPORT void objc_clear_stack(unsigned long options)
  100. __OSX_DEPRECATED(10.5, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE;
  101. OBJC_EXPORT BOOL objc_is_finalized(void *ptr)
  102. __OSX_DEPRECATED(10.4, 10.8, "it always returns NO") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE;
  103. OBJC_EXPORT void objc_finalizeOnMainThread(Class cls)
  104. __OSX_DEPRECATED(10.5, 10.5, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE;
  105. OBJC_EXPORT BOOL objc_collecting_enabled(void)
  106. __OSX_DEPRECATED(10.4, 10.5, "it always returns NO") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE;
  107. OBJC_EXPORT void objc_set_collection_threshold(size_t threshold)
  108. __OSX_DEPRECATED(10.4, 10.5, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE;
  109. OBJC_EXPORT void objc_set_collection_ratio(size_t ratio)
  110. __OSX_DEPRECATED(10.4, 10.5, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE;
  111. OBJC_EXPORT void objc_start_collector_thread(void)
  112. __OSX_DEPRECATED(10.4, 10.5, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE;
  113. OBJC_EXPORT void objc_startCollectorThread(void)
  114. __OSX_DEPRECATED(10.5, 10.7, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE;
  115. OBJC_EXPORT id objc_allocate_object(Class cls, int extra)
  116. __OSX_DEPRECATED(10.4, 10.4, "use class_createInstance instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE;
  117. /* !defined(OBJC_NO_GC) */
  118. #else
  119. /* defined(OBJC_NO_GC) */
  120. /* Inline declarations */
  121. OBJC_GC_DEPRECATED("it does nothing")
  122. static OBJC_INLINE void objc_collect(unsigned long options __unused) { }
  123. OBJC_GC_DEPRECATED("it always returns NO")
  124. static OBJC_INLINE BOOL objc_collectingEnabled(void) { return NO; }
  125. #if TARGET_OS_OSX
  126. OBJC_GC_DEPRECATED("it always returns nil")
  127. static OBJC_INLINE malloc_zone_t *objc_collectableZone(void) { return nil; }
  128. #endif
  129. OBJC_GC_DEPRECATED("it does nothing")
  130. static OBJC_INLINE void objc_setCollectionThreshold(size_t threshold __unused) { }
  131. OBJC_GC_DEPRECATED("it does nothing")
  132. static OBJC_INLINE void objc_setCollectionRatio(size_t ratio __unused) { }
  133. OBJC_GC_DEPRECATED("it does nothing")
  134. static OBJC_INLINE void objc_startCollectorThread(void) { }
  135. #if __has_feature(objc_arc)
  136. /* Covers for GC memory operations are unavailable in ARC */
  137. #else
  138. OBJC_GC_DEPRECATED("use OSAtomicCompareAndSwapPtr instead")
  139. static OBJC_INLINE BOOL objc_atomicCompareAndSwapPtr(id predicate, id replacement, volatile id *objectLocation)
  140. { return OSAtomicCompareAndSwapPtr((void *)predicate, (void *)replacement, (void * volatile *)objectLocation); }
  141. OBJC_GC_DEPRECATED("use OSAtomicCompareAndSwapPtrBarrier instead")
  142. static OBJC_INLINE BOOL objc_atomicCompareAndSwapPtrBarrier(id predicate, id replacement, volatile id *objectLocation)
  143. { return OSAtomicCompareAndSwapPtrBarrier((void *)predicate, (void *)replacement, (void * volatile *)objectLocation); }
  144. OBJC_GC_DEPRECATED("use OSAtomicCompareAndSwapPtr instead")
  145. static OBJC_INLINE BOOL objc_atomicCompareAndSwapGlobal(id predicate, id replacement, volatile id *objectLocation)
  146. { return objc_atomicCompareAndSwapPtr(predicate, replacement, objectLocation); }
  147. OBJC_GC_DEPRECATED("use OSAtomicCompareAndSwapPtrBarrier instead")
  148. static OBJC_INLINE BOOL objc_atomicCompareAndSwapGlobalBarrier(id predicate, id replacement, volatile id *objectLocation)
  149. { return objc_atomicCompareAndSwapPtrBarrier(predicate, replacement, objectLocation); }
  150. OBJC_GC_DEPRECATED("use OSAtomicCompareAndSwapPtr instead")
  151. static OBJC_INLINE BOOL objc_atomicCompareAndSwapInstanceVariable(id predicate, id replacement, volatile id *objectLocation)
  152. { return objc_atomicCompareAndSwapPtr(predicate, replacement, objectLocation); }
  153. OBJC_GC_DEPRECATED("use OSAtomicCompareAndSwapPtrBarrier instead")
  154. static OBJC_INLINE BOOL objc_atomicCompareAndSwapInstanceVariableBarrier(id predicate, id replacement, volatile id *objectLocation)
  155. { return objc_atomicCompareAndSwapPtrBarrier(predicate, replacement, objectLocation); }
  156. OBJC_GC_DEPRECATED("use a simple assignment instead")
  157. static OBJC_INLINE id objc_assign_strongCast(id val, id *dest)
  158. { return (*dest = val); }
  159. OBJC_GC_DEPRECATED("use a simple assignment instead")
  160. static OBJC_INLINE id objc_assign_global(id val, id *dest)
  161. { return (*dest = val); }
  162. OBJC_GC_DEPRECATED("use a simple assignment instead")
  163. static OBJC_INLINE id objc_assign_threadlocal(id val, id *dest)
  164. { return (*dest = val); }
  165. OBJC_GC_DEPRECATED("use a simple assignment instead")
  166. static OBJC_INLINE id objc_assign_ivar(id val, id dest, ptrdiff_t offset)
  167. { return (*(id*)((intptr_t)(char *)dest+offset) = val); }
  168. OBJC_GC_DEPRECATED("use a simple read instead, or convert to zeroing __weak")
  169. static OBJC_INLINE id objc_read_weak(id *location)
  170. { return *location; }
  171. OBJC_GC_DEPRECATED("use a simple assignment instead, or convert to zeroing __weak")
  172. static OBJC_INLINE id objc_assign_weak(id value, id *location)
  173. { return (*location = value); }
  174. /* MRC */
  175. #endif
  176. OBJC_GC_DEPRECATED("use memmove instead")
  177. static OBJC_INLINE void *objc_memmove_collectable(void *dst, const void *src, size_t size)
  178. { return memmove(dst, src, size); }
  179. OBJC_GC_DEPRECATED("it does nothing")
  180. static OBJC_INLINE void objc_finalizeOnMainThread(Class cls __unused) { }
  181. OBJC_GC_DEPRECATED("it always returns NO")
  182. static OBJC_INLINE BOOL objc_is_finalized(void *ptr __unused) { return NO; }
  183. OBJC_GC_DEPRECATED("it does nothing")
  184. static OBJC_INLINE void objc_clear_stack(unsigned long options __unused) { }
  185. OBJC_GC_DEPRECATED("it always returns NO")
  186. static OBJC_INLINE BOOL objc_collecting_enabled(void) { return NO; }
  187. OBJC_GC_DEPRECATED("it does nothing")
  188. static OBJC_INLINE void objc_set_collection_threshold(size_t threshold __unused) { }
  189. OBJC_GC_DEPRECATED("it does nothing")
  190. static OBJC_INLINE void objc_set_collection_ratio(size_t ratio __unused) { }
  191. OBJC_GC_DEPRECATED("it does nothing")
  192. static OBJC_INLINE void objc_start_collector_thread(void) { }
  193. #if __has_feature(objc_arc)
  194. extern id objc_allocate_object(Class cls, int extra) UNAVAILABLE_ATTRIBUTE;
  195. #else
  196. OBJC_EXPORT id class_createInstance(Class cls, size_t extraBytes)
  197. OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
  198. OBJC_GC_DEPRECATED("use class_createInstance instead")
  199. static OBJC_INLINE id objc_allocate_object(Class cls, int extra)
  200. { return class_createInstance(cls, (size_t)extra); }
  201. #endif
  202. OBJC_GC_DEPRECATED("it does nothing")
  203. static OBJC_INLINE void objc_registerThreadWithCollector() { }
  204. OBJC_GC_DEPRECATED("it does nothing")
  205. static OBJC_INLINE void objc_unregisterThreadWithCollector() { }
  206. OBJC_GC_DEPRECATED("it does nothing")
  207. static OBJC_INLINE void objc_assertRegisteredThreadWithCollector() { }
  208. /* defined(OBJC_NO_GC) */
  209. #endif
  210. #endif