xref: /dflybsd-src/contrib/gcc-4.7/libobjc/objects.c (revision 04febcfb30580676d3e95f58a16c5137ee478b32)
1*e4b17023SJohn Marino /* GNU Objective C Runtime class related functions
2*e4b17023SJohn Marino    Copyright (C) 1993, 1995, 1996, 2009, 2010 Free Software Foundation, Inc.
3*e4b17023SJohn Marino    Contributed by Kresten Krab Thorup
4*e4b17023SJohn Marino 
5*e4b17023SJohn Marino This file is part of GCC.
6*e4b17023SJohn Marino 
7*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under the
8*e4b17023SJohn Marino terms of the GNU General Public License as published by the Free Software
9*e4b17023SJohn Marino Foundation; either version 3, or (at your option) any later version.
10*e4b17023SJohn Marino 
11*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12*e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13*e4b17023SJohn Marino FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14*e4b17023SJohn Marino details.
15*e4b17023SJohn Marino 
16*e4b17023SJohn Marino Under Section 7 of GPL version 3, you are granted additional
17*e4b17023SJohn Marino permissions described in the GCC Runtime Library Exception, version
18*e4b17023SJohn Marino 3.1, as published by the Free Software Foundation.
19*e4b17023SJohn Marino 
20*e4b17023SJohn Marino You should have received a copy of the GNU General Public License and
21*e4b17023SJohn Marino a copy of the GCC Runtime Library Exception along with this program;
22*e4b17023SJohn Marino see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23*e4b17023SJohn Marino <http://www.gnu.org/licenses/>.  */
24*e4b17023SJohn Marino 
25*e4b17023SJohn Marino #include "objc-private/common.h"
26*e4b17023SJohn Marino #include "objc/runtime.h"
27*e4b17023SJohn Marino #include "objc/thr.h"                   /* Required by objc-private/runtime.h.  */
28*e4b17023SJohn Marino #include "objc-private/module-abi-8.h"  /* For CLS_ISCLASS and similar.  */
29*e4b17023SJohn Marino #include "objc-private/runtime.h"	/* the kitchen sink */
30*e4b17023SJohn Marino 
31*e4b17023SJohn Marino #include <string.h>                     /* For memcpy()  */
32*e4b17023SJohn Marino 
33*e4b17023SJohn Marino #if OBJC_WITH_GC
34*e4b17023SJohn Marino # include <gc.h>
35*e4b17023SJohn Marino # include <gc_typed.h>
36*e4b17023SJohn Marino #endif
37*e4b17023SJohn Marino 
38*e4b17023SJohn Marino /* FIXME: The semantics of extraBytes are not really clear.  */
39*e4b17023SJohn Marino inline
40*e4b17023SJohn Marino id
class_createInstance(Class class,size_t extraBytes)41*e4b17023SJohn Marino class_createInstance (Class class, size_t extraBytes)
42*e4b17023SJohn Marino {
43*e4b17023SJohn Marino   id new = nil;
44*e4b17023SJohn Marino 
45*e4b17023SJohn Marino #if OBJC_WITH_GC
46*e4b17023SJohn Marino   if (CLS_ISCLASS (class))
47*e4b17023SJohn Marino     new = (id) GC_malloc_explicitly_typed (class->instance_size + extraBytes,
48*e4b17023SJohn Marino 					   (GC_descr)class->gc_object_type);
49*e4b17023SJohn Marino #else
50*e4b17023SJohn Marino   if (CLS_ISCLASS (class))
51*e4b17023SJohn Marino     new = (id) objc_calloc (class->instance_size + extraBytes, 1);
52*e4b17023SJohn Marino #endif
53*e4b17023SJohn Marino 
54*e4b17023SJohn Marino   if (new != nil)
55*e4b17023SJohn Marino     {
56*e4b17023SJohn Marino       /* There is no need to zero the memory, since both
57*e4b17023SJohn Marino 	 GC_malloc_explicitly_typed and objc_calloc return zeroed
58*e4b17023SJohn Marino 	 memory.  */
59*e4b17023SJohn Marino       new->class_pointer = class;
60*e4b17023SJohn Marino     }
61*e4b17023SJohn Marino 
62*e4b17023SJohn Marino   /* TODO: Invoke C++ constructors on all appropriate C++ instance
63*e4b17023SJohn Marino      variables of the new object.  */
64*e4b17023SJohn Marino 
65*e4b17023SJohn Marino   return new;
66*e4b17023SJohn Marino }
67*e4b17023SJohn Marino 
68*e4b17023SJohn Marino /* Traditional GNU Objective-C Runtime API.  */
69*e4b17023SJohn Marino id
object_copy(id object,size_t extraBytes)70*e4b17023SJohn Marino object_copy (id object, size_t extraBytes)
71*e4b17023SJohn Marino {
72*e4b17023SJohn Marino   if ((object != nil) && CLS_ISCLASS (object->class_pointer))
73*e4b17023SJohn Marino     {
74*e4b17023SJohn Marino       /* TODO: How should it work with C++ constructors ? */
75*e4b17023SJohn Marino       id copy = class_createInstance (object->class_pointer, extraBytes);
76*e4b17023SJohn Marino       memcpy (copy, object, object->class_pointer->instance_size + extraBytes);
77*e4b17023SJohn Marino       return copy;
78*e4b17023SJohn Marino     }
79*e4b17023SJohn Marino   else
80*e4b17023SJohn Marino     return nil;
81*e4b17023SJohn Marino }
82*e4b17023SJohn Marino 
83*e4b17023SJohn Marino id
object_dispose(id object)84*e4b17023SJohn Marino object_dispose (id object)
85*e4b17023SJohn Marino {
86*e4b17023SJohn Marino   if ((object != nil) && CLS_ISCLASS (object->class_pointer))
87*e4b17023SJohn Marino     {
88*e4b17023SJohn Marino       /* TODO: Invoke C++ destructors on all appropriate C++ instance
89*e4b17023SJohn Marino 	 variables.  But what happens with the garbage collector ?
90*e4b17023SJohn Marino 	 Would object_dispose() be ever called in that case ?  */
91*e4b17023SJohn Marino 
92*e4b17023SJohn Marino       objc_free (object);
93*e4b17023SJohn Marino     }
94*e4b17023SJohn Marino   return nil;
95*e4b17023SJohn Marino }
96*e4b17023SJohn Marino 
97*e4b17023SJohn Marino const char *
object_getClassName(id object)98*e4b17023SJohn Marino object_getClassName (id object)
99*e4b17023SJohn Marino {
100*e4b17023SJohn Marino   if (object != nil)
101*e4b17023SJohn Marino     return object->class_pointer->name;
102*e4b17023SJohn Marino   else
103*e4b17023SJohn Marino     return "Nil";
104*e4b17023SJohn Marino }
105*e4b17023SJohn Marino 
106*e4b17023SJohn Marino Class
object_setClass(id object,Class class_)107*e4b17023SJohn Marino object_setClass (id object, Class class_)
108*e4b17023SJohn Marino {
109*e4b17023SJohn Marino   if (object == nil)
110*e4b17023SJohn Marino     return Nil;
111*e4b17023SJohn Marino   else
112*e4b17023SJohn Marino     {
113*e4b17023SJohn Marino       Class old_class = object->class_pointer;
114*e4b17023SJohn Marino 
115*e4b17023SJohn Marino       object->class_pointer = class_;
116*e4b17023SJohn Marino       return old_class;
117*e4b17023SJohn Marino     }
118*e4b17023SJohn Marino }
119