]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/libsupc++/eh_alloc.cc
6bc46fc9a912260753526cc07b89c3030c81a525
[thirdparty/gcc.git] / libstdc++-v3 / libsupc++ / eh_alloc.cc
1 // -*- C++ -*- Allocate exception objects.
2 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2008
3 // Free Software Foundation, Inc.
4 //
5 // This file is part of GCC.
6 //
7 // GCC is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11 //
12 // GCC is distributed in the hope that it will be useful,
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 //
17 // You should have received a copy of the GNU General Public License
18 // along with GCC; see the file COPYING. If not, write to
19 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 // Boston, MA 02110-1301, USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 // This is derived from the C++ ABI for IA-64. Where we diverge
32 // for cross-architecture compatibility are noted with "@@@".
33
34 #include <bits/c++config.h>
35 #include <cstdlib>
36 #if _GLIBCXX_HOSTED
37 #include <cstring>
38 #endif
39 #include <climits>
40 #include <exception>
41 #include "unwind-cxx.h"
42 #include <ext/concurrence.h>
43
44 #if _GLIBCXX_HOSTED
45 using std::free;
46 using std::malloc;
47 using std::memset;
48 #else
49 // In a freestanding environment, these functions may not be available
50 // -- but for now, we assume that they are.
51 extern "C" void *malloc (std::size_t);
52 extern "C" void free(void *);
53 extern "C" void *memset (void *, int, std::size_t);
54 #endif
55
56 using namespace __cxxabiv1;
57
58 // ??? How to control these parameters.
59
60 // Guess from the size of basic types how large a buffer is reasonable.
61 // Note that the basic c++ exception header has 13 pointers and 2 ints,
62 // so on a system with PSImode pointers we're talking about 56 bytes
63 // just for overhead.
64
65 #if INT_MAX == 32767
66 # define EMERGENCY_OBJ_SIZE 128
67 # define EMERGENCY_OBJ_COUNT 16
68 #elif LONG_MAX == 2147483647
69 # define EMERGENCY_OBJ_SIZE 512
70 # define EMERGENCY_OBJ_COUNT 32
71 #else
72 # define EMERGENCY_OBJ_SIZE 1024
73 # define EMERGENCY_OBJ_COUNT 64
74 #endif
75
76 #ifndef __GTHREADS
77 # undef EMERGENCY_OBJ_COUNT
78 # define EMERGENCY_OBJ_COUNT 4
79 #endif
80
81 #if INT_MAX == 32767 || EMERGENCY_OBJ_COUNT <= 32
82 typedef unsigned int bitmask_type;
83 #else
84 typedef unsigned long bitmask_type;
85 #endif
86
87
88 typedef char one_buffer[EMERGENCY_OBJ_SIZE] __attribute__((aligned));
89 static one_buffer emergency_buffer[EMERGENCY_OBJ_COUNT];
90 static bitmask_type emergency_used;
91
92 static __cxa_dependent_exception dependents_buffer[EMERGENCY_OBJ_COUNT];
93 static bitmask_type dependents_used;
94
95 namespace
96 {
97 // A single mutex controlling emergency allocations.
98 __gnu_cxx::__mutex emergency_mutex;
99 }
100
101 extern "C" void *
102 __cxxabiv1::__cxa_allocate_exception(std::size_t thrown_size) throw()
103 {
104 void *ret;
105
106 thrown_size += sizeof (__cxa_exception);
107 ret = malloc (thrown_size);
108
109 if (! ret)
110 {
111 __gnu_cxx::__scoped_lock sentry(emergency_mutex);
112
113 bitmask_type used = emergency_used;
114 unsigned int which = 0;
115
116 if (thrown_size > EMERGENCY_OBJ_SIZE)
117 goto failed;
118 while (used & 1)
119 {
120 used >>= 1;
121 if (++which >= EMERGENCY_OBJ_COUNT)
122 goto failed;
123 }
124
125 emergency_used |= (bitmask_type)1 << which;
126 ret = &emergency_buffer[which][0];
127
128 failed:;
129
130 if (!ret)
131 std::terminate ();
132 }
133
134 // We have an uncaught exception as soon as we allocate memory. This
135 // yields uncaught_exception() true during the copy-constructor that
136 // initializes the exception object. See Issue 475.
137 __cxa_eh_globals *globals = __cxa_get_globals ();
138 globals->uncaughtExceptions += 1;
139
140 memset (ret, 0, sizeof (__cxa_exception));
141
142 return (void *)((char *)ret + sizeof (__cxa_exception));
143 }
144
145
146 extern "C" void
147 __cxxabiv1::__cxa_free_exception(void *vptr) throw()
148 {
149 char *base = (char *) emergency_buffer;
150 char *ptr = (char *) vptr;
151 if (ptr >= base
152 && ptr < base + sizeof (emergency_buffer))
153 {
154 const unsigned int which
155 = (unsigned) (ptr - base) / EMERGENCY_OBJ_SIZE;
156
157 __gnu_cxx::__scoped_lock sentry(emergency_mutex);
158 emergency_used &= ~((bitmask_type)1 << which);
159 }
160 else
161 free (ptr - sizeof (__cxa_exception));
162 }
163
164
165 extern "C" __cxa_dependent_exception*
166 __cxxabiv1::__cxa_allocate_dependent_exception() throw()
167 {
168 __cxa_dependent_exception *ret;
169
170 ret = static_cast<__cxa_dependent_exception*>
171 (malloc (sizeof (__cxa_dependent_exception)));
172
173 if (!ret)
174 {
175 __gnu_cxx::__scoped_lock sentry(emergency_mutex);
176
177 bitmask_type used = dependents_used;
178 unsigned int which = 0;
179
180 while (used & 1)
181 {
182 used >>= 1;
183 if (++which >= EMERGENCY_OBJ_COUNT)
184 goto failed;
185 }
186
187 dependents_used |= (bitmask_type)1 << which;
188 ret = &dependents_buffer[which];
189
190 failed:;
191
192 if (!ret)
193 std::terminate ();
194 }
195
196 // We have an uncaught exception as soon as we allocate memory. This
197 // yields uncaught_exception() true during the copy-constructor that
198 // initializes the exception object. See Issue 475.
199 __cxa_eh_globals *globals = __cxa_get_globals ();
200 globals->uncaughtExceptions += 1;
201
202 memset (ret, 0, sizeof (__cxa_dependent_exception));
203
204 return ret;
205 }
206
207
208 extern "C" void
209 __cxxabiv1::__cxa_free_dependent_exception
210 (__cxa_dependent_exception *vptr) throw()
211 {
212 char *base = (char *) dependents_buffer;
213 char *ptr = (char *) vptr;
214 if (ptr >= base
215 && ptr < base + sizeof (dependents_buffer))
216 {
217 const unsigned int which
218 = (unsigned) (ptr - base) / sizeof (__cxa_dependent_exception);
219
220 __gnu_cxx::__scoped_lock sentry(emergency_mutex);
221 dependents_used &= ~((bitmask_type)1 << which);
222 }
223 else
224 free (vptr);
225 }