OSDN Git Service

d37a95d17b4a108161569b5148549d58e0758670
[pf3gnuchains/gcc-fork.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/objc.h"
27 #include "objc/objc-api.h"
28 #include "objc-private/runtime.h"               /* the kitchen sink */
29
30 #include <string.h> /* For memcpy()  */
31
32 #if OBJC_WITH_GC
33 # include <gc.h>
34 # include <gc_typed.h>
35 #endif
36
37 /* FIXME: The semantics of extraBytes are not really clear.  */
38 inline
39 id
40 class_createInstance (Class class, size_t extraBytes)
41 {
42   id new = nil;
43
44 #if OBJC_WITH_GC
45   if (CLS_ISCLASS (class))
46     new = (id) GC_malloc_explicitly_typed (class->instance_size + extraBytes,
47                                            (GC_descr)class->gc_object_type);
48 #else
49   if (CLS_ISCLASS (class))
50     new = (id) objc_calloc (class->instance_size + extraBytes, 1);
51 #endif
52
53   if (new != nil)
54     {
55       /* There is no need to zero the memory, since both
56          GC_malloc_explicitly_typed and objc_calloc return zeroed
57          memory.  */
58       new->class_pointer = class;
59     }
60
61   /* TODO: Invoke C++ constructors on all appropriate C++ instance
62      variables of the new object.  */
63
64   return new;
65 }
66
67 /* Traditional GNU Objective-C Runtime API.  */
68 id
69 class_create_instance (Class class)
70 {
71   return class_createInstance (class, 0);
72 }
73
74 /* Temporary, while we are including objc-api.h instead of runtime.h.  */
75 #undef object_copy
76
77 id
78 object_copy (id object, size_t extraBytes)
79 {
80   if ((object != nil) && CLS_ISCLASS (object->class_pointer))
81     {
82       /* TODO: How should it work with C++ constructors ? */
83       id copy = class_createInstance (object->class_pointer, extraBytes);
84       memcpy (copy, object, object->class_pointer->instance_size + extraBytes);
85       return copy;
86     }
87   else
88     return nil;
89 }
90
91 id
92 object_dispose (id object)
93 {
94   if ((object != nil) && CLS_ISCLASS (object->class_pointer))
95     {
96       /* TODO: Invoke C++ destructors on all appropriate C++ instance
97          variables.  But what happens with the garbage collector ?
98          Would object_dispose() be ever called in that case ?  */
99
100       objc_free (object);
101     }
102   return nil;
103 }
104
105 /*
106   Hook functions for memory allocation and disposal.  Deprecated
107   and currently unused.
108 */
109
110 id (*_objc_object_alloc) (Class)   = 0;
111 id (*_objc_object_dispose) (id)    = 0;
112 id (*_objc_object_copy) (id)       = 0;