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)
66 #if !defined(OBJC_NO_GC) || \
67 (OBJC_DECLARE_SYMBOLS && !defined(OBJC_NO_GC_API))
70 /* Out-of-line declarations */
72 OBJC_EXPORT
void objc_collect(unsigned long options
)
73 __OSX_DEPRECATED(10.6, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
74 OBJC_EXPORT BOOL
objc_collectingEnabled(void)
75 __OSX_DEPRECATED(10.5, 10.8, "it always returns NO") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
76 OBJC_EXPORT malloc_zone_t
*objc_collectableZone(void)
77 __OSX_DEPRECATED(10.7, 10.8, "it always returns nil") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
78 OBJC_EXPORT
void objc_setCollectionThreshold(size_t threshold
)
79 __OSX_DEPRECATED(10.5, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
80 OBJC_EXPORT
void objc_setCollectionRatio(size_t ratio
)
81 __OSX_DEPRECATED(10.5, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
82 OBJC_EXPORT BOOL
objc_atomicCompareAndSwapPtr(id predicate
, id replacement
, volatile id
*objectLocation
)
83 __OSX_DEPRECATED(10.6, 10.8, "use OSAtomicCompareAndSwapPtr instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE OBJC_ARC_UNAVAILABLE
;
84 OBJC_EXPORT BOOL
objc_atomicCompareAndSwapPtrBarrier(id predicate
, id replacement
, volatile id
*objectLocation
)
85 __OSX_DEPRECATED(10.6, 10.8, "use OSAtomicCompareAndSwapPtrBarrier instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE OBJC_ARC_UNAVAILABLE
;
86 OBJC_EXPORT BOOL
objc_atomicCompareAndSwapGlobal(id predicate
, id replacement
, volatile id
*objectLocation
)
87 __OSX_DEPRECATED(10.6, 10.8, "use OSAtomicCompareAndSwapPtr instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE OBJC_ARC_UNAVAILABLE
;
88 OBJC_EXPORT BOOL
objc_atomicCompareAndSwapGlobalBarrier(id predicate
, id replacement
, volatile id
*objectLocation
)
89 __OSX_DEPRECATED(10.6, 10.8, "use OSAtomicCompareAndSwapPtrBarrier instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE OBJC_ARC_UNAVAILABLE
;
90 OBJC_EXPORT BOOL
objc_atomicCompareAndSwapInstanceVariable(id predicate
, id replacement
, volatile id
*objectLocation
)
91 __OSX_DEPRECATED(10.6, 10.8, "use OSAtomicCompareAndSwapPtr instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE OBJC_ARC_UNAVAILABLE
;
92 OBJC_EXPORT BOOL
objc_atomicCompareAndSwapInstanceVariableBarrier(id predicate
, id replacement
, volatile id
*objectLocation
)
93 __OSX_DEPRECATED(10.6, 10.8, "use OSAtomicCompareAndSwapPtrBarrier instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE OBJC_ARC_UNAVAILABLE
;
94 OBJC_EXPORT id
objc_assign_strongCast(id val
, id
*dest
)
95 __OSX_DEPRECATED(10.4, 10.8, "use a simple assignment instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
96 OBJC_EXPORT id
objc_assign_global(id val
, id
*dest
)
97 __OSX_DEPRECATED(10.4, 10.8, "use a simple assignment instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
98 OBJC_EXPORT id
objc_assign_threadlocal(id val
, id
*dest
)
99 __OSX_DEPRECATED(10.7, 10.8, "use a simple assignment instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
100 OBJC_EXPORT id
objc_assign_ivar(id value
, id dest
, ptrdiff_t offset
)
101 __OSX_DEPRECATED(10.4, 10.8, "use a simple assignment instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
102 OBJC_EXPORT
void *objc_memmove_collectable(void *dst
, const void *src
, size_t size
)
103 __OSX_DEPRECATED(10.4, 10.8, "use memmove instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
104 OBJC_EXPORT id
objc_read_weak(id
*location
)
105 __OSX_DEPRECATED(10.5, 10.8, "use a simple read instead, or convert to zeroing __weak") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
106 OBJC_EXPORT id
objc_assign_weak(id value
, id
*location
)
107 __OSX_DEPRECATED(10.5, 10.8, "use a simple assignment instead, or convert to zeroing __weak") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
108 OBJC_EXPORT
void objc_registerThreadWithCollector(void)
109 __OSX_DEPRECATED(10.6, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
110 OBJC_EXPORT
void objc_unregisterThreadWithCollector(void)
111 __OSX_DEPRECATED(10.6, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
112 OBJC_EXPORT
void objc_assertRegisteredThreadWithCollector(void)
113 __OSX_DEPRECATED(10.6, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
114 OBJC_EXPORT
void objc_clear_stack(unsigned long options
)
115 __OSX_DEPRECATED(10.5, 10.8, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
116 OBJC_EXPORT BOOL
objc_is_finalized(void *ptr
)
117 __OSX_DEPRECATED(10.4, 10.8, "it always returns NO") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
118 OBJC_EXPORT
void objc_finalizeOnMainThread(Class cls
)
119 __OSX_DEPRECATED(10.5, 10.5, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
120 OBJC_EXPORT BOOL
objc_collecting_enabled(void)
121 __OSX_DEPRECATED(10.4, 10.5, "it always returns NO") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
122 OBJC_EXPORT
void objc_set_collection_threshold(size_t threshold
)
123 __OSX_DEPRECATED(10.4, 10.5, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
124 OBJC_EXPORT
void objc_set_collection_ratio(size_t ratio
)
125 __OSX_DEPRECATED(10.4, 10.5, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
126 OBJC_EXPORT
void objc_start_collector_thread(void)
127 __OSX_DEPRECATED(10.4, 10.5, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
128 OBJC_EXPORT
void objc_startCollectorThread(void)
129 __OSX_DEPRECATED(10.5, 10.7, "it does nothing") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
130 OBJC_EXPORT id
objc_allocate_object(Class cls
, int extra
)
131 __OSX_DEPRECATED(10.4, 10.4, "use class_createInstance instead") __IOS_UNAVAILABLE __TVOS_UNAVAILABLE __WATCHOS_UNAVAILABLE __BRIDGEOS_UNAVAILABLE
;
134 /* !defined(OBJC_NO_GC) */
136 /* defined(OBJC_NO_GC) */
139 /* Inline declarations */
141 OBJC_GC_DEPRECATED("it does nothing")
142 static OBJC_INLINE
void objc_collect(unsigned long options __unused
) { }
143 OBJC_GC_DEPRECATED("it always returns NO")
144 static OBJC_INLINE BOOL
objc_collectingEnabled(void) { return NO
; }
146 OBJC_GC_DEPRECATED("it always returns nil")
147 static OBJC_INLINE malloc_zone_t
*objc_collectableZone(void) { return nil
; }
149 OBJC_GC_DEPRECATED("it does nothing")
150 static OBJC_INLINE
void objc_setCollectionThreshold(size_t threshold __unused
) { }
151 OBJC_GC_DEPRECATED("it does nothing")
152 static OBJC_INLINE
void objc_setCollectionRatio(size_t ratio __unused
) { }
153 OBJC_GC_DEPRECATED("it does nothing")
154 static OBJC_INLINE
void objc_startCollectorThread(void) { }
156 #if __has_feature(objc_arc)
158 /* Covers for GC memory operations are unavailable in ARC */
162 OBJC_GC_DEPRECATED("use OSAtomicCompareAndSwapPtr instead")
163 static OBJC_INLINE BOOL
objc_atomicCompareAndSwapPtr(id predicate
, id replacement
, volatile id
*objectLocation
)
164 { return OSAtomicCompareAndSwapPtr((void *)predicate
, (void *)replacement
, (void * volatile *)objectLocation
); }
166 OBJC_GC_DEPRECATED("use OSAtomicCompareAndSwapPtrBarrier instead")
167 static OBJC_INLINE BOOL
objc_atomicCompareAndSwapPtrBarrier(id predicate
, id replacement
, volatile id
*objectLocation
)
168 { return OSAtomicCompareAndSwapPtrBarrier((void *)predicate
, (void *)replacement
, (void * volatile *)objectLocation
); }
170 OBJC_GC_DEPRECATED("use OSAtomicCompareAndSwapPtr instead")
171 static OBJC_INLINE BOOL
objc_atomicCompareAndSwapGlobal(id predicate
, id replacement
, volatile id
*objectLocation
)
172 { return objc_atomicCompareAndSwapPtr(predicate
, replacement
, objectLocation
); }
174 OBJC_GC_DEPRECATED("use OSAtomicCompareAndSwapPtrBarrier instead")
175 static OBJC_INLINE BOOL
objc_atomicCompareAndSwapGlobalBarrier(id predicate
, id replacement
, volatile id
*objectLocation
)
176 { return objc_atomicCompareAndSwapPtrBarrier(predicate
, replacement
, objectLocation
); }
178 OBJC_GC_DEPRECATED("use OSAtomicCompareAndSwapPtr instead")
179 static OBJC_INLINE BOOL
objc_atomicCompareAndSwapInstanceVariable(id predicate
, id replacement
, volatile id
*objectLocation
)
180 { return objc_atomicCompareAndSwapPtr(predicate
, replacement
, objectLocation
); }
182 OBJC_GC_DEPRECATED("use OSAtomicCompareAndSwapPtrBarrier instead")
183 static OBJC_INLINE BOOL
objc_atomicCompareAndSwapInstanceVariableBarrier(id predicate
, id replacement
, volatile id
*objectLocation
)
184 { return objc_atomicCompareAndSwapPtrBarrier(predicate
, replacement
, objectLocation
); }
187 OBJC_GC_DEPRECATED("use a simple assignment instead")
188 static OBJC_INLINE id
objc_assign_strongCast(id val
, id
*dest
)
189 { return (*dest
= val
); }
191 OBJC_GC_DEPRECATED("use a simple assignment instead")
192 static OBJC_INLINE id
objc_assign_global(id val
, id
*dest
)
193 { return (*dest
= val
); }
195 OBJC_GC_DEPRECATED("use a simple assignment instead")
196 static OBJC_INLINE id
objc_assign_threadlocal(id val
, id
*dest
)
197 { return (*dest
= val
); }
199 OBJC_GC_DEPRECATED("use a simple assignment instead")
200 static OBJC_INLINE id
objc_assign_ivar(id val
, id dest
, ptrdiff_t offset
)
201 { return (*(id
*)((intptr_t)(char *)dest
+offset
) = val
); }
203 OBJC_GC_DEPRECATED("use a simple read instead, or convert to zeroing __weak")
204 static OBJC_INLINE id
objc_read_weak(id
*location
)
205 { return *location
; }
207 OBJC_GC_DEPRECATED("use a simple assignment instead, or convert to zeroing __weak")
208 static OBJC_INLINE id
objc_assign_weak(id value
, id
*location
)
209 { return (*location
= value
); }
214 OBJC_GC_DEPRECATED("use memmove instead")
215 static OBJC_INLINE
void *objc_memmove_collectable(void *dst
, const void *src
, size_t size
)
216 { return memmove(dst
, src
, size
); }
218 OBJC_GC_DEPRECATED("it does nothing")
219 static OBJC_INLINE
void objc_finalizeOnMainThread(Class cls __unused
) { }
220 OBJC_GC_DEPRECATED("it always returns NO")
221 static OBJC_INLINE BOOL
objc_is_finalized(void *ptr __unused
) { return NO
; }
222 OBJC_GC_DEPRECATED("it does nothing")
223 static OBJC_INLINE
void objc_clear_stack(unsigned long options __unused
) { }
224 OBJC_GC_DEPRECATED("it always returns NO")
225 static OBJC_INLINE BOOL
objc_collecting_enabled(void) { return NO
; }
226 OBJC_GC_DEPRECATED("it does nothing")
227 static OBJC_INLINE
void objc_set_collection_threshold(size_t threshold __unused
) { }
228 OBJC_GC_DEPRECATED("it does nothing")
229 static OBJC_INLINE
void objc_set_collection_ratio(size_t ratio __unused
) { }
230 OBJC_GC_DEPRECATED("it does nothing")
231 static OBJC_INLINE
void objc_start_collector_thread(void) { }
233 #if __has_feature(objc_arc)
234 extern id
objc_allocate_object(Class cls
, int extra
) UNAVAILABLE_ATTRIBUTE
;
236 OBJC_EXPORT id
class_createInstance(Class cls
, size_t extraBytes
)
237 OBJC_AVAILABLE(10.0, 2.0, 9.0, 1.0, 2.0);
238 OBJC_GC_DEPRECATED("use class_createInstance instead")
239 static OBJC_INLINE id
objc_allocate_object(Class cls
, int extra
)
240 { return class_createInstance(cls
, (size_t)extra
); }
243 OBJC_GC_DEPRECATED("it does nothing")
244 static OBJC_INLINE
void objc_registerThreadWithCollector() { }
245 OBJC_GC_DEPRECATED("it does nothing")
246 static OBJC_INLINE
void objc_unregisterThreadWithCollector() { }
247 OBJC_GC_DEPRECATED("it does nothing")
248 static OBJC_INLINE
void objc_assertRegisteredThreadWithCollector() { }
250 /* defined(OBJC_NO_GC) */