| 1 |
// { dg-do run } |
| 2 |
// Origin: Mark Mitchell <[email protected]> |
| 3 |
// Avoid use of none-overridable new/delete operators in shared |
| 4 |
// { dg-options "-static" { target *-*-mingw* } } |
| 5 |
/* { dg-skip-if "requires hosted libstdc++ for cstdlib size_t" { ! hostedlib } } */ |
| 6 |
|
| 7 |
#if defined (__GXX_ABI_VERSION) && __GXX_ABI_VERSION >= 100 |
| 8 |
|
| 9 |
#include <cstdlib> |
| 10 |
#include <new> |
| 11 |
|
| 12 |
void* p; |
| 13 |
|
| 14 |
void* operator new[](size_t s) |
| 15 |
#if __cplusplus <= 199711L |
| 16 |
throw (std::bad_alloc) |
| 17 |
#endif |
| 18 |
{ |
| 19 |
// Record the base of the last array allocated. |
| 20 |
p = malloc (s); |
| 21 |
return p; |
| 22 |
} |
| 23 |
|
| 24 |
template <typename T> |
| 25 |
void check_no_cookie (int i) |
| 26 |
{ |
| 27 |
void* a = new T[7]; |
| 28 |
if (p != a) |
| 29 |
exit (i); |
| 30 |
} |
| 31 |
|
| 32 |
template <typename T> |
| 33 |
void check_no_placement_cookie (int i) |
| 34 |
{ |
| 35 |
p = malloc (13 * sizeof (T)); |
| 36 |
void* a = new (p) T[13]; |
| 37 |
if (p != a) |
| 38 |
exit (i); |
| 39 |
} |
| 40 |
|
| 41 |
template <typename T> |
| 42 |
void check_cookie (int i) |
| 43 |
{ |
| 44 |
void* a = new T[11]; |
| 45 |
size_t x; |
| 46 |
|
| 47 |
// Compute the cookie location manually. |
| 48 |
#ifdef __ARM_EABI__ |
| 49 |
x = 8; |
| 50 |
#else |
| 51 |
x = __alignof__ (T); |
| 52 |
if (x < sizeof (size_t)) |
| 53 |
x = sizeof (size_t); |
| 54 |
#endif |
| 55 |
if ((char *) a - x != (char *) p) |
| 56 |
exit (i); |
| 57 |
|
| 58 |
// Check the cookie value. |
| 59 |
size_t *sp = ((size_t *) a) - 1; |
| 60 |
if (*sp != 11) |
| 61 |
exit (i); |
| 62 |
|
| 63 |
#ifdef __ARM_EABI__ |
| 64 |
sp = ((size_t *) a) - 2; |
| 65 |
if (*sp != sizeof (T)) |
| 66 |
exit (i); |
| 67 |
#endif |
| 68 |
} |
| 69 |
|
| 70 |
template <typename T> |
| 71 |
void check_placement_cookie (int i) |
| 72 |
{ |
| 73 |
p = malloc (sizeof (T) * 11 + 100); |
| 74 |
void* a = new (p) T[11]; |
| 75 |
size_t x; |
| 76 |
|
| 77 |
// Compute the cookie location manually. |
| 78 |
#ifdef __ARM_EABI__ |
| 79 |
x = 8; |
| 80 |
#else |
| 81 |
x = __alignof__ (T); |
| 82 |
if (x < sizeof (size_t)) |
| 83 |
x = sizeof (size_t); |
| 84 |
#endif |
| 85 |
if ((char *) a - x != (char *) p) |
| 86 |
exit (i); |
| 87 |
|
| 88 |
// Check the cookie value. |
| 89 |
size_t *sp = ((size_t *) a) - 1; |
| 90 |
if (*sp != 11) |
| 91 |
exit (i); |
| 92 |
|
| 93 |
#ifdef __ARM_EABI__ |
| 94 |
sp = ((size_t *) a) - 2; |
| 95 |
if (*sp != sizeof (T)) |
| 96 |
exit (i); |
| 97 |
#endif |
| 98 |
} |
| 99 |
|
| 100 |
struct X {}; |
| 101 |
|
| 102 |
template <typename T> |
| 103 |
struct Y { int i; virtual void f () {} }; |
| 104 |
|
| 105 |
// A class with a non-trivial destructor -- it needs a cookie. |
| 106 |
struct Z { ~Z () {} }; |
| 107 |
// Likewise, but this class needs a bigger cookie so that the array |
| 108 |
// elements are correctly aligned. |
| 109 |
struct Z2 { ~Z2 () {} long double d; }; |
| 110 |
|
| 111 |
struct W1 { void operator delete[] (void *, size_t) {} }; |
| 112 |
struct W2 { void operator delete[] (void *) {} |
| 113 |
void operator delete[] (void *, size_t) {} }; |
| 114 |
struct W3 { void operator delete[] (void *, size_t) {} |
| 115 |
void operator delete[] (void *) {} }; |
| 116 |
struct W4 : public W1 {}; |
| 117 |
|
| 118 |
struct V { void *operator new[] (size_t s, void *p) |
| 119 |
{ return p; } |
| 120 |
~V () {} |
| 121 |
}; |
| 122 |
|
| 123 |
int main () |
| 124 |
{ |
| 125 |
// There should be no cookies for types with trivial destructors. |
| 126 |
check_no_cookie<int> (1); |
| 127 |
check_no_cookie<X> (2); |
| 128 |
check_no_cookie<Y<double> > (3); |
| 129 |
|
| 130 |
// There should be no cookies for allocations using global placement |
| 131 |
// new. |
| 132 |
check_no_placement_cookie<int> (4); |
| 133 |
check_no_placement_cookie<X> (5); |
| 134 |
check_no_placement_cookie<Z> (6); |
| 135 |
|
| 136 |
// There should be a cookie when using a non-trivial destructor. |
| 137 |
check_cookie<Z> (7); |
| 138 |
check_cookie<Z2> (8); |
| 139 |
|
| 140 |
// There should be a cookie when using the two-argument array delete |
| 141 |
// operator. |
| 142 |
check_cookie<W1> (9); |
| 143 |
check_cookie<W4> (10); |
| 144 |
// But not when the one-argument version is also available. |
| 145 |
check_no_cookie<W2> (11); |
| 146 |
check_no_cookie<W3> (12); |
| 147 |
|
| 148 |
// There should be a cookie when using a non-global placement new. |
| 149 |
check_placement_cookie<V> (13); |
| 150 |
} |
| 151 |
|
| 152 |
#else /* !(defined (__GXX_ABI_VERSION) && __GXX_ABI_VERSION >= 100) */ |
| 153 |
|
| 154 |
int main () |
| 155 |
{ |
| 156 |
} |
| 157 |
|
| 158 |
#endif /* !(defined (__GXX_ABI_VERSION) && __GXX_ABI_VERSION >= 100) */ |