see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
<http://www.gnu.org/licenses/>. */
+/* This file includes the standard functions for memory allocation and
+ disposal. Users should use these functions in their ObjC programs
+ so that they work properly with garbage collectors. */
+
+/* TODO: Turn these into macros or inline functions. */
+
#include "objc-private/common.h"
#include "objc-private/error.h"
/* __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.
-*/
+ need it, but it can't hurt. */
#define __USE_FIXED_PROTOTYPES__
#include <stdlib.h>
-#include "objc/objc.h"
-#include "objc/objc-api.h"
-#include "objc-private/runtime.h"
+#include "objc/runtime.h"
-/*
- 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.
-*/
+#if OBJC_WITH_GC
+#include <gc.h>
void *
objc_malloc (size_t size)
{
- void *res = (void *) (*_objc_malloc) (size);
+ void *res = (void *)(GC_malloc (size));
if (! res)
_objc_abort ("Virtual memory exhausted\n");
return res;
void *
objc_atomic_malloc (size_t size)
{
- void *res = (void *) (*_objc_atomic_malloc) (size);
+ void *res = (void *)(GC_malloc_atomic (size));
if (! res)
_objc_abort ("Virtual memory exhausted\n");
return res;
}
void *
-objc_valloc (size_t size)
+objc_realloc (void *mem, size_t size)
{
- void *res = (void *) (*_objc_valloc) (size);
+ void *res = (void *)(GC_realloc (mem, size));
if (! res)
_objc_abort ("Virtual memory exhausted\n");
return res;
}
void *
-objc_realloc (void *mem, size_t size)
+objc_calloc (size_t nelem, size_t size)
{
- void *res = (void *) (*_objc_realloc) (mem, size);
+ /* Note that GC_malloc returns cleared memory (see documentation) so
+ there is no need to clear it. */
+ void *res = (void *)(GC_malloc (nelem * size));
if (! res)
_objc_abort ("Virtual memory exhausted\n");
return res;
}
+void
+objc_free (void *mem __attribute__ ((__unused__)))
+{
+ return;
+}
+
+#else
+
void *
-objc_calloc (size_t nelem, size_t size)
+objc_malloc (size_t size)
{
- void *res = (void *) (*_objc_calloc) (nelem, size);
+ void *res = (void *)(malloc (size));
if (! res)
_objc_abort ("Virtual memory exhausted\n");
return res;
}
-void
-objc_free (void *mem)
+void *
+objc_atomic_malloc (size_t size)
{
- (*_objc_free) (mem);
+ void *res = (void *)(malloc (size));
+ if (! res)
+ _objc_abort ("Virtual memory exhausted\n");
+ return res;
}
-/*
- 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.
-*/
-
-#if OBJC_WITH_GC
-#include <gc.h>
-
-/* FIXME: The following sounds pointless because the GC_malloc
- documentation says that it returns memory that is already zeroed!
-*/
-static void *
-GC_calloc (size_t nelem, size_t size)
+void *
+objc_realloc (void *mem, size_t size)
{
- void *p = GC_malloc (nelem * size);
- if (! p)
- _objc_abort ("Virtual memory exhausted!\n");
-
- memset (p, 0, nelem * size);
- return p;
+ void *res = (void *)(realloc (mem, size));
+ if (! res)
+ _objc_abort ("Virtual memory exhausted\n");
+ return res;
}
-static void
-noFree (void *p)
+void *
+objc_calloc (size_t nelem, size_t size)
{
+ void *res = (void *)(calloc (nelem, size));
+ if (! res)
+ _objc_abort ("Virtual memory exhausted\n");
+ return res;
}
-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;
-
-#else /* !OBJC_WITH_GC */
-
-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;
-
+void
+objc_free (void *mem)
+{
+ free (mem);
+}
#endif /* !OBJC_WITH_GC */