2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / testsuite / g++.old-deja / g++.eh / badalloc1.C
blob907474e13fac5470d316cb1d1e8a011dc73f42a1
1 // { dg-do run { xfail xstormy16-*-* *-*-darwin* } }
2 // Copyright (C) 2000, 2002, 2003 Free Software Foundation, Inc.
3 // Contributed by Nathan Sidwell 6 June 2000 <nathan@codesourcery.com>
5 // Check we can throw a bad_alloc exception when malloc dies.
7 typedef __SIZE_TYPE__ size_t;
8 extern "C" void abort();
9 extern "C" void *memcpy(void *, const void *, size_t);
11 // Assume that STACK_SIZE defined implies a system that does not have a
12 // large data space either, and additionally that we're not linking against
13 // a shared libstdc++ (which requires quite a bit more initialization space).
14 #ifdef STACK_SIZE
15 const int arena_size = 256;
16 #else
17 #ifdef __FreeBSD__
18 // FreeBSD with threads requires even more space at initialization time.
19 #include "bits/c++config.h"
20 #include "bits/gthr.h"
21 const int arena_size = 131072;
22 #else
23 const int arena_size = 32768;
24 #endif
25 #endif
27 struct object
29   size_t size __attribute__((aligned));
32 static char arena[arena_size] __attribute__((aligned));
33 static size_t pos;
35 // So we can force a failure when needed.
36 static int fail;
38 extern "C" void *malloc (size_t size)
40   object *p = reinterpret_cast<object *>(&arena[pos]);
42   if (fail)
43     return 0;
45   p->size = size;
46   size = (size + __alignof__(object) - 1) & - __alignof__(object);
47   pos += size + sizeof(object);
49   // Verify that we didn't run out of memory before getting initialized.
50   if (pos > arena_size)
51     abort ();
53   return p + 1;
56 extern "C" void free (void *)
60 extern "C" void *realloc (void *p, size_t size)
62   void *r;
64   if (p)
65     {
66       object *o = reinterpret_cast<object *>(p) - 1;
67       size_t old_size = o->size;
69       if (old_size >= size)
70         {
71           r = p;
72           o->size = size;
73         }
74       else
75         {
76           r = malloc (size);
77           memcpy (r, p, old_size);
78           free (p);
79         }
80     }
81   else
82     r = malloc (size);
84   return r;
87 void fn_throw() throw(int)
89   throw 1;
92 void fn_rethrow() throw(int)
94   try{fn_throw();}
95   catch(int a){
96     throw;}
99 void fn_catchthrow() throw(int)
101   try{fn_throw();}
102   catch(int a){
103     throw a + 1;}
106 int main()
108 #ifdef __FreeBSD__
109 // FreeBSD with threads fails the test unless each thread primes itself.
110   if (__gthread_active_p())
111     {
112       try{fn_throw();}
113       catch(int a){}
114     }
115 // This was added to test with well-known idiom to detect regressions here
116 // rather than always failing with -pthread.
117 #endif
119   fail = 1;
121   try{fn_throw();}
122   catch(int a){}
124   try{fn_rethrow();}
125   catch(int a){}
127   try{fn_catchthrow();}
128   catch(int a){}
129   
130   return 0;