accessors.m 9.2 KB

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