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[3-7]* } || vxworks_rtp } } }
6 // Copyright (C) 2000, 2002, 2003, 2010, 2012, 2014 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 // libstdc++ requires a large initialization time allocation for the
16 // emergency EH allocation pool. Add that to the arena size.
18 #if defined(__FreeBSD__) || defined(__sun__) || defined(__hpux__)
19 // FreeBSD, Solaris and HP-UX require even more space at initialization time.
20 // FreeBSD 5 now requires over 131072 bytes.
21 const int arena_size = 262144 + 72 * 1024;
23 // Because pointers make up the bulk of our exception-initialization
24 // allocations, we scale by the pointer size from the original
25 // 32-bit-systems-based estimate.
26 const int arena_size = 32768 * ((sizeof (void *) + 3)/4) + 72 * 1024;
31 size_t size __attribute__((aligned));
34 static char arena[arena_size] __attribute__((aligned));
37 // So we can force a failure when needed.
40 extern "C" void *malloc (size_t size)
42 object *p = reinterpret_cast<object *>(&arena[pos]);
48 size = (size + __alignof__(object) - 1) & - __alignof__(object);
49 pos += size + sizeof(object);
51 // Verify that we didn't run out of memory before getting initialized.
58 extern "C" void free (void *)
62 extern "C" void *realloc (void *p, size_t size)
68 object *o = reinterpret_cast<object *>(p) - 1;
69 size_t old_size = o->size;
79 memcpy (r, p, old_size);
90 #if __cplusplus <= 201402L
91 throw(int) // { dg-warning "deprecated" "" { target { c++11 && { ! c++17 } } } }
98 #if __cplusplus <= 201402L
99 throw(int) // { dg-warning "deprecated" "" { target { c++11 && { ! c++17 } } } }
108 #if __cplusplus <= 201402L
109 throw(int) // { dg-warning "deprecated" "" { target { c++11 && { ! c++17 } } } }
119 /* On some systems (including FreeBSD and Solaris 2.10),
120 __cxa_get_globals will try to call "malloc" when threads are in
121 use. Therefore, we throw one exception up front so that
122 __cxa_get_globals is all set up. Ideally, this would not be
123 necessary, but it is a well-known idiom, and using this technique
124 means that we can still validate the fact that exceptions can be
125 thrown when malloc fails. */
137 try{fn_catchthrow();}