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 #include <objc/objc.h>
28 #include <malloc/malloc.h>
32 #include <Availability.h>
33 #include <TargetConditionals.h>
35 #include <sys/types.h>
36 #include <libkern/OSAtomic.h>
39 // Define OBJC_SILENCE_GC_DEPRECATIONS=1 to temporarily
40 // silence deprecation warnings for GC functions.
42 #if OBJC_SILENCE_GC_DEPRECATIONS
43 # define OBJC_GC_DEPRECATED(message)
44 #elif __has_extension(attribute_deprecated_with_message)
45 # define OBJC_GC_DEPRECATED(message) __attribute__((deprecated(message ". Define OBJC_SILENCE_GC_DEPRECATIONS=1 to temporarily silence this diagnostic.")))
47 # define OBJC_GC_DEPRECATED(message) __attribute__((deprecated))
52 OBJC_RATIO_COLLECTION
= (0 << 0),
53 OBJC_GENERATIONAL_COLLECTION
= (1 << 0),
54 OBJC_FULL_COLLECTION
= (2 << 0),
55 OBJC_EXHAUSTIVE_COLLECTION
= (3 << 0),
57 OBJC_COLLECT_IF_NEEDED
= (1 << 3),
58 OBJC_WAIT_UNTIL_DONE
= (1 << 4)
62 OBJC_CLEAR_RESIDENT_STACK
= (1 << 0)
69 /* Out-of-line declarations */
71 OBJC_EXPORT
void objc_collect(unsigned long options
)
72 __OSX_DEPRECATED(10.6, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
73 OBJC_EXPORT BOOL
objc_collectingEnabled(void)
74 __OSX_DEPRECATED(10.5, 10.8, "it always returns NO") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
75 OBJC_EXPORT malloc_zone_t
*objc_collectableZone(void)
76 __OSX_DEPRECATED(10.7, 10.8, "it always returns nil") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
77 OBJC_EXPORT
void objc_setCollectionThreshold(size_t threshold
)
78 __OSX_DEPRECATED(10.5, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
79 OBJC_EXPORT
void objc_setCollectionRatio(size_t ratio
)
80 __OSX_DEPRECATED(10.5, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
81 OBJC_EXPORT BOOL
objc_atomicCompareAndSwapPtr(id predicate
, id replacement
, volatile id
*objectLocation
)
82 __OSX_DEPRECATED(10.6, 10.8, "use OSAtomicCompareAndSwapPtr instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE OBJC_ARC_UNAVAILABLE
;
83 OBJC_EXPORT BOOL
objc_atomicCompareAndSwapPtrBarrier(id predicate
, id replacement
, volatile id
*objectLocation
)
84 __OSX_DEPRECATED(10.6, 10.8, "use OSAtomicCompareAndSwapPtrBarrier instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE OBJC_ARC_UNAVAILABLE
;
85 OBJC_EXPORT BOOL
objc_atomicCompareAndSwapGlobal(id predicate
, id replacement
, volatile id
*objectLocation
)
86 __OSX_DEPRECATED(10.6, 10.8, "use OSAtomicCompareAndSwapPtr instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE OBJC_ARC_UNAVAILABLE
;
87 OBJC_EXPORT BOOL
objc_atomicCompareAndSwapGlobalBarrier(id predicate
, id replacement
, volatile id
*objectLocation
)
88 __OSX_DEPRECATED(10.6, 10.8, "use OSAtomicCompareAndSwapPtrBarrier instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE OBJC_ARC_UNAVAILABLE
;
89 OBJC_EXPORT BOOL
objc_atomicCompareAndSwapInstanceVariable(id predicate
, id replacement
, volatile id
*objectLocation
)
90 __OSX_DEPRECATED(10.6, 10.8, "use OSAtomicCompareAndSwapPtr instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE OBJC_ARC_UNAVAILABLE
;
91 OBJC_EXPORT BOOL
objc_atomicCompareAndSwapInstanceVariableBarrier(id predicate
, id replacement
, volatile id
*objectLocation
)
92 __OSX_DEPRECATED(10.6, 10.8, "use OSAtomicCompareAndSwapPtrBarrier instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE OBJC_ARC_UNAVAILABLE
;
93 OBJC_EXPORT id
objc_assign_strongCast(id val
, id
*dest
)
94 __OSX_DEPRECATED(10.4, 10.8, "use a simple assignment instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
95 OBJC_EXPORT id
objc_assign_global(id val
, id
*dest
)
96 __OSX_DEPRECATED(10.4, 10.8, "use a simple assignment instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
97 OBJC_EXPORT id
objc_assign_threadlocal(id val
, id
*dest
)
98 __OSX_DEPRECATED(10.7, 10.8, "use a simple assignment instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
99 OBJC_EXPORT id
objc_assign_ivar(id value
, id dest
, ptrdiff_t offset
)
100 __OSX_DEPRECATED(10.4, 10.8, "use a simple assignment instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
101 OBJC_EXPORT
void *objc_memmove_collectable(void *dst
, const void *src
, size_t size
)
102 __OSX_DEPRECATED(10.4, 10.8, "use memmove instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
103 OBJC_EXPORT id
objc_read_weak(id
*location
)
104 __OSX_DEPRECATED(10.5, 10.8, "use a simple read instead, or convert to zeroing __weak") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
105 OBJC_EXPORT id
objc_assign_weak(id value
, id
*location
)
106 __OSX_DEPRECATED(10.5, 10.8, "use a simple assignment instead, or convert to zeroing __weak") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
107 OBJC_EXPORT
void objc_registerThreadWithCollector(void)
108 __OSX_DEPRECATED(10.6, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
109 OBJC_EXPORT
void objc_unregisterThreadWithCollector(void)
110 __OSX_DEPRECATED(10.6, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
111 OBJC_EXPORT
void objc_assertRegisteredThreadWithCollector(void)
112 __OSX_DEPRECATED(10.6, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
113 OBJC_EXPORT
void objc_clear_stack(unsigned long options
)
114 __OSX_DEPRECATED(10.5, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
115 OBJC_EXPORT BOOL
objc_is_finalized(void *ptr
)
116 __OSX_DEPRECATED(10.4, 10.8, "it always returns NO") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
117 OBJC_EXPORT
void objc_finalizeOnMainThread(Class cls
)
118 __OSX_DEPRECATED(10.5, 10.5, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
119 OBJC_EXPORT BOOL
objc_collecting_enabled(void)
120 __OSX_DEPRECATED(10.4, 10.5, "it always returns NO") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
121 OBJC_EXPORT
void objc_set_collection_threshold(size_t threshold
)
122 __OSX_DEPRECATED(10.4, 10.5, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
123 OBJC_EXPORT
void objc_set_collection_ratio(size_t ratio
)
124 __OSX_DEPRECATED(10.4, 10.5, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
125 OBJC_EXPORT
void objc_start_collector_thread(void)
126 __OSX_DEPRECATED(10.4, 10.5, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
127 OBJC_EXPORT
void objc_startCollectorThread(void)
128 __OSX_DEPRECATED(10.5, 10.7, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
129 OBJC_EXPORT id
objc_allocate_object(Class cls
, int extra
)
130 __OSX_DEPRECATED(10.4, 10.4, "use class_createInstance instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
133 /* !defined(OBJC_NO_GC) */
135 /* defined(OBJC_NO_GC) */
138 /* Inline declarations */
140 OBJC_GC_DEPRECATED("it does nothing")
141 static OBJC_INLINE
void objc_collect(unsigned long options __unused
) { }
142 OBJC_GC_DEPRECATED("it always returns NO")
143 static OBJC_INLINE BOOL
objc_collectingEnabled(void) { return NO
; }
145 OBJC_GC_DEPRECATED("it always returns nil")
146 static OBJC_INLINE malloc_zone_t
*objc_collectableZone(void) { return nil
; }
148 OBJC_GC_DEPRECATED("it does nothing")
149 static OBJC_INLINE
void objc_setCollectionThreshold(size_t threshold __unused
) { }
150 OBJC_GC_DEPRECATED("it does nothing")
151 static OBJC_INLINE
void objc_setCollectionRatio(size_t ratio __unused
) { }
152 OBJC_GC_DEPRECATED("it does nothing")
153 static OBJC_INLINE
void objc_startCollectorThread(void) { }
155 #if __has_feature(objc_arc)
157 /* Covers for GC memory operations are unavailable in ARC */
161 OBJC_GC_DEPRECATED("use OSAtomicCompareAndSwapPtr instead")
162 static OBJC_INLINE BOOL
objc_atomicCompareAndSwapPtr(id predicate
, id replacement
, volatile id
*objectLocation
)
163 { return OSAtomicCompareAndSwapPtr((void *)predicate
, (void *)replacement
, (void * volatile *)objectLocation
); }
165 OBJC_GC_DEPRECATED("use OSAtomicCompareAndSwapPtrBarrier instead")
166 static OBJC_INLINE BOOL
objc_atomicCompareAndSwapPtrBarrier(id predicate
, id replacement
, volatile id
*objectLocation
)
167 { return OSAtomicCompareAndSwapPtrBarrier((void *)predicate
, (void *)replacement
, (void * volatile *)objectLocation
); }
169 OBJC_GC_DEPRECATED("use OSAtomicCompareAndSwapPtr instead")
170 static OBJC_INLINE BOOL
objc_atomicCompareAndSwapGlobal(id predicate
, id replacement
, volatile id
*objectLocation
)
171 { return objc_atomicCompareAndSwapPtr(predicate
, replacement
, objectLocation
); }
173 OBJC_GC_DEPRECATED("use OSAtomicCompareAndSwapPtrBarrier instead")
174 static OBJC_INLINE BOOL
objc_atomicCompareAndSwapGlobalBarrier(id predicate
, id replacement
, volatile id
*objectLocation
)
175 { return objc_atomicCompareAndSwapPtrBarrier(predicate
, replacement
, objectLocation
); }
177 OBJC_GC_DEPRECATED("use OSAtomicCompareAndSwapPtr instead")
178 static OBJC_INLINE BOOL
objc_atomicCompareAndSwapInstanceVariable(id predicate
, id replacement
, volatile id
*objectLocation
)
179 { return objc_atomicCompareAndSwapPtr(predicate
, replacement
, objectLocation
); }
181 OBJC_GC_DEPRECATED("use OSAtomicCompareAndSwapPtrBarrier instead")
182 static OBJC_INLINE BOOL
objc_atomicCompareAndSwapInstanceVariableBarrier(id predicate
, id replacement
, volatile id
*objectLocation
)
183 { return objc_atomicCompareAndSwapPtrBarrier(predicate
, replacement
, objectLocation
); }
186 OBJC_GC_DEPRECATED("use a simple assignment instead")
187 static OBJC_INLINE id
objc_assign_strongCast(id val
, id
*dest
)
188 { return (*dest
= val
); }
190 OBJC_GC_DEPRECATED("use a simple assignment instead")
191 static OBJC_INLINE id
objc_assign_global(id val
, id
*dest
)
192 { return (*dest
= val
); }
194 OBJC_GC_DEPRECATED("use a simple assignment instead")
195 static OBJC_INLINE id
objc_assign_threadlocal(id val
, id
*dest
)
196 { return (*dest
= val
); }
198 OBJC_GC_DEPRECATED("use a simple assignment instead")
199 static OBJC_INLINE id
objc_assign_ivar(id val
, id dest
, ptrdiff_t offset
)
200 { return (*(id
*)((intptr_t)(char *)dest
+offset
) = val
); }
202 OBJC_GC_DEPRECATED("use a simple read instead, or convert to zeroing __weak")
203 static OBJC_INLINE id
objc_read_weak(id
*location
)
204 { return *location
; }
206 OBJC_GC_DEPRECATED("use a simple assignment instead, or convert to zeroing __weak")
207 static OBJC_INLINE id
objc_assign_weak(id value
, id
*location
)
208 { return (*location
= value
); }
213 OBJC_GC_DEPRECATED("use memmove instead")
214 static OBJC_INLINE
void *objc_memmove_collectable(void *dst
, const void *src
, size_t size
)
215 { return memmove(dst
, src
, size
); }
217 OBJC_GC_DEPRECATED("it does nothing")
218 static OBJC_INLINE
void objc_finalizeOnMainThread(Class cls __unused
) { }
219 OBJC_GC_DEPRECATED("it always returns NO")
220 static OBJC_INLINE BOOL
objc_is_finalized(void *ptr __unused
) { return NO
; }
221 OBJC_GC_DEPRECATED("it does nothing")
222 static OBJC_INLINE
void objc_clear_stack(unsigned long options __unused
) { }
223 OBJC_GC_DEPRECATED("it always returns NO")
224 static OBJC_INLINE BOOL
objc_collecting_enabled(void) { return NO
; }
225 OBJC_GC_DEPRECATED("it does nothing")
226 static OBJC_INLINE
void objc_set_collection_threshold(size_t threshold __unused
) { }
227 OBJC_GC_DEPRECATED("it does nothing")
228 static OBJC_INLINE
void objc_set_collection_ratio(size_t ratio __unused
) { }
229 OBJC_GC_DEPRECATED("it does nothing")
230 static OBJC_INLINE
void objc_start_collector_thread(void) { }
232 #if __has_feature(objc_arc)
233 extern id
objc_allocate_object(Class cls
, int extra
) UNAVAILABLE_ATTRIBUTE
;
235 OBJC_EXPORT id
class_createInstance(Class cls
, size_t extraBytes
)
236 OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
237 OBJC_GC_DEPRECATED("use class_createInstance instead")
238 static OBJC_INLINE id
objc_allocate_object(Class cls
, int extra
)
239 { return class_createInstance(cls
, (size_t)extra
); }
242 OBJC_GC_DEPRECATED("it does nothing")
243 static OBJC_INLINE
void objc_registerThreadWithCollector() { }
244 OBJC_GC_DEPRECATED("it does nothing")
245 static OBJC_INLINE
void objc_unregisterThreadWithCollector() { }
246 OBJC_GC_DEPRECATED("it does nothing")
247 static OBJC_INLINE
void objc_assertRegisteredThreadWithCollector() { }
249 /* defined(OBJC_NO_GC) */