xref: /oneTBB/test/tbb/test_allocators.cpp (revision c21e688a)
151c0b2f7Stbbdev /*
2*c21e688aSSergey Zheltov     Copyright (c) 2005-2022 Intel Corporation
351c0b2f7Stbbdev 
451c0b2f7Stbbdev     Licensed under the Apache License, Version 2.0 (the "License");
551c0b2f7Stbbdev     you may not use this file except in compliance with the License.
651c0b2f7Stbbdev     You may obtain a copy of the License at
751c0b2f7Stbbdev 
851c0b2f7Stbbdev         http://www.apache.org/licenses/LICENSE-2.0
951c0b2f7Stbbdev 
1051c0b2f7Stbbdev     Unless required by applicable law or agreed to in writing, software
1151c0b2f7Stbbdev     distributed under the License is distributed on an "AS IS" BASIS,
1251c0b2f7Stbbdev     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1351c0b2f7Stbbdev     See the License for the specific language governing permissions and
1451c0b2f7Stbbdev     limitations under the License.
1551c0b2f7Stbbdev */
1651c0b2f7Stbbdev 
1751c0b2f7Stbbdev #include "tbb/cache_aligned_allocator.h"
1851c0b2f7Stbbdev #include "tbb/tbb_allocator.h"
1951c0b2f7Stbbdev 
2051c0b2f7Stbbdev // the real body of the test is there:
2151c0b2f7Stbbdev #include "common/allocator_test_common.h"
2251c0b2f7Stbbdev #include "common/allocator_stl_test_common.h"
2351c0b2f7Stbbdev 
2451c0b2f7Stbbdev //! \file test_allocators.cpp
2551c0b2f7Stbbdev //! \brief Test for [memory_allocation.cache_aligned_allocator memory_allocation.tbb_allocator memory_allocation.cache_aligned_resource] specifications
2651c0b2f7Stbbdev 
2751c0b2f7Stbbdev #if TBB_USE_EXCEPTIONS
2851c0b2f7Stbbdev //! Test that cache_aligned_allocate() throws bad_alloc if cannot allocate memory.
2951c0b2f7Stbbdev //! \brief \ref requirement
3051c0b2f7Stbbdev TEST_CASE("Test cache_aligned_allocate throws") {
3151c0b2f7Stbbdev     #if __APPLE__
3251c0b2f7Stbbdev         // On macOS*, failure to map memory results in messages to stderr;
3351c0b2f7Stbbdev         // suppress them.
3451c0b2f7Stbbdev         DisableStderr disableStderr;
3551c0b2f7Stbbdev     #endif
3651c0b2f7Stbbdev 
3751c0b2f7Stbbdev     using namespace tbb::detail::r1;
3851c0b2f7Stbbdev 
3951c0b2f7Stbbdev     // First, allocate a reasonably big amount of memory, big enough
4051c0b2f7Stbbdev     // to not cause warp around in system allocator after adding object header
4151c0b2f7Stbbdev     // during address2 allocation.
4251c0b2f7Stbbdev     const size_t itemsize = 1024;
4351c0b2f7Stbbdev     const size_t nitems   = 1024;
4457f524caSIlya Isaev     void *address1 = nullptr;
4551c0b2f7Stbbdev     try {
4651c0b2f7Stbbdev         address1 = cache_aligned_allocate(nitems * itemsize);
4751c0b2f7Stbbdev     } catch(...) {
4851c0b2f7Stbbdev         // intentionally empty
4951c0b2f7Stbbdev     }
5051c0b2f7Stbbdev     REQUIRE_MESSAGE(address1, "cache_aligned_allocate unable to obtain 1024*1024 bytes");
5151c0b2f7Stbbdev 
5251c0b2f7Stbbdev     bool exception_caught = false;
5351c0b2f7Stbbdev     try {
5451c0b2f7Stbbdev         // Try allocating more memory than left in the address space; should cause std::bad_alloc
5551c0b2f7Stbbdev         (void)cache_aligned_allocate(~size_t(0) - itemsize * nitems + cache_line_size());
5651c0b2f7Stbbdev     } catch (std::bad_alloc&) {
5751c0b2f7Stbbdev         exception_caught = true;
5851c0b2f7Stbbdev     } catch (...) {
5951c0b2f7Stbbdev         REQUIRE_MESSAGE(false, "Unexpected exception type (std::bad_alloc was expected)");
6051c0b2f7Stbbdev         exception_caught = true;
6151c0b2f7Stbbdev     }
6251c0b2f7Stbbdev     REQUIRE_MESSAGE(exception_caught, "cache_aligned_allocate did not throw bad_alloc");
6351c0b2f7Stbbdev 
6451c0b2f7Stbbdev     try {
6551c0b2f7Stbbdev         cache_aligned_deallocate(address1);
6651c0b2f7Stbbdev     } catch (...) {
6751c0b2f7Stbbdev         REQUIRE_MESSAGE(false, "cache_aligned_deallocate did not accept the address obtained with cache_aligned_allocate");
6851c0b2f7Stbbdev     }
6951c0b2f7Stbbdev }
7051c0b2f7Stbbdev #endif /* TBB_USE_EXCEPTIONS */
7151c0b2f7Stbbdev 
7251c0b2f7Stbbdev #if TBB_ALLOCATOR_TRAITS_BROKEN
7351c0b2f7Stbbdev //! Testing allocator types in case std::allocator traits is broken
7451c0b2f7Stbbdev //! \brief \ref error_guessing
7551c0b2f7Stbbdev TEST_CASE("Broken allocator concept") {
7651c0b2f7Stbbdev     TestAllocator<tbb::cache_aligned_allocator<void>>(Broken);
7751c0b2f7Stbbdev     TestAllocator<tbb::tbb_allocator<void>>(Broken);
7851c0b2f7Stbbdev }
7951c0b2f7Stbbdev #endif
8051c0b2f7Stbbdev 
8151c0b2f7Stbbdev //! Testing allocators compatibility with STL containers
8251c0b2f7Stbbdev //! \brief \ref interface
8351c0b2f7Stbbdev TEST_CASE("Test allocators with STL containers") {
8451c0b2f7Stbbdev     TestAllocatorWithSTL<tbb::cache_aligned_allocator<void>>();
8551c0b2f7Stbbdev     TestAllocatorWithSTL<tbb::tbb_allocator<void>>();
8651c0b2f7Stbbdev }
8751c0b2f7Stbbdev 
8851c0b2f7Stbbdev #if __TBB_CPP17_MEMORY_RESOURCE_PRESENT
8951c0b2f7Stbbdev //! Testing memory resources compatibility with STL containers through the
9051c0b2f7Stbbdev //! std::pmr::polymorphic_allocator
9151c0b2f7Stbbdev //! \brief \ref interface
9251c0b2f7Stbbdev TEST_CASE("polymorphic_allocator test") {
9351c0b2f7Stbbdev     tbb::cache_aligned_resource aligned_resource;
9451c0b2f7Stbbdev     tbb::cache_aligned_resource equal_aligned_resource(std::pmr::get_default_resource());
9551c0b2f7Stbbdev     REQUIRE_MESSAGE(aligned_resource.is_equal(equal_aligned_resource),
9651c0b2f7Stbbdev             "Underlying upstream resources should be equal.");
9751c0b2f7Stbbdev     REQUIRE_MESSAGE(!aligned_resource.is_equal(*std::pmr::null_memory_resource()),
9851c0b2f7Stbbdev             "Cache aligned resource upstream shouldn't be equal to the standard resource.");
9951c0b2f7Stbbdev     TestAllocatorWithSTL(std::pmr::polymorphic_allocator<void>(&aligned_resource));
10051c0b2f7Stbbdev }
10151c0b2f7Stbbdev #endif
10251c0b2f7Stbbdev 
103