]>
Commit | Line | Data |
---|---|---|
52a11cbf | 1 | // -*- C++ -*- Allocate exception objects. |
c4bca01b | 2 | // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2008, 2009 |
656032b8 | 3 | // Free Software Foundation, Inc. |
52a11cbf | 4 | // |
cbecceb9 | 5 | // This file is part of GCC. |
52a11cbf | 6 | // |
cbecceb9 | 7 | // GCC is free software; you can redistribute it and/or modify |
52a11cbf | 8 | // it under the terms of the GNU General Public License as published by |
748086b7 | 9 | // the Free Software Foundation; either version 3, or (at your option) |
52a11cbf RH |
10 | // any later version. |
11 | // | |
cbecceb9 | 12 | // GCC is distributed in the hope that it will be useful, |
52a11cbf RH |
13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | // GNU General Public License for more details. | |
16 | // | |
748086b7 JJ |
17 | // Under Section 7 of GPL version 3, you are granted additional |
18 | // permissions described in the GCC Runtime Library Exception, version | |
19 | // 3.1, as published by the Free Software Foundation. | |
52a11cbf | 20 | |
748086b7 JJ |
21 | // You should have received a copy of the GNU General Public License and |
22 | // a copy of the GCC Runtime Library Exception along with this program; | |
23 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
24 | // <http://www.gnu.org/licenses/>. | |
52a11cbf RH |
25 | |
26 | // This is derived from the C++ ABI for IA-64. Where we diverge | |
27 | // for cross-architecture compatibility are noted with "@@@". | |
28 | ||
b967bf25 | 29 | #include <bits/c++config.h> |
52a11cbf | 30 | #include <cstdlib> |
4c24b21a | 31 | #if _GLIBCXX_HOSTED |
52a11cbf | 32 | #include <cstring> |
4c24b21a | 33 | #endif |
213c2316 | 34 | #include <climits> |
93b85586 | 35 | #include <exception> |
52a11cbf | 36 | #include "unwind-cxx.h" |
2e362c74 | 37 | #include <ext/concurrence.h> |
52a11cbf | 38 | |
4c24b21a MM |
39 | #if _GLIBCXX_HOSTED |
40 | using std::free; | |
41 | using std::malloc; | |
05a79eb6 | 42 | using std::memset; |
4c24b21a | 43 | #else |
3cbc7af0 BK |
44 | // In a freestanding environment, these functions may not be available |
45 | // -- but for now, we assume that they are. | |
4c24b21a MM |
46 | extern "C" void *malloc (std::size_t); |
47 | extern "C" void free(void *); | |
656032b8 | 48 | extern "C" void *memset (void *, int, std::size_t); |
4c24b21a | 49 | #endif |
52a11cbf | 50 | |
4c24b21a | 51 | using namespace __cxxabiv1; |
52a11cbf RH |
52 | |
53 | // ??? How to control these parameters. | |
54 | ||
55 | // Guess from the size of basic types how large a buffer is reasonable. | |
56 | // Note that the basic c++ exception header has 13 pointers and 2 ints, | |
57 | // so on a system with PSImode pointers we're talking about 56 bytes | |
58 | // just for overhead. | |
59 | ||
60 | #if INT_MAX == 32767 | |
61 | # define EMERGENCY_OBJ_SIZE 128 | |
62 | # define EMERGENCY_OBJ_COUNT 16 | |
63 | #elif LONG_MAX == 2147483647 | |
64 | # define EMERGENCY_OBJ_SIZE 512 | |
65 | # define EMERGENCY_OBJ_COUNT 32 | |
66 | #else | |
67 | # define EMERGENCY_OBJ_SIZE 1024 | |
68 | # define EMERGENCY_OBJ_COUNT 64 | |
69 | #endif | |
70 | ||
71 | #ifndef __GTHREADS | |
72 | # undef EMERGENCY_OBJ_COUNT | |
73 | # define EMERGENCY_OBJ_COUNT 4 | |
74 | #endif | |
75 | ||
76 | #if INT_MAX == 32767 || EMERGENCY_OBJ_COUNT <= 32 | |
77 | typedef unsigned int bitmask_type; | |
78 | #else | |
79 | typedef unsigned long bitmask_type; | |
80 | #endif | |
81 | ||
82 | ||
83 | typedef char one_buffer[EMERGENCY_OBJ_SIZE] __attribute__((aligned)); | |
84 | static one_buffer emergency_buffer[EMERGENCY_OBJ_COUNT]; | |
85 | static bitmask_type emergency_used; | |
86 | ||
30a333ce PC |
87 | static __cxa_dependent_exception dependents_buffer[EMERGENCY_OBJ_COUNT]; |
88 | static bitmask_type dependents_used; | |
89 | ||
2e362c74 | 90 | namespace |
52a11cbf | 91 | { |
2e362c74 BK |
92 | // A single mutex controlling emergency allocations. |
93 | __gnu_cxx::__mutex emergency_mutex; | |
52a11cbf | 94 | } |
52a11cbf RH |
95 | |
96 | extern "C" void * | |
723acbd5 | 97 | __cxxabiv1::__cxa_allocate_exception(std::size_t thrown_size) throw() |
52a11cbf RH |
98 | { |
99 | void *ret; | |
100 | ||
c4bca01b | 101 | thrown_size += sizeof (__cxa_refcounted_exception); |
4c24b21a | 102 | ret = malloc (thrown_size); |
52a11cbf RH |
103 | |
104 | if (! ret) | |
105 | { | |
2e362c74 | 106 | __gnu_cxx::__scoped_lock sentry(emergency_mutex); |
52a11cbf RH |
107 | |
108 | bitmask_type used = emergency_used; | |
109 | unsigned int which = 0; | |
110 | ||
f847167e NS |
111 | if (thrown_size > EMERGENCY_OBJ_SIZE) |
112 | goto failed; | |
52a11cbf RH |
113 | while (used & 1) |
114 | { | |
115 | used >>= 1; | |
116 | if (++which >= EMERGENCY_OBJ_COUNT) | |
f847167e | 117 | goto failed; |
52a11cbf RH |
118 | } |
119 | ||
120 | emergency_used |= (bitmask_type)1 << which; | |
121 | ret = &emergency_buffer[which][0]; | |
122 | ||
f847167e | 123 | failed:; |
2e362c74 | 124 | |
f847167e NS |
125 | if (!ret) |
126 | std::terminate (); | |
52a11cbf RH |
127 | } |
128 | ||
39609077 RH |
129 | // We have an uncaught exception as soon as we allocate memory. This |
130 | // yields uncaught_exception() true during the copy-constructor that | |
131 | // initializes the exception object. See Issue 475. | |
132 | __cxa_eh_globals *globals = __cxa_get_globals (); | |
133 | globals->uncaughtExceptions += 1; | |
134 | ||
c4bca01b | 135 | memset (ret, 0, sizeof (__cxa_refcounted_exception)); |
52a11cbf | 136 | |
c4bca01b | 137 | return (void *)((char *)ret + sizeof (__cxa_refcounted_exception)); |
52a11cbf RH |
138 | } |
139 | ||
140 | ||
141 | extern "C" void | |
723acbd5 | 142 | __cxxabiv1::__cxa_free_exception(void *vptr) throw() |
52a11cbf | 143 | { |
20fdc40c | 144 | char *base = (char *) emergency_buffer; |
52a11cbf | 145 | char *ptr = (char *) vptr; |
20fdc40c BE |
146 | if (ptr >= base |
147 | && ptr < base + sizeof (emergency_buffer)) | |
52a11cbf | 148 | { |
2e362c74 | 149 | const unsigned int which |
20fdc40c | 150 | = (unsigned) (ptr - base) / EMERGENCY_OBJ_SIZE; |
52a11cbf | 151 | |
2e362c74 | 152 | __gnu_cxx::__scoped_lock sentry(emergency_mutex); |
52a11cbf | 153 | emergency_used &= ~((bitmask_type)1 << which); |
52a11cbf RH |
154 | } |
155 | else | |
c4bca01b | 156 | free (ptr - sizeof (__cxa_refcounted_exception)); |
52a11cbf | 157 | } |
30a333ce PC |
158 | |
159 | ||
160 | extern "C" __cxa_dependent_exception* | |
161 | __cxxabiv1::__cxa_allocate_dependent_exception() throw() | |
162 | { | |
163 | __cxa_dependent_exception *ret; | |
164 | ||
165 | ret = static_cast<__cxa_dependent_exception*> | |
166 | (malloc (sizeof (__cxa_dependent_exception))); | |
167 | ||
168 | if (!ret) | |
169 | { | |
170 | __gnu_cxx::__scoped_lock sentry(emergency_mutex); | |
171 | ||
172 | bitmask_type used = dependents_used; | |
173 | unsigned int which = 0; | |
174 | ||
175 | while (used & 1) | |
176 | { | |
177 | used >>= 1; | |
178 | if (++which >= EMERGENCY_OBJ_COUNT) | |
179 | goto failed; | |
180 | } | |
181 | ||
182 | dependents_used |= (bitmask_type)1 << which; | |
183 | ret = &dependents_buffer[which]; | |
184 | ||
185 | failed:; | |
186 | ||
187 | if (!ret) | |
188 | std::terminate (); | |
189 | } | |
190 | ||
191 | // We have an uncaught exception as soon as we allocate memory. This | |
192 | // yields uncaught_exception() true during the copy-constructor that | |
193 | // initializes the exception object. See Issue 475. | |
194 | __cxa_eh_globals *globals = __cxa_get_globals (); | |
195 | globals->uncaughtExceptions += 1; | |
196 | ||
197 | memset (ret, 0, sizeof (__cxa_dependent_exception)); | |
198 | ||
199 | return ret; | |
200 | } | |
201 | ||
202 | ||
203 | extern "C" void | |
204 | __cxxabiv1::__cxa_free_dependent_exception | |
205 | (__cxa_dependent_exception *vptr) throw() | |
206 | { | |
207 | char *base = (char *) dependents_buffer; | |
208 | char *ptr = (char *) vptr; | |
209 | if (ptr >= base | |
210 | && ptr < base + sizeof (dependents_buffer)) | |
211 | { | |
212 | const unsigned int which | |
213 | = (unsigned) (ptr - base) / sizeof (__cxa_dependent_exception); | |
214 | ||
215 | __gnu_cxx::__scoped_lock sentry(emergency_mutex); | |
216 | dependents_used &= ~((bitmask_type)1 << which); | |
217 | } | |
218 | else | |
219 | free (vptr); | |
220 | } |