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
29int new_handler_called = 0;
30
31void my_new_handler() {
32 ++new_handler_called;
33 std::set_new_handler(nullptr);
34}
35
36int 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 info;
66 TrackLifetimeOverAligned* x = new TrackLifetimeOverAligned(info);
67 assert(x != nullptr);
68 assert(reinterpret_cast<std::uintptr_t>(x) % alignof(TrackLifetimeOverAligned) == 0);
69 assert(info.address_constructed == x);
70
71 delete x;
72 assert(info.address_destroyed == x);
73 }
74
75 return 0;
76}
77

source code of libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align.pass.cpp