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)
40 // Compute the cookie location manually.
41 size_t x = __alignof__ (T);
42 if (x < sizeof (size_t))
44 if ((char *) a - x != (char *) p)
47 // Check the cookie value.
48 size_t *sp = ((size_t *) a) - 1;
54 void check_placement_cookie (int i)
56 p = malloc (sizeof (T) * 11 + 100);
57 void* a = new (p) T[11];
59 // Compute the cookie location manually.
60 size_t x = __alignof__ (T);
61 if (x < sizeof (size_t))
63 if ((char *) a - x != (char *) p)
66 // Check the cookie value.
67 size_t *sp = ((size_t *) a) - 1;
75 struct Y { int i; virtual void f () {}; };
77 // A class with a non-trivial destructor -- it needs a cookie.
78 struct Z { ~Z () {}; };
79 // Likewise, but this class needs a bigger cookie so that the array
80 // elements are correctly aligned.
81 struct Z2 { ~Z2 () {}; long double d; };
83 struct W1 { void operator delete[] (void *, size_t) {}; };
84 struct W2 { void operator delete[] (void *) {};
85 void operator delete[] (void *, size_t) {}; };
86 struct W3 { void operator delete[] (void *, size_t) {};
87 void operator delete[] (void *) {}; };
88 struct W4 : public W1 {};
90 struct V { void *operator new[] (size_t s, void *p)
97 // There should be no cookies for types with trivial destructors.
98 check_no_cookie<int> (1);
99 check_no_cookie<X> (2);
100 check_no_cookie<Y<double> > (3);
102 // There should be no cookies for allocations using global placement
104 check_no_placement_cookie<int> (4);
105 check_no_placement_cookie<X> (5);
106 check_no_placement_cookie<Z> (6);
108 // There should be a cookie when using a non-trivial destructor.
110 check_cookie<Z2> (8);
112 // There should be a cookie when using the two-argument array delete
114 check_cookie<W1> (9);
115 check_cookie<W4> (10);
116 // But not when the one-argument version is also available.
117 check_no_cookie<W2> (11);
118 check_no_cookie<W3> (12);
120 // There should be a cookie when using a non-global placement new.
121 check_placement_cookie<V> (13);
124 #else /* !(defined (__GXX_ABI_VERSION) && __GXX_ABI_VERSION >= 100) */
130 #endif /* !(defined (__GXX_ABI_VERSION) && __GXX_ABI_VERSION >= 100) */