1 | //===----------------------------------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | // void* operator new[](std::size_t, std::align_val_t); |
10 | |
11 | // UNSUPPORTED: c++03, c++11, c++14 |
12 | |
13 | // asan and msan will not call the new handler. |
14 | // UNSUPPORTED: sanitizer-new-delete |
15 | |
16 | // Libc++ when built for z/OS doesn't contain the aligned allocation functions, |
17 | // nor does the dynamic library shipped with z/OS. |
18 | // XFAIL: target={{.+}}-zos{{.*}} |
19 | |
20 | #include <new> |
21 | #include <cstddef> |
22 | #include <cassert> |
23 | #include <cstdint> |
24 | #include <limits> |
25 | |
26 | #include "test_macros.h" |
27 | #include "../types.h" |
28 | |
29 | int new_handler_called = 0; |
30 | |
31 | void my_new_handler() { |
32 | ++new_handler_called; |
33 | std::set_new_handler(nullptr); |
34 | } |
35 | |
36 | int main(int, char**) { |
37 | // Test that we can call the function directly |
38 | { |
39 | void* x = operator new[](10, static_cast<std::align_val_t>(64)); |
40 | assert(x != nullptr); |
41 | assert(reinterpret_cast<std::uintptr_t>(x) % 64 == 0); |
42 | operator delete[](x, static_cast<std::align_val_t>(64)); |
43 | } |
44 | |
45 | // Test that the new handler is called if allocation fails |
46 | { |
47 | #ifndef TEST_HAS_NO_EXCEPTIONS |
48 | std::set_new_handler(my_new_handler); |
49 | try { |
50 | void* x = operator new[] (std::numeric_limits<std::size_t>::max(), |
51 | static_cast<std::align_val_t>(32)); |
52 | (void)x; |
53 | assert(false); |
54 | } catch (std::bad_alloc const&) { |
55 | assert(new_handler_called == 1); |
56 | } catch (...) { |
57 | assert(false); |
58 | } |
59 | #endif |
60 | } |
61 | |
62 | // Test that a new expression constructs the right object |
63 | // and a delete expression deletes it |
64 | { |
65 | LifetimeInformation infos[3]; |
66 | TrackLifetimeOverAligned* x = new TrackLifetimeOverAligned[3]{infos[0], infos[1], infos[2]}; |
67 | assert(x != nullptr); |
68 | assert(reinterpret_cast<std::uintptr_t>(x) % alignof(TrackLifetimeOverAligned) == 0); |
69 | |
70 | void* addresses[3] = {&x[0], &x[1], &x[2]}; |
71 | assert(infos[0].address_constructed == addresses[0]); |
72 | assert(infos[1].address_constructed == addresses[1]); |
73 | assert(infos[2].address_constructed == addresses[2]); |
74 | |
75 | delete[] x; |
76 | assert(infos[0].address_destroyed == addresses[0]); |
77 | assert(infos[1].address_destroyed == addresses[1]); |
78 | assert(infos[2].address_destroyed == addresses[2]); |
79 | } |
80 | |
81 | return 0; |
82 | } |
83 | |