gc.c 11.5 KB
Newer Older
1
/* Basic data types for Objective C.
2
   Copyright (C) 1998-2013 Free Software Foundation, Inc.
3 4
   Contributed by Ovidiu Predescu.

5
This file is part of GCC.
6

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 11
any later version.

12
GCC is distributed in the hope that it will be useful,
13 14 15 16
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

17 18 19
Under Section 7 of GPL version 3, you are granted additional
permissions described in the GCC Runtime Library Exception, version
3.1, as published by the Free Software Foundation.
20

21 22 23 24
You should have received a copy of the GNU General Public License and
a copy of the GCC Runtime Library Exception along with this program;
see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
<http://www.gnu.org/licenses/>.  */
25

Nicola Pero committed
26
#include "objc-private/common.h"
27
#include "objc/objc.h"
28

29 30 31
#if OBJC_WITH_GC

#include "tconfig.h"
32
#include <assert.h>
33
#include <ctype.h> /* For isdigit.  */
34
#include <string.h>
35
#include <stdlib.h>
36 37
#include "objc/runtime.h"
#include "objc-private/module-abi-8.h"
38 39

#include <gc.h>
40
#include <limits.h>
41 42 43 44

/* gc_typed.h uses the following but doesn't declare them */
typedef GC_word word;
typedef GC_signed_word signed_word;
45
#define BITS_PER_WORD (CHAR_BIT * sizeof (word))
46 47 48 49 50 51 52

#include <gc_typed.h>

/* The following functions set up in `mask` the corresponding pointers.
   The offset is incremented with the size of the type.  */

#define ROUND(V, A) \
53 54
  ({ typeof (V) __v = (V); typeof (A) __a = (A); \
     __a * ((__v+__a - 1)/__a); })
55 56

#define SET_BIT_FOR_OFFSET(mask, offset) \
57
  GC_set_bit (mask, offset / sizeof (void *))
58 59 60 61 62 63 64 65 66 67 68

/* Some prototypes */
static void
__objc_gc_setup_struct (GC_bitmap mask, const char *type, int offset);
static void
__objc_gc_setup_union (GC_bitmap mask, const char *type, int offset);


static void
__objc_gc_setup_array (GC_bitmap mask, const char *type, int offset)
{
69
  int i, len = atoi (type + 1);
70

71
  while (isdigit (*++type))
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
    /* do nothing */;		/* skip the size of the array */

  switch (*type) {
  case _C_ARY_B:
    for (i = 0; i < len; i++)
      __objc_gc_setup_array (mask, type, offset);
    break;

  case _C_STRUCT_B:
    for (i = 0; i < len; i++)
      __objc_gc_setup_struct (mask, type, offset);
    break;

  case _C_UNION_B:
    for (i = 0; i < len; i++)
      __objc_gc_setup_union (mask, type, offset);
    break;

  default:
    break;
  }
}

static void
__objc_gc_setup_struct (GC_bitmap mask, const char *type, int offset)
{
  struct objc_struct_layout layout;
  unsigned int position;
  const char *mtype;

  objc_layout_structure (type, &layout);

  while (objc_layout_structure_next_member (&layout))
    {
      BOOL gc_invisible = NO;

      objc_layout_structure_get_info (&layout, &position, NULL, &mtype);

      /* Skip the variable name */
      if (*mtype == '"')
	{
	  for (mtype++; *mtype++ != '"';)
	    /* do nothing */;
	}

      if (*mtype == _C_GCINVISIBLE)
	{
	  gc_invisible = YES;
	  mtype++;
	}

      /* Add to position the offset of this structure */
      position += offset;

      switch (*mtype) {
      case _C_ID:
      case _C_CLASS:
      case _C_SEL:
      case _C_PTR:
      case _C_CHARPTR:
      case _C_ATOM:
133 134
	if (! gc_invisible)
	  SET_BIT_FOR_OFFSET (mask, position);
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
	break;

      case _C_ARY_B:
	__objc_gc_setup_array (mask, mtype, position);
	break;

      case _C_STRUCT_B:
	__objc_gc_setup_struct (mask, mtype, position);
	break;

      case _C_UNION_B:
	__objc_gc_setup_union (mask, mtype, position);
	break;

      default:
        break;
      }
    }
}

static void
__objc_gc_setup_union (GC_bitmap mask, const char *type, int offset)
{
  /* Sub-optimal, quick implementation: assume the union is made of
     pointers, set up the mask accordingly. */

  int i, size, align;

  /* Skip the variable name */
  if (*type == '"')
    {
      for (type++; *type++ != '"';)
	/* do nothing */;
    }

  size = objc_sizeof_type (type);
  align = objc_alignof_type (type);

173 174
  offset = ROUND (offset, align);
  for (i = 0; i < size; i += sizeof (void *))
175
    {
176 177
      SET_BIT_FOR_OFFSET (mask, offset);
      offset += sizeof (void *);
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    }
}


/* Iterates over the types in the structure that represents the class
   encoding and sets the bits in mask according to each ivar type.  */
static void
__objc_gc_type_description_from_type (GC_bitmap mask, const char *type)
{
  struct objc_struct_layout layout;
  unsigned int offset, align;
  const char *ivar_type;

  objc_layout_structure (type, &layout);

  while (objc_layout_structure_next_member (&layout))
    {
      BOOL gc_invisible = NO;

      objc_layout_structure_get_info (&layout, &offset, &align, &ivar_type);

      /* Skip the variable name */
      if (*ivar_type == '"')
	{
	  for (ivar_type++; *ivar_type++ != '"';)
	    /* do nothing */;
	}

      if (*ivar_type == _C_GCINVISIBLE)
	{
	  gc_invisible = YES;
	  ivar_type++;
	}

      switch (*ivar_type) {
      case _C_ID:
      case _C_CLASS:
      case _C_SEL:
      case _C_PTR:
      case _C_CHARPTR:
218 219
        if (! gc_invisible)
          SET_BIT_FOR_OFFSET (mask, offset);
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
	break;

      case _C_ARY_B:
	__objc_gc_setup_array (mask, ivar_type, offset);
	break;

      case _C_STRUCT_B:
	__objc_gc_setup_struct (mask, ivar_type, offset);
	break;

      case _C_UNION_B:
	__objc_gc_setup_union (mask, ivar_type, offset);
	break;

      default:
        break;
      }
    }
}

/* Computes in *type the full type encoding of this class including
   its super classes. '*size' gives the total number of bytes allocated
   into *type, '*current' the number of bytes used so far by the
   encoding. */
static void
__objc_class_structure_encoding (Class class, char **type, int *size,
                                 int *current)
{
  int i, ivar_count;
249
  struct objc_ivar_list *ivars;
250

251
  if (! class)
252 253
    {
      strcat (*type, "{");
254
      (*current)++;
255 256 257 258 259 260 261
      return;
    }

  /* Add the type encodings of the super classes */
  __objc_class_structure_encoding (class->super_class, type, size, current);

  ivars = class->ivars;
262
  if (! ivars)
263 264 265 266 267 268 269 270 271 272 273 274 275 276
    return;

  ivar_count = ivars->ivar_count;

  for (i = 0; i < ivar_count; i++)
    {
      struct objc_ivar *ivar = &(ivars->ivar_list[i]);
      const char *ivar_type = ivar->ivar_type;
      int len = strlen (ivar_type);

      if (*current + len + 1 >= *size)
        {
          /* Increase the size of the encoding string so that it
             contains this ivar's type. */
277
          *size = ROUND (*current + len + 1, 10);
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
          *type = objc_realloc (*type, *size);
        }
      strcat (*type + *current, ivar_type);
      *current += len;
    }
}


/* Allocates the memory that will hold the type description for class
   and calls the __objc_class_structure_encoding that generates this
   value. */
void
__objc_generate_gc_type_description (Class class)
{
  GC_bitmap mask;
  int bits_no, size;
  int type_size = 10, current;
  char *class_structure_type;

297
  if (! CLS_ISCLASS (class))
298 299 300 301 302 303 304 305
    return;

  /* We have to create a mask in which each bit counts for a pointer member.
     We take into consideration all the non-pointer instance variables and we
     round them up to the alignment. */

  /* The number of bits in the mask is the size of an instance in bytes divided
     by the size of a pointer. */
306
  bits_no = (ROUND (class_getInstanceSize (class), sizeof (void *))
307 308
             / sizeof (void *));
  size = ROUND (bits_no, BITS_PER_WORD) / BITS_PER_WORD;
309 310 311 312 313 314 315 316 317 318
  mask = objc_atomic_malloc (size * sizeof (int));
  memset (mask, 0, size * sizeof (int));

  class_structure_type = objc_atomic_malloc (type_size);
  *class_structure_type = current = 0;
  __objc_class_structure_encoding (class, &class_structure_type,
                                   &type_size, &current);
  if (current + 1 == type_size)
    class_structure_type = objc_realloc (class_structure_type, ++type_size);
  strcat (class_structure_type + current, "}");
319 320 321
#ifdef DEBUG
  printf ("type description for '%s' is %s\n", class->name, class_structure_type);
#endif
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
  
  __objc_gc_type_description_from_type (mask, class_structure_type);
  objc_free (class_structure_type);

#ifdef DEBUG
  printf ("  mask for '%s', type '%s' (bits %d, mask size %d) is:",
	  class_structure_type, class->name, bits_no, size);
  {
    int i;
    for (i = 0; i < size; i++)
      printf (" %lx", mask[i]);
  }
  puts ("");
#endif

337
  class->gc_object_type = (void *) GC_make_descriptor (mask, bits_no);
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
}


/* Returns YES if type denotes a pointer type, NO otherwise */
static inline BOOL
__objc_ivar_pointer (const char *type)
{
  type = objc_skip_type_qualifiers (type);

  return (*type == _C_ID
          || *type == _C_CLASS
          || *type == _C_SEL
          || *type == _C_PTR
          || *type == _C_CHARPTR
          || *type == _C_ATOM);
}


/* Mark the instance variable whose name is given by ivarname as a
   weak pointer (a pointer hidden to the garbage collector) if
   gc_invisible is true. If gc_invisible is false it unmarks the
   instance variable and makes it a normal pointer, visible to the
   garbage collector.

   This operation only makes sense on instance variables that are
   pointers.  */
void
365
class_ivar_set_gcinvisible (Class class, const char *ivarname,
366 367 368
                            BOOL gc_invisible)
{
  int i, ivar_count;
369
  struct objc_ivar_list *ivars;
370

371
  if (! class || ! ivarname)
372 373 374
    return;

  ivars = class->ivars;
375
  if (! ivars)
376 377 378 379 380 381 382 383 384
    return;

  ivar_count = ivars->ivar_count;

  for (i = 0; i < ivar_count; i++)
    {
      struct objc_ivar *ivar = &(ivars->ivar_list[i]);
      const char *type;

385
      if (! ivar->ivar_name || strcmp (ivar->ivar_name, ivarname))
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
	continue;

      assert (ivar->ivar_type);
      type = ivar->ivar_type;

      /* Skip the variable name */
      if (*type == '"')
	{
	  for (type++; *type++ != '"';)
	    /* do nothing */;
	}

      if (*type == _C_GCINVISIBLE)
	{
	  char *new_type;
401
	  size_t len;
402

403
	  if (gc_invisible || ! __objc_ivar_pointer (type))
404 405 406
	    return;	/* The type of the variable already matches the
			   requested gc_invisible type */

407 408 409 410 411
	  /* The variable is gc_invisible so we make it gc visible.  */
	  new_type = objc_atomic_malloc (strlen(ivar->ivar_type));
	  len = (type - ivar->ivar_type);
	  memcpy (new_type, ivar->ivar_type, len);
	  new_type[len] = 0;
412 413 414 415 416 417
	  strcat (new_type, type + 1);
	  ivar->ivar_type = new_type;
	}
      else
	{
	  char *new_type;
418
	  size_t len;
419

420
	  if (! gc_invisible || ! __objc_ivar_pointer (type))
421 422 423
	    return;	/* The type of the variable already matches the
			   requested gc_invisible type */

424 425
	  /* The variable is gc visible so we make it gc_invisible.  */
	  new_type = objc_malloc (strlen(ivar->ivar_type) + 2);
426 427

	  /* Copy the variable name.  */
428 429
	  len = (type - ivar->ivar_type);
	  memcpy (new_type, ivar->ivar_type, len);
430 431 432 433 434
	  /* Add '!'.  */
	  new_type[len++] = _C_GCINVISIBLE;
	  /* Copy the original types.  */
	  strcpy (new_type + len, type);

435 436 437 438 439 440 441 442 443 444 445 446 447 448
	  ivar->ivar_type = new_type;
	}

      __objc_generate_gc_type_description (class);
      return;
    }

  /* Search the instance variable in the superclasses */
  class_ivar_set_gcinvisible (class->super_class, ivarname, gc_invisible);
}

#else /* !OBJC_WITH_GC */

void
449
__objc_generate_gc_type_description (Class class __attribute__ ((__unused__)))
450 451 452
{
}

453
void class_ivar_set_gcinvisible (Class class __attribute__ ((__unused__)),
454
				 const char *ivarname __attribute__ ((__unused__)),
455
				 BOOL gc_invisible __attribute__ ((__unused__)))
456 457 458 459
{
}

#endif /* OBJC_WITH_GC */