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// UNSUPPORTED: sanitizer-new-delete
12
13// XFAIL: no-aligned-allocation
14
15// test operator new replacement
16
17#include <new>
18#include <cstddef>
19#include <cstdlib>
20#include <cstdint>
21#include <cassert>
22#include <limits>
23
24#include "test_macros.h"
25
26constexpr auto OverAligned = alignof(std::max_align_t) * 2;
27
28bool A_constructed = false;
29
30struct alignas(OverAligned) A {
31    A() {A_constructed = true;}
32    ~A() {A_constructed = false;}
33};
34
35
36bool B_constructed = false;
37
38struct alignas(std::max_align_t) B
39{
40    std::max_align_t member;
41    B() {B_constructed = true;}
42    ~B() {B_constructed = false;}
43};
44
45int new_called = 0;
46
47alignas(OverAligned) char DummyData[OverAligned];
48
49void* operator new(std::size_t s, std::align_val_t a) TEST_THROW_SPEC(std::bad_alloc)
50{
51    assert(new_called == 0); // We already allocated
52    assert(s <= sizeof(DummyData));
53    assert(static_cast<std::size_t>(a) == OverAligned);
54    ++new_called;
55    return DummyData;
56}
57
58void  operator delete(void* p, std::align_val_t) TEST_NOEXCEPT
59{
60    assert(new_called == 1);
61    --new_called;
62    assert(p == DummyData);
63}
64
65
66int main()
67{
68    {
69        A* ap = new A;
70        assert(ap);
71        assert(A_constructed);
72        assert(new_called);
73        delete ap;
74        assert(!A_constructed);
75        assert(!new_called);
76    }
77    {
78        B* bp = new B;
79        assert(bp);
80        assert(B_constructed);
81        assert(!new_called);
82        delete bp;
83        assert(!new_called);
84    }
85}
86