2 * Copyright (c) 2006-2008 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 <libkern/OSAtomic.h>
29 #include "objc-private.h"
32 // stub interface declarations to make compiler happy.
34 @interface __NSCopyable
35 - (id)copyWithZone:(void *)zone;
38 @interface __NSMutableCopyable
39 - (id)mutableCopyWithZone:(void *)zone;
42 StripedMap<spinlock_t> PropertyLocks;
43 StripedMap<spinlock_t> StructLocks;
44 StripedMap<spinlock_t> CppObjectLocks;
46 #define MUTABLE_COPY 2
48 id objc_getProperty(id self, SEL _cmd, ptrdiff_t offset, BOOL atomic) {
50 return object_getClass(self);
53 // Retain release world
54 id *slot = (id*) ((char*)self + offset);
55 if (!atomic) return *slot;
57 // Atomic retain release world
58 spinlock_t& slotlock = PropertyLocks[slot];
60 id value = objc_retain(*slot);
63 // for performance, we (safely) issue the autorelease OUTSIDE of the spinlock.
64 return objc_autoreleaseReturnValue(value);
68 static inline void reallySetProperty(id self, SEL _cmd, id newValue, ptrdiff_t offset, bool atomic, bool copy, bool mutableCopy) __attribute__((always_inline));
70 static inline void reallySetProperty(id self, SEL _cmd, id newValue, ptrdiff_t offset, bool atomic, bool copy, bool mutableCopy)
73 object_setClass(self, newValue);
78 id *slot = (id*) ((char*)self + offset);
81 newValue = [newValue copyWithZone:nil];
82 } else if (mutableCopy) {
83 newValue = [newValue mutableCopyWithZone:nil];
85 if (*slot == newValue) return;
86 newValue = objc_retain(newValue);
93 spinlock_t& slotlock = PropertyLocks[slot];
100 objc_release(oldValue);
103 void objc_setProperty(id self, SEL _cmd, ptrdiff_t offset, id newValue, BOOL atomic, signed char shouldCopy)
105 bool copy = (shouldCopy && shouldCopy != MUTABLE_COPY);
106 bool mutableCopy = (shouldCopy == MUTABLE_COPY);
107 reallySetProperty(self, _cmd, newValue, offset, atomic, copy, mutableCopy);
110 void objc_setProperty_atomic(id self, SEL _cmd, id newValue, ptrdiff_t offset)
112 reallySetProperty(self, _cmd, newValue, offset, true, false, false);
115 void objc_setProperty_nonatomic(id self, SEL _cmd, id newValue, ptrdiff_t offset)
117 reallySetProperty(self, _cmd, newValue, offset, false, false, false);
121 void objc_setProperty_atomic_copy(id self, SEL _cmd, id newValue, ptrdiff_t offset)
123 reallySetProperty(self, _cmd, newValue, offset, true, true, false);
126 void objc_setProperty_nonatomic_copy(id self, SEL _cmd, id newValue, ptrdiff_t offset)
128 reallySetProperty(self, _cmd, newValue, offset, false, true, false);
132 // This entry point was designed wrong. When used as a getter, src needs to be locked so that
133 // if simultaneously used for a setter then there would be contention on src.
134 // So we need two locks - one of which will be contended.
135 void objc_copyStruct(void *dest, const void *src, ptrdiff_t size, BOOL atomic, BOOL hasStrong __unused) {
136 spinlock_t *srcLock = nil;
137 spinlock_t *dstLock = nil;
139 srcLock = &StructLocks[src];
140 dstLock = &StructLocks[dest];
141 spinlock_t::lockTwo(srcLock, dstLock);
144 memmove(dest, src, size);
147 spinlock_t::unlockTwo(srcLock, dstLock);
151 void objc_copyCppObjectAtomic(void *dest, const void *src, void (*copyHelper) (void *dest, const void *source)) {
152 spinlock_t *srcLock = &CppObjectLocks[src];
153 spinlock_t *dstLock = &CppObjectLocks[dest];
154 spinlock_t::lockTwo(srcLock, dstLock);
156 // let C++ code perform the actual copy.
157 copyHelper(dest, src);
159 spinlock_t::unlockTwo(srcLock, dstLock);