1 // { dg-do run { xfail xstormy16-*-* *-*-darwin[1-7]* } }
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).
15 const int arena_size = 256;
17 #if defined(__FreeBSD__) || defined(__sun__) || defined(__hpux__)
18 // FreeBSD, Solaris and HP-UX with threads require even more
19 // space at initialization time. FreeBSD 5 now requires over 131072 bytes.
20 const int arena_size = 262144;
22 const int arena_size = 32768;
28 size_t size __attribute__((aligned));
31 static char arena[arena_size] __attribute__((aligned));
34 // So we can force a failure when needed.
37 extern "C" void *malloc (size_t size)
39 object *p = reinterpret_cast<object *>(&arena[pos]);
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.
55 extern "C" void free (void *)
59 extern "C" void *realloc (void *p, size_t size)
65 object *o = reinterpret_cast<object *>(p) - 1;
66 size_t old_size = o->size;
76 memcpy (r, p, old_size);
86 void fn_throw() throw(int)
91 void fn_rethrow() throw(int)
98 void fn_catchthrow() throw(int)
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. */
125 try{fn_catchthrow();}