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