2 // Origin: Mark Mitchell <mark@codesourcery.com>
4 #if defined (__GXX_ABI_VERSION) && __GXX_ABI_VERSION >= 100
11 void* operator new[](size_t s) throw (std::bad_alloc)
13 // Record the base of the last array allocated.
19 void check_no_cookie (int i)
27 void check_no_placement_cookie (int i)
29 p = malloc (13 * sizeof (T));
30 void* a = new (p) T[13];
36 void check_cookie (int i)
41 // Compute the cookie location manually.
46 if (x < sizeof (size_t))
49 if ((char *) a - x != (char *) p)
52 // Check the cookie value.
53 size_t *sp = ((size_t *) a) - 1;
58 sp = ((size_t *) a) - 2;
59 if (*sp != sizeof (T))
65 void check_placement_cookie (int i)
67 p = malloc (sizeof (T) * 11 + 100);
68 void* a = new (p) T[11];
71 // Compute the cookie location manually.
76 if (x < sizeof (size_t))
79 if ((char *) a - x != (char *) p)
82 // Check the cookie value.
83 size_t *sp = ((size_t *) a) - 1;
88 sp = ((size_t *) a) - 2;
89 if (*sp != sizeof (T))
97 struct Y { int i; virtual void f () {}; };
99 // A class with a non-trivial destructor -- it needs a cookie.
100 struct Z { ~Z () {}; };
101 // Likewise, but this class needs a bigger cookie so that the array
102 // elements are correctly aligned.
103 struct Z2 { ~Z2 () {}; long double d; };
105 struct W1 { void operator delete[] (void *, size_t) {}; };
106 struct W2 { void operator delete[] (void *) {};
107 void operator delete[] (void *, size_t) {}; };
108 struct W3 { void operator delete[] (void *, size_t) {};
109 void operator delete[] (void *) {}; };
110 struct W4 : public W1 {};
112 struct V { void *operator new[] (size_t s, void *p)
119 // There should be no cookies for types with trivial destructors.
120 check_no_cookie<int> (1);
121 check_no_cookie<X> (2);
122 check_no_cookie<Y<double> > (3);
124 // There should be no cookies for allocations using global placement
126 check_no_placement_cookie<int> (4);
127 check_no_placement_cookie<X> (5);
128 check_no_placement_cookie<Z> (6);
130 // There should be a cookie when using a non-trivial destructor.
132 check_cookie<Z2> (8);
134 // There should be a cookie when using the two-argument array delete
136 check_cookie<W1> (9);
137 check_cookie<W4> (10);
138 // But not when the one-argument version is also available.
139 check_no_cookie<W2> (11);
140 check_no_cookie<W3> (12);
142 // There should be a cookie when using a non-global placement new.
143 check_placement_cookie<V> (13);
146 #else /* !(defined (__GXX_ABI_VERSION) && __GXX_ABI_VERSION >= 100) */
152 #endif /* !(defined (__GXX_ABI_VERSION) && __GXX_ABI_VERSION >= 100) */