gc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /* Basic data types for Objective C.
  2. Copyright (C) 1998-2022 Free Software Foundation, Inc.
  3. Contributed by Ovidiu Predescu.
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. Under Section 7 of GPL version 3, you are granted additional
  14. permissions described in the GCC Runtime Library Exception, version
  15. 3.1, as published by the Free Software Foundation.
  16. You should have received a copy of the GNU General Public License and
  17. a copy of the GCC Runtime Library Exception along with this program;
  18. see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. <http://www.gnu.org/licenses/>. */
  20. #include "objc-private/common.h"
  21. #include "objc/objc.h"
  22. #if OBJC_WITH_GC
  23. #include "tconfig.h"
  24. #include <assert.h>
  25. #include <ctype.h> /* For isdigit. */
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include "objc/runtime.h"
  29. #include "objc-private/module-abi-8.h"
  30. #include <gc/gc.h>
  31. #include <limits.h>
  32. /* gc_typed.h uses the following but doesn't declare them */
  33. typedef GC_word word;
  34. typedef GC_signed_word signed_word;
  35. #define BITS_PER_WORD (CHAR_BIT * sizeof (word))
  36. #include <gc/gc_typed.h>
  37. /* The following functions set up in `mask` the corresponding pointers.
  38. The offset is incremented with the size of the type. */
  39. #define ROUND(V, A) \
  40. ({ typeof (V) __v = (V); typeof (A) __a = (A); \
  41. __a * ((__v+__a - 1)/__a); })
  42. #define SET_BIT_FOR_OFFSET(mask, offset) \
  43. GC_set_bit (mask, offset / sizeof (void *))
  44. /* Some prototypes */
  45. static void
  46. __objc_gc_setup_struct (GC_bitmap mask, const char *type, int offset);
  47. static void
  48. __objc_gc_setup_union (GC_bitmap mask, const char *type, int offset);
  49. static void
  50. __objc_gc_setup_array (GC_bitmap mask, const char *type, int offset)
  51. {
  52. int i, len = atoi (type + 1);
  53. while (isdigit (*++type))
  54. /* do nothing */; /* skip the size of the array */
  55. switch (*type) {
  56. case _C_ARY_B:
  57. for (i = 0; i < len; i++)
  58. __objc_gc_setup_array (mask, type, offset);
  59. break;
  60. case _C_STRUCT_B:
  61. for (i = 0; i < len; i++)
  62. __objc_gc_setup_struct (mask, type, offset);
  63. break;
  64. case _C_UNION_B:
  65. for (i = 0; i < len; i++)
  66. __objc_gc_setup_union (mask, type, offset);
  67. break;
  68. default:
  69. break;
  70. }
  71. }
  72. static void
  73. __objc_gc_setup_struct (GC_bitmap mask, const char *type, int offset)
  74. {
  75. struct objc_struct_layout layout;
  76. unsigned int position;
  77. const char *mtype;
  78. objc_layout_structure (type, &layout);
  79. while (objc_layout_structure_next_member (&layout))
  80. {
  81. BOOL gc_invisible = NO;
  82. objc_layout_structure_get_info (&layout, &position, NULL, &mtype);
  83. /* Skip the variable name */
  84. if (*mtype == '"')
  85. {
  86. for (mtype++; *mtype++ != '"';)
  87. /* do nothing */;
  88. }
  89. if (*mtype == _C_GCINVISIBLE)
  90. {
  91. gc_invisible = YES;
  92. mtype++;
  93. }
  94. /* Add to position the offset of this structure */
  95. position += offset;
  96. switch (*mtype) {
  97. case _C_ID:
  98. case _C_CLASS:
  99. case _C_SEL:
  100. case _C_PTR:
  101. case _C_CHARPTR:
  102. case _C_ATOM:
  103. if (! gc_invisible)
  104. SET_BIT_FOR_OFFSET (mask, position);
  105. break;
  106. case _C_ARY_B:
  107. __objc_gc_setup_array (mask, mtype, position);
  108. break;
  109. case _C_STRUCT_B:
  110. __objc_gc_setup_struct (mask, mtype, position);
  111. break;
  112. case _C_UNION_B:
  113. __objc_gc_setup_union (mask, mtype, position);
  114. break;
  115. default:
  116. break;
  117. }
  118. }
  119. }
  120. static void
  121. __objc_gc_setup_union (GC_bitmap mask, const char *type, int offset)
  122. {
  123. /* Sub-optimal, quick implementation: assume the union is made of
  124. pointers, set up the mask accordingly. */
  125. int i, size, align;
  126. /* Skip the variable name */
  127. if (*type == '"')
  128. {
  129. for (type++; *type++ != '"';)
  130. /* do nothing */;
  131. }
  132. size = objc_sizeof_type (type);
  133. align = objc_alignof_type (type);
  134. offset = ROUND (offset, align);
  135. for (i = 0; i < size; i += sizeof (void *))
  136. {
  137. SET_BIT_FOR_OFFSET (mask, offset);
  138. offset += sizeof (void *);
  139. }
  140. }
  141. /* Iterates over the types in the structure that represents the class
  142. encoding and sets the bits in mask according to each ivar type. */
  143. static void
  144. __objc_gc_type_description_from_type (GC_bitmap mask, const char *type)
  145. {
  146. struct objc_struct_layout layout;
  147. unsigned int offset, align;
  148. const char *ivar_type;
  149. objc_layout_structure (type, &layout);
  150. while (objc_layout_structure_next_member (&layout))
  151. {
  152. BOOL gc_invisible = NO;
  153. objc_layout_structure_get_info (&layout, &offset, &align, &ivar_type);
  154. /* Skip the variable name */
  155. if (*ivar_type == '"')
  156. {
  157. for (ivar_type++; *ivar_type++ != '"';)
  158. /* do nothing */;
  159. }
  160. if (*ivar_type == _C_GCINVISIBLE)
  161. {
  162. gc_invisible = YES;
  163. ivar_type++;
  164. }
  165. switch (*ivar_type) {
  166. case _C_ID:
  167. case _C_CLASS:
  168. case _C_SEL:
  169. case _C_PTR:
  170. case _C_CHARPTR:
  171. if (! gc_invisible)
  172. SET_BIT_FOR_OFFSET (mask, offset);
  173. break;
  174. case _C_ARY_B:
  175. __objc_gc_setup_array (mask, ivar_type, offset);
  176. break;
  177. case _C_STRUCT_B:
  178. __objc_gc_setup_struct (mask, ivar_type, offset);
  179. break;
  180. case _C_UNION_B:
  181. __objc_gc_setup_union (mask, ivar_type, offset);
  182. break;
  183. default:
  184. break;
  185. }
  186. }
  187. }
  188. /* Computes in *type the full type encoding of this class including
  189. its super classes. '*size' gives the total number of bytes allocated
  190. into *type, '*current' the number of bytes used so far by the
  191. encoding. */
  192. static void
  193. __objc_class_structure_encoding (Class class, char **type, int *size,
  194. int *current)
  195. {
  196. int i, ivar_count;
  197. struct objc_ivar_list *ivars;
  198. if (! class)
  199. {
  200. strcat (*type, "{");
  201. (*current)++;
  202. return;
  203. }
  204. /* Add the type encodings of the super classes */
  205. __objc_class_structure_encoding (class->super_class, type, size, current);
  206. ivars = class->ivars;
  207. if (! ivars)
  208. return;
  209. ivar_count = ivars->ivar_count;
  210. for (i = 0; i < ivar_count; i++)
  211. {
  212. struct objc_ivar *ivar = &(ivars->ivar_list[i]);
  213. const char *ivar_type = ivar->ivar_type;
  214. int len = strlen (ivar_type);
  215. if (*current + len + 1 >= *size)
  216. {
  217. /* Increase the size of the encoding string so that it
  218. contains this ivar's type. */
  219. *size = ROUND (*current + len + 1, 10);
  220. *type = objc_realloc (*type, *size);
  221. }
  222. strcat (*type + *current, ivar_type);
  223. *current += len;
  224. }
  225. }
  226. /* Allocates the memory that will hold the type description for class
  227. and calls the __objc_class_structure_encoding that generates this
  228. value. */
  229. void
  230. __objc_generate_gc_type_description (Class class)
  231. {
  232. GC_bitmap mask;
  233. int bits_no, size;
  234. int type_size = 10, current;
  235. char *class_structure_type;
  236. if (! CLS_ISCLASS (class))
  237. return;
  238. /* We have to create a mask in which each bit counts for a pointer member.
  239. We take into consideration all the non-pointer instance variables and we
  240. round them up to the alignment. */
  241. /* The number of bits in the mask is the size of an instance in bytes divided
  242. by the size of a pointer. */
  243. bits_no = (ROUND (class_getInstanceSize (class), sizeof (void *))
  244. / sizeof (void *));
  245. size = ROUND (bits_no, BITS_PER_WORD) / BITS_PER_WORD;
  246. mask = objc_atomic_malloc (size * sizeof (int));
  247. memset (mask, 0, size * sizeof (int));
  248. class_structure_type = objc_atomic_malloc (type_size);
  249. *class_structure_type = current = 0;
  250. __objc_class_structure_encoding (class, &class_structure_type,
  251. &type_size, &current);
  252. if (current + 1 == type_size)
  253. class_structure_type = objc_realloc (class_structure_type, ++type_size);
  254. strcat (class_structure_type + current, "}");
  255. #ifdef DEBUG
  256. printf ("type description for '%s' is %s\n", class->name, class_structure_type);
  257. #endif
  258. __objc_gc_type_description_from_type (mask, class_structure_type);
  259. objc_free (class_structure_type);
  260. #ifdef DEBUG
  261. printf (" mask for '%s', type '%s' (bits %d, mask size %d) is:",
  262. class_structure_type, class->name, bits_no, size);
  263. {
  264. int i;
  265. for (i = 0; i < size; i++)
  266. printf (" %lx", mask[i]);
  267. }
  268. puts ("");
  269. #endif
  270. class->gc_object_type = (void *) GC_make_descriptor (mask, bits_no);
  271. }
  272. /* Returns YES if type denotes a pointer type, NO otherwise */
  273. static inline BOOL
  274. __objc_ivar_pointer (const char *type)
  275. {
  276. type = objc_skip_type_qualifiers (type);
  277. return (*type == _C_ID
  278. || *type == _C_CLASS
  279. || *type == _C_SEL
  280. || *type == _C_PTR
  281. || *type == _C_CHARPTR
  282. || *type == _C_ATOM);
  283. }
  284. /* Mark the instance variable whose name is given by ivarname as a
  285. weak pointer (a pointer hidden to the garbage collector) if
  286. gc_invisible is true. If gc_invisible is false it unmarks the
  287. instance variable and makes it a normal pointer, visible to the
  288. garbage collector.
  289. This operation only makes sense on instance variables that are
  290. pointers. */
  291. void
  292. class_ivar_set_gcinvisible (Class class, const char *ivarname,
  293. BOOL gc_invisible)
  294. {
  295. int i, ivar_count;
  296. struct objc_ivar_list *ivars;
  297. if (! class || ! ivarname)
  298. return;
  299. ivars = class->ivars;
  300. if (! ivars)
  301. return;
  302. ivar_count = ivars->ivar_count;
  303. for (i = 0; i < ivar_count; i++)
  304. {
  305. struct objc_ivar *ivar = &(ivars->ivar_list[i]);
  306. const char *type;
  307. if (! ivar->ivar_name || strcmp (ivar->ivar_name, ivarname))
  308. continue;
  309. assert (ivar->ivar_type);
  310. type = ivar->ivar_type;
  311. /* Skip the variable name */
  312. if (*type == '"')
  313. {
  314. for (type++; *type++ != '"';)
  315. /* do nothing */;
  316. }
  317. if (*type == _C_GCINVISIBLE)
  318. {
  319. char *new_type;
  320. size_t len;
  321. if (gc_invisible || ! __objc_ivar_pointer (type))
  322. return; /* The type of the variable already matches the
  323. requested gc_invisible type */
  324. /* The variable is gc_invisible so we make it gc visible. */
  325. new_type = objc_atomic_malloc (strlen(ivar->ivar_type));
  326. len = (type - ivar->ivar_type);
  327. memcpy (new_type, ivar->ivar_type, len);
  328. new_type[len] = 0;
  329. strcat (new_type, type + 1);
  330. ivar->ivar_type = new_type;
  331. }
  332. else
  333. {
  334. char *new_type;
  335. size_t len;
  336. if (! gc_invisible || ! __objc_ivar_pointer (type))
  337. return; /* The type of the variable already matches the
  338. requested gc_invisible type */
  339. /* The variable is gc visible so we make it gc_invisible. */
  340. new_type = objc_malloc (strlen(ivar->ivar_type) + 2);
  341. /* Copy the variable name. */
  342. len = (type - ivar->ivar_type);
  343. memcpy (new_type, ivar->ivar_type, len);
  344. /* Add '!'. */
  345. new_type[len++] = _C_GCINVISIBLE;
  346. /* Copy the original types. */
  347. strcpy (new_type + len, type);
  348. ivar->ivar_type = new_type;
  349. }
  350. __objc_generate_gc_type_description (class);
  351. return;
  352. }
  353. /* Search the instance variable in the superclasses */
  354. class_ivar_set_gcinvisible (class->super_class, ivarname, gc_invisible);
  355. }
  356. #else /* !OBJC_WITH_GC */
  357. void
  358. __objc_generate_gc_type_description (Class class __attribute__ ((__unused__)))
  359. {
  360. }
  361. void class_ivar_set_gcinvisible (Class class __attribute__ ((__unused__)),
  362. const char *ivarname __attribute__ ((__unused__)),
  363. BOOL gc_invisible __attribute__ ((__unused__)))
  364. {
  365. }
  366. #endif /* OBJC_WITH_GC */