Debian Codegraph @

libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size.pass.cpp

raw
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);
10
11 // asan and msan will not call the new handler.
12 // UNSUPPORTED: sanitizer-new-delete
13
14 #include <new>
15 #include <cstddef>
16 #include <cassert>
17 #include <limits>
18
19 #include "test_macros.h"
20 #include "../types.h"
21
22 int new_handler_called = 0;
23
24 void my_new_handler() {
25 ++new_handler_called;
26 std::set_new_handler(nullptr);
27 }
28
29 int main(int, char**) {
30 // Test that we can call the function directly
31 {
32 void* x = operator new(10);
33 assert(x != nullptr);
34 operator delete(x);
35 }
36
37 // Test that the new handler is called if allocation fails
38 {
39 #ifndef TEST_HAS_NO_EXCEPTIONS
40 std::set_new_handler(my_new_handler);
41 try {
42 void* x = operator new(std::numeric_limits<std::size_t>::max());
43 (void)x;
44 assert(false);
45 } catch (std::bad_alloc const&) {
46 assert(new_handler_called == 1);
47 } catch (...) {
48 assert(false);
49 }
50 #endif
51 }
52
53 // Test that a new expression constructs the right object
54 // and a delete expression deletes it
55 {
56 LifetimeInformation info;
57 TrackLifetime* x = new TrackLifetime(info);
58 assert(x != nullptr);
59 assert(info.address_constructed == x);
60
61 delete x;
62 assert(info.address_destroyed == x);
63 }
64
65 return 0;
66 }