]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-accessors.mm
objc4-680.tar.gz
[apple/objc4.git] / runtime / objc-accessors.mm
1 /*
2 * Copyright (c) 2006-2008 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 #include <string.h>
25 #include <stddef.h>
26
27 #include <libkern/OSAtomic.h>
28
29 #include "objc-private.h"
30 #include "objc-auto.h"
31 #include "runtime.h"
32 #include "objc-accessors.h"
33
34 // stub interface declarations to make compiler happy.
35
36 @interface __NSCopyable
37 - (id)copyWithZone:(void *)zone;
38 @end
39
40 @interface __NSMutableCopyable
41 - (id)mutableCopyWithZone:(void *)zone;
42 @end
43
44 static StripedMap<spinlock_t> PropertyLocks;
45
46 #define MUTABLE_COPY 2
47
48 id objc_getProperty_non_gc(id self, SEL _cmd, ptrdiff_t offset, BOOL atomic) {
49 if (offset == 0) {
50 return object_getClass(self);
51 }
52
53 // Retain release world
54 id *slot = (id*) ((char*)self + offset);
55 if (!atomic) return *slot;
56
57 // Atomic retain release world
58 spinlock_t& slotlock = PropertyLocks[slot];
59 slotlock.lock();
60 id value = objc_retain(*slot);
61 slotlock.unlock();
62
63 // for performance, we (safely) issue the autorelease OUTSIDE of the spinlock.
64 return objc_autoreleaseReturnValue(value);
65 }
66
67
68 static inline void reallySetProperty(id self, SEL _cmd, id newValue, ptrdiff_t offset, bool atomic, bool copy, bool mutableCopy) __attribute__((always_inline));
69
70 static inline void reallySetProperty(id self, SEL _cmd, id newValue, ptrdiff_t offset, bool atomic, bool copy, bool mutableCopy)
71 {
72 if (offset == 0) {
73 object_setClass(self, newValue);
74 return;
75 }
76
77 id oldValue;
78 id *slot = (id*) ((char*)self + offset);
79
80 if (copy) {
81 newValue = [newValue copyWithZone:nil];
82 } else if (mutableCopy) {
83 newValue = [newValue mutableCopyWithZone:nil];
84 } else {
85 if (*slot == newValue) return;
86 newValue = objc_retain(newValue);
87 }
88
89 if (!atomic) {
90 oldValue = *slot;
91 *slot = newValue;
92 } else {
93 spinlock_t& slotlock = PropertyLocks[slot];
94 slotlock.lock();
95 oldValue = *slot;
96 *slot = newValue;
97 slotlock.unlock();
98 }
99
100 objc_release(oldValue);
101 }
102
103 void objc_setProperty_non_gc(id self, SEL _cmd, ptrdiff_t offset, id newValue, BOOL atomic, signed char shouldCopy)
104 {
105 bool copy = (shouldCopy && shouldCopy != MUTABLE_COPY);
106 bool mutableCopy = (shouldCopy == MUTABLE_COPY);
107 reallySetProperty(self, _cmd, newValue, offset, atomic, copy, mutableCopy);
108 }
109
110 void objc_setProperty_atomic(id self, SEL _cmd, id newValue, ptrdiff_t offset)
111 {
112 reallySetProperty(self, _cmd, newValue, offset, true, false, false);
113 }
114
115 void objc_setProperty_nonatomic(id self, SEL _cmd, id newValue, ptrdiff_t offset)
116 {
117 reallySetProperty(self, _cmd, newValue, offset, false, false, false);
118 }
119
120
121 void objc_setProperty_atomic_copy(id self, SEL _cmd, id newValue, ptrdiff_t offset)
122 {
123 reallySetProperty(self, _cmd, newValue, offset, true, true, false);
124 }
125
126 void objc_setProperty_nonatomic_copy(id self, SEL _cmd, id newValue, ptrdiff_t offset)
127 {
128 reallySetProperty(self, _cmd, newValue, offset, false, true, false);
129 }
130
131
132 #if SUPPORT_GC
133
134 id objc_getProperty_gc(id self, SEL _cmd, ptrdiff_t offset, BOOL atomic) {
135 return *(id*) ((char*)self + offset);
136 }
137
138 void objc_setProperty_gc(id self, SEL _cmd, ptrdiff_t offset, id newValue, BOOL atomic, signed char shouldCopy) {
139 if (shouldCopy) {
140 newValue = (shouldCopy == MUTABLE_COPY ? [newValue mutableCopyWithZone:nil] : [newValue copyWithZone:nil]);
141 }
142 objc_assign_ivar(newValue, self, offset);
143 }
144
145 // objc_getProperty and objc_setProperty are resolver functions in objc-auto.mm
146
147 #else
148
149 id
150 objc_getProperty(id self, SEL _cmd, ptrdiff_t offset, BOOL atomic)
151 {
152 return objc_getProperty_non_gc(self, _cmd, offset, atomic);
153 }
154
155 void
156 objc_setProperty(id self, SEL _cmd, ptrdiff_t offset, id newValue,
157 BOOL atomic, signed char shouldCopy)
158 {
159 objc_setProperty_non_gc(self, _cmd, offset, newValue, atomic, shouldCopy);
160 }
161
162 #endif
163
164
165 // This entry point was designed wrong. When used as a getter, src needs to be locked so that
166 // if simultaneously used for a setter then there would be contention on src.
167 // So we need two locks - one of which will be contended.
168 void objc_copyStruct(void *dest, const void *src, ptrdiff_t size, BOOL atomic, BOOL hasStrong) {
169 static StripedMap<spinlock_t> StructLocks;
170 spinlock_t *srcLock = nil;
171 spinlock_t *dstLock = nil;
172 if (atomic) {
173 srcLock = &StructLocks[src];
174 dstLock = &StructLocks[dest];
175 spinlock_t::lockTwo(srcLock, dstLock);
176 }
177 #if SUPPORT_GC
178 if (UseGC && hasStrong) {
179 auto_zone_write_barrier_memmove(gc_zone, dest, src, size);
180 } else
181 #endif
182 {
183 memmove(dest, src, size);
184 }
185 if (atomic) {
186 spinlock_t::unlockTwo(srcLock, dstLock);
187 }
188 }
189
190 void objc_copyCppObjectAtomic(void *dest, const void *src, void (*copyHelper) (void *dest, const void *source)) {
191 static StripedMap<spinlock_t> CppObjectLocks;
192 spinlock_t *srcLock = &CppObjectLocks[src];
193 spinlock_t *dstLock = &CppObjectLocks[dest];
194 spinlock_t::lockTwo(srcLock, dstLock);
195
196 // let C++ code perform the actual copy.
197 copyHelper(dest, src);
198
199 spinlock_t::unlockTwo(srcLock, dstLock);
200 }