OSDN Git Service

Update comment for last commit.
[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
26 #include "tconfig.h"         /* include defs of bzero for target */
27 #include "objc/objc.h"
28 #include "objc/runtime.h"               /* the kitchen sink */
29
30 #if OBJC_WITH_GC
31 # include <gc.h>
32 #endif
33
34 id __objc_object_alloc (Class);
35 id __objc_object_dispose (id);
36 id __objc_object_copy (id);
37
38 id (*_objc_object_alloc) (Class)   = __objc_object_alloc;   /* !T:SINGLE */ 
39 id (*_objc_object_dispose) (id)    = __objc_object_dispose; /* !T:SINGLE */
40 id (*_objc_object_copy) (id)       = __objc_object_copy;    /* !T:SINGLE */
41
42 id
43 class_create_instance (Class class)
44 {
45   id new = nil;
46
47 #if OBJC_WITH_GC
48   if (CLS_ISCLASS (class))
49     new = (id) GC_malloc_explicitly_typed (class->instance_size,
50                                            class->gc_object_type);
51 #else
52   if (CLS_ISCLASS (class))
53     new = (*_objc_object_alloc) (class);
54 #endif
55
56   if (new != nil)
57     {
58       memset (new, 0, class->instance_size);
59       new->class_pointer = class;
60     }
61   return new;
62 }
63
64 id
65 object_copy (id object)
66 {
67   if ((object != nil) && CLS_ISCLASS (object->class_pointer))
68     return (*_objc_object_copy) (object);
69   else
70     return nil;
71 }
72
73 id
74 object_dispose (id object)
75 {
76   if ((object != nil) && CLS_ISCLASS (object->class_pointer))
77     {
78       if (_objc_object_dispose)
79         (*_objc_object_dispose) (object);
80       else
81         objc_free (object);
82     }
83   return nil;
84 }
85
86 id __objc_object_alloc (Class class)
87 {
88   return (id) objc_malloc (class->instance_size);
89 }
90
91 id __objc_object_dispose (id object) 
92 {
93   objc_free (object);
94   return 0;
95 }
96
97 id __objc_object_copy (id object)
98 {
99   id copy = class_create_instance (object->class_pointer);
100   memcpy (copy, object, object->class_pointer->instance_size);
101   return copy;
102 }