151c0b2f7Stbbdev /* 2*b15aabb3Stbbdev Copyright (c) 2005-2021 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 //! \file test_malloc_new_handler.cpp 1851c0b2f7Stbbdev //! \brief Test for [memory_allocation] functionality 1951c0b2f7Stbbdev 2051c0b2f7Stbbdev #define __TBB_NO_IMPLICIT_LINKAGE 1 2151c0b2f7Stbbdev 2251c0b2f7Stbbdev #include "common/test.h" 2351c0b2f7Stbbdev #include "common/utils.h" 2451c0b2f7Stbbdev 2551c0b2f7Stbbdev #include "common/allocator_overload.h" 2651c0b2f7Stbbdev 2751c0b2f7Stbbdev #if !HARNESS_SKIP_TEST && TBB_USE_EXCEPTIONS 2851c0b2f7Stbbdev 2951c0b2f7Stbbdev #include "../../src/tbb/tls.h" 3051c0b2f7Stbbdev 3151c0b2f7Stbbdev tbb::detail::r1::tls<bool> new_handler_called; 3251c0b2f7Stbbdev void customNewHandler() { 3351c0b2f7Stbbdev new_handler_called = true; 3451c0b2f7Stbbdev throw std::bad_alloc(); 3551c0b2f7Stbbdev } 3651c0b2f7Stbbdev 3751c0b2f7Stbbdev // Return true if operator new threw exception 3851c0b2f7Stbbdev bool allocateWithException(size_t big_mem) { 3951c0b2f7Stbbdev bool exception_caught = false; 4051c0b2f7Stbbdev try { 4151c0b2f7Stbbdev // Allocate big array (should throw exception) 4251c0b2f7Stbbdev char* volatile big_array = new char[big_mem]; 4351c0b2f7Stbbdev // If succeeded, double the size (unless it overflows) and recursively retry 4451c0b2f7Stbbdev if (big_mem * 2 > big_mem) { 4551c0b2f7Stbbdev exception_caught = allocateWithException(big_mem * 2); 4651c0b2f7Stbbdev } 4751c0b2f7Stbbdev delete[] big_array; 4851c0b2f7Stbbdev } catch (const std::bad_alloc&) { 4951c0b2f7Stbbdev bool is_called = new_handler_called; 5051c0b2f7Stbbdev REQUIRE_MESSAGE(is_called, "User provided new_handler was not called."); 5151c0b2f7Stbbdev exception_caught = true; 5251c0b2f7Stbbdev } 5351c0b2f7Stbbdev return exception_caught; 5451c0b2f7Stbbdev } 5551c0b2f7Stbbdev 5651c0b2f7Stbbdev class AllocLoopBody : utils::NoAssign { 5751c0b2f7Stbbdev public: 5851c0b2f7Stbbdev void operator()(int) const { 5951c0b2f7Stbbdev size_t BIG_MEM = 100 * 1024 * 1024; 6051c0b2f7Stbbdev new_handler_called = false; 6151c0b2f7Stbbdev REQUIRE_MESSAGE(allocateWithException(BIG_MEM), "Operator new did not throw bad_alloc."); 6251c0b2f7Stbbdev } 6351c0b2f7Stbbdev }; 6451c0b2f7Stbbdev 6551c0b2f7Stbbdev //! \brief \ref error_guessing 6651c0b2f7Stbbdev TEST_CASE("New handler callback") { 6751c0b2f7Stbbdev #if __TBB_CPP11_GET_NEW_HANDLER_PRESENT 6851c0b2f7Stbbdev std::new_handler default_handler = std::get_new_handler(); 6951c0b2f7Stbbdev REQUIRE_MESSAGE(default_handler == nullptr, "No handler should be set at this point."); 7051c0b2f7Stbbdev #endif 7151c0b2f7Stbbdev // Define the handler for new operations 7251c0b2f7Stbbdev std::set_new_handler(customNewHandler); 7351c0b2f7Stbbdev // Run the test 7451c0b2f7Stbbdev utils::NativeParallelFor(8, AllocLoopBody()); 7551c0b2f7Stbbdev // Undo custom handler 7651c0b2f7Stbbdev std::set_new_handler(0); 7751c0b2f7Stbbdev } 7851c0b2f7Stbbdev #endif // !HARNESS_SKIP_TEST && TBB_USE_EXCEPTIONS 79