1 /* GNU Objective C Runtime accessors functions
2 Copyright (C) 2010 Free Software Foundation, Inc.
3 Contributed by Nicola Pero
5 This file is part of GCC.
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.
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
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.
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/>. */
25 #include "objc-private/common.h"
26 #include "objc/objc.h"
28 #include <string.h> /* For memcpy */
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. */
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
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
50 #define ACCESSORS_NUMBER_OF_LOCKS 16
52 #define ACCESSORS_HASH(POINTER) ((((size_t)POINTER >> 8) ^ (size_t)POINTER) & (ACCESSORS_NUMBER_OF_LOCKS - 1))
54 static objc_mutex_t accessors_locks[ACCESSORS_NUMBER_OF_LOCKS];
56 /* This is called at startup to setup the locks. */
58 __objc_accessors_init (void)
62 for (i = 0; i < ACCESSORS_NUMBER_OF_LOCKS; i++)
63 accessors_locks[i] = objc_mutex_allocate ();
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;
77 #define COPY(X) [((_libobjcNSObject *)(X)) copyWithZone: NULL]
78 #define MUTABLE_COPY(X) [((_libobjcNSObject *)(X)) mutableCopyWithZone: NULL]
83 # define AUTORELEASE(X) (X)
85 # define RETAIN(X) (X)
89 @interface _libobjcNSObject (RetainReleaseMethods)
91 - (oneway void) release;
94 # define AUTORELEASE(X) [((_libobjcNSObject *)(X)) autorelease]
95 # define RELEASE(X) [((_libobjcNSObject *)(X)) release]
96 # define RETAIN(X) [((_libobjcNSObject *)(X)) retain]
100 /* The compiler uses this function when implementing some synthesized
101 getters for properties of type 'id'. */
103 objc_getProperty (id self, SEL __attribute__((unused)) _cmd, ptrdiff_t offset, BOOL is_atomic)
107 id *pointer_to_ivar = (id *)((char *)self + offset);
110 return AUTORELEASE (RETAIN (*pointer_to_ivar));
113 objc_mutex_t lock = accessors_locks[ACCESSORS_HASH (pointer_to_ivar)];
116 objc_mutex_lock (lock);
117 result = RETAIN (*(pointer_to_ivar));
118 objc_mutex_unlock (lock);
120 return AUTORELEASE (result);
127 /* The compiler uses this function when implementing some synthesized
128 setters for properties of type 'id'.
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. */
134 objc_setProperty (id self, SEL __attribute__((unused)) _cmd, ptrdiff_t offset, id new_value, BOOL is_atomic, BOOL should_copy)
138 id *pointer_to_ivar = (id *)((char *)self + offset);
148 if (*pointer_to_ivar == new_value)
150 retained_value = RETAIN (new_value);
153 case 2: /* mutable copy */
155 retained_value = MUTABLE_COPY (new_value);
161 retained_value = COPY (new_value);
169 old_value = *pointer_to_ivar;
171 *pointer_to_ivar = retained_value;
175 objc_mutex_t lock = accessors_locks[ACCESSORS_HASH (pointer_to_ivar)];
177 objc_mutex_lock (lock);
179 old_value = *pointer_to_ivar;
181 *pointer_to_ivar = retained_value;
182 objc_mutex_unlock (lock);
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. */
195 objc_getPropertyStruct (void *destination, const void *source, ptrdiff_t size, BOOL is_atomic, BOOL __attribute__((unused)) has_strong)
198 memcpy (destination, source, size);
201 objc_mutex_t lock = accessors_locks[ACCESSORS_HASH (source)];
203 objc_mutex_lock (lock);
204 memcpy (destination, source, size);
205 objc_mutex_unlock (lock);
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. */
214 objc_setPropertyStruct (void *destination, const void *source, ptrdiff_t size, BOOL is_atomic, BOOL __attribute__((unused)) has_strong)
217 memcpy (destination, source, size);
220 objc_mutex_t lock = accessors_locks[ACCESSORS_HASH (destination)];
222 objc_mutex_lock (lock);
223 memcpy (destination, source, size);
224 objc_mutex_unlock (lock);
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
236 objc_copyStruct (void *destination, const void *source, ptrdiff_t size, BOOL is_atomic, BOOL __attribute__((unused)) has_strong)
239 memcpy (destination, source, size);
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;
252 if (ACCESSORS_HASH (source) == ACCESSORS_HASH (destination))
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);
262 if (ACCESSORS_HASH (source) > ACCESSORS_HASH (destination))
264 first_lock = accessors_locks[ACCESSORS_HASH (source)];
265 second_lock = accessors_locks[ACCESSORS_HASH (destination)];
269 first_lock = accessors_locks[ACCESSORS_HASH (destination)];
270 second_lock = accessors_locks[ACCESSORS_HASH (source)];
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);