2 * Copyright (c) 2004-2007 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
27 #pragma GCC system_header
29 #include <objc/objc.h>
30 #include <malloc/malloc.h>
34 #include <Availability.h>
35 #include <TargetConditionals.h>
37 #include <sys/types.h>
38 #include <libkern/OSAtomic.h>
41 // Define OBJC_SILENCE_GC_DEPRECATIONS=1 to temporarily
42 // silence deprecation warnings for GC functions.
44 #if OBJC_SILENCE_GC_DEPRECATIONS
45 # define OBJC_GC_DEPRECATED(message)
46 #elif __has_extension(attribute_deprecated_with_message)
47 # define OBJC_GC_DEPRECATED(message) __attribute__((deprecated(message ". Define OBJC_SILENCE_GC_DEPRECATIONS=1 to temporarily silence this diagnostic.")))
49 # define OBJC_GC_DEPRECATED(message) __attribute__((deprecated))
54 OBJC_RATIO_COLLECTION
= (0 << 0),
55 OBJC_GENERATIONAL_COLLECTION
= (1 << 0),
56 OBJC_FULL_COLLECTION
= (2 << 0),
57 OBJC_EXHAUSTIVE_COLLECTION
= (3 << 0),
59 OBJC_COLLECT_IF_NEEDED
= (1 << 3),
60 OBJC_WAIT_UNTIL_DONE
= (1 << 4),
64 OBJC_CLEAR_RESIDENT_STACK
= (1 << 0)
71 /* Out-of-line declarations */
73 OBJC_EXPORT
void objc_collect(unsigned long options
)
74 __OSX_DEPRECATED(10.6, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE
;
75 OBJC_EXPORT BOOL
objc_collectingEnabled(void)
76 __OSX_DEPRECATED(10.5, 10.8, "it always returns NO") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE
;
77 OBJC_EXPORT malloc_zone_t
*objc_collectableZone(void)
78 __OSX_DEPRECATED(10.7, 10.8, "it always returns nil") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE
;
79 OBJC_EXPORT
void objc_setCollectionThreshold(size_t threshold
)
80 __OSX_DEPRECATED(10.5, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE
;
81 OBJC_EXPORT
void objc_setCollectionRatio(size_t ratio
)
82 __OSX_DEPRECATED(10.5, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE
;
83 OBJC_EXPORT BOOL
objc_atomicCompareAndSwapPtr(id predicate
, id replacement
, volatile id
*objectLocation
)
84 __OSX_DEPRECATED(10.6, 10.8, "use OSAtomicCompareAndSwapPtr instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE OBJC_ARC_UNAVAILABLE
;
85 OBJC_EXPORT BOOL
objc_atomicCompareAndSwapPtrBarrier(id predicate
, id replacement
, volatile id
*objectLocation
)
86 __OSX_DEPRECATED(10.6, 10.8, "use OSAtomicCompareAndSwapPtrBarrier instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE OBJC_ARC_UNAVAILABLE
;
87 OBJC_EXPORT BOOL
objc_atomicCompareAndSwapGlobal(id predicate
, id replacement
, volatile id
*objectLocation
)
88 __OSX_DEPRECATED(10.6, 10.8, "use OSAtomicCompareAndSwapPtr instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE OBJC_ARC_UNAVAILABLE
;
89 OBJC_EXPORT BOOL
objc_atomicCompareAndSwapGlobalBarrier(id predicate
, id replacement
, volatile id
*objectLocation
)
90 __OSX_DEPRECATED(10.6, 10.8, "use OSAtomicCompareAndSwapPtrBarrier instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE OBJC_ARC_UNAVAILABLE
;
91 OBJC_EXPORT BOOL
objc_atomicCompareAndSwapInstanceVariable(id predicate
, id replacement
, volatile id
*objectLocation
)
92 __OSX_DEPRECATED(10.6, 10.8, "use OSAtomicCompareAndSwapPtr instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE OBJC_ARC_UNAVAILABLE
;
93 OBJC_EXPORT BOOL
objc_atomicCompareAndSwapInstanceVariableBarrier(id predicate
, id replacement
, volatile id
*objectLocation
)
94 __OSX_DEPRECATED(10.6, 10.8, "use OSAtomicCompareAndSwapPtrBarrier instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE OBJC_ARC_UNAVAILABLE
;
95 OBJC_EXPORT id
objc_assign_strongCast(id val
, id
*dest
)
96 __OSX_DEPRECATED(10.4, 10.8, "use a simple assignment instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE
;
97 OBJC_EXPORT id
objc_assign_global(id val
, id
*dest
)
98 __OSX_DEPRECATED(10.4, 10.8, "use a simple assignment instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE
;
99 OBJC_EXPORT id
objc_assign_threadlocal(id val
, id
*dest
)
100 __OSX_DEPRECATED(10.7, 10.8, "use a simple assignment instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE
;
101 OBJC_EXPORT id
objc_assign_ivar(id value
, id dest
, ptrdiff_t offset
)
102 __OSX_DEPRECATED(10.4, 10.8, "use a simple assignment instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE
;
103 OBJC_EXPORT
void *objc_memmove_collectable(void *dst
, const void *src
, size_t size
)
104 __OSX_DEPRECATED(10.4, 10.8, "use memmove instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE
;
105 OBJC_EXPORT id
objc_read_weak(id
*location
)
106 __OSX_DEPRECATED(10.5, 10.8, "use a simple read instead, or convert to zeroing __weak") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE
;
107 OBJC_EXPORT id
objc_assign_weak(id value
, id
*location
)
108 __OSX_DEPRECATED(10.5, 10.8, "use a simple assignment instead, or convert to zeroing __weak") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE
;
109 OBJC_EXPORT
void objc_registerThreadWithCollector(void)
110 __OSX_DEPRECATED(10.6, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE
;
111 OBJC_EXPORT
void objc_unregisterThreadWithCollector(void)
112 __OSX_DEPRECATED(10.6, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE
;
113 OBJC_EXPORT
void objc_assertRegisteredThreadWithCollector(void)
114 __OSX_DEPRECATED(10.6, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE
;
115 OBJC_EXPORT
void objc_clear_stack(unsigned long options
)
116 __OSX_DEPRECATED(10.5, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE
;
117 OBJC_EXPORT BOOL
objc_is_finalized(void *ptr
)
118 __OSX_DEPRECATED(10.4, 10.8, "it always returns NO") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE
;
119 OBJC_EXPORT
void objc_finalizeOnMainThread(Class cls
)
120 __OSX_DEPRECATED(10.5, 10.5, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE
;
121 OBJC_EXPORT BOOL
objc_collecting_enabled(void)
122 __OSX_DEPRECATED(10.4, 10.5, "it always returns NO") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE
;
123 OBJC_EXPORT
void objc_set_collection_threshold(size_t threshold
)
124 __OSX_DEPRECATED(10.4, 10.5, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE
;
125 OBJC_EXPORT
void objc_set_collection_ratio(size_t ratio
)
126 __OSX_DEPRECATED(10.4, 10.5, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE
;
127 OBJC_EXPORT
void objc_start_collector_thread(void)
128 __OSX_DEPRECATED(10.4, 10.5, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE
;
129 OBJC_EXPORT
void objc_startCollectorThread(void)
130 __OSX_DEPRECATED(10.5, 10.7, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE
;
131 OBJC_EXPORT id
objc_allocate_object(Class cls
, int extra
)
132 __OSX_DEPRECATED(10.4, 10.4, "use class_createInstance instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE
;
135 /* !defined(OBJC_NO_GC) */
137 /* defined(OBJC_NO_GC) */
140 /* Inline declarations */
142 OBJC_GC_DEPRECATED("it does nothing")
143 static OBJC_INLINE
void objc_collect(unsigned long options __unused
) { }
144 OBJC_GC_DEPRECATED("it always returns NO")
145 static OBJC_INLINE BOOL
objc_collectingEnabled(void) { return NO
; }
147 OBJC_GC_DEPRECATED("it always returns nil")
148 static OBJC_INLINE malloc_zone_t
*objc_collectableZone(void) { return nil
; }
150 OBJC_GC_DEPRECATED("it does nothing")
151 static OBJC_INLINE
void objc_setCollectionThreshold(size_t threshold __unused
) { }
152 OBJC_GC_DEPRECATED("it does nothing")
153 static OBJC_INLINE
void objc_setCollectionRatio(size_t ratio __unused
) { }
154 OBJC_GC_DEPRECATED("it does nothing")
155 static OBJC_INLINE
void objc_startCollectorThread(void) { }
157 #if __has_feature(objc_arc)
159 /* Covers for GC memory operations are unavailable in ARC */
163 OBJC_GC_DEPRECATED("use OSAtomicCompareAndSwapPtr instead")
164 static OBJC_INLINE BOOL
objc_atomicCompareAndSwapPtr(id predicate
, id replacement
, volatile id
*objectLocation
)
165 { return OSAtomicCompareAndSwapPtr((void *)predicate
, (void *)replacement
, (void * volatile *)objectLocation
); }
167 OBJC_GC_DEPRECATED("use OSAtomicCompareAndSwapPtrBarrier instead")
168 static OBJC_INLINE BOOL
objc_atomicCompareAndSwapPtrBarrier(id predicate
, id replacement
, volatile id
*objectLocation
)
169 { return OSAtomicCompareAndSwapPtrBarrier((void *)predicate
, (void *)replacement
, (void * volatile *)objectLocation
); }
171 OBJC_GC_DEPRECATED("use OSAtomicCompareAndSwapPtr instead")
172 static OBJC_INLINE BOOL
objc_atomicCompareAndSwapGlobal(id predicate
, id replacement
, volatile id
*objectLocation
)
173 { return objc_atomicCompareAndSwapPtr(predicate
, replacement
, objectLocation
); }
175 OBJC_GC_DEPRECATED("use OSAtomicCompareAndSwapPtrBarrier instead")
176 static OBJC_INLINE BOOL
objc_atomicCompareAndSwapGlobalBarrier(id predicate
, id replacement
, volatile id
*objectLocation
)
177 { return objc_atomicCompareAndSwapPtrBarrier(predicate
, replacement
, objectLocation
); }
179 OBJC_GC_DEPRECATED("use OSAtomicCompareAndSwapPtr instead")
180 static OBJC_INLINE BOOL
objc_atomicCompareAndSwapInstanceVariable(id predicate
, id replacement
, volatile id
*objectLocation
)
181 { return objc_atomicCompareAndSwapPtr(predicate
, replacement
, objectLocation
); }
183 OBJC_GC_DEPRECATED("use OSAtomicCompareAndSwapPtrBarrier instead")
184 static OBJC_INLINE BOOL
objc_atomicCompareAndSwapInstanceVariableBarrier(id predicate
, id replacement
, volatile id
*objectLocation
)
185 { return objc_atomicCompareAndSwapPtrBarrier(predicate
, replacement
, objectLocation
); }
188 OBJC_GC_DEPRECATED("use a simple assignment instead")
189 static OBJC_INLINE id
objc_assign_strongCast(id val
, id
*dest
)
190 { return (*dest
= val
); }
192 OBJC_GC_DEPRECATED("use a simple assignment instead")
193 static OBJC_INLINE id
objc_assign_global(id val
, id
*dest
)
194 { return (*dest
= val
); }
196 OBJC_GC_DEPRECATED("use a simple assignment instead")
197 static OBJC_INLINE id
objc_assign_threadlocal(id val
, id
*dest
)
198 { return (*dest
= val
); }
200 OBJC_GC_DEPRECATED("use a simple assignment instead")
201 static OBJC_INLINE id
objc_assign_ivar(id val
, id dest
, ptrdiff_t offset
)
202 { return (*(id
*)((char *)dest
+offset
) = val
); }
204 OBJC_GC_DEPRECATED("use a simple read instead, or convert to zeroing __weak")
205 static OBJC_INLINE id
objc_read_weak(id
*location
)
206 { return *location
; }
208 OBJC_GC_DEPRECATED("use a simple assignment instead, or convert to zeroing __weak")
209 static OBJC_INLINE id
objc_assign_weak(id value
, id
*location
)
210 { return (*location
= value
); }
215 OBJC_GC_DEPRECATED("use memmove instead")
216 static OBJC_INLINE
void *objc_memmove_collectable(void *dst
, const void *src
, size_t size
)
217 { return memmove(dst
, src
, size
); }
219 OBJC_GC_DEPRECATED("it does nothing")
220 static OBJC_INLINE
void objc_finalizeOnMainThread(Class cls __unused
) { }
221 OBJC_GC_DEPRECATED("it always returns NO")
222 static OBJC_INLINE BOOL
objc_is_finalized(void *ptr __unused
) { return NO
; }
223 OBJC_GC_DEPRECATED("it does nothing")
224 static OBJC_INLINE
void objc_clear_stack(unsigned long options __unused
) { }
225 OBJC_GC_DEPRECATED("it always returns NO")
226 static OBJC_INLINE BOOL
objc_collecting_enabled(void) { return NO
; }
227 OBJC_GC_DEPRECATED("it does nothing")
228 static OBJC_INLINE
void objc_set_collection_threshold(size_t threshold __unused
) { }
229 OBJC_GC_DEPRECATED("it does nothing")
230 static OBJC_INLINE
void objc_set_collection_ratio(size_t ratio __unused
) { }
231 OBJC_GC_DEPRECATED("it does nothing")
232 static OBJC_INLINE
void objc_start_collector_thread(void) { }
234 #if __has_feature(objc_arc)
235 extern id
objc_allocate_object(Class cls
, int extra
) UNAVAILABLE_ATTRIBUTE
;
237 OBJC_EXPORT id
class_createInstance(Class cls
, size_t extraBytes
)
238 OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0);
239 OBJC_GC_DEPRECATED("use class_createInstance instead")
240 static OBJC_INLINE id
objc_allocate_object(Class cls
, int extra
)
241 { return class_createInstance(cls
, extra
); }
244 OBJC_GC_DEPRECATED("it does nothing")
245 static OBJC_INLINE
void objc_registerThreadWithCollector() { }
246 OBJC_GC_DEPRECATED("it does nothing")
247 static OBJC_INLINE
void objc_unregisterThreadWithCollector() { }
248 OBJC_GC_DEPRECATED("it does nothing")
249 static OBJC_INLINE
void objc_assertRegisteredThreadWithCollector() { }
251 /* defined(OBJC_NO_GC) */