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