FSF GCC merge 02/23/03
[official-gcc.git] / gcc / testsuite / g++.old-deja / g++.eh / badalloc1.C
blobd5722bde52f3526225d13392a1cc9063cd49cb91
1 // excess errors test - XFAIL xstormy16-*-* *-*-darwin*
2 // Copyright (C) 2000, 2002 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 const int arena_size = 32768;
18 #endif
20 struct object
22   size_t size __attribute__((aligned));
25 static char arena[arena_size] __attribute__((aligned));
26 static size_t pos;
28 // So we can force a failure when needed.
29 static int fail;
31 extern "C" void *malloc (size_t size)
33   object *p = reinterpret_cast<object *>(&arena[pos]);
35   if (fail)
36     return 0;
38   p->size = size;
39   size = (size + __alignof__(object) - 1) & - __alignof__(object);
40   pos += size + sizeof(object);
42   // Verify that we didn't run out of memory before getting initialized.
43   if (pos > arena_size)
44     abort ();
46   return p + 1;
49 extern "C" void free (void *)
53 extern "C" void *realloc (void *p, size_t size)
55   void *r;
57   if (p)
58     {
59       object *o = reinterpret_cast<object *>(p) - 1;
60       size_t old_size = o->size;
62       if (old_size >= size)
63         {
64           r = p;
65           o->size = size;
66         }
67       else
68         {
69           r = malloc (size);
70           memcpy (r, p, old_size);
71           free (p);
72         }
73     }
74   else
75     r = malloc (size);
77   return r;
80 void fn_throw() throw(int)
82   throw 1;
85 void fn_rethrow() throw(int)
87   try{fn_throw();}
88   catch(int a){
89     throw;}
92 void fn_catchthrow() throw(int)
94   try{fn_throw();}
95   catch(int a){
96     throw a + 1;}
99 int main()
101   fail = 1;
103   try{fn_throw();}
104   catch(int a){}
106   try{fn_rethrow();}
107   catch(int a){}
109   try{fn_catchthrow();}
110   catch(int a){}
111   
112   return 0;