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
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under the
8terms of the GNU General Public License as published by the Free Software
9Foundation; either version 3, or (at your option) any later version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14details.
15
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/>.  */
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
34id __objc_object_alloc (Class);
35id __objc_object_dispose (id);
36id __objc_object_copy (id);
37
38id (*_objc_object_alloc) (Class)   = __objc_object_alloc;   /* !T:SINGLE */
39id (*_objc_object_dispose) (id)    = __objc_object_dispose; /* !T:SINGLE */
40id (*_objc_object_copy) (id)       = __objc_object_copy;    /* !T:SINGLE */
41
42id
43class_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
64id
65object_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
73id
74object_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
86id __objc_object_alloc (Class class)
87{
88  return (id) objc_malloc (class->instance_size);
89}
90
91id __objc_object_dispose (id object)
92{
93  objc_free (object);
94  return 0;
95}
96
97id __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}
103