Mark omp thread functions as parallelized
[official-gcc.git] / libobjc / gc.c
blob32fadc3afcdf390fad117ebe40a9329bacec1ca3
1 /* Basic data types for Objective C.
2 Copyright (C) 1998-2015 Free Software Foundation, Inc.
3 Contributed by Ovidiu Predescu.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #include "objc-private/common.h"
27 #include "objc/objc.h"
29 #if OBJC_WITH_GC
31 #include "tconfig.h"
32 #include <assert.h>
33 #include <ctype.h> /* For isdigit. */
34 #include <string.h>
35 #include <stdlib.h>
36 #include "objc/runtime.h"
37 #include "objc-private/module-abi-8.h"
39 #include <gc.h>
40 #include <limits.h>
42 /* gc_typed.h uses the following but doesn't declare them */
43 typedef GC_word word;
44 typedef GC_signed_word signed_word;
45 #define BITS_PER_WORD (CHAR_BIT * sizeof (word))
47 #include <gc_typed.h>
49 /* The following functions set up in `mask` the corresponding pointers.
50 The offset is incremented with the size of the type. */
52 #define ROUND(V, A) \
53 ({ typeof (V) __v = (V); typeof (A) __a = (A); \
54 __a * ((__v+__a - 1)/__a); })
56 #define SET_BIT_FOR_OFFSET(mask, offset) \
57 GC_set_bit (mask, offset / sizeof (void *))
59 /* Some prototypes */
60 static void
61 __objc_gc_setup_struct (GC_bitmap mask, const char *type, int offset);
62 static void
63 __objc_gc_setup_union (GC_bitmap mask, const char *type, int offset);
66 static void
67 __objc_gc_setup_array (GC_bitmap mask, const char *type, int offset)
69 int i, len = atoi (type + 1);
71 while (isdigit (*++type))
72 /* do nothing */; /* skip the size of the array */
74 switch (*type) {
75 case _C_ARY_B:
76 for (i = 0; i < len; i++)
77 __objc_gc_setup_array (mask, type, offset);
78 break;
80 case _C_STRUCT_B:
81 for (i = 0; i < len; i++)
82 __objc_gc_setup_struct (mask, type, offset);
83 break;
85 case _C_UNION_B:
86 for (i = 0; i < len; i++)
87 __objc_gc_setup_union (mask, type, offset);
88 break;
90 default:
91 break;
95 static void
96 __objc_gc_setup_struct (GC_bitmap mask, const char *type, int offset)
98 struct objc_struct_layout layout;
99 unsigned int position;
100 const char *mtype;
102 objc_layout_structure (type, &layout);
104 while (objc_layout_structure_next_member (&layout))
106 BOOL gc_invisible = NO;
108 objc_layout_structure_get_info (&layout, &position, NULL, &mtype);
110 /* Skip the variable name */
111 if (*mtype == '"')
113 for (mtype++; *mtype++ != '"';)
114 /* do nothing */;
117 if (*mtype == _C_GCINVISIBLE)
119 gc_invisible = YES;
120 mtype++;
123 /* Add to position the offset of this structure */
124 position += offset;
126 switch (*mtype) {
127 case _C_ID:
128 case _C_CLASS:
129 case _C_SEL:
130 case _C_PTR:
131 case _C_CHARPTR:
132 case _C_ATOM:
133 if (! gc_invisible)
134 SET_BIT_FOR_OFFSET (mask, position);
135 break;
137 case _C_ARY_B:
138 __objc_gc_setup_array (mask, mtype, position);
139 break;
141 case _C_STRUCT_B:
142 __objc_gc_setup_struct (mask, mtype, position);
143 break;
145 case _C_UNION_B:
146 __objc_gc_setup_union (mask, mtype, position);
147 break;
149 default:
150 break;
155 static void
156 __objc_gc_setup_union (GC_bitmap mask, const char *type, int offset)
158 /* Sub-optimal, quick implementation: assume the union is made of
159 pointers, set up the mask accordingly. */
161 int i, size, align;
163 /* Skip the variable name */
164 if (*type == '"')
166 for (type++; *type++ != '"';)
167 /* do nothing */;
170 size = objc_sizeof_type (type);
171 align = objc_alignof_type (type);
173 offset = ROUND (offset, align);
174 for (i = 0; i < size; i += sizeof (void *))
176 SET_BIT_FOR_OFFSET (mask, offset);
177 offset += sizeof (void *);
182 /* Iterates over the types in the structure that represents the class
183 encoding and sets the bits in mask according to each ivar type. */
184 static void
185 __objc_gc_type_description_from_type (GC_bitmap mask, const char *type)
187 struct objc_struct_layout layout;
188 unsigned int offset, align;
189 const char *ivar_type;
191 objc_layout_structure (type, &layout);
193 while (objc_layout_structure_next_member (&layout))
195 BOOL gc_invisible = NO;
197 objc_layout_structure_get_info (&layout, &offset, &align, &ivar_type);
199 /* Skip the variable name */
200 if (*ivar_type == '"')
202 for (ivar_type++; *ivar_type++ != '"';)
203 /* do nothing */;
206 if (*ivar_type == _C_GCINVISIBLE)
208 gc_invisible = YES;
209 ivar_type++;
212 switch (*ivar_type) {
213 case _C_ID:
214 case _C_CLASS:
215 case _C_SEL:
216 case _C_PTR:
217 case _C_CHARPTR:
218 if (! gc_invisible)
219 SET_BIT_FOR_OFFSET (mask, offset);
220 break;
222 case _C_ARY_B:
223 __objc_gc_setup_array (mask, ivar_type, offset);
224 break;
226 case _C_STRUCT_B:
227 __objc_gc_setup_struct (mask, ivar_type, offset);
228 break;
230 case _C_UNION_B:
231 __objc_gc_setup_union (mask, ivar_type, offset);
232 break;
234 default:
235 break;
240 /* Computes in *type the full type encoding of this class including
241 its super classes. '*size' gives the total number of bytes allocated
242 into *type, '*current' the number of bytes used so far by the
243 encoding. */
244 static void
245 __objc_class_structure_encoding (Class class, char **type, int *size,
246 int *current)
248 int i, ivar_count;
249 struct objc_ivar_list *ivars;
251 if (! class)
253 strcat (*type, "{");
254 (*current)++;
255 return;
258 /* Add the type encodings of the super classes */
259 __objc_class_structure_encoding (class->super_class, type, size, current);
261 ivars = class->ivars;
262 if (! ivars)
263 return;
265 ivar_count = ivars->ivar_count;
267 for (i = 0; i < ivar_count; i++)
269 struct objc_ivar *ivar = &(ivars->ivar_list[i]);
270 const char *ivar_type = ivar->ivar_type;
271 int len = strlen (ivar_type);
273 if (*current + len + 1 >= *size)
275 /* Increase the size of the encoding string so that it
276 contains this ivar's type. */
277 *size = ROUND (*current + len + 1, 10);
278 *type = objc_realloc (*type, *size);
280 strcat (*type + *current, ivar_type);
281 *current += len;
286 /* Allocates the memory that will hold the type description for class
287 and calls the __objc_class_structure_encoding that generates this
288 value. */
289 void
290 __objc_generate_gc_type_description (Class class)
292 GC_bitmap mask;
293 int bits_no, size;
294 int type_size = 10, current;
295 char *class_structure_type;
297 if (! CLS_ISCLASS (class))
298 return;
300 /* We have to create a mask in which each bit counts for a pointer member.
301 We take into consideration all the non-pointer instance variables and we
302 round them up to the alignment. */
304 /* The number of bits in the mask is the size of an instance in bytes divided
305 by the size of a pointer. */
306 bits_no = (ROUND (class_getInstanceSize (class), sizeof (void *))
307 / sizeof (void *));
308 size = ROUND (bits_no, BITS_PER_WORD) / BITS_PER_WORD;
309 mask = objc_atomic_malloc (size * sizeof (int));
310 memset (mask, 0, size * sizeof (int));
312 class_structure_type = objc_atomic_malloc (type_size);
313 *class_structure_type = current = 0;
314 __objc_class_structure_encoding (class, &class_structure_type,
315 &type_size, &current);
316 if (current + 1 == type_size)
317 class_structure_type = objc_realloc (class_structure_type, ++type_size);
318 strcat (class_structure_type + current, "}");
319 #ifdef DEBUG
320 printf ("type description for '%s' is %s\n", class->name, class_structure_type);
321 #endif
323 __objc_gc_type_description_from_type (mask, class_structure_type);
324 objc_free (class_structure_type);
326 #ifdef DEBUG
327 printf (" mask for '%s', type '%s' (bits %d, mask size %d) is:",
328 class_structure_type, class->name, bits_no, size);
330 int i;
331 for (i = 0; i < size; i++)
332 printf (" %lx", mask[i]);
334 puts ("");
335 #endif
337 class->gc_object_type = (void *) GC_make_descriptor (mask, bits_no);
341 /* Returns YES if type denotes a pointer type, NO otherwise */
342 static inline BOOL
343 __objc_ivar_pointer (const char *type)
345 type = objc_skip_type_qualifiers (type);
347 return (*type == _C_ID
348 || *type == _C_CLASS
349 || *type == _C_SEL
350 || *type == _C_PTR
351 || *type == _C_CHARPTR
352 || *type == _C_ATOM);
356 /* Mark the instance variable whose name is given by ivarname as a
357 weak pointer (a pointer hidden to the garbage collector) if
358 gc_invisible is true. If gc_invisible is false it unmarks the
359 instance variable and makes it a normal pointer, visible to the
360 garbage collector.
362 This operation only makes sense on instance variables that are
363 pointers. */
364 void
365 class_ivar_set_gcinvisible (Class class, const char *ivarname,
366 BOOL gc_invisible)
368 int i, ivar_count;
369 struct objc_ivar_list *ivars;
371 if (! class || ! ivarname)
372 return;
374 ivars = class->ivars;
375 if (! ivars)
376 return;
378 ivar_count = ivars->ivar_count;
380 for (i = 0; i < ivar_count; i++)
382 struct objc_ivar *ivar = &(ivars->ivar_list[i]);
383 const char *type;
385 if (! ivar->ivar_name || strcmp (ivar->ivar_name, ivarname))
386 continue;
388 assert (ivar->ivar_type);
389 type = ivar->ivar_type;
391 /* Skip the variable name */
392 if (*type == '"')
394 for (type++; *type++ != '"';)
395 /* do nothing */;
398 if (*type == _C_GCINVISIBLE)
400 char *new_type;
401 size_t len;
403 if (gc_invisible || ! __objc_ivar_pointer (type))
404 return; /* The type of the variable already matches the
405 requested gc_invisible type */
407 /* The variable is gc_invisible so we make it gc visible. */
408 new_type = objc_atomic_malloc (strlen(ivar->ivar_type));
409 len = (type - ivar->ivar_type);
410 memcpy (new_type, ivar->ivar_type, len);
411 new_type[len] = 0;
412 strcat (new_type, type + 1);
413 ivar->ivar_type = new_type;
415 else
417 char *new_type;
418 size_t len;
420 if (! gc_invisible || ! __objc_ivar_pointer (type))
421 return; /* The type of the variable already matches the
422 requested gc_invisible type */
424 /* The variable is gc visible so we make it gc_invisible. */
425 new_type = objc_malloc (strlen(ivar->ivar_type) + 2);
427 /* Copy the variable name. */
428 len = (type - ivar->ivar_type);
429 memcpy (new_type, ivar->ivar_type, len);
430 /* Add '!'. */
431 new_type[len++] = _C_GCINVISIBLE;
432 /* Copy the original types. */
433 strcpy (new_type + len, type);
435 ivar->ivar_type = new_type;
438 __objc_generate_gc_type_description (class);
439 return;
442 /* Search the instance variable in the superclasses */
443 class_ivar_set_gcinvisible (class->super_class, ivarname, gc_invisible);
446 #else /* !OBJC_WITH_GC */
448 void
449 __objc_generate_gc_type_description (Class class __attribute__ ((__unused__)))
453 void class_ivar_set_gcinvisible (Class class __attribute__ ((__unused__)),
454 const char *ivarname __attribute__ ((__unused__)),
455 BOOL gc_invisible __attribute__ ((__unused__)))
459 #endif /* OBJC_WITH_GC */