Merge from mainline (gomp-merge-2005-02-26).
[official-gcc.git] / gcc / testsuite / g++.old-deja / g++.eh / badalloc1.C
blobb4a245045852f28147338af65687a644b4d59b3d
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 #if defined(__FreeBSD__) || defined(__sun__)
18 // FreeBSD with threads and Solaris with threads require even more
19 // space at initialization time.  FreeBSD 5 now requires over 131072 bytes.
20 const int arena_size = 262144;
21 #else
22 const int arena_size = 32768;
23 #endif
24 #endif
26 struct object
28   size_t size __attribute__((aligned));
31 static char arena[arena_size] __attribute__((aligned));
32 static size_t pos;
34 // So we can force a failure when needed.
35 static int fail;
37 extern "C" void *malloc (size_t size)
39   object *p = reinterpret_cast<object *>(&arena[pos]);
41   if (fail)
42     return 0;
44   p->size = size;
45   size = (size + __alignof__(object) - 1) & - __alignof__(object);
46   pos += size + sizeof(object);
48   // Verify that we didn't run out of memory before getting initialized.
49   if (pos > arena_size)
50     abort ();
52   return p + 1;
55 extern "C" void free (void *)
59 extern "C" void *realloc (void *p, size_t size)
61   void *r;
63   if (p)
64     {
65       object *o = reinterpret_cast<object *>(p) - 1;
66       size_t old_size = o->size;
68       if (old_size >= size)
69         {
70           r = p;
71           o->size = size;
72         }
73       else
74         {
75           r = malloc (size);
76           memcpy (r, p, old_size);
77           free (p);
78         }
79     }
80   else
81     r = malloc (size);
83   return r;
86 void fn_throw() throw(int)
88   throw 1;
91 void fn_rethrow() throw(int)
93   try{fn_throw();}
94   catch(int a){
95     throw;}
98 void fn_catchthrow() throw(int)
100   try{fn_throw();}
101   catch(int a){
102     throw a + 1;}
105 int main()
107   /* On some systems (including FreeBSD and Solaris 2.10),
108      __cxa_get_globals will try to call "malloc" when threads are in
109      use.  Therefore, we throw one exception up front so that
110      __cxa_get_globals is all set up.  Ideally, this would not be
111      necessary, but it is a well-known idiom, and using this technique
112      means that we can still validate the fact that exceptions can be
113      thrown when malloc fails.  */
114   try{fn_throw();}
115   catch(int a){}
117   fail = 1;
119   try{fn_throw();}
120   catch(int a){}
122   try{fn_rethrow();}
123   catch(int a){}
125   try{fn_catchthrow();}
126   catch(int a){}
127   
128   return 0;