]>
Commit | Line | Data |
---|---|---|
88e17b57 | 1 | /* GNU Objective C Runtime Miscellaneous |
748086b7 | 2 | Copyright (C) 1993, 1994, 1995, 1996, 1997, 2002, 2009 |
40165636 | 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 | 9 | under the terms of the GNU General Public License as published by the |
748086b7 | 10 | Free Software Foundation; either version 3, or (at your option) any |
88e17b57 BE |
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 | ||
748086b7 JJ |
18 | Under Section 7 of GPL version 3, you are granted additional |
19 | permissions described in the GCC Runtime Library Exception, version | |
20 | 3.1, as published by the Free Software Foundation. | |
21 | ||
22 | You should have received a copy of the GNU General Public License and | |
23 | a copy of the GCC Runtime Library Exception along with this program; | |
24 | see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
25 | <http://www.gnu.org/licenses/>. */ | |
88e17b57 | 26 | |
6dead247 | 27 | #include "objc-private/common.h" |
88e17b57 | 28 | |
6dead247 NP |
29 | /* __USE_FIXED_PROTOTYPES__ used to be required to get prototypes for |
30 | malloc, free, etc. on some platforms. It is unclear if we still | |
31 | need it, but it can't hurt. | |
32 | */ | |
88e17b57 BE |
33 | #define __USE_FIXED_PROTOTYPES__ |
34 | #include <stdlib.h> | |
6dead247 | 35 | |
a19fac96 NP |
36 | #include "objc/objc.h" |
37 | #include "objc/objc-api.h" | |
a19fac96 | 38 | #include "objc-private/runtime.h" |
88e17b57 BE |
39 | |
40 | /* | |
41 | ** Error handler function | |
42 | ** NULL so that default is to just print to stderr | |
43 | */ | |
44 | static objc_error_handler _objc_error_handler = NULL; | |
45 | ||
46 | /* Trigger an objc error */ | |
47 | void | |
40165636 | 48 | objc_error (id object, int code, const char *fmt, ...) |
88e17b57 BE |
49 | { |
50 | va_list ap; | |
51 | ||
40165636 RB |
52 | va_start (ap, fmt); |
53 | objc_verror (object, code, fmt, ap); | |
54 | va_end (ap); | |
88e17b57 BE |
55 | } |
56 | ||
57 | /* Trigger an objc error */ | |
58 | void | |
40165636 | 59 | objc_verror (id object, int code, const char *fmt, va_list ap) |
88e17b57 BE |
60 | { |
61 | BOOL result = NO; | |
62 | ||
63 | /* Call the error handler if its there | |
64 | Otherwise print to stderr */ | |
65 | if (_objc_error_handler) | |
40165636 | 66 | result = (*_objc_error_handler) (object, code, fmt, ap); |
88e17b57 BE |
67 | else |
68 | vfprintf (stderr, fmt, ap); | |
69 | ||
70 | /* Continue if the error handler says its ok | |
71 | Otherwise abort the program */ | |
72 | if (result) | |
73 | return; | |
74 | else | |
40165636 | 75 | abort (); |
88e17b57 BE |
76 | } |
77 | ||
78 | /* Set the error handler */ | |
79 | objc_error_handler | |
40165636 | 80 | objc_set_error_handler (objc_error_handler func) |
88e17b57 BE |
81 | { |
82 | objc_error_handler temp = _objc_error_handler; | |
83 | _objc_error_handler = func; | |
84 | return temp; | |
85 | } | |
86 | ||
87 | /* | |
88 | ** Standard functions for memory allocation and disposal. | |
89 | ** Users should use these functions in their ObjC programs so | |
90 | ** that they work properly with garbage collectors as well as | |
91 | ** can take advantage of the exception/error handling available. | |
92 | */ | |
93 | ||
94 | void * | |
40165636 | 95 | objc_malloc (size_t size) |
88e17b57 | 96 | { |
40165636 RB |
97 | void *res = (void *) (*_objc_malloc) (size); |
98 | if (! res) | |
99 | objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n"); | |
88e17b57 BE |
100 | return res; |
101 | } | |
102 | ||
103 | void * | |
40165636 | 104 | objc_atomic_malloc (size_t size) |
88e17b57 | 105 | { |
40165636 RB |
106 | void *res = (void *) (*_objc_atomic_malloc) (size); |
107 | if (! res) | |
108 | objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n"); | |
88e17b57 BE |
109 | return res; |
110 | } | |
111 | ||
112 | void * | |
40165636 | 113 | objc_valloc (size_t size) |
88e17b57 | 114 | { |
40165636 RB |
115 | void *res = (void *) (*_objc_valloc) (size); |
116 | if (! res) | |
117 | objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n"); | |
88e17b57 BE |
118 | return res; |
119 | } | |
120 | ||
121 | void * | |
40165636 | 122 | objc_realloc (void *mem, size_t size) |
88e17b57 | 123 | { |
40165636 RB |
124 | void *res = (void *) (*_objc_realloc) (mem, size); |
125 | if (! res) | |
126 | objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n"); | |
88e17b57 BE |
127 | return res; |
128 | } | |
129 | ||
130 | void * | |
40165636 | 131 | objc_calloc (size_t nelem, size_t size) |
88e17b57 | 132 | { |
40165636 RB |
133 | void *res = (void *) (*_objc_calloc) (nelem, size); |
134 | if (! res) | |
135 | objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted\n"); | |
88e17b57 BE |
136 | return res; |
137 | } | |
138 | ||
139 | void | |
40165636 | 140 | objc_free (void *mem) |
88e17b57 | 141 | { |
40165636 | 142 | (*_objc_free) (mem); |
88e17b57 BE |
143 | } |
144 | ||
145 | /* | |
146 | ** Hook functions for memory allocation and disposal. | |
147 | ** This makes it easy to substitute garbage collection systems | |
148 | ** such as Boehm's GC by assigning these function pointers | |
149 | ** to the GC's allocation routines. By default these point | |
150 | ** to the ANSI standard malloc, realloc, free, etc. | |
151 | ** | |
152 | ** Users should call the normal objc routines above for | |
153 | ** memory allocation and disposal within their programs. | |
154 | */ | |
155 | ||
156 | #if OBJC_WITH_GC | |
157 | #include <gc.h> | |
158 | ||
40165636 RB |
159 | static void * |
160 | GC_calloc (size_t nelem, size_t size) | |
88e17b57 | 161 | { |
40165636 RB |
162 | void *p = GC_malloc (nelem * size); |
163 | if (! p) | |
88e17b57 BE |
164 | objc_error (nil, OBJC_ERR_MEMORY, "Virtual memory exhausted!\n"); |
165 | ||
166 | memset (p, 0, nelem * size); | |
167 | return p; | |
168 | } | |
169 | ||
40165636 RB |
170 | static void |
171 | noFree (void *p) | |
172 | { | |
173 | } | |
88e17b57 | 174 | |
40165636 RB |
175 | void *(*_objc_malloc) (size_t) = GC_malloc; |
176 | void *(*_objc_atomic_malloc) (size_t) = GC_malloc_atomic; | |
177 | void *(*_objc_valloc) (size_t) = GC_malloc; | |
178 | void *(*_objc_realloc) (void *, size_t) = GC_realloc; | |
179 | void *(*_objc_calloc) (size_t, size_t) = GC_calloc; | |
180 | void (*_objc_free) (void *) = noFree; | |
88e17b57 | 181 | |
40165636 | 182 | #else /* !OBJC_WITH_GC */ |
88e17b57 | 183 | |
40165636 RB |
184 | void *(*_objc_malloc) (size_t) = malloc; |
185 | void *(*_objc_atomic_malloc) (size_t) = malloc; | |
186 | void *(*_objc_valloc) (size_t) = malloc; | |
187 | void *(*_objc_realloc) (void *, size_t) = realloc; | |
188 | void *(*_objc_calloc) (size_t, size_t) = calloc; | |
189 | void (*_objc_free) (void *) = free; | |
88e17b57 BE |
190 | |
191 | ||
40165636 | 192 | #endif /* !OBJC_WITH_GC */ |