1 // This fails for VxWorks RTPs because the initialization of
2 // __cxa_allocate_exception's emergency buffer mutex will
3 // itself call malloc(), and will fail if there is no more
5 // { dg-do run { xfail { { xstormy16-*-* *-*-darwin[1-7]* } || vxworks_rtp } } }
6 // Copyright (C) 2000, 2002, 2003 Free Software Foundation, Inc.
7 // Contributed by Nathan Sidwell 6 June 2000 <nathan@codesourcery.com>
9 // Check we can throw a bad_alloc exception when malloc dies.
11 typedef __SIZE_TYPE__ size_t;
12 extern "C" void abort();
13 extern "C" void *memcpy(void *, const void *, size_t);
15 // Assume that STACK_SIZE defined implies a system that does not have a
16 // large data space either, and additionally that we're not linking against
17 // a shared libstdc++ (which requires quite a bit more initialization space).
19 const int arena_size = 256;
21 #if defined(__FreeBSD__) || defined(__sun__) || defined(__hpux__)
22 // FreeBSD, Solaris and HP-UX with threads require even more
23 // space at initialization time. FreeBSD 5 now requires over 131072 bytes.
24 const int arena_size = 262144;
26 const int arena_size = 32768;
32 size_t size __attribute__((aligned));
35 static char arena[arena_size] __attribute__((aligned));
38 // So we can force a failure when needed.
41 extern "C" void *malloc (size_t size)
43 object *p = reinterpret_cast<object *>(&arena[pos]);
49 size = (size + __alignof__(object) - 1) & - __alignof__(object);
50 pos += size + sizeof(object);
52 // Verify that we didn't run out of memory before getting initialized.
59 extern "C" void free (void *)
63 extern "C" void *realloc (void *p, size_t size)
69 object *o = reinterpret_cast<object *>(p) - 1;
70 size_t old_size = o->size;
80 memcpy (r, p, old_size);
90 void fn_throw() throw(int)
95 void fn_rethrow() throw(int)
102 void fn_catchthrow() throw(int)
111 /* On some systems (including FreeBSD and Solaris 2.10),
112 __cxa_get_globals will try to call "malloc" when threads are in
113 use. Therefore, we throw one exception up front so that
114 __cxa_get_globals is all set up. Ideally, this would not be
115 necessary, but it is a well-known idiom, and using this technique
116 means that we can still validate the fact that exceptions can be
117 thrown when malloc fails. */
129 try{fn_catchthrow();}