1998-09-21 03:22:07 +02:00
|
|
|
/* GNU Objective C Runtime class related functions
|
2014-01-02 23:26:09 +01:00
|
|
|
Copyright (C) 1993-2014 Free Software Foundation, Inc.
|
1998-09-21 03:22:07 +02:00
|
|
|
Contributed by Kresten Krab Thorup
|
|
|
|
|
hash.c, [...]: Replace "GNU CC" with "GCC".
* hash.c, init.c, libobjc.def, libobjc_entry.c, linking.m,
makefile.dos, misc.c, nil_method.c, objects.c, sarray.c,
selector.c, sendmsg.c, thr-dce.c, thr-decosf1.c, thr-irix.c,
thr-mach.c, thr-objc.c, thr-os2.c, thr-posix.c, thr-pthreads.c,
thr-rtems.c, thr-single.c, thr-solaris.c, thr-vxworks.c,
thr-win32.c, thr.c: Replace "GNU CC" with "GCC".
From-SVN: r67134
2003-05-23 22:25:39 +02:00
|
|
|
This file is part of GCC.
|
1998-09-21 03:22:07 +02:00
|
|
|
|
hash.c, [...]: Replace "GNU CC" with "GCC".
* hash.c, init.c, libobjc.def, libobjc_entry.c, linking.m,
makefile.dos, misc.c, nil_method.c, objects.c, sarray.c,
selector.c, sendmsg.c, thr-dce.c, thr-decosf1.c, thr-irix.c,
thr-mach.c, thr-objc.c, thr-os2.c, thr-posix.c, thr-pthreads.c,
thr-rtems.c, thr-single.c, thr-solaris.c, thr-vxworks.c,
thr-win32.c, thr.c: Replace "GNU CC" with "GCC".
From-SVN: r67134
2003-05-23 22:25:39 +02:00
|
|
|
GCC is free software; you can redistribute it and/or modify it under the
|
1998-09-21 03:22:07 +02:00
|
|
|
terms of the GNU General Public License as published by the Free Software
|
2009-04-09 17:00:19 +02:00
|
|
|
Foundation; either version 3, or (at your option) any later version.
|
1998-09-21 03:22:07 +02:00
|
|
|
|
hash.c, [...]: Replace "GNU CC" with "GCC".
* hash.c, init.c, libobjc.def, libobjc_entry.c, linking.m,
makefile.dos, misc.c, nil_method.c, objects.c, sarray.c,
selector.c, sendmsg.c, thr-dce.c, thr-decosf1.c, thr-irix.c,
thr-mach.c, thr-objc.c, thr-os2.c, thr-posix.c, thr-pthreads.c,
thr-rtems.c, thr-single.c, thr-solaris.c, thr-vxworks.c,
thr-win32.c, thr.c: Replace "GNU CC" with "GCC".
From-SVN: r67134
2003-05-23 22:25:39 +02:00
|
|
|
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
|
1998-09-21 03:22:07 +02:00
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
|
|
|
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
|
|
|
details.
|
|
|
|
|
2009-04-09 17:00:19 +02:00
|
|
|
Under Section 7 of GPL version 3, you are granted additional
|
|
|
|
permissions described in the GCC Runtime Library Exception, version
|
|
|
|
3.1, as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License and
|
|
|
|
a copy of the GCC Runtime Library Exception along with this program;
|
|
|
|
see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
|
|
|
|
<http://www.gnu.org/licenses/>. */
|
1998-09-21 03:22:07 +02:00
|
|
|
|
2010-09-12 00:47:14 +02:00
|
|
|
#include "objc-private/common.h"
|
2010-10-12 20:43:54 +02:00
|
|
|
#include "objc/runtime.h"
|
|
|
|
#include "objc/thr.h" /* Required by objc-private/runtime.h. */
|
|
|
|
#include "objc-private/module-abi-8.h" /* For CLS_ISCLASS and similar. */
|
|
|
|
#include "objc-private/runtime.h" /* the kitchen sink */
|
1998-09-21 03:22:07 +02:00
|
|
|
|
2010-10-12 20:43:54 +02:00
|
|
|
#include <string.h> /* For memcpy() */
|
2010-10-11 01:28:12 +02:00
|
|
|
|
1998-09-21 03:22:07 +02:00
|
|
|
#if OBJC_WITH_GC
|
|
|
|
# include <gc.h>
|
2010-10-11 01:28:12 +02:00
|
|
|
# include <gc_typed.h>
|
1998-09-21 03:22:07 +02:00
|
|
|
#endif
|
|
|
|
|
2010-10-11 01:28:12 +02:00
|
|
|
/* FIXME: The semantics of extraBytes are not really clear. */
|
|
|
|
inline
|
1998-09-21 03:22:07 +02:00
|
|
|
id
|
2010-10-11 01:28:12 +02:00
|
|
|
class_createInstance (Class class, size_t extraBytes)
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
|
|
|
id new = nil;
|
|
|
|
|
|
|
|
#if OBJC_WITH_GC
|
2002-07-02 21:43:03 +02:00
|
|
|
if (CLS_ISCLASS (class))
|
2010-10-11 01:28:12 +02:00
|
|
|
new = (id) GC_malloc_explicitly_typed (class->instance_size + extraBytes,
|
|
|
|
(GC_descr)class->gc_object_type);
|
1998-09-21 03:22:07 +02:00
|
|
|
#else
|
2002-07-02 21:43:03 +02:00
|
|
|
if (CLS_ISCLASS (class))
|
2010-10-11 01:28:12 +02:00
|
|
|
new = (id) objc_calloc (class->instance_size + extraBytes, 1);
|
1998-09-21 03:22:07 +02:00
|
|
|
#endif
|
|
|
|
|
2002-07-02 21:43:03 +02:00
|
|
|
if (new != nil)
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
2010-10-11 01:28:12 +02:00
|
|
|
/* There is no need to zero the memory, since both
|
|
|
|
GC_malloc_explicitly_typed and objc_calloc return zeroed
|
|
|
|
memory. */
|
1998-09-21 03:22:07 +02:00
|
|
|
new->class_pointer = class;
|
|
|
|
}
|
2010-10-11 01:28:12 +02:00
|
|
|
|
|
|
|
/* TODO: Invoke C++ constructors on all appropriate C++ instance
|
|
|
|
variables of the new object. */
|
|
|
|
|
1998-09-21 03:22:07 +02:00
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
2010-10-11 01:28:12 +02:00
|
|
|
/* Traditional GNU Objective-C Runtime API. */
|
1998-09-21 03:22:07 +02:00
|
|
|
id
|
2010-10-11 01:28:12 +02:00
|
|
|
object_copy (id object, size_t extraBytes)
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
2002-07-02 21:43:03 +02:00
|
|
|
if ((object != nil) && CLS_ISCLASS (object->class_pointer))
|
2010-10-11 01:28:12 +02:00
|
|
|
{
|
|
|
|
/* TODO: How should it work with C++ constructors ? */
|
|
|
|
id copy = class_createInstance (object->class_pointer, extraBytes);
|
|
|
|
memcpy (copy, object, object->class_pointer->instance_size + extraBytes);
|
|
|
|
return copy;
|
|
|
|
}
|
1998-09-21 03:22:07 +02:00
|
|
|
else
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
id
|
2002-07-02 21:43:03 +02:00
|
|
|
object_dispose (id object)
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
2002-07-02 21:43:03 +02:00
|
|
|
if ((object != nil) && CLS_ISCLASS (object->class_pointer))
|
1998-09-21 03:22:07 +02:00
|
|
|
{
|
2010-10-11 01:28:12 +02:00
|
|
|
/* TODO: Invoke C++ destructors on all appropriate C++ instance
|
|
|
|
variables. But what happens with the garbage collector ?
|
|
|
|
Would object_dispose() be ever called in that case ? */
|
|
|
|
|
|
|
|
objc_free (object);
|
1998-09-21 03:22:07 +02:00
|
|
|
}
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2010-10-11 04:36:32 +02:00
|
|
|
const char *
|
|
|
|
object_getClassName (id object)
|
|
|
|
{
|
|
|
|
if (object != nil)
|
|
|
|
return object->class_pointer->name;
|
|
|
|
else
|
|
|
|
return "Nil";
|
|
|
|
}
|
|
|
|
|
|
|
|
Class
|
|
|
|
object_setClass (id object, Class class_)
|
|
|
|
{
|
|
|
|
if (object == nil)
|
|
|
|
return Nil;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Class old_class = object->class_pointer;
|
|
|
|
|
|
|
|
object->class_pointer = class_;
|
|
|
|
return old_class;
|
|
|
|
}
|
|
|
|
}
|