OSDN Git Service

ee9f31c3ca62a30428bb660c0ba7738ba36ecfd3
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / libsupc++ / eh_alloc.cc
1 // -*- C++ -*- Allocate exception objects.
2 // Copyright (C) 2001 Free Software Foundation, Inc.
3 //
4 // This file is part of GNU CC.
5 //
6 // GNU CC is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10 //
11 // GNU CC is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with GNU CC; see the file COPYING.  If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330,
19 // Boston, MA 02111-1307, USA.
20
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29
30 // This is derived from the C++ ABI for IA-64.  Where we diverge
31 // for cross-architecture compatibility are noted with "@@@".
32
33 #include <exception>
34 #include <cstdlib>
35 #include <cstring>
36 #include <limits.h>
37 #include "unwind-cxx.h"
38 #include "gthr.h"
39
40 using namespace __cxxabiv1;
41
42
43 // ??? How to control these parameters.
44
45 // Guess from the size of basic types how large a buffer is reasonable.
46 // Note that the basic c++ exception header has 13 pointers and 2 ints,
47 // so on a system with PSImode pointers we're talking about 56 bytes
48 // just for overhead.
49
50 #if INT_MAX == 32767
51 # define EMERGENCY_OBJ_SIZE     128
52 # define EMERGENCY_OBJ_COUNT    16
53 #elif LONG_MAX == 2147483647
54 # define EMERGENCY_OBJ_SIZE     512
55 # define EMERGENCY_OBJ_COUNT    32
56 #else
57 # define EMERGENCY_OBJ_SIZE     1024
58 # define EMERGENCY_OBJ_COUNT    64
59 #endif
60
61 #ifndef __GTHREADS
62 # undef EMERGENCY_OBJ_COUNT
63 # define EMERGENCY_OBJ_COUNT    4
64 #endif
65
66 #if INT_MAX == 32767 || EMERGENCY_OBJ_COUNT <= 32
67 typedef unsigned int bitmask_type;
68 #else
69 typedef unsigned long bitmask_type;
70 #endif
71
72
73 typedef char one_buffer[EMERGENCY_OBJ_SIZE] __attribute__((aligned));
74 static one_buffer emergency_buffer[EMERGENCY_OBJ_COUNT];
75 static bitmask_type emergency_used;
76
77
78 #ifdef __GTHREADS
79 #ifdef __GTHREAD_MUTEX_INIT
80 static __gthread_mutex_t emergency_mutex =__GTHREAD_MUTEX_INIT;
81 #else 
82 static __gthread_mutex_t emergency_mutex;
83 #endif
84
85 #ifdef __GTHREAD_MUTEX_INIT_FUNCTION
86 static void
87 emergency_mutex_init ()
88 {
89   __GTHREAD_MUTEX_INIT_FUNCTION (&emergency_mutex);
90 }
91 #endif
92 #endif
93
94
95 extern "C" void *
96 __cxa_allocate_exception(std::size_t thrown_size)
97 {
98   void *ret;
99
100   thrown_size += sizeof (__cxa_exception);
101   ret = std::malloc (thrown_size);
102
103   if (! ret)
104     {
105 #ifdef __GTHREADS
106 #ifdef __GTHREAD_MUTEX_INIT_FUNCTION
107       static __gthread_once_t once = __GTHREAD_ONCE_INIT;
108       __gthread_once (&once, emergency_mutex_init);
109 #endif
110       __gthread_mutex_lock (&emergency_mutex);
111 #endif
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 #ifdef __GTHREADS
130       __gthread_mutex_unlock (&emergency_mutex);
131 #endif
132       if (!ret)
133         std::terminate ();
134     }
135
136   std::memset (ret, 0, sizeof (__cxa_exception));
137
138   return (void *)((char *)ret + sizeof (__cxa_exception));
139 }
140
141
142 extern "C" void
143 __cxa_free_exception(void *vptr)
144 {
145   char *ptr = (char *) vptr;
146   if (ptr >= &emergency_buffer[0][0]
147       && ptr < &emergency_buffer[0][0] + sizeof (emergency_buffer))
148     {
149       unsigned int which
150         = (unsigned)(ptr - &emergency_buffer[0][0]) / EMERGENCY_OBJ_SIZE;
151
152 #ifdef __GTHREADS
153       __gthread_mutex_lock (&emergency_mutex);
154       emergency_used &= ~((bitmask_type)1 << which);
155       __gthread_mutex_unlock (&emergency_mutex);
156 #else
157       emergency_used &= ~((bitmask_type)1 << which);
158 #endif
159     }
160   else
161     std::free (ptr - sizeof (__cxa_exception));
162 }