diff --git a/include/gridtools/sid/allocator.hpp b/include/gridtools/sid/allocator.hpp index 08280e12e..6cc197a50 100644 --- a/include/gridtools/sid/allocator.hpp +++ b/include/gridtools/sid/allocator.hpp @@ -122,7 +122,6 @@ namespace gridtools { template friend auto allocate(allocator &self, LazyT, size_t size) { using type = typename LazyT::type; - auto ptr = self.m_impl(sizeof(type) * size); self.m_buffers.push_back(self.m_impl(sizeof(type) * size)); return simple_ptr_holder(reinterpret_cast(self.m_buffers.back().get())); } diff --git a/tests/unit_tests/sid/CMakeLists.txt b/tests/unit_tests/sid/CMakeLists.txt index f9cb03cec..3cbf67e6e 100644 --- a/tests/unit_tests/sid/CMakeLists.txt +++ b/tests/unit_tests/sid/CMakeLists.txt @@ -1,3 +1,4 @@ +gridtools_add_unit_test(test_sid_allocator SOURCES test_sid_allocator.cpp) gridtools_add_unit_test(test_sid_as_const SOURCES test_sid_as_const.cpp) gridtools_add_unit_test(test_sid_block SOURCES test_sid_block.cpp) gridtools_add_unit_test(test_sid_composite SOURCES test_sid_composite.cpp) diff --git a/tests/unit_tests/sid/test_sid_allocator.cpp b/tests/unit_tests/sid/test_sid_allocator.cpp new file mode 100644 index 000000000..7def9b5a4 --- /dev/null +++ b/tests/unit_tests/sid/test_sid_allocator.cpp @@ -0,0 +1,37 @@ +/* + * GridTools + * + * Copyright (c) 2014-2023, ETH Zurich + * All rights reserved. + * + * Please, refer to the LICENSE file in the root directory. + * SPDX-License-Identifier: BSD-3-Clause + */ + +#include + +#include +#include + +#include + +#include + +namespace gridtools { + namespace { + + TEST(allocator, allocates_once_per_request) { + int count = 0; + auto alloc = sid::allocator([&count](size_t size) { + ++count; + return std::make_unique(size); + }); + + allocate(alloc, meta::lazy::id{}, 3); + EXPECT_EQ(1, count); + + allocate(alloc, meta::lazy::id{}, 3); + EXPECT_EQ(2, count); + } + } // namespace +} // namespace gridtools