objects.c 2.77 KB
Newer Older
1 2 3 4
/* GNU Objective C Runtime class related functions
   Copyright (C) 1993, 1995, 1996 Free Software Foundation, Inc.
   Contributed by Kresten Krab Thorup

5
This file is part of GCC.
6

7
GCC is free software; you can redistribute it and/or modify it under the
8 9 10
terms of the GNU General Public License as published by the Free Software
Foundation; either version 2, or (at your option) any later version.

11
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 13 14 15 16
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
details.

You should have received a copy of the GNU General Public License along with
17
GCC; see the file COPYING.  If not, write to the Free Software
18 19 20 21 22 23 24 25 26
Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */

/* As a special exception, if you link this library with files compiled with
   GCC to produce an executable, this does not cause the resulting executable
   to be covered by the GNU General Public License. This exception does not
   however invalidate any other reasons why the executable file might be
   covered by the GNU General Public License.  */

Ben Elliston committed
27
#include "tconfig.h"         /* include defs of bzero for target */
28 29 30 31 32 33 34
#include "objc.h"
#include "runtime.h"		/* the kitchen sink */

#if OBJC_WITH_GC
# include <gc.h>
#endif

35 36 37
id __objc_object_alloc (Class);
id __objc_object_dispose (id);
id __objc_object_copy (id);
38

39 40 41
id (*_objc_object_alloc) (Class)   = __objc_object_alloc;   /* !T:SINGLE */ 
id (*_objc_object_dispose) (id)    = __objc_object_dispose; /* !T:SINGLE */
id (*_objc_object_copy) (id)       = __objc_object_copy;    /* !T:SINGLE */
42 43

id
44
class_create_instance (Class class)
45 46 47 48
{
  id new = nil;

#if OBJC_WITH_GC
49 50 51
  if (CLS_ISCLASS (class))
    new = (id) GC_malloc_explicitly_typed (class->instance_size,
					   class->gc_object_type);
52
#else
53 54
  if (CLS_ISCLASS (class))
    new = (*_objc_object_alloc) (class);
55 56
#endif

57
  if (new != nil)
58 59 60 61 62 63 64 65
    {
      memset (new, 0, class->instance_size);
      new->class_pointer = class;
    }
  return new;
}

id
66
object_copy (id object)
67
{
68 69
  if ((object != nil) && CLS_ISCLASS (object->class_pointer))
    return (*_objc_object_copy) (object);
70 71 72 73 74
  else
    return nil;
}

id
75
object_dispose (id object)
76
{
77
  if ((object != nil) && CLS_ISCLASS (object->class_pointer))
78 79
    {
      if (_objc_object_dispose)
80
        (*_objc_object_dispose) (object);
81
      else
82
        objc_free (object);
83 84 85 86
    }
  return nil;
}

87
id __objc_object_alloc (Class class)
88
{
89
  return (id) objc_malloc (class->instance_size);
90 91
}

92
id __objc_object_dispose (id object) 
93
{
94
  objc_free (object);
95 96 97
  return 0;
}

98
id __objc_object_copy (id object)
99
{
100 101
  id copy = class_create_instance (object->class_pointer);
  memcpy (copy, object, object->class_pointer->instance_size);
102 103
  return copy;
}