Debian Codegraph @

gcc/testsuite/g++.dg/init/new45.C

raw
1 // { dg-do compile }
2 // { dg-options "-O1" }
3 // { dg-final { scan-assembler-not "abort" } }
4
5 typedef __SIZE_TYPE__ size_t;
6
7 extern "C" {
8 void abort ();
9 void* malloc (size_t);
10 }
11
12 struct UDClass {
13 static int n;
14 UDClass () { ++n; }
15 virtual ~UDClass () { --n; }
16 };
17
18 int UDClass::n;
19
20 struct POD {
21 char buf [sizeof (UDClass)];
22 };
23
24 enum { N = 123 };
25
26 #if defined (__arm__) && defined (__ARM_EABI__)
27 // On ARM EABI the cookie is always 8 bytes as per Section 3.2.2 of
28 // http://infocenter.arm.com/help/topic/com.arm.doc.ihi0041d/IHI0041D_cppabi.pdf
29 static const size_t cookie_size = 8;
30 #else
31 // On all other targets, the cookie size is the size of size_t
32 // GCC, and ideally the C++ standard, should provide an API to
33 // retrieve this constant.)
34 static const size_t cookie_size = sizeof (size_t);
35 #endif
36
37 inline __attribute__ ((always_inline))
38 void* operator new[] (size_t n)
39 {
40 // Verify that array new is invoked with an argument large enough
41 // for the array and a size_t cookie to store the number of elements.
42 // (This holds for classes with user-defined types but not POD types).
43
44 if (n != N * sizeof (UDClass) + cookie_size) abort ();
45 return malloc (n);
46 }
47
48 inline __attribute__ ((always_inline))
49 void* operator new[] (size_t n, void *p)
50 {
51 // Verify that the default placement array new is invoked with
52 // an argument just large enough for the array (and no cookie),
53 // regardless of whether the type is a POD or class with a user
54 // defined ctor.
55 if (n != N * sizeof (UDClass)) abort ();
56 return p;
57 }
58
59 inline __attribute__ ((always_inline))
60 void* operator new[] (size_t n, POD *p)
61 {
62 // Verify that placement array new overload for a POD type is
63 // invoked with an argument large enough for the array and
64 // a cookie.
65 if (n != N * sizeof (POD)) abort ();
66 return p;
67 }
68
69 inline __attribute__ ((always_inline))
70 void* operator new[] (size_t n, UDClass *p)
71 {
72 // Verify that placement array new overload for a class type with
73 // a user-defined ctor and dtor is invoked with an argument large
74 // enough for the array and a cookie.
75 if (n != N * sizeof (UDClass) + cookie_size) abort ();
76 return p;
77 }
78
79 // UDClassllocate a sufficiently large buffer to construct arrays into.
80 static unsigned char buf [N * N];
81
82 POD* test_new_POD ()
83 {
84 // Avoid testing PODs since for those, the global new is invoked
85 // without the overhead of a cookie.
86 // return new POD [N];
87 return 0;
88 }
89
90 POD* test_default_placement_new_POD ()
91 {
92 // Vefify that no overhead is allocated.
93 return new (buf) POD [N];
94 }
95
96 POD* test_overloaded_placement_new_POD ()
97 {
98 // Vefify that no overhead is allocated.
99 return new ((POD*)buf) POD [N];
100 }
101
102 UDClass* test_new_UDClass ()
103 {
104 // Vefify that space for a cookie is allocated.
105 return new UDClass [N];
106 }
107
108 UDClass* test_default_placement_new_UDClass ()
109 {
110 // Vefify that no overhead is allocated.
111 return new (buf) UDClass [N];
112 }
113
114 UDClass* test_overloaded_placement_new_UDClass ()
115 {
116 // Vefify that space for a cookie is allocated.
117 return new ((UDClass*)buf) UDClass [N];
118 }