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>
38 #include <sys/types.h>
39 #include <libkern/OSAtomic.h>
41 # define WINVER 0x0501 // target Windows XP and later
42 # define _WIN32_WINNT 0x0501 // target Windows XP and later
43 # define WIN32_LEAN_AND_MEAN
44 // workaround: windef.h typedefs BOOL as int
51 /* GC is unsupported on some architectures. */
53 #if TARGET_OS_EMBEDDED || TARGET_OS_IPHONE || TARGET_OS_WIN32
58 /* objc_collect() options */
61 OBJC_RATIO_COLLECTION
= (0 << 0), // run "ratio" generational collections, then a full
62 OBJC_GENERATIONAL_COLLECTION
= (1 << 0), // run fast incremental collection
63 OBJC_FULL_COLLECTION
= (2 << 0), // run full collection.
64 OBJC_EXHAUSTIVE_COLLECTION
= (3 << 0), // run full collections until memory available stops improving
66 OBJC_COLLECT_IF_NEEDED
= (1 << 3), // run collection only if needed (allocation threshold exceeded)
67 OBJC_WAIT_UNTIL_DONE
= (1 << 4), // wait (when possible) for collection to end before returning (when collector is running on dedicated thread)
70 /* objc_clear_stack() options */
72 OBJC_CLEAR_RESIDENT_STACK
= (1 << 0)
80 /* Collection utilities */
82 OBJC_EXPORT
void objc_collect(unsigned long options
)
83 __OSX_AVAILABLE_STARTING(__MAC_10_6
, __IPHONE_NA
);
84 OBJC_EXPORT BOOL
objc_collectingEnabled(void)
85 __OSX_AVAILABLE_STARTING(__MAC_10_5
, __IPHONE_NA
);
86 OBJC_EXPORT malloc_zone_t
*objc_collectableZone(void)
87 __OSX_AVAILABLE_STARTING(__MAC_10_7
, __IPHONE_NA
);
89 /* GC configuration */
91 /* Tells collector to wait until specified bytes have been allocated before trying to collect again. */
92 OBJC_EXPORT
void objc_setCollectionThreshold(size_t threshold
)
93 __OSX_AVAILABLE_STARTING(__MAC_10_5
, __IPHONE_NA
);
95 /* Tells collector to run a full collection for every ratio generational collections. */
96 OBJC_EXPORT
void objc_setCollectionRatio(size_t ratio
)
97 __OSX_AVAILABLE_STARTING(__MAC_10_5
, __IPHONE_NA
);
100 // GC-safe compare-and-swap
103 /* Atomic update, with write barrier. */
104 OBJC_EXPORT BOOL
objc_atomicCompareAndSwapPtr(id predicate
, id replacement
, volatile id
*objectLocation
)
105 __OSX_AVAILABLE_STARTING(__MAC_10_6
, __IPHONE_NA
) OBJC_ARC_UNAVAILABLE
;
106 /* "Barrier" version also includes memory barrier. */
107 OBJC_EXPORT BOOL
objc_atomicCompareAndSwapPtrBarrier(id predicate
, id replacement
, volatile id
*objectLocation
)
108 __OSX_AVAILABLE_STARTING(__MAC_10_6
, __IPHONE_NA
) OBJC_ARC_UNAVAILABLE
;
110 // atomic update of a global variable
111 OBJC_EXPORT BOOL
objc_atomicCompareAndSwapGlobal(id predicate
, id replacement
, volatile id
*objectLocation
)
112 __OSX_AVAILABLE_STARTING(__MAC_10_6
, __IPHONE_NA
) OBJC_ARC_UNAVAILABLE
;
113 OBJC_EXPORT BOOL
objc_atomicCompareAndSwapGlobalBarrier(id predicate
, id replacement
, volatile id
*objectLocation
)
114 __OSX_AVAILABLE_STARTING(__MAC_10_6
, __IPHONE_NA
) OBJC_ARC_UNAVAILABLE
;
115 // atomic update of an instance variable
116 OBJC_EXPORT BOOL
objc_atomicCompareAndSwapInstanceVariable(id predicate
, id replacement
, volatile id
*objectLocation
)
117 __OSX_AVAILABLE_STARTING(__MAC_10_6
, __IPHONE_NA
) OBJC_ARC_UNAVAILABLE
;
118 OBJC_EXPORT BOOL
objc_atomicCompareAndSwapInstanceVariableBarrier(id predicate
, id replacement
, volatile id
*objectLocation
)
119 __OSX_AVAILABLE_STARTING(__MAC_10_6
, __IPHONE_NA
) OBJC_ARC_UNAVAILABLE
;
123 // Read and write barriers
126 OBJC_EXPORT id
objc_assign_strongCast(id val
, id
*dest
)
127 __OSX_AVAILABLE_STARTING(__MAC_10_4
, __IPHONE_NA
);
128 OBJC_EXPORT id
objc_assign_global(id val
, id
*dest
)
129 __OSX_AVAILABLE_STARTING(__MAC_10_4
, __IPHONE_NA
);
130 OBJC_EXPORT id
objc_assign_threadlocal(id val
, id
*dest
)
131 __OSX_AVAILABLE_STARTING(__MAC_10_7
, __IPHONE_NA
);
132 OBJC_EXPORT id
objc_assign_ivar(id value
, id dest
, ptrdiff_t offset
)
133 __OSX_AVAILABLE_STARTING(__MAC_10_4
, __IPHONE_NA
);
134 OBJC_EXPORT
void *objc_memmove_collectable(void *dst
, const void *src
, size_t size
)
135 __OSX_AVAILABLE_STARTING(__MAC_10_4
, __IPHONE_NA
);
137 OBJC_EXPORT id
objc_read_weak(id
*location
)
138 __OSX_AVAILABLE_STARTING(__MAC_10_5
, __IPHONE_NA
);
139 OBJC_EXPORT id
objc_assign_weak(id value
, id
*location
)
140 __OSX_AVAILABLE_STARTING(__MAC_10_5
, __IPHONE_NA
);
147 /* Register the calling thread with the garbage collector. */
148 OBJC_EXPORT
void objc_registerThreadWithCollector(void)
149 __OSX_AVAILABLE_STARTING(__MAC_10_6
, __IPHONE_NA
);
151 /* Unregisters the calling thread with the garbage collector.
152 Unregistration also happens automatically at thread exit. */
153 OBJC_EXPORT
void objc_unregisterThreadWithCollector(void)
154 __OSX_AVAILABLE_STARTING(__MAC_10_6
, __IPHONE_NA
);
156 /* To be called from code which must only execute on a registered thread. */
157 /* If the calling thread is unregistered then an error message is emitted and the thread is implicitly registered. */
158 OBJC_EXPORT
void objc_assertRegisteredThreadWithCollector(void)
159 __OSX_AVAILABLE_STARTING(__MAC_10_6
, __IPHONE_NA
);
161 /* Erases any stale references in unused parts of the stack. */
162 OBJC_EXPORT
void objc_clear_stack(unsigned long options
)
163 __OSX_AVAILABLE_STARTING(__MAC_10_5
, __IPHONE_NA
);
170 /* Returns true if object has been scheduled for finalization. Can be used to avoid operations that may lead to resurrection, which are fatal. */
171 OBJC_EXPORT BOOL
objc_is_finalized(void *ptr
)
172 __OSX_AVAILABLE_STARTING(__MAC_10_4
, __IPHONE_NA
);
174 // Deprcated. Tells runtime to issue finalize calls on the main thread only.
175 OBJC_EXPORT
void objc_finalizeOnMainThread(Class cls
)
176 __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_5
,__MAC_10_5
, __IPHONE_NA
,__IPHONE_NA
);
183 /* Deprecated. Use objc_collectingEnabled() instead. */
184 OBJC_EXPORT BOOL
objc_collecting_enabled(void)
185 __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_4
,__MAC_10_5
, __IPHONE_NA
,__IPHONE_NA
);
186 /* Deprecated. Use objc_setCollectionThreshold() instead. */
187 OBJC_EXPORT
void objc_set_collection_threshold(size_t threshold
)
188 __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_4
,__MAC_10_5
, __IPHONE_NA
,__IPHONE_NA
);
189 /* Deprecated. Use objc_setCollectionRatio() instead. */
190 OBJC_EXPORT
void objc_set_collection_ratio(size_t ratio
)
191 __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_4
,__MAC_10_5
, __IPHONE_NA
,__IPHONE_NA
);
192 /* Deprecated. Use objc_startCollectorThread() instead. */
193 OBJC_EXPORT
void objc_start_collector_thread(void)
194 __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_4
,__MAC_10_5
, __IPHONE_NA
,__IPHONE_NA
);
195 /* Deprecated. No replacement. Formerly told the collector to run using a dedicated background thread. */
196 OBJC_EXPORT
void objc_startCollectorThread(void)
197 __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_5
,__MAC_10_7
, __IPHONE_NA
,__IPHONE_NA
);
200 /* Deprecated. Use class_createInstance() instead. */
201 OBJC_EXPORT id
objc_allocate_object(Class cls
, int extra
)
202 __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_10_4
,__MAC_10_4
, __IPHONE_NA
,__IPHONE_NA
);
205 /* !defined(OBJC_NO_GC) */
207 /* defined(OBJC_NO_GC) */
210 /* Non-GC declarations */
212 static OBJC_INLINE
void objc_collect(unsigned long options __unused
) { }
213 static OBJC_INLINE BOOL
objc_collectingEnabled(void) { return NO
; }
214 static OBJC_INLINE
void objc_setCollectionThreshold(size_t threshold __unused
) { }
215 static OBJC_INLINE
void objc_setCollectionRatio(size_t ratio __unused
) { }
216 static OBJC_INLINE
void objc_startCollectorThread(void) { }
218 #if __has_feature(objc_arc)
220 /* Covers for GC memory operations are unavailable in ARC */
225 static OBJC_INLINE BOOL
objc_atomicCompareAndSwapPtr(id predicate
, id replacement
, volatile id
*objectLocation
)
226 { void *original
= InterlockedCompareExchangePointer((void * volatile *)objectLocation
, (void *)replacement
, (void *)predicate
); return (original
== predicate
); }
228 static OBJC_INLINE BOOL
objc_atomicCompareAndSwapPtrBarrier(id predicate
, id replacement
, volatile id
*objectLocation
)
229 { void *original
= InterlockedCompareExchangePointer((void * volatile *)objectLocation
, (void *)replacement
, (void *)predicate
); return (original
== predicate
); }
231 static OBJC_INLINE BOOL
objc_atomicCompareAndSwapPtr(id predicate
, id replacement
, volatile id
*objectLocation
)
232 { return OSAtomicCompareAndSwapPtr((void *)predicate
, (void *)replacement
, (void * volatile *)objectLocation
); }
234 static OBJC_INLINE BOOL
objc_atomicCompareAndSwapPtrBarrier(id predicate
, id replacement
, volatile id
*objectLocation
)
235 { return OSAtomicCompareAndSwapPtrBarrier((void *)predicate
, (void *)replacement
, (void * volatile *)objectLocation
); }
238 static OBJC_INLINE BOOL
objc_atomicCompareAndSwapGlobal(id predicate
, id replacement
, volatile id
*objectLocation
)
239 { return objc_atomicCompareAndSwapPtr(predicate
, replacement
, objectLocation
); }
241 static OBJC_INLINE BOOL
objc_atomicCompareAndSwapGlobalBarrier(id predicate
, id replacement
, volatile id
*objectLocation
)
242 { return objc_atomicCompareAndSwapPtrBarrier(predicate
, replacement
, objectLocation
); }
244 static OBJC_INLINE BOOL
objc_atomicCompareAndSwapInstanceVariable(id predicate
, id replacement
, volatile id
*objectLocation
)
245 { return objc_atomicCompareAndSwapPtr(predicate
, replacement
, objectLocation
); }
247 static OBJC_INLINE BOOL
objc_atomicCompareAndSwapInstanceVariableBarrier(id predicate
, id replacement
, volatile id
*objectLocation
)
248 { return objc_atomicCompareAndSwapPtrBarrier(predicate
, replacement
, objectLocation
); }
251 static OBJC_INLINE id
objc_assign_strongCast(id val
, id
*dest
)
252 { return (*dest
= val
); }
254 static OBJC_INLINE id
objc_assign_global(id val
, id
*dest
)
255 { return (*dest
= val
); }
257 static OBJC_INLINE id
objc_assign_ivar(id val
, id dest
, ptrdiff_t offset
)
258 { return (*(id
*)((char *)dest
+offset
) = val
); }
260 static OBJC_INLINE id
objc_read_weak(id
*location
)
261 { return *location
; }
263 static OBJC_INLINE id
objc_assign_weak(id value
, id
*location
)
264 { return (*location
= value
); }
269 static OBJC_INLINE
void *objc_memmove_collectable(void *dst
, const void *src
, size_t size
)
270 { return memmove(dst
, src
, size
); }
272 static OBJC_INLINE
void objc_finalizeOnMainThread(Class cls __unused
) { }
273 static OBJC_INLINE BOOL
objc_is_finalized(void *ptr __unused
) { return NO
; }
274 static OBJC_INLINE
void objc_clear_stack(unsigned long options __unused
) { }
276 static OBJC_INLINE BOOL
objc_collecting_enabled(void) { return NO
; }
277 static OBJC_INLINE
void objc_set_collection_threshold(size_t threshold __unused
) { }
278 static OBJC_INLINE
void objc_set_collection_ratio(size_t ratio __unused
) { }
279 static OBJC_INLINE
void objc_start_collector_thread(void) { }
281 #if __has_feature(objc_arc)
282 extern id
objc_allocate_object(Class cls
, int extra
) UNAVAILABLE_ATTRIBUTE
;
284 OBJC_EXPORT id
class_createInstance(Class cls
, size_t extraBytes
)
285 __OSX_AVAILABLE_STARTING(__MAC_10_0
, __IPHONE_2_0
);
286 static OBJC_INLINE id
objc_allocate_object(Class cls
, int extra
)
287 { return class_createInstance(cls
, extra
); }
290 static OBJC_INLINE
void objc_registerThreadWithCollector() { }
291 static OBJC_INLINE
void objc_unregisterThreadWithCollector() { }
292 static OBJC_INLINE
void objc_assertRegisteredThreadWithCollector() { }
294 /* defined(OBJC_NO_GC) */
298 #if TARGET_OS_EMBEDDED
300 OBJC_GENERATIONAL
= (1 << 0)
302 static OBJC_INLINE
void objc_collect_if_needed(unsigned long options
) __attribute__((deprecated
));
303 static OBJC_INLINE
void objc_collect_if_needed(unsigned long options __unused
) { }