]>
Commit | Line | Data |
---|---|---|
88e17b57 | 1 | /* GNU Objective C Runtime Miscellaneous |
40165636 RB |
2 | Copyright (C) 1993, 1994, 1995, 1996, 1997, 2002 |
3 | Free Software Foundation, Inc. | |
88e17b57 BE |
4 | Contributed by Kresten Krab Thorup |
5 | ||
38709cad | 6 | This file is part of GCC. |
88e17b57 | 7 | |
38709cad | 8 | GCC is free software; you can redistribute it and/or modify it |
88e17b57 BE |
9 | under the terms of the GNU General Public License as published by the |
10 | Free Software Foundation; either version 2, or (at your option) any | |
11 | later version. | |
12 | ||
38709cad | 13 | GCC is distributed in the hope that it will be useful, but WITHOUT |
88e17b57 BE |
14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 | for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
38709cad | 19 | along with GCC; see the file COPYING. If not, write to the Free |
f9d09c43 KC |
20 | Software Foundation, 51 Franklin Street, Fifth Floor, |
21 | Boston, MA 02110-1301, USA. */ | |
88e17b57 BE |
22 | |
23 | /* As a special exception, if you link this library with files compiled with | |
24 | GCC to produce an executable, this does not cause the resulting executable | |
25 | to be covered by the GNU General Public License. This exception does not | |
26 | however invalidate any other reasons why the executable file might be | |
27 | covered by the GNU General Public License. */ | |
28 | ||
29 | #define __USE_FIXED_PROTOTYPES__ | |
30 | #include <stdlib.h> | |
348a3445 | 31 | #include "objc/runtime.h" |
88e17b57 BE |
32 | |
33 | /* | |
34 | ** Error handler function | |
35 | ** NULL so that default is to just print to stderr | |
36 | */ | |
37 | static objc_error_handler _objc_error_handler = NULL; | |
38 | ||
39 | /* Trigger an objc error */ | |
40 | void | |
40165636 | 41 | objc_error (id object, int code, const char *fmt, ...) |
88e17b57 BE |
42 | { |
43 | va_list ap; | |
44 | ||
40165636 RB |
45 | va_start (ap, fmt); |
46 | objc_verror (object, code, fmt, ap); | |
47 | va_end (ap); | |
88e17b57 BE |
48 | } |
49 | ||
50 | /* Trigger an objc error */ | |
51 | void | |
40165636 | 52 | objc_verror (id object, int code, const char *fmt, va_list ap) |
88e17b57 BE |
53 | { |
54 | BOOL result = NO; | |
55 | ||
56 | /* Call the error handler if its there | |
57 | Otherwise print to stderr */ | |
58 | if (_objc_error_handler) | |
40165636 | 59 | result = (*_objc_error_handler) (object, code, fmt, ap); |
88e17b57 BE |
60 | else |
61 | vfprintf (stderr, fmt, ap); | |
62 | ||
63 | /* Continue if the error handler says its ok | |
64 | Otherwise abort the program */ | |
65 | if (result) | |
66 | return; | |
67 | else | |
40165636 | 68 | abort (); |
88e17b57 BE |
69 | } |
70 | ||
71 | /* Set the error handler */ | |
72 | objc_error_handler | |
40165636 | 73 | objc_set_error_handler (objc_error_handler func) |
88e17b57 BE |
74 | { |
75 | objc_error_handler temp = _objc_error_handler; | |
76 | _objc_error_handler = func; | |
77 | return temp; | |
78 | } | |
79 | ||
80 | /* | |
81 | ** Standard functions for memory allocation and disposal. | |
82 | ** Users should use these functions in their ObjC programs so | |
83 | ** that they work properly with garbage collectors as well as | |
84 | ** can take advantage of the exception/error handling available. | |
85 | */ | |
86 | ||
87 | void * | |
40165636 | 88 | objc_malloc (size_t size) |
88e17b57 | 89 | { |
40165636 RB |
90 | void *res = (void *) (*_objc_malloc) (size); |
91 | if (! res) | |
92 | objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n"); | |
88e17b57 BE |
93 | return res; |
94 | } | |
95 | ||
96 | void * | |
40165636 | 97 | objc_atomic_malloc (size_t size) |
88e17b57 | 98 | { |
40165636 RB |
99 | void *res = (void *) (*_objc_atomic_malloc) (size); |
100 | if (! res) | |
101 | objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n"); | |
88e17b57 BE |
102 | return res; |
103 | } | |
104 | ||
105 | void * | |
40165636 | 106 | objc_valloc (size_t size) |
88e17b57 | 107 | { |
40165636 RB |
108 | void *res = (void *) (*_objc_valloc) (size); |
109 | if (! res) | |
110 | objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n"); | |
88e17b57 BE |
111 | return res; |
112 | } | |
113 | ||
114 | void * | |
40165636 | 115 | objc_realloc (void *mem, size_t size) |
88e17b57 | 116 | { |
40165636 RB |
117 | void *res = (void *) (*_objc_realloc) (mem, size); |
118 | if (! res) | |
119 | objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n"); | |
88e17b57 BE |
120 | return res; |
121 | } | |
122 | ||
123 | void * | |
40165636 | 124 | objc_calloc (size_t nelem, size_t size) |
88e17b57 | 125 | { |
40165636 RB |
126 | void *res = (void *) (*_objc_calloc) (nelem, size); |
127 | if (! res) | |
128 | objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n"); | |
88e17b57 BE |
129 | return res; |
130 | } | |
131 | ||
132 | void | |
40165636 | 133 | objc_free (void *mem) |
88e17b57 | 134 | { |
40165636 | 135 | (*_objc_free) (mem); |
88e17b57 BE |
136 | } |
137 | ||
138 | /* | |
139 | ** Hook functions for memory allocation and disposal. | |
140 | ** This makes it easy to substitute garbage collection systems | |
141 | ** such as Boehm's GC by assigning these function pointers | |
142 | ** to the GC's allocation routines. By default these point | |
143 | ** to the ANSI standard malloc, realloc, free, etc. | |
144 | ** | |
145 | ** Users should call the normal objc routines above for | |
146 | ** memory allocation and disposal within their programs. | |
147 | */ | |
148 | ||
149 | #if OBJC_WITH_GC | |
150 | #include <gc.h> | |
151 | ||
40165636 RB |
152 | static void * |
153 | GC_calloc (size_t nelem, size_t size) | |
88e17b57 | 154 | { |
40165636 RB |
155 | void *p = GC_malloc (nelem * size); |
156 | if (! p) | |
88e17b57 BE |
157 | objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted!\n"); |
158 | ||
159 | memset (p, 0, nelem * size); | |
160 | return p; | |
161 | } | |
162 | ||
40165636 RB |
163 | static void |
164 | noFree (void *p) | |
165 | { | |
166 | } | |
88e17b57 | 167 | |
40165636 RB |
168 | void *(*_objc_malloc) (size_t) = GC_malloc; |
169 | void *(*_objc_atomic_malloc) (size_t) = GC_malloc_atomic; | |
170 | void *(*_objc_valloc) (size_t) = GC_malloc; | |
171 | void *(*_objc_realloc) (void *, size_t) = GC_realloc; | |
172 | void *(*_objc_calloc) (size_t, size_t) = GC_calloc; | |
173 | void (*_objc_free) (void *) = noFree; | |
88e17b57 | 174 | |
40165636 | 175 | #else /* !OBJC_WITH_GC */ |
88e17b57 | 176 | |
40165636 RB |
177 | void *(*_objc_malloc) (size_t) = malloc; |
178 | void *(*_objc_atomic_malloc) (size_t) = malloc; | |
179 | void *(*_objc_valloc) (size_t) = malloc; | |
180 | void *(*_objc_realloc) (void *, size_t) = realloc; | |
181 | void *(*_objc_calloc) (size_t, size_t) = calloc; | |
182 | void (*_objc_free) (void *) = free; | |
88e17b57 BE |
183 | |
184 | ||
40165636 | 185 | #endif /* !OBJC_WITH_GC */ |