OSDN Git Service

2008-08-13 Sebastian Redl <sebastian.redl@getdesigned.at>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / libsupc++ / eh_ptr.cc
1 // -*- C++ -*- Implement the members of exception_ptr.
2 // Copyright (C) 2008 Free Software Foundation, Inc.
3 //
4 // This file is part of GCC.
5 //
6 // GCC 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 // GCC 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 GCC; see the file COPYING.  If not, write to
18 // the Free Software Foundation, 51 Franklin Street, Fifth Floor,
19 // Boston, MA 02110-1301, 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 #include <bits/c++config.h>
31 #include <exception>
32 #include <exception_ptr.h>
33 #include "unwind-cxx.h"
34
35 using namespace __cxxabiv1;
36
37
38 std::__exception_ptr::exception_ptr::exception_ptr() throw()
39   : _M_exception_object(0)
40 {
41 }
42
43
44 std::__exception_ptr::exception_ptr::exception_ptr(void* obj) throw()
45   : _M_exception_object(obj)
46 {
47   _M_addref();
48 }
49
50
51 std::__exception_ptr::exception_ptr::exception_ptr(__safe_bool) throw()
52   : _M_exception_object(0)
53 {
54 }
55
56
57 std::__exception_ptr::exception_ptr::exception_ptr(
58                         const exception_ptr& other) throw()
59   : _M_exception_object(other._M_exception_object)
60 {
61   _M_addref();
62 }
63
64
65 std::__exception_ptr::exception_ptr::~exception_ptr() throw()
66 {
67   _M_release();
68 }
69
70
71 std::__exception_ptr::exception_ptr&
72 std::__exception_ptr::exception_ptr::operator=(
73                     const exception_ptr& other) throw()
74 {
75   exception_ptr(other).swap(*this);
76   return *this;
77 }
78
79
80 void
81 std::__exception_ptr::exception_ptr::_M_addref() throw()
82 {
83   if (_M_exception_object)
84     {
85       __cxa_exception *eh =
86         __get_exception_header_from_obj (_M_exception_object);
87       __sync_add_and_fetch (&eh->referenceCount, 1);
88     }
89 }
90
91
92 void
93 std::__exception_ptr::exception_ptr::_M_release() throw()
94 {
95   if (_M_exception_object)
96     {
97       __cxa_exception *eh =
98         __get_exception_header_from_obj (_M_exception_object);
99       if (__sync_sub_and_fetch (&eh->referenceCount, 1) == 0)
100         {
101           if (eh->exceptionDestructor)
102             eh->exceptionDestructor (_M_exception_object);
103
104           __cxa_free_exception (_M_exception_object);
105           _M_exception_object = 0;
106         }
107     }
108 }
109
110
111 void*
112 std::__exception_ptr::exception_ptr::_M_get() const throw()
113 {
114   return _M_exception_object;
115 }
116
117
118 void
119 std::__exception_ptr::exception_ptr::_M_safe_bool_dummy()
120 {
121 }
122
123
124 void
125 std::__exception_ptr::exception_ptr::swap(exception_ptr &other) throw()
126 {
127   void *tmp = _M_exception_object;
128   _M_exception_object = other._M_exception_object;
129   other._M_exception_object = tmp;
130 }
131
132
133 bool
134 std::__exception_ptr::exception_ptr::operator!() const throw()
135 {
136   return _M_exception_object == 0;
137 }
138
139
140 std::__exception_ptr::exception_ptr::operator __safe_bool() const throw()
141 {
142   return _M_exception_object ? &exception_ptr::_M_safe_bool_dummy : 0;
143 }
144
145
146 const std::type_info*
147 std::__exception_ptr::exception_ptr::__cxa_exception_type() const throw()
148 {
149   __cxa_exception *eh = __get_exception_header_from_obj (_M_exception_object);
150   return eh->exceptionType;
151 }
152
153
154 bool std::__exception_ptr::operator==(const exception_ptr& lhs,
155                                       const exception_ptr& rhs) throw()
156 {
157   return lhs._M_exception_object == rhs._M_exception_object;
158 }
159
160
161 bool std::__exception_ptr::operator!=(const exception_ptr& lhs,
162                                       const exception_ptr& rhs) throw()
163 {
164   return !(lhs == rhs);
165 }
166
167
168 std::exception_ptr
169 std::current_exception() throw()
170 {
171   __cxa_eh_globals *globals = __cxa_get_globals ();
172   __cxa_exception *header = globals->caughtExceptions;
173
174   if (!header)
175     return std::exception_ptr();
176
177   // Since foreign exceptions can't be counted, we can't return them.
178   if (!__is_gxx_exception_class (header->unwindHeader.exception_class))
179     return std::exception_ptr();
180
181   return std::exception_ptr(
182     __get_object_from_ambiguous_exception (header));
183 }
184
185
186 static void
187 __gxx_dependent_exception_cleanup (_Unwind_Reason_Code code,
188                                    _Unwind_Exception *exc)
189 {
190   // This cleanup is set only for dependents.
191   __cxa_dependent_exception *dep = __get_dependent_exception_from_ue (exc);
192   __cxa_exception *header =
193     __get_exception_header_from_obj (dep->primaryException);
194
195   // We only want to be called through _Unwind_DeleteException.
196   // _Unwind_DeleteException in the HP-UX IA64 libunwind library
197   // returns _URC_NO_REASON and not _URC_FOREIGN_EXCEPTION_CAUGHT
198   // like the GCC _Unwind_DeleteException function does.
199   if (code != _URC_FOREIGN_EXCEPTION_CAUGHT && code != _URC_NO_REASON)
200     __terminate (header->terminateHandler);
201
202   if (__sync_sub_and_fetch (&header->referenceCount, 1) == 0)
203     {
204       if (header->exceptionDestructor)
205         header->exceptionDestructor (header + 1);
206
207       __cxa_free_exception (header + 1);
208     }
209 }
210
211
212 void
213 std::rethrow_exception(std::exception_ptr ep)
214 {
215   void *obj = ep._M_get();
216   __cxa_exception *eh = __get_exception_header_from_obj (obj);
217
218   __cxa_dependent_exception *dep = __cxa_allocate_dependent_exception ();
219   dep->primaryException = obj;
220   __sync_add_and_fetch (&eh->referenceCount, 1);
221
222   dep->unexpectedHandler = __unexpected_handler;
223   dep->terminateHandler = __terminate_handler;
224   __GXX_INIT_DEPENDENT_EXCEPTION_CLASS(dep->unwindHeader.exception_class);
225   dep->unwindHeader.exception_cleanup = __gxx_dependent_exception_cleanup;
226
227 #ifdef _GLIBCXX_SJLJ_EXCEPTIONS
228   _Unwind_SjLj_RaiseException (&dep->unwindHeader);
229 #else
230   _Unwind_RaiseException (&dep->unwindHeader);
231 #endif
232
233   // Some sort of unwinding error.  Note that terminate is a handler.
234   __cxa_begin_catch (&dep->unwindHeader);
235   std::terminate ();
236 }