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