2010-09-12 08:43:15 +08:00
|
|
|
/* GNU Objective C Runtime Memory allocation functions
|
|
|
|
Copyright (C) 1993, 1994, 1995, 1996, 1997, 2002, 2009, 2010
|
2002-07-03 03:43:03 +08:00
|
|
|
Free Software Foundation, Inc.
|
1998-09-21 09:22:07 +08: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-24 04:25:39 +08:00
|
|
|
This file is part of GCC.
|
1998-09-21 09:22:07 +08: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-24 04:25:39 +08:00
|
|
|
GCC is free software; you can redistribute it and/or modify it
|
1998-09-21 09:22:07 +08:00
|
|
|
under the terms of the GNU General Public License as published by the
|
2009-04-09 23:00:19 +08:00
|
|
|
Free Software Foundation; either version 3, or (at your option) any
|
1998-09-21 09:22:07 +08:00
|
|
|
later version.
|
|
|
|
|
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-24 04:25:39 +08:00
|
|
|
GCC is distributed in the hope that it will be useful, but WITHOUT
|
1998-09-21 09:22:07 +08:00
|
|
|
ANY 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 23:00:19 +08: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 09:22:07 +08:00
|
|
|
|
2010-09-12 06:47:14 +08:00
|
|
|
#include "objc-private/common.h"
|
2010-09-12 08:43:15 +08:00
|
|
|
#include "objc-private/error.h"
|
1998-09-21 09:22:07 +08:00
|
|
|
|
2010-09-12 06:47:14 +08:00
|
|
|
/* __USE_FIXED_PROTOTYPES__ used to be required to get prototypes for
|
|
|
|
malloc, free, etc. on some platforms. It is unclear if we still
|
|
|
|
need it, but it can't hurt.
|
|
|
|
*/
|
1998-09-21 09:22:07 +08:00
|
|
|
#define __USE_FIXED_PROTOTYPES__
|
|
|
|
#include <stdlib.h>
|
2010-09-12 06:47:14 +08:00
|
|
|
|
2010-09-11 20:58:27 +08:00
|
|
|
#include "objc/objc.h"
|
|
|
|
#include "objc/objc-api.h"
|
|
|
|
#include "objc-private/runtime.h"
|
1998-09-21 09:22:07 +08:00
|
|
|
|
|
|
|
/*
|
2010-09-12 08:43:15 +08:00
|
|
|
Standard functions for memory allocation and disposal. Users should
|
|
|
|
use these functions in their ObjC programs so that they work
|
|
|
|
properly with garbage collectors as well as can take advantage of
|
|
|
|
the exception/error handling available.
|
1998-09-21 09:22:07 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
void *
|
2002-07-03 03:43:03 +08:00
|
|
|
objc_malloc (size_t size)
|
1998-09-21 09:22:07 +08:00
|
|
|
{
|
2002-07-03 03:43:03 +08:00
|
|
|
void *res = (void *) (*_objc_malloc) (size);
|
|
|
|
if (! res)
|
2010-09-12 08:43:15 +08:00
|
|
|
_objc_abort ("Virtual memory exhausted\n");
|
1998-09-21 09:22:07 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2002-07-03 03:43:03 +08:00
|
|
|
objc_atomic_malloc (size_t size)
|
1998-09-21 09:22:07 +08:00
|
|
|
{
|
2002-07-03 03:43:03 +08:00
|
|
|
void *res = (void *) (*_objc_atomic_malloc) (size);
|
|
|
|
if (! res)
|
2010-09-12 08:43:15 +08:00
|
|
|
_objc_abort ("Virtual memory exhausted\n");
|
1998-09-21 09:22:07 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2002-07-03 03:43:03 +08:00
|
|
|
objc_valloc (size_t size)
|
1998-09-21 09:22:07 +08:00
|
|
|
{
|
2002-07-03 03:43:03 +08:00
|
|
|
void *res = (void *) (*_objc_valloc) (size);
|
|
|
|
if (! res)
|
2010-09-12 08:43:15 +08:00
|
|
|
_objc_abort ("Virtual memory exhausted\n");
|
1998-09-21 09:22:07 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2002-07-03 03:43:03 +08:00
|
|
|
objc_realloc (void *mem, size_t size)
|
1998-09-21 09:22:07 +08:00
|
|
|
{
|
2002-07-03 03:43:03 +08:00
|
|
|
void *res = (void *) (*_objc_realloc) (mem, size);
|
|
|
|
if (! res)
|
2010-09-12 08:43:15 +08:00
|
|
|
_objc_abort ("Virtual memory exhausted\n");
|
1998-09-21 09:22:07 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2002-07-03 03:43:03 +08:00
|
|
|
objc_calloc (size_t nelem, size_t size)
|
1998-09-21 09:22:07 +08:00
|
|
|
{
|
2002-07-03 03:43:03 +08:00
|
|
|
void *res = (void *) (*_objc_calloc) (nelem, size);
|
|
|
|
if (! res)
|
2010-09-12 08:43:15 +08:00
|
|
|
_objc_abort ("Virtual memory exhausted\n");
|
1998-09-21 09:22:07 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2002-07-03 03:43:03 +08:00
|
|
|
objc_free (void *mem)
|
1998-09-21 09:22:07 +08:00
|
|
|
{
|
2002-07-03 03:43:03 +08:00
|
|
|
(*_objc_free) (mem);
|
1998-09-21 09:22:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-09-12 08:43:15 +08:00
|
|
|
Hook functions for memory allocation and disposal. This makes it
|
|
|
|
easy to substitute garbage collection systems such as Boehm's GC by
|
|
|
|
assigning these function pointers to the GC's allocation routines.
|
|
|
|
By default these point to the ANSI standard malloc, realloc, free,
|
|
|
|
etc.
|
|
|
|
|
|
|
|
Users should call the normal objc routines above for memory
|
|
|
|
allocation and disposal within their programs.
|
1998-09-21 09:22:07 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#if OBJC_WITH_GC
|
|
|
|
#include <gc.h>
|
|
|
|
|
2010-09-12 08:43:15 +08:00
|
|
|
/* FIXME: The following sounds pointless because the GC_malloc
|
|
|
|
documentation says that it returns memory that is already zeroed!
|
|
|
|
*/
|
2002-07-03 03:43:03 +08:00
|
|
|
static void *
|
|
|
|
GC_calloc (size_t nelem, size_t size)
|
1998-09-21 09:22:07 +08:00
|
|
|
{
|
2002-07-03 03:43:03 +08:00
|
|
|
void *p = GC_malloc (nelem * size);
|
|
|
|
if (! p)
|
2010-09-12 08:43:15 +08:00
|
|
|
_objc_abort ("Virtual memory exhausted!\n");
|
1998-09-21 09:22:07 +08:00
|
|
|
|
|
|
|
memset (p, 0, nelem * size);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2002-07-03 03:43:03 +08:00
|
|
|
static void
|
|
|
|
noFree (void *p)
|
|
|
|
{
|
|
|
|
}
|
1998-09-21 09:22:07 +08:00
|
|
|
|
2002-07-03 03:43:03 +08:00
|
|
|
void *(*_objc_malloc) (size_t) = GC_malloc;
|
|
|
|
void *(*_objc_atomic_malloc) (size_t) = GC_malloc_atomic;
|
|
|
|
void *(*_objc_valloc) (size_t) = GC_malloc;
|
|
|
|
void *(*_objc_realloc) (void *, size_t) = GC_realloc;
|
|
|
|
void *(*_objc_calloc) (size_t, size_t) = GC_calloc;
|
|
|
|
void (*_objc_free) (void *) = noFree;
|
1998-09-21 09:22:07 +08:00
|
|
|
|
2002-07-03 03:43:03 +08:00
|
|
|
#else /* !OBJC_WITH_GC */
|
1998-09-21 09:22:07 +08:00
|
|
|
|
2002-07-03 03:43:03 +08:00
|
|
|
void *(*_objc_malloc) (size_t) = malloc;
|
|
|
|
void *(*_objc_atomic_malloc) (size_t) = malloc;
|
|
|
|
void *(*_objc_valloc) (size_t) = malloc;
|
|
|
|
void *(*_objc_realloc) (void *, size_t) = realloc;
|
|
|
|
void *(*_objc_calloc) (size_t, size_t) = calloc;
|
|
|
|
void (*_objc_free) (void *) = free;
|
1998-09-21 09:22:07 +08:00
|
|
|
|
|
|
|
|
2002-07-03 03:43:03 +08:00
|
|
|
#endif /* !OBJC_WITH_GC */
|