1//===----------------------------------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// UNSUPPORTED: c++98, c++03, c++11, c++14
11
12// asan and msan will not call the new handler.
13// UNSUPPORTED: sanitizer-new-delete
14
15// FIXME turn this into an XFAIL
16// UNSUPPORTED: no-aligned-allocation
17
18// test operator new
19
20#include <new>
21#include <cstddef>
22#include <cassert>
23#include <cstdint>
24#include <limits>
25
26#include "test_macros.h"
27
28constexpr auto OverAligned = alignof(std::max_align_t) * 2;
29
30int new_handler_called = 0;
31
32void my_new_handler()
33{
34    ++new_handler_called;
35    std::set_new_handler(0);
36}
37
38bool A_constructed = false;
39
40struct alignas(OverAligned) A
41{
42    A() {A_constructed = true;}
43    ~A() {A_constructed = false;}
44};
45
46void test_throw_max_size() {
47#ifndef TEST_HAS_NO_EXCEPTIONS
48    std::set_new_handler(my_new_handler);
49    try
50    {
51        void* vp = operator new (std::numeric_limits<std::size_t>::max(),
52                                 static_cast<std::align_val_t>(32));
53        ((void)vp);
54        assert(false);
55    }
56    catch (std::bad_alloc&)
57    {
58        assert(new_handler_called == 1);
59    }
60    catch (...)
61    {
62        assert(false);
63    }
64#endif
65}
66
67int main()
68{
69    {
70        A* ap = new A;
71        assert(ap);
72        assert(reinterpret_cast<std::uintptr_t>(ap) % OverAligned == 0);
73        assert(A_constructed);
74        delete ap;
75        assert(!A_constructed);
76    }
77    {
78        test_throw_max_size();
79    }
80}
81