aboutsummaryrefslogtreecommitdiff
path: root/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align.replace.indirect.pass.cpp
blob: 66cbb4b9c8eb614a869a26775f1979186f349203 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// void* operator new[](std::size_t, std::align_val_t);

// Test that we can replace the operator by replacing `operator new(std::size_t, std::align_val_t)` (the non-array version).

// UNSUPPORTED: c++03, c++11, c++14
// UNSUPPORTED: sanitizer-new-delete

// XFAIL: LIBCXX-AIX-FIXME

// Libc++ when built for z/OS doesn't contain the aligned allocation functions,
// nor does the dynamic library shipped with z/OS.
// UNSUPPORTED: target={{.+}}-zos{{.*}}

#include <new>
#include <cstddef>
#include <cstdlib>
#include <cstdint>
#include <cassert>
#include <limits>

#include "test_macros.h"
#include "../types.h"

int new_called = 0;
int delete_called = 0;

alignas(OverAligned) char DummyData[alignof(OverAligned) * 3];

TEST_WORKAROUND_BUG_109234844_WEAK
void* operator new(std::size_t s, std::align_val_t a) {
    assert(s <= sizeof(DummyData));
    assert(static_cast<std::size_t>(a) == alignof(OverAligned));
    ++new_called;
    return DummyData;
}

void operator delete(void*, std::align_val_t) noexcept {
    ++delete_called;
    // nothing to delete, we didn't actually allocate in `operator new`
}

int main(int, char**) {
    // Test with an overaligned type
    {
        new_called = delete_called = 0;
        OverAligned* x = DoNotOptimize(new OverAligned[3]);
        ASSERT_WITH_OPERATOR_NEW_FALLBACKS(static_cast<void*>(x) == DummyData);
        ASSERT_WITH_OPERATOR_NEW_FALLBACKS(new_called == 1);

        delete[] x;
        ASSERT_WITH_OPERATOR_NEW_FALLBACKS(delete_called == 1);
    }

    // Test with a type that is right on the verge of being overaligned
    {
        new_called = delete_called = 0;
        MaxAligned* x = DoNotOptimize(new MaxAligned[3]);
        assert(x != nullptr);
        assert(new_called == 0);

        delete[] x;
        assert(delete_called == 0);
    }

    // Test with a type that is clearly not overaligned
    {
        new_called = delete_called = 0;
        int* x = DoNotOptimize(new int[3]);
        assert(x != nullptr);
        assert(new_called == 0);

        delete[] x;
        assert(delete_called == 0);
    }

    return 0;
}