1*38fd1498Szrj // -*- C++ -*- Allocate exception objects.
2*38fd1498Szrj // Copyright (C) 2001-2018 Free Software Foundation, Inc.
3*38fd1498Szrj //
4*38fd1498Szrj // This file is part of GCC.
5*38fd1498Szrj //
6*38fd1498Szrj // GCC is free software; you can redistribute it and/or modify
7*38fd1498Szrj // it under the terms of the GNU General Public License as published by
8*38fd1498Szrj // the Free Software Foundation; either version 3, or (at your option)
9*38fd1498Szrj // any later version.
10*38fd1498Szrj //
11*38fd1498Szrj // GCC is distributed in the hope that it will be useful,
12*38fd1498Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*38fd1498Szrj // GNU General Public License for more details.
15*38fd1498Szrj //
16*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional
17*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version
18*38fd1498Szrj // 3.1, as published by the Free Software Foundation.
19*38fd1498Szrj
20*38fd1498Szrj // You should have received a copy of the GNU General Public License and
21*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program;
22*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23*38fd1498Szrj // <http://www.gnu.org/licenses/>.
24*38fd1498Szrj
25*38fd1498Szrj // This is derived from the C++ ABI for IA-64. Where we diverge
26*38fd1498Szrj // for cross-architecture compatibility are noted with "@@@".
27*38fd1498Szrj
28*38fd1498Szrj #include <bits/c++config.h>
29*38fd1498Szrj #include <cstdlib>
30*38fd1498Szrj #if _GLIBCXX_HOSTED
31*38fd1498Szrj #include <cstring>
32*38fd1498Szrj #endif
33*38fd1498Szrj #include <climits>
34*38fd1498Szrj #include <exception>
35*38fd1498Szrj #include "unwind-cxx.h"
36*38fd1498Szrj #include <ext/concurrence.h>
37*38fd1498Szrj #include <new>
38*38fd1498Szrj
39*38fd1498Szrj #if _GLIBCXX_HOSTED
40*38fd1498Szrj using std::free;
41*38fd1498Szrj using std::malloc;
42*38fd1498Szrj using std::memset;
43*38fd1498Szrj #else
44*38fd1498Szrj // In a freestanding environment, these functions may not be available
45*38fd1498Szrj // -- but for now, we assume that they are.
46*38fd1498Szrj extern "C" void *malloc (std::size_t);
47*38fd1498Szrj extern "C" void free(void *);
48*38fd1498Szrj extern "C" void *memset (void *, int, std::size_t);
49*38fd1498Szrj #endif
50*38fd1498Szrj
51*38fd1498Szrj using namespace __cxxabiv1;
52*38fd1498Szrj
53*38fd1498Szrj // ??? How to control these parameters.
54*38fd1498Szrj
55*38fd1498Szrj // Guess from the size of basic types how large a buffer is reasonable.
56*38fd1498Szrj // Note that the basic c++ exception header has 13 pointers and 2 ints,
57*38fd1498Szrj // so on a system with PSImode pointers we're talking about 56 bytes
58*38fd1498Szrj // just for overhead.
59*38fd1498Szrj
60*38fd1498Szrj #if INT_MAX == 32767
61*38fd1498Szrj # define EMERGENCY_OBJ_SIZE 128
62*38fd1498Szrj # define EMERGENCY_OBJ_COUNT 16
63*38fd1498Szrj #elif !defined (_GLIBCXX_LLP64) && LONG_MAX == 2147483647
64*38fd1498Szrj # define EMERGENCY_OBJ_SIZE 512
65*38fd1498Szrj # define EMERGENCY_OBJ_COUNT 32
66*38fd1498Szrj #else
67*38fd1498Szrj # define EMERGENCY_OBJ_SIZE 1024
68*38fd1498Szrj # define EMERGENCY_OBJ_COUNT 64
69*38fd1498Szrj #endif
70*38fd1498Szrj
71*38fd1498Szrj #ifndef __GTHREADS
72*38fd1498Szrj # undef EMERGENCY_OBJ_COUNT
73*38fd1498Szrj # define EMERGENCY_OBJ_COUNT 4
74*38fd1498Szrj #endif
75*38fd1498Szrj
76*38fd1498Szrj namespace __gnu_cxx
77*38fd1498Szrj {
78*38fd1498Szrj void __freeres();
79*38fd1498Szrj }
80*38fd1498Szrj
81*38fd1498Szrj namespace
82*38fd1498Szrj {
83*38fd1498Szrj // A fixed-size heap, variable size object allocator
84*38fd1498Szrj class pool
85*38fd1498Szrj {
86*38fd1498Szrj public:
87*38fd1498Szrj pool();
88*38fd1498Szrj
89*38fd1498Szrj void *allocate (std::size_t);
90*38fd1498Szrj void free (void *);
91*38fd1498Szrj
92*38fd1498Szrj bool in_pool (void *);
93*38fd1498Szrj
94*38fd1498Szrj private:
95*38fd1498Szrj struct free_entry {
96*38fd1498Szrj std::size_t size;
97*38fd1498Szrj free_entry *next;
98*38fd1498Szrj };
99*38fd1498Szrj struct allocated_entry {
100*38fd1498Szrj std::size_t size;
101*38fd1498Szrj char data[] __attribute__((aligned));
102*38fd1498Szrj };
103*38fd1498Szrj
104*38fd1498Szrj // A single mutex controlling emergency allocations.
105*38fd1498Szrj __gnu_cxx::__mutex emergency_mutex;
106*38fd1498Szrj
107*38fd1498Szrj // The free-list
108*38fd1498Szrj free_entry *first_free_entry;
109*38fd1498Szrj // The arena itself - we need to keep track of these only
110*38fd1498Szrj // to implement in_pool.
111*38fd1498Szrj char *arena;
112*38fd1498Szrj std::size_t arena_size;
113*38fd1498Szrj
114*38fd1498Szrj friend void __gnu_cxx::__freeres();
115*38fd1498Szrj };
116*38fd1498Szrj
pool()117*38fd1498Szrj pool::pool()
118*38fd1498Szrj {
119*38fd1498Szrj // Allocate the arena - we could add a GLIBCXX_EH_ARENA_SIZE environment
120*38fd1498Szrj // to make this tunable.
121*38fd1498Szrj arena_size = (EMERGENCY_OBJ_SIZE * EMERGENCY_OBJ_COUNT
122*38fd1498Szrj + EMERGENCY_OBJ_COUNT * sizeof (__cxa_dependent_exception));
123*38fd1498Szrj arena = (char *)malloc (arena_size);
124*38fd1498Szrj if (!arena)
125*38fd1498Szrj {
126*38fd1498Szrj // If the allocation failed go without an emergency pool.
127*38fd1498Szrj arena_size = 0;
128*38fd1498Szrj first_free_entry = NULL;
129*38fd1498Szrj return;
130*38fd1498Szrj }
131*38fd1498Szrj
132*38fd1498Szrj // Populate the free-list with a single entry covering the whole arena
133*38fd1498Szrj first_free_entry = reinterpret_cast <free_entry *> (arena);
134*38fd1498Szrj new (first_free_entry) free_entry;
135*38fd1498Szrj first_free_entry->size = arena_size;
136*38fd1498Szrj first_free_entry->next = NULL;
137*38fd1498Szrj }
138*38fd1498Szrj
allocate(std::size_t size)139*38fd1498Szrj void *pool::allocate (std::size_t size)
140*38fd1498Szrj {
141*38fd1498Szrj __gnu_cxx::__scoped_lock sentry(emergency_mutex);
142*38fd1498Szrj // We need an additional size_t member plus the padding to
143*38fd1498Szrj // ensure proper alignment of data.
144*38fd1498Szrj size += offsetof (allocated_entry, data);
145*38fd1498Szrj // And we need to at least hand out objects of the size of
146*38fd1498Szrj // a freelist entry.
147*38fd1498Szrj if (size < sizeof (free_entry))
148*38fd1498Szrj size = sizeof (free_entry);
149*38fd1498Szrj // And we need to align objects we hand out to the maximum
150*38fd1498Szrj // alignment required on the target (this really aligns the
151*38fd1498Szrj // tail which will become a new freelist entry).
152*38fd1498Szrj size = ((size + __alignof__ (allocated_entry::data) - 1)
153*38fd1498Szrj & ~(__alignof__ (allocated_entry::data) - 1));
154*38fd1498Szrj // Search for an entry of proper size on the freelist.
155*38fd1498Szrj free_entry **e;
156*38fd1498Szrj for (e = &first_free_entry;
157*38fd1498Szrj *e && (*e)->size < size;
158*38fd1498Szrj e = &(*e)->next)
159*38fd1498Szrj ;
160*38fd1498Szrj if (!*e)
161*38fd1498Szrj return NULL;
162*38fd1498Szrj allocated_entry *x;
163*38fd1498Szrj if ((*e)->size - size >= sizeof (free_entry))
164*38fd1498Szrj {
165*38fd1498Szrj // Split block if it is too large.
166*38fd1498Szrj free_entry *f = reinterpret_cast <free_entry *>
167*38fd1498Szrj (reinterpret_cast <char *> (*e) + size);
168*38fd1498Szrj std::size_t sz = (*e)->size;
169*38fd1498Szrj free_entry *next = (*e)->next;
170*38fd1498Szrj new (f) free_entry;
171*38fd1498Szrj f->next = next;
172*38fd1498Szrj f->size = sz - size;
173*38fd1498Szrj x = reinterpret_cast <allocated_entry *> (*e);
174*38fd1498Szrj new (x) allocated_entry;
175*38fd1498Szrj x->size = size;
176*38fd1498Szrj *e = f;
177*38fd1498Szrj }
178*38fd1498Szrj else
179*38fd1498Szrj {
180*38fd1498Szrj // Exact size match or too small overhead for a free entry.
181*38fd1498Szrj std::size_t sz = (*e)->size;
182*38fd1498Szrj free_entry *next = (*e)->next;
183*38fd1498Szrj x = reinterpret_cast <allocated_entry *> (*e);
184*38fd1498Szrj new (x) allocated_entry;
185*38fd1498Szrj x->size = sz;
186*38fd1498Szrj *e = next;
187*38fd1498Szrj }
188*38fd1498Szrj return &x->data;
189*38fd1498Szrj }
190*38fd1498Szrj
free(void * data)191*38fd1498Szrj void pool::free (void *data)
192*38fd1498Szrj {
193*38fd1498Szrj __gnu_cxx::__scoped_lock sentry(emergency_mutex);
194*38fd1498Szrj allocated_entry *e = reinterpret_cast <allocated_entry *>
195*38fd1498Szrj (reinterpret_cast <char *> (data) - offsetof (allocated_entry, data));
196*38fd1498Szrj std::size_t sz = e->size;
197*38fd1498Szrj if (!first_free_entry
198*38fd1498Szrj || (reinterpret_cast <char *> (e) + sz
199*38fd1498Szrj < reinterpret_cast <char *> (first_free_entry)))
200*38fd1498Szrj {
201*38fd1498Szrj // If the free list is empty or the entry is before the
202*38fd1498Szrj // first element and cannot be merged with it add it as
203*38fd1498Szrj // the first free entry.
204*38fd1498Szrj free_entry *f = reinterpret_cast <free_entry *> (e);
205*38fd1498Szrj new (f) free_entry;
206*38fd1498Szrj f->size = sz;
207*38fd1498Szrj f->next = first_free_entry;
208*38fd1498Szrj first_free_entry = f;
209*38fd1498Szrj }
210*38fd1498Szrj else if (reinterpret_cast <char *> (e) + sz
211*38fd1498Szrj == reinterpret_cast <char *> (first_free_entry))
212*38fd1498Szrj {
213*38fd1498Szrj // Check if we can merge with the first free entry being right
214*38fd1498Szrj // after us.
215*38fd1498Szrj free_entry *f = reinterpret_cast <free_entry *> (e);
216*38fd1498Szrj new (f) free_entry;
217*38fd1498Szrj f->size = sz + first_free_entry->size;
218*38fd1498Szrj f->next = first_free_entry->next;
219*38fd1498Szrj first_free_entry = f;
220*38fd1498Szrj }
221*38fd1498Szrj else
222*38fd1498Szrj {
223*38fd1498Szrj // Else search for a free item we can merge with at its end.
224*38fd1498Szrj free_entry **fe;
225*38fd1498Szrj for (fe = &first_free_entry;
226*38fd1498Szrj (*fe)->next
227*38fd1498Szrj && (reinterpret_cast <char *> ((*fe)->next)
228*38fd1498Szrj > reinterpret_cast <char *> (e) + sz);
229*38fd1498Szrj fe = &(*fe)->next)
230*38fd1498Szrj ;
231*38fd1498Szrj // If we can merge the next block into us do so and continue
232*38fd1498Szrj // with the cases below.
233*38fd1498Szrj if (reinterpret_cast <char *> (e) + sz
234*38fd1498Szrj == reinterpret_cast <char *> ((*fe)->next))
235*38fd1498Szrj {
236*38fd1498Szrj sz += (*fe)->next->size;
237*38fd1498Szrj (*fe)->next = (*fe)->next->next;
238*38fd1498Szrj }
239*38fd1498Szrj if (reinterpret_cast <char *> (*fe) + (*fe)->size
240*38fd1498Szrj == reinterpret_cast <char *> (e))
241*38fd1498Szrj // Merge with the freelist entry.
242*38fd1498Szrj (*fe)->size += sz;
243*38fd1498Szrj else
244*38fd1498Szrj {
245*38fd1498Szrj // Else put it after it which keeps the freelist sorted.
246*38fd1498Szrj free_entry *f = reinterpret_cast <free_entry *> (e);
247*38fd1498Szrj new (f) free_entry;
248*38fd1498Szrj f->size = sz;
249*38fd1498Szrj f->next = (*fe)->next;
250*38fd1498Szrj (*fe)->next = f;
251*38fd1498Szrj }
252*38fd1498Szrj }
253*38fd1498Szrj }
254*38fd1498Szrj
in_pool(void * ptr)255*38fd1498Szrj bool pool::in_pool (void *ptr)
256*38fd1498Szrj {
257*38fd1498Szrj char *p = reinterpret_cast <char *> (ptr);
258*38fd1498Szrj return (p > arena
259*38fd1498Szrj && p < arena + arena_size);
260*38fd1498Szrj }
261*38fd1498Szrj
262*38fd1498Szrj pool emergency_pool;
263*38fd1498Szrj }
264*38fd1498Szrj
265*38fd1498Szrj namespace __gnu_cxx
266*38fd1498Szrj {
267*38fd1498Szrj void
__freeres()268*38fd1498Szrj __freeres()
269*38fd1498Szrj {
270*38fd1498Szrj if (emergency_pool.arena)
271*38fd1498Szrj {
272*38fd1498Szrj ::free(emergency_pool.arena);
273*38fd1498Szrj emergency_pool.arena = 0;
274*38fd1498Szrj }
275*38fd1498Szrj }
276*38fd1498Szrj }
277*38fd1498Szrj
278*38fd1498Szrj extern "C" void *
__cxa_allocate_exception(std::size_t thrown_size)279*38fd1498Szrj __cxxabiv1::__cxa_allocate_exception(std::size_t thrown_size) _GLIBCXX_NOTHROW
280*38fd1498Szrj {
281*38fd1498Szrj void *ret;
282*38fd1498Szrj
283*38fd1498Szrj thrown_size += sizeof (__cxa_refcounted_exception);
284*38fd1498Szrj ret = malloc (thrown_size);
285*38fd1498Szrj
286*38fd1498Szrj if (!ret)
287*38fd1498Szrj ret = emergency_pool.allocate (thrown_size);
288*38fd1498Szrj
289*38fd1498Szrj if (!ret)
290*38fd1498Szrj std::terminate ();
291*38fd1498Szrj
292*38fd1498Szrj memset (ret, 0, sizeof (__cxa_refcounted_exception));
293*38fd1498Szrj
294*38fd1498Szrj return (void *)((char *)ret + sizeof (__cxa_refcounted_exception));
295*38fd1498Szrj }
296*38fd1498Szrj
297*38fd1498Szrj
298*38fd1498Szrj extern "C" void
__cxa_free_exception(void * vptr)299*38fd1498Szrj __cxxabiv1::__cxa_free_exception(void *vptr) _GLIBCXX_NOTHROW
300*38fd1498Szrj {
301*38fd1498Szrj char *ptr = (char *) vptr - sizeof (__cxa_refcounted_exception);
302*38fd1498Szrj if (emergency_pool.in_pool (ptr))
303*38fd1498Szrj emergency_pool.free (ptr);
304*38fd1498Szrj else
305*38fd1498Szrj free (ptr);
306*38fd1498Szrj }
307*38fd1498Szrj
308*38fd1498Szrj
309*38fd1498Szrj extern "C" __cxa_dependent_exception*
__cxa_allocate_dependent_exception()310*38fd1498Szrj __cxxabiv1::__cxa_allocate_dependent_exception() _GLIBCXX_NOTHROW
311*38fd1498Szrj {
312*38fd1498Szrj __cxa_dependent_exception *ret;
313*38fd1498Szrj
314*38fd1498Szrj ret = static_cast<__cxa_dependent_exception*>
315*38fd1498Szrj (malloc (sizeof (__cxa_dependent_exception)));
316*38fd1498Szrj
317*38fd1498Szrj if (!ret)
318*38fd1498Szrj ret = static_cast <__cxa_dependent_exception*>
319*38fd1498Szrj (emergency_pool.allocate (sizeof (__cxa_dependent_exception)));
320*38fd1498Szrj
321*38fd1498Szrj if (!ret)
322*38fd1498Szrj std::terminate ();
323*38fd1498Szrj
324*38fd1498Szrj memset (ret, 0, sizeof (__cxa_dependent_exception));
325*38fd1498Szrj
326*38fd1498Szrj return ret;
327*38fd1498Szrj }
328*38fd1498Szrj
329*38fd1498Szrj
330*38fd1498Szrj extern "C" void
__cxa_free_dependent_exception(__cxa_dependent_exception * vptr)331*38fd1498Szrj __cxxabiv1::__cxa_free_dependent_exception
332*38fd1498Szrj (__cxa_dependent_exception *vptr) _GLIBCXX_NOTHROW
333*38fd1498Szrj {
334*38fd1498Szrj if (emergency_pool.in_pool (vptr))
335*38fd1498Szrj emergency_pool.free (vptr);
336*38fd1498Szrj else
337*38fd1498Szrj free (vptr);
338*38fd1498Szrj }
339