OSDN Git Service

* g++.old-deja/g++.eh/badalloc1.C (arena_size): New.
[pf3gnuchains/gcc-fork.git] / gcc / testsuite / g++.old-deja / g++.eh / badalloc1.C
1 // excess errors test - XFAIL xstormy16-*-*
2 // Copyright (C) 2000 Free Software Foundation, Inc.
3 // Contributed by Nathan Sidwell 6 June 2000 <nathan@codesourcery.com>
4
5 // Check we can throw a bad_alloc exception when malloc dies.
6
7 typedef __SIZE_TYPE__ size_t;
8 extern "C" void abort();
9 extern "C" void *memcpy(void *, const void *, size_t);
10
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
19
20 struct object
21 {
22   size_t size __attribute__((aligned));
23 };
24
25 static char arena[arena_size] __attribute__((aligned));
26 static size_t pos;
27
28 // So we can force a failure when needed.
29 static int fail;
30
31 extern "C" void *malloc (size_t size)
32 {
33   object *p = reinterpret_cast<object *>(&arena[pos]);
34
35   if (fail)
36     return 0;
37
38   p->size = size;
39   size = (size + __alignof__(object) + 1) & - __alignof__(object);
40   pos += size + sizeof(object);
41
42   // Verify that we didn't run out of memory before getting initialized.
43   if (pos > arena_size)
44     abort ();
45
46   return p + 1;
47 }
48
49 extern "C" void free (void *)
50 {
51 }
52
53 extern "C" void *realloc (void *p, size_t size)
54 {
55   void *r;
56
57   if (p)
58     {
59       object *o = reinterpret_cast<object *>(p) - 1;
60       size_t old_size = o->size;
61
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);
76
77   return r;
78 }
79
80 void fn_throw() throw(int)
81 {
82   throw 1;
83 }
84
85 void fn_rethrow() throw(int)
86 {
87   try{fn_throw();}
88   catch(int a){
89     throw;}
90 }
91
92 void fn_catchthrow() throw(int)
93 {
94   try{fn_throw();}
95   catch(int a){
96     throw a + 1;}
97 }
98
99 int main()
100 {
101   fail = 1;
102
103   try{fn_throw();}
104   catch(int a){}
105
106   try{fn_rethrow();}
107   catch(int a){}
108
109   try{fn_catchthrow();}
110   catch(int a){}
111   
112   return 0;
113 }