1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
// $Id$
//#define WITH_ALLOC_CALLBACKS 0
#define GC_DEBUG 1
static __inline__ void *objc_malloc(size_t size);
static __inline__ void *objc_atomic_malloc(size_t size);
static __inline__ void *objc_valloc(size_t size);
static __inline__ void *objc_realloc(void *mem, size_t size);
static __inline__ void *objc_calloc(size_t nelem, size_t size);
static __inline__ void objc_free(void *mem);
#if !defined(OBJC_MEMORY)
#define OBJC_MEMORY
#ifndef OBJC_ERR_MEMORY
# define OBJC_ERR_MEMORY 10 /* Out of memory */
#endif
objc_EXPORT void objc_error(id object, int code, const char* fmt, ...);
#if !WITH_ALLOC_CALLBACKS
# if OBJC_WITH_GC
# include "objc/gc.h"
# else
# include <stdlib.h>
# endif
#endif
/*
** 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.
*/
static __inline__ void *objc_malloc(size_t size) {
#if WITH_ALLOC_CALLBACKS
void* res = (void*) (*_objc_malloc)(size);
#else
#if OBJC_WITH_GC
void *res = GC_MALLOC(size);
#else
void *res = malloc(size);
#endif
#endif
if(!res)
objc_error(nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n");
return res;
}
#include <stdio.h>
static __inline__ void *objc_atomic_malloc(size_t size) {
#if WITH_ALLOC_CALLBACKS
void* res = (void*) (*_objc_atomic_malloc)(size);
#else
#if OBJC_WITH_GC
void *res = GC_MALLOC_ATOMIC(size);
#else
void *res = malloc(size);
#endif
#endif
if(!res)
objc_error(nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n");
return res;
}
static __inline__ void *objc_valloc(size_t size) {
#if WITH_ALLOC_CALLBACKS
void* res = (void*) (*_objc_valloc)(size);
#else
#if OBJC_WITH_GC
void *res = GC_MALLOC(size);
if (res) memset(res, 0, size);
#else
void *res = malloc(size);
#endif
#endif
if(!res)
objc_error(nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n");
return res;
}
static __inline__ void *objc_realloc(void *mem, size_t size) {
#if WITH_ALLOC_CALLBACKS
void* res = (void*) (*_objc_realloc)(mem, size);
#else
#if OBJC_WITH_GC
void *res = GC_REALLOC(mem, size);
#else
void *res = realloc(mem, size);
#endif
#endif
if(!res)
objc_error(nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n");
return res;
}
static __inline__ void *objc_calloc(size_t nelem, size_t size) {
#if WITH_ALLOC_CALLBACKS
void* res = (void*) (*_objc_calloc)(nelem, size);
#else
#if OBJC_WITH_GC
register size_t s = nelem * size;
void *res = GC_MALLOC(s);
if (res) memset(res, 0, s);
#else
void *res = calloc(nelem, size);
#endif
#endif
if(!res)
objc_error(nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n");
return res;
}
static __inline__ void objc_free(void *mem) {
#if WITH_ALLOC_CALLBACKS
(*_objc_free)(mem);
#else
#if OBJC_WITH_GC
GC_FREE(mem); mem = NULL;
#else
free(mem);
#endif
#endif
}
#endif
|