gc.c
上传用户:shenzhenrh
上传日期:2013-05-12
资源大小:2904k
文件大小:11k
源码类别:

信息检索与抽取

开发平台:

Unix_Linux

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