]> git.saurik.com Git - apple/objc4.git/blob - runtime/objc-accessors.mm
objc4-706.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 "runtime.h"
31
32 // stub interface declarations to make compiler happy.
33
34 @interface __NSCopyable
35 - (id)copyWithZone:(void *)zone;
36 @end
37
38 @interface __NSMutableCopyable
39 - (id)mutableCopyWithZone:(void *)zone;
40 @end
41
42 // These locks must not be at function scope.
43 static StripedMap<spinlock_t> PropertyLocks;
44 static StripedMap<spinlock_t> StructLocks;
45 static StripedMap<spinlock_t> CppObjectLocks;
46
47 #define MUTABLE_COPY 2
48
49 id objc_getProperty(id self, SEL _cmd, ptrdiff_t offset, BOOL atomic) {
50 if (offset == 0) {
51 return object_getClass(self);
52 }
53
54 // Retain release world
55 id *slot = (id*) ((char*)self + offset);
56 if (!atomic) return *slot;
57
58 // Atomic retain release world
59 spinlock_t& slotlock = PropertyLocks[slot];
60 slotlock.lock();
61 id value = objc_retain(*slot);
62 slotlock.unlock();
63
64 // for performance, we (safely) issue the autorelease OUTSIDE of the spinlock.
65 return objc_autoreleaseReturnValue(value);
66 }
67
68
69 static inline void reallySetProperty(id self, SEL _cmd, id newValue, ptrdiff_t offset, bool atomic, bool copy, bool mutableCopy) __attribute__((always_inline));
70
71 static inline void reallySetProperty(id self, SEL _cmd, id newValue, ptrdiff_t offset, bool atomic, bool copy, bool mutableCopy)
72 {
73 if (offset == 0) {
74 object_setClass(self, newValue);
75 return;
76 }
77
78 id oldValue;
79 id *slot = (id*) ((char*)self + offset);
80
81 if (copy) {
82 newValue = [newValue copyWithZone:nil];
83 } else if (mutableCopy) {
84 newValue = [newValue mutableCopyWithZone:nil];
85 } else {
86 if (*slot == newValue) return;
87 newValue = objc_retain(newValue);
88 }
89
90 if (!atomic) {
91 oldValue = *slot;
92 *slot = newValue;
93 } else {
94 spinlock_t& slotlock = PropertyLocks[slot];
95 slotlock.lock();
96 oldValue = *slot;
97 *slot = newValue;
98 slotlock.unlock();
99 }
100
101 objc_release(oldValue);
102 }
103
104 void objc_setProperty(id self, SEL _cmd, ptrdiff_t offset, id newValue, BOOL atomic, signed char shouldCopy)
105 {
106 bool copy = (shouldCopy && shouldCopy != MUTABLE_COPY);
107 bool mutableCopy = (shouldCopy == MUTABLE_COPY);
108 reallySetProperty(self, _cmd, newValue, offset, atomic, copy, mutableCopy);
109 }
110
111 void objc_setProperty_atomic(id self, SEL _cmd, id newValue, ptrdiff_t offset)
112 {
113 reallySetProperty(self, _cmd, newValue, offset, true, false, false);
114 }
115
116 void objc_setProperty_nonatomic(id self, SEL _cmd, id newValue, ptrdiff_t offset)
117 {
118 reallySetProperty(self, _cmd, newValue, offset, false, false, false);
119 }
120
121
122 void objc_setProperty_atomic_copy(id self, SEL _cmd, id newValue, ptrdiff_t offset)
123 {
124 reallySetProperty(self, _cmd, newValue, offset, true, true, false);
125 }
126
127 void objc_setProperty_nonatomic_copy(id self, SEL _cmd, id newValue, ptrdiff_t offset)
128 {
129 reallySetProperty(self, _cmd, newValue, offset, false, true, false);
130 }
131
132
133 // This entry point was designed wrong. When used as a getter, src needs to be locked so that
134 // if simultaneously used for a setter then there would be contention on src.
135 // So we need two locks - one of which will be contended.
136 void objc_copyStruct(void *dest, const void *src, ptrdiff_t size, BOOL atomic, BOOL hasStrong __unused) {
137 spinlock_t *srcLock = nil;
138 spinlock_t *dstLock = nil;
139 if (atomic) {
140 srcLock = &StructLocks[src];
141 dstLock = &StructLocks[dest];
142 spinlock_t::lockTwo(srcLock, dstLock);
143 }
144
145 memmove(dest, src, size);
146
147 if (atomic) {
148 spinlock_t::unlockTwo(srcLock, dstLock);
149 }
150 }
151
152 void objc_copyCppObjectAtomic(void *dest, const void *src, void (*copyHelper) (void *dest, const void *source)) {
153 spinlock_t *srcLock = &CppObjectLocks[src];
154 spinlock_t *dstLock = &CppObjectLocks[dest];
155 spinlock_t::lockTwo(srcLock, dstLock);
156
157 // let C++ code perform the actual copy.
158 copyHelper(dest, src);
159
160 spinlock_t::unlockTwo(srcLock, dstLock);
161 }