File: test_allocators.cpp

package info (click to toggle)
onetbb 2022.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 19,440 kB
  • sloc: cpp: 129,228; ansic: 9,745; python: 808; xml: 183; objc: 176; makefile: 66; sh: 66; awk: 41; javascript: 37
file content (116 lines) | stat: -rw-r--r-- 4,754 bytes parent folder | download
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
    Copyright (c) 2005-2025 Intel Corporation

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

#include <tbb/version.h> // For __TBB_GLIBCXX_VERSION and __TBB_CPP20_PRESENT

// Intel LLVM compiler triggers a deprecation warning in the implementation of std::allocator_traits::destroy
// inside Standard Library while using STL PMR containers since std::polymorphic_allocator::destroy is deprecated since C++20
#define TEST_LLVM_COMPILER_PMR_DESTROY_DEPRECATED_BROKEN __INTEL_LLVM_COMPILER >= 20250000 && __INTEL_LLVM_COMPILER <= 20250100 && __TBB_GLIBCXX_VERSION == 110000 && __TBB_CPP20_PRESENT

#if TEST_LLVM_COMPILER_PMR_DESTROY_DEPRECATED_BROKEN
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#endif

#include "tbb/cache_aligned_allocator.h"
#include "tbb/tbb_allocator.h"

// the real body of the test is there:
#include "common/allocator_test_common.h"
#include "common/allocator_stl_test_common.h"

//! \file test_allocators.cpp
//! \brief Test for [memory_allocation.cache_aligned_allocator memory_allocation.tbb_allocator memory_allocation.cache_aligned_resource] specifications

#if TBB_USE_EXCEPTIONS
//! Test that cache_aligned_allocate() throws bad_alloc if cannot allocate memory.
//! \brief \ref requirement
TEST_CASE("Test cache_aligned_allocate throws") {
    #if __APPLE__
        // On macOS*, failure to map memory results in messages to stderr;
        // suppress them.
        DisableStderr disableStderr;
    #endif

    using namespace tbb::detail::r1;

    // First, allocate a reasonably big amount of memory, big enough
    // to not cause warp around in system allocator after adding object header
    // during address2 allocation.
    const size_t itemsize = 1024;
    const size_t nitems   = 1024;
    void *address1 = nullptr;
    try {
        address1 = cache_aligned_allocate(nitems * itemsize);
    } catch(...) {
        // intentionally empty
    }
    REQUIRE_MESSAGE(address1, "cache_aligned_allocate unable to obtain 1024*1024 bytes");

    bool exception_caught = false;
    try {
        // Try allocating more memory than left in the address space; should cause std::bad_alloc
        (void)cache_aligned_allocate(~size_t(0) - itemsize * nitems + cache_line_size());
    } catch (std::bad_alloc&) {
        exception_caught = true;
    } catch (...) {
        REQUIRE_MESSAGE(false, "Unexpected exception type (std::bad_alloc was expected)");
        exception_caught = true;
    }
    REQUIRE_MESSAGE(exception_caught, "cache_aligned_allocate did not throw bad_alloc");

    try {
        cache_aligned_deallocate(address1);
    } catch (...) {
        REQUIRE_MESSAGE(false, "cache_aligned_deallocate did not accept the address obtained with cache_aligned_allocate");
    }
}
#endif /* TBB_USE_EXCEPTIONS */

#if TBB_ALLOCATOR_TRAITS_BROKEN
//! Testing allocator types in case std::allocator traits is broken
//! \brief \ref error_guessing
TEST_CASE("Broken allocator concept") {
    TestAllocator<tbb::cache_aligned_allocator<void>>(Broken);
    TestAllocator<tbb::tbb_allocator<void>>(Broken);
}
#endif

//! Testing allocators compatibility with STL containers
//! \brief \ref interface
TEST_CASE("Test allocators with STL containers") {
    TestAllocatorWithSTL<tbb::cache_aligned_allocator<void>>();
    TestAllocatorWithSTL<tbb::tbb_allocator<void>>();
}

#if __TBB_CPP17_MEMORY_RESOURCE_PRESENT
//! Testing memory resources compatibility with STL containers through the
//! std::pmr::polymorphic_allocator
//! \brief \ref interface
TEST_CASE("polymorphic_allocator test") {
    tbb::cache_aligned_resource aligned_resource;
    tbb::cache_aligned_resource equal_aligned_resource(std::pmr::get_default_resource());
    REQUIRE_MESSAGE(aligned_resource.is_equal(equal_aligned_resource),
            "Underlying upstream resources should be equal.");
    REQUIRE_MESSAGE(!aligned_resource.is_equal(*std::pmr::null_memory_resource()),
            "Cache aligned resource upstream shouldn't be equal to the standard resource.");
    TestAllocatorWithSTL(std::pmr::polymorphic_allocator<void>(&aligned_resource));
}
#endif

#if TEST_LLVM_COMPILER_PMR_DESTROY_DEPRECATED_BROKEN
#pragma clang diagnostic pop // "-Wdeprecated-declarations"
#endif