]>
Commit | Line | Data |
---|---|---|
682e805a NP |
1 | /* GNU Objective C Runtime accessors functions |
2 | Copyright (C) 2010 Free Software Foundation, Inc. | |
3 | Contributed by Nicola Pero | |
4 | ||
5 | This file is part of GCC. | |
6 | ||
7 | GCC is free software; you can redistribute it and/or modify it under the | |
8 | terms of the GNU General Public License as published by the Free Software | |
9 | Foundation; either version 3, or (at your option) any later version. | |
10 | ||
11 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | |
13 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | |
14 | details. | |
15 | ||
16 | Under Section 7 of GPL version 3, you are granted additional | |
17 | permissions described in the GCC Runtime Library Exception, version | |
18 | 3.1, as published by the Free Software Foundation. | |
19 | ||
20 | You should have received a copy of the GNU General Public License and | |
21 | a copy of the GCC Runtime Library Exception along with this program; | |
22 | see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | <http://www.gnu.org/licenses/>. */ | |
24 | ||
25 | #include "objc-private/common.h" | |
26 | #include "objc/objc.h" | |
27 | #include "objc/thr.h" | |
28 | #include <string.h> /* For memcpy */ | |
29 | ||
30 | /* This file contains functions that the compiler uses when | |
31 | synthesizing accessors (getters/setters) for properties. The | |
32 | functions are part of the ABI, but are meant to be used by the | |
33 | compiler and not by users; for this reason, they are not declared | |
34 | in public header files. The compiler automatically generates | |
35 | declarations for these functions. */ | |
36 | ||
37 | /* Properties can be "atomic", which requires protecting them from | |
38 | concurrency issues using a lock. Unfortunately, we can't have a | |
39 | lock for each property, so we'll go with a small pool of locks. | |
40 | Any time a property is accessed in an "atomic" way, we pick a | |
41 | random lock from the pool (random, but always the same one for the | |
42 | same property of the same object) and use it to protect access to | |
43 | the property. | |
44 | ||
45 | The size of the pool is currently 16. A bigger pool can help | |
46 | reduce contention, ie, reduce the chances that two threads, | |
47 | operating on unrelated properties, will have to wait for each other | |
48 | because the properties use the same lock. 16 seems big enough at | |
49 | the moment. */ | |
50 | #define ACCESSORS_NUMBER_OF_LOCKS 16 | |
51 | ||
52 | #define ACCESSORS_HASH(POINTER) ((((size_t)POINTER >> 8) ^ (size_t)POINTER) & (ACCESSORS_NUMBER_OF_LOCKS - 1)) | |
53 | ||
54 | static objc_mutex_t accessors_locks[ACCESSORS_NUMBER_OF_LOCKS]; | |
55 | ||
56 | /* This is called at startup to setup the locks. */ | |
57 | void | |
58 | __objc_accessors_init (void) | |
59 | { | |
60 | int i; | |
61 | ||
62 | for (i = 0; i < ACCESSORS_NUMBER_OF_LOCKS; i++) | |
63 | accessors_locks[i] = objc_mutex_allocate (); | |
64 | } | |
65 | ||
66 | /* The property accessors automatically call various methods from the | |
67 | Foundation library (eg, GNUstep-base). These methods are not | |
68 | implemented here, but we need to declare them so we can compile the | |
69 | runtime. The Foundation library will need to provide | |
70 | implementations of these methods (most likely in the root class, | |
71 | eg, NSObject) as the accessors only work with objects of classes | |
72 | that implement these methods. */ | |
73 | @interface _libobjcNSObject | |
74 | - (id) copyWithZone: (void *)zone; | |
75 | - (id) mutableCopyWithZone: (void *)zone; | |
76 | @end | |
77 | #define COPY(X) [((_libobjcNSObject *)(X)) copyWithZone: NULL] | |
78 | #define MUTABLE_COPY(X) [((_libobjcNSObject *)(X)) mutableCopyWithZone: NULL] | |
79 | ||
80 | ||
81 | #if OBJC_WITH_GC | |
82 | ||
83 | # define AUTORELEASE(X) (X) | |
84 | # define RELEASE(X) | |
85 | # define RETAIN(X) (X) | |
86 | ||
87 | #else | |
88 | ||
89 | @interface _libobjcNSObject (RetainReleaseMethods) | |
90 | - (id) autorelease; | |
91 | - (oneway void) release; | |
92 | - (id) retain; | |
93 | @end | |
94 | # define AUTORELEASE(X) [((_libobjcNSObject *)(X)) autorelease] | |
95 | # define RELEASE(X) [((_libobjcNSObject *)(X)) release] | |
96 | # define RETAIN(X) [((_libobjcNSObject *)(X)) retain] | |
97 | ||
98 | #endif | |
99 | ||
100 | /* The compiler uses this function when implementing some synthesized | |
101 | getters for properties of type 'id'. */ | |
102 | id | |
103 | objc_getProperty (id self, SEL __attribute__((unused)) _cmd, ptrdiff_t offset, BOOL is_atomic) | |
104 | { | |
105 | if (self != nil) | |
106 | { | |
107 | id *pointer_to_ivar = (id *)((char *)self + offset); | |
108 | ||
109 | if (is_atomic == NO) | |
110 | return AUTORELEASE (RETAIN (*pointer_to_ivar)); | |
111 | else | |
112 | { | |
113 | objc_mutex_t lock = accessors_locks[ACCESSORS_HASH (pointer_to_ivar)]; | |
114 | id result; | |
115 | ||
116 | objc_mutex_lock (lock); | |
117 | result = RETAIN (*(pointer_to_ivar)); | |
118 | objc_mutex_unlock (lock); | |
119 | ||
120 | return AUTORELEASE (result); | |
121 | } | |
122 | } | |
123 | ||
124 | return nil; | |
125 | } | |
126 | ||
127 | /* The compiler uses this function when implementing some synthesized | |
128 | setters for properties of type 'id'. | |
129 | ||
130 | PS: Note how 'should_copy' is declared 'BOOL' but then actually | |
131 | takes values from 0 to 2. This hack was introduced by Apple; we | |
132 | do the same for compatibility reasons. */ | |
133 | void | |
134 | objc_setProperty (id self, SEL __attribute__((unused)) _cmd, ptrdiff_t offset, id new_value, BOOL is_atomic, BOOL should_copy) | |
135 | { | |
136 | if (self != nil) | |
137 | { | |
138 | id *pointer_to_ivar = (id *)((char *)self + offset); | |
139 | id retained_value; | |
140 | #if !OBJC_WITH_GC | |
141 | id old_value; | |
142 | #endif | |
143 | ||
144 | switch (should_copy) | |
145 | { | |
146 | case 0: /* retain */ | |
147 | { | |
148 | if (*pointer_to_ivar == new_value) | |
149 | return; | |
150 | retained_value = RETAIN (new_value); | |
151 | break; | |
152 | } | |
153 | case 2: /* mutable copy */ | |
154 | { | |
155 | retained_value = MUTABLE_COPY (new_value); | |
156 | break; | |
157 | } | |
158 | case 1: /* copy */ | |
159 | default: | |
160 | { | |
161 | retained_value = COPY (new_value); | |
162 | break; | |
163 | } | |
164 | } | |
165 | ||
166 | if (is_atomic == NO) | |
167 | { | |
168 | #if !OBJC_WITH_GC | |
169 | old_value = *pointer_to_ivar; | |
170 | #endif | |
171 | *pointer_to_ivar = retained_value; | |
172 | } | |
173 | else | |
174 | { | |
175 | objc_mutex_t lock = accessors_locks[ACCESSORS_HASH (pointer_to_ivar)]; | |
176 | ||
177 | objc_mutex_lock (lock); | |
178 | #if !OBJC_WITH_GC | |
179 | old_value = *pointer_to_ivar; | |
180 | #endif | |
181 | *pointer_to_ivar = retained_value; | |
182 | objc_mutex_unlock (lock); | |
183 | } | |
184 | #if !OBJC_WITH_GC | |
185 | RELEASE (old_value); | |
186 | #endif | |
187 | } | |
188 | } | |
189 | ||
190 | /* The compiler uses this function when implementing some synthesized | |
191 | getters for properties of arbitrary C types. The data is just | |
192 | copied. Compatibility Note: this function does not exist in the | |
193 | Apple/NeXT runtime. */ | |
194 | void | |
195 | objc_getPropertyStruct (void *destination, const void *source, ptrdiff_t size, BOOL is_atomic, BOOL __attribute__((unused)) has_strong) | |
196 | { | |
197 | if (is_atomic == NO) | |
198 | memcpy (destination, source, size); | |
199 | else | |
200 | { | |
201 | objc_mutex_t lock = accessors_locks[ACCESSORS_HASH (source)]; | |
202 | ||
203 | objc_mutex_lock (lock); | |
204 | memcpy (destination, source, size); | |
205 | objc_mutex_unlock (lock); | |
206 | } | |
207 | } | |
208 | ||
209 | /* The compiler uses this function when implementing some synthesized | |
210 | setters for properties of arbitrary C types. The data is just | |
211 | copied. Compatibility Note: this function does not exist in the | |
212 | Apple/NeXT runtime. */ | |
213 | void | |
214 | objc_setPropertyStruct (void *destination, const void *source, ptrdiff_t size, BOOL is_atomic, BOOL __attribute__((unused)) has_strong) | |
215 | { | |
216 | if (is_atomic == NO) | |
217 | memcpy (destination, source, size); | |
218 | else | |
219 | { | |
220 | objc_mutex_t lock = accessors_locks[ACCESSORS_HASH (destination)]; | |
221 | ||
222 | objc_mutex_lock (lock); | |
223 | memcpy (destination, source, size); | |
224 | objc_mutex_unlock (lock); | |
225 | } | |
226 | } | |
227 | ||
228 | /* This is the function that the Apple/NeXT runtime has instead of | |
229 | objc_getPropertyStruct and objc_setPropertyStruct. We include it | |
230 | for API compatibility (just for people who may have used | |
231 | objc_copyStruct on the NeXT runtime thinking it was a public API); | |
232 | the compiler never generates calls to it with the GNU runtime. | |
233 | This function is clumsy because it requires two locks instead of | |
234 | one. */ | |
235 | void | |
236 | objc_copyStruct (void *destination, const void *source, ptrdiff_t size, BOOL is_atomic, BOOL __attribute__((unused)) has_strong) | |
237 | { | |
238 | if (is_atomic == NO) | |
239 | memcpy (destination, source, size); | |
240 | else | |
241 | { | |
242 | /* We don't know which one is the property, so we have to lock | |
243 | both. One of them is most likely a temporary buffer in the | |
244 | local stack and we really wouldn't want to lock it (our | |
245 | objc_getPropertyStruct and objc_setPropertyStruct functions | |
246 | don't lock it). Note that if we're locking more than one | |
247 | accessor lock at once, we need to always lock them in the | |
248 | same order to avoid deadlocks. */ | |
249 | objc_mutex_t first_lock; | |
250 | objc_mutex_t second_lock; | |
251 | ||
252 | if (ACCESSORS_HASH (source) == ACCESSORS_HASH (destination)) | |
253 | { | |
254 | /* A lucky collision. */ | |
255 | first_lock = accessors_locks[ACCESSORS_HASH (source)]; | |
256 | objc_mutex_lock (first_lock); | |
257 | memcpy (destination, source, size); | |
258 | objc_mutex_unlock (first_lock); | |
259 | return; | |
260 | } | |
261 | ||
262 | if (ACCESSORS_HASH (source) > ACCESSORS_HASH (destination)) | |
263 | { | |
264 | first_lock = accessors_locks[ACCESSORS_HASH (source)]; | |
265 | second_lock = accessors_locks[ACCESSORS_HASH (destination)]; | |
266 | } | |
267 | else | |
268 | { | |
269 | first_lock = accessors_locks[ACCESSORS_HASH (destination)]; | |
270 | second_lock = accessors_locks[ACCESSORS_HASH (source)]; | |
271 | } | |
272 | ||
273 | objc_mutex_lock (first_lock); | |
274 | objc_mutex_lock (second_lock); | |
275 | memcpy (destination, source, size); | |
276 | objc_mutex_unlock (second_lock); | |
277 | objc_mutex_unlock (first_lock); | |
278 | } | |
279 | } |