From 6b6d5b7c5ae0465639bfe72cc3312fcc6dd92efe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Gonz=C3=A1lez=20Paredes?= Date: Fri, 27 Nov 2026 07:42:50 +0000 Subject: [PATCH 1/2] Fix double allocation in sid::allocator allocate() called the allocation functor twice and used only the second buffer; the first one was freed right away. --- include/gridtools/sid/allocator.hpp | 1 - 1 file changed, 1 deletion(-) 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())); } From fa851a8ec6c69ebfcbd5c426cbd4691a10b74f12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Gonz=C3=A1lez=20Paredes?= Date: Fri, 27 Nov 2026 07:42:50 +0000 Subject: [PATCH 2/2] Test that sid::allocator allocates once per request --- tests/unit_tests/sid/CMakeLists.txt | 1 + tests/unit_tests/sid/test_sid_allocator.cpp | 37 +++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 tests/unit_tests/sid/test_sid_allocator.cpp 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