3 //#define WITH_ALLOC_CALLBACKS 0
6 static __inline__ void *objc_malloc(size_t size);
7 static __inline__ void *objc_atomic_malloc(size_t size);
8 static __inline__ void *objc_valloc(size_t size);
9 static __inline__ void *objc_realloc(void *mem, size_t size);
10 static __inline__ void *objc_calloc(size_t nelem, size_t size);
11 static __inline__ void objc_free(void *mem);
13 #if !defined(OBJC_MEMORY)
16 #ifndef OBJC_ERR_MEMORY
17 # define OBJC_ERR_MEMORY 10 /* Out of memory */
20 objc_EXPORT void objc_error(id object, int code, const char* fmt, ...);
22 #if !WITH_ALLOC_CALLBACKS
31 ** Standard functions for memory allocation and disposal.
32 ** Users should use these functions in their ObjC programs so
33 ** that they work properly with garbage collectors as well as
34 ** can take advantage of the exception/error handling available.
36 static __inline__ void *objc_malloc(size_t size) {
37 #if WITH_ALLOC_CALLBACKS
38 void* res = (void*) (*_objc_malloc)(size);
41 void *res = GC_MALLOC(size);
43 void *res = malloc(size);
48 objc_error(nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n");
54 static __inline__ void *objc_atomic_malloc(size_t size) {
55 #if WITH_ALLOC_CALLBACKS
56 void* res = (void*) (*_objc_atomic_malloc)(size);
59 void *res = GC_MALLOC_ATOMIC(size);
61 void *res = malloc(size);
65 objc_error(nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n");
69 static __inline__ void *objc_valloc(size_t size) {
70 #if WITH_ALLOC_CALLBACKS
71 void* res = (void*) (*_objc_valloc)(size);
74 void *res = GC_MALLOC(size);
75 if (res) memset(res, 0, size);
77 void *res = malloc(size);
81 objc_error(nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n");
85 static __inline__ void *objc_realloc(void *mem, size_t size) {
86 #if WITH_ALLOC_CALLBACKS
87 void* res = (void*) (*_objc_realloc)(mem, size);
90 void *res = GC_REALLOC(mem, size);
92 void *res = realloc(mem, size);
96 objc_error(nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n");
100 static __inline__ void *objc_calloc(size_t nelem, size_t size) {
101 #if WITH_ALLOC_CALLBACKS
102 void* res = (void*) (*_objc_calloc)(nelem, size);
105 register size_t s = nelem * size;
106 void *res = GC_MALLOC(s);
107 if (res) memset(res, 0, s);
109 void *res = calloc(nelem, size);
113 objc_error(nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n");
117 static __inline__ void objc_free(void *mem) {
118 #if WITH_ALLOC_CALLBACKS
122 GC_FREE(mem); mem = NULL;