* config/rs6000/rs6000.c (rs6000_delegitimize_address): Check that
[official-gcc.git] / libstdc++-v3 / libsupc++ / eh_alloc.cc
blob82ed249af2488f06717137d19dda1f693895d9a7
1 // -*- C++ -*- Allocate exception objects.
2 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2008, 2009, 2011
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 3, or (at your option)
10 // any later version.
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.
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.
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/>.
26 // This is derived from the C++ ABI for IA-64. Where we diverge
27 // for cross-architecture compatibility are noted with "@@@".
29 #include <bits/c++config.h>
30 #include <cstdlib>
31 #if _GLIBCXX_HOSTED
32 #include <cstring>
33 #endif
34 #include <climits>
35 #include <exception>
36 #include "unwind-cxx.h"
37 #include <ext/concurrence.h>
39 #if _GLIBCXX_HOSTED
40 using std::free;
41 using std::malloc;
42 using std::memset;
43 #else
44 // In a freestanding environment, these functions may not be available
45 // -- but for now, we assume that they are.
46 extern "C" void *malloc (std::size_t);
47 extern "C" void free(void *);
48 extern "C" void *memset (void *, int, std::size_t);
49 #endif
51 using namespace __cxxabiv1;
53 // ??? How to control these parameters.
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.
60 #if INT_MAX == 32767
61 # define EMERGENCY_OBJ_SIZE 128
62 # define EMERGENCY_OBJ_COUNT 16
63 #elif !defined (_GLIBCXX_LLP64) && 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
71 #ifndef __GTHREADS
72 # undef EMERGENCY_OBJ_COUNT
73 # define EMERGENCY_OBJ_COUNT 4
74 #endif
76 #if INT_MAX == 32767 || EMERGENCY_OBJ_COUNT <= 32
77 typedef unsigned int bitmask_type;
78 #else
79 #if defined (_GLIBCXX_LLP64)
80 typedef unsigned long long bitmask_type;
81 #else
82 typedef unsigned long bitmask_type;
83 #endif
84 #endif
87 typedef char one_buffer[EMERGENCY_OBJ_SIZE] __attribute__((aligned));
88 static one_buffer emergency_buffer[EMERGENCY_OBJ_COUNT];
89 static bitmask_type emergency_used;
91 static __cxa_dependent_exception dependents_buffer[EMERGENCY_OBJ_COUNT];
92 static bitmask_type dependents_used;
94 namespace
96 // A single mutex controlling emergency allocations.
97 __gnu_cxx::__mutex emergency_mutex;
100 extern "C" void *
101 __cxxabiv1::__cxa_allocate_exception(std::size_t thrown_size) _GLIBCXX_NOTHROW
103 void *ret;
105 thrown_size += sizeof (__cxa_refcounted_exception);
106 ret = malloc (thrown_size);
108 if (! ret)
110 __gnu_cxx::__scoped_lock sentry(emergency_mutex);
112 bitmask_type used = emergency_used;
113 unsigned int which = 0;
115 if (thrown_size > EMERGENCY_OBJ_SIZE)
116 goto failed;
117 while (used & 1)
119 used >>= 1;
120 if (++which >= EMERGENCY_OBJ_COUNT)
121 goto failed;
124 emergency_used |= (bitmask_type)1 << which;
125 ret = &emergency_buffer[which][0];
127 failed:;
129 if (!ret)
130 std::terminate ();
133 // We have an uncaught exception as soon as we allocate memory. This
134 // yields uncaught_exception() true during the copy-constructor that
135 // initializes the exception object. See Issue 475.
136 __cxa_eh_globals *globals = __cxa_get_globals ();
137 globals->uncaughtExceptions += 1;
139 memset (ret, 0, sizeof (__cxa_refcounted_exception));
141 return (void *)((char *)ret + sizeof (__cxa_refcounted_exception));
145 extern "C" void
146 __cxxabiv1::__cxa_free_exception(void *vptr) _GLIBCXX_NOTHROW
148 char *base = (char *) emergency_buffer;
149 char *ptr = (char *) vptr;
150 if (ptr >= base
151 && ptr < base + sizeof (emergency_buffer))
153 const unsigned int which
154 = (unsigned) (ptr - base) / EMERGENCY_OBJ_SIZE;
156 __gnu_cxx::__scoped_lock sentry(emergency_mutex);
157 emergency_used &= ~((bitmask_type)1 << which);
159 else
160 free (ptr - sizeof (__cxa_refcounted_exception));
164 extern "C" __cxa_dependent_exception*
165 __cxxabiv1::__cxa_allocate_dependent_exception() _GLIBCXX_NOTHROW
167 __cxa_dependent_exception *ret;
169 ret = static_cast<__cxa_dependent_exception*>
170 (malloc (sizeof (__cxa_dependent_exception)));
172 if (!ret)
174 __gnu_cxx::__scoped_lock sentry(emergency_mutex);
176 bitmask_type used = dependents_used;
177 unsigned int which = 0;
179 while (used & 1)
181 used >>= 1;
182 if (++which >= EMERGENCY_OBJ_COUNT)
183 goto failed;
186 dependents_used |= (bitmask_type)1 << which;
187 ret = &dependents_buffer[which];
189 failed:;
191 if (!ret)
192 std::terminate ();
195 // We have an uncaught exception as soon as we allocate memory. This
196 // yields uncaught_exception() true during the copy-constructor that
197 // initializes the exception object. See Issue 475.
198 __cxa_eh_globals *globals = __cxa_get_globals ();
199 globals->uncaughtExceptions += 1;
201 memset (ret, 0, sizeof (__cxa_dependent_exception));
203 return ret;
207 extern "C" void
208 __cxxabiv1::__cxa_free_dependent_exception
209 (__cxa_dependent_exception *vptr) _GLIBCXX_NOTHROW
211 char *base = (char *) dependents_buffer;
212 char *ptr = (char *) vptr;
213 if (ptr >= base
214 && ptr < base + sizeof (dependents_buffer))
216 const unsigned int which
217 = (unsigned) (ptr - base) / sizeof (__cxa_dependent_exception);
219 __gnu_cxx::__scoped_lock sentry(emergency_mutex);
220 dependents_used &= ~((bitmask_type)1 << which);
222 else
223 free (vptr);