OSDN Git Service

IA-64 ABI Exception Handling.
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / libsupc++ / vec.cc
1 // New abi Support -*- C++ -*-
2
3 // Copyright (C) 2000, 2001 Free Software Foundation, Inc.
4 //  
5 // This file is part of GNU CC.
6 //
7 // GNU CC 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 // GNU CC 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 GNU CC; see the file COPYING.  If not, write to
19 // the Free Software Foundation, 59 Temple Place - Suite 330,
20 // Boston, MA 02111-1307, 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 // Written by Nathan Sidwell, Codesourcery LLC, <nathan@codesourcery.com>
32
33 #include <cxxabi.h>
34 #include <new>
35 #include <exception>
36 #include <exception_defines.h>
37
38 #include "unwind-cxx.h"
39
40 namespace __cxxabiv1
41 {
42   namespace 
43   {
44     struct uncatch_exception 
45     {
46       uncatch_exception ();
47       ~uncatch_exception () { __cxa_begin_catch (&p->unwindHeader); }
48       
49       __cxa_exception *p;
50     };
51
52     uncatch_exception::uncatch_exception ()
53     {
54       __cxa_eh_globals *globals = __cxa_get_globals_fast ();
55
56       p = globals->caughtExceptions;
57       p->handlerCount -= 1;
58       globals->caughtExceptions = p->nextException;
59       globals->uncaughtExceptions += 1;
60     }
61   }
62
63   // Allocate and construct array.
64   extern "C" void *
65   __cxa_vec_new(std::size_t element_count,
66                 std::size_t element_size,
67                 std::size_t padding_size,
68                 void (*constructor) (void *),
69                 void (*destructor) (void *))
70   {
71     return __cxa_vec_new2(element_count, element_size, padding_size,
72                            constructor, destructor,
73                            &operator new[], &operator delete []);
74   }
75
76   extern "C" void *
77   __cxa_vec_new2(std::size_t element_count,
78                  std::size_t element_size,
79                  std::size_t padding_size,
80                  void (*constructor) (void *),
81                  void (*destructor) (void *),
82                  void *(*alloc) (std::size_t),
83                  void (*dealloc) (void *))
84   {
85     std::size_t size = element_count * element_size + padding_size;
86     char *base = static_cast <char *> (alloc (size));
87     
88     if (padding_size)
89       {
90         base += padding_size;
91         reinterpret_cast <std::size_t *> (base)[-1] = element_count;
92       }
93     try
94       {
95         __cxa_vec_ctor(base, element_count, element_size,
96                        constructor, destructor);
97       }
98     catch (...)
99       {
100         {
101           uncatch_exception ue;
102           dealloc(base - padding_size);
103         }
104         __throw_exception_again;
105       }
106     return base;
107   }
108   
109   extern "C" void *
110   __cxa_vec_new3(std::size_t element_count,
111                  std::size_t element_size,
112                  std::size_t padding_size,
113                  void (*constructor) (void *),
114                  void (*destructor) (void *),
115                  void *(*alloc) (std::size_t),
116                  void (*dealloc) (void *, std::size_t))
117   {
118     std::size_t size = element_count * element_size + padding_size;
119     char *base = static_cast<char *>(alloc (size));
120     
121     if (padding_size)
122       {
123         base += padding_size;
124         reinterpret_cast<std::size_t *>(base)[-1] = element_count;
125       }
126     try
127       {
128         __cxa_vec_ctor(base, element_count, element_size,
129                        constructor, destructor);
130       }
131     catch (...)
132       {
133         {
134           uncatch_exception ue;
135           dealloc(base - padding_size, size);
136         }
137         __throw_exception_again;
138       }
139     return base;
140   }
141   
142   // Construct array.
143   extern "C" void
144   __cxa_vec_ctor(void *array_address,
145                  std::size_t element_count,
146                  std::size_t element_size,
147                  void (*constructor) (void *),
148                  void (*destructor) (void *))
149   {
150     std::size_t ix = 0;
151     char *ptr = static_cast<char *>(array_address);
152     
153     try
154       {
155         if (constructor)
156           for (; ix != element_count; ix++, ptr += element_size)
157             constructor(ptr);
158       }
159     catch (...)
160       {
161         {
162           uncatch_exception ue;
163           __cxa_vec_cleanup(array_address, ix, element_size, destructor);
164         }
165         __throw_exception_again;
166       }
167   }
168   
169   // Construct an array by copying.
170   extern "C" void
171   __cxa_vec_cctor(void *dest_array,
172                   void *src_array,
173                   std::size_t element_count,
174                   std::size_t element_size,
175                   void (*constructor) (void *, void *),
176                   void (*destructor) (void *))
177   {
178     std::size_t ix = 0;
179     char *dest_ptr = static_cast<char *>(dest_array);
180     char *src_ptr = static_cast<char *>(src_array);
181     
182     try
183       {
184         if (constructor)
185           for (; ix != element_count; 
186                ix++, src_ptr += element_size, dest_ptr += element_size)
187             constructor(dest_ptr, src_ptr);
188       }
189     catch (...)
190       {
191         {
192           uncatch_exception ue;
193           __cxa_vec_cleanup(dest_array, ix, element_size, destructor);
194         }
195         __throw_exception_again;
196       }
197   }
198   
199   // Destruct array.
200   extern "C" void
201   __cxa_vec_dtor(void *array_address,
202                  std::size_t element_count,
203                  std::size_t element_size,
204                  void (*destructor) (void *))
205   {
206     if (destructor)
207       {
208         char *ptr = static_cast<char *>(array_address);
209         std::size_t ix = element_count;
210
211         ptr += element_count * element_size;
212
213         try
214           {
215             while (ix--)
216               {
217                 ptr -= element_size;
218                 destructor(ptr);
219               }
220           }
221         catch (...)
222           {
223             {
224               uncatch_exception ue;
225               __cxa_vec_cleanup(array_address, ix, element_size, destructor);
226             }
227             __throw_exception_again;
228           }
229       }
230   }
231
232   // Destruct array as a result of throwing an exception.
233   // [except.ctor]/3 If a destructor called during stack unwinding
234   // exits with an exception, terminate is called.
235   extern "C" void
236   __cxa_vec_cleanup(void *array_address,
237                     std::size_t element_count,
238                     std::size_t element_size,
239                     void (*destructor) (void *))
240   {
241     if (destructor)
242       {
243         char *ptr = static_cast <char *> (array_address);
244         std::size_t ix = element_count;
245
246         ptr += element_count * element_size;
247
248         try
249           {
250             while (ix--)
251               {
252                 ptr -= element_size;
253                 destructor(ptr);
254               }
255           }
256         catch (...)
257           {
258             std::terminate();
259           }
260       }
261   }
262
263   // Destruct and release array.
264   extern "C" void
265   __cxa_vec_delete(void *array_address,
266                    std::size_t element_size,
267                    std::size_t padding_size,
268                    void (*destructor) (void *))
269   {
270     __cxa_vec_delete2(array_address, element_size, padding_size,
271                        destructor,
272                        &operator delete []);
273   }
274
275   extern "C" void
276   __cxa_vec_delete2(void *array_address,
277                     std::size_t element_size,
278                     std::size_t padding_size,
279                     void (*destructor) (void *),
280                     void (*dealloc) (void *))
281   {
282     char *base = static_cast<char *>(array_address);
283   
284     if (padding_size)
285       {
286         std::size_t element_count = reinterpret_cast<std::size_t *>(base)[-1];
287         base -= padding_size;
288         try
289           {
290             __cxa_vec_dtor(array_address, element_count, element_size,
291                            destructor);
292           }
293         catch (...)
294           {
295             {
296               uncatch_exception ue;
297               dealloc(base);
298             }
299             __throw_exception_again;
300           }
301       }
302     dealloc(base);
303   }
304
305   extern "C" void
306   __cxa_vec_delete3(void *array_address,
307                     std::size_t element_size,
308                     std::size_t padding_size,
309                      void (*destructor) (void *),
310                     void (*dealloc) (void *, std::size_t))
311   {
312     char *base = static_cast <char *> (array_address);
313     std::size_t size = 0;
314     
315     if (padding_size)
316       {
317         std::size_t element_count = reinterpret_cast<std::size_t *> (base)[-1];
318         base -= padding_size;
319         size = element_count * element_size + padding_size;
320         try
321           {
322             __cxa_vec_dtor(array_address, element_count, element_size,
323                            destructor);
324           }
325         catch (...)
326           {
327             {
328               uncatch_exception ue;
329               dealloc(base, size);
330             }
331             __throw_exception_again;
332           }
333       }
334     dealloc(base, size);
335   }
336 } // namespace __cxxabiv1
337