From bcc183cccefdc84a09e43965d6b88a8bec3a5446 Mon Sep 17 00:00:00 2001 From: Martin Leitner-Ankerl Date: Sun, 19 Nov 2023 15:57:03 +0100 Subject: [PATCH] pool: make sure PoolAllocator uses the correct alignment This changes the PoolAllocator to default the alignment to the given type. This makes the code simpler, and most importantly fixes a bug on ARM 32bit that caused OOM: The class CTxOut has a member CAmount which is an int64_t and on ARM 32bit int64_t are 8 byte aligned which is larger than the pointer alignment of 4 bytes. So for CCoinsMap to be able to use the pool, we need to use the alignment of the member instead of just alignof(void*). Github-Pull: #28913 Rebased-From: ce881bf9fcb7c30bb1fafd6ce38844f4f829452a --- src/bench/pool.cpp | 3 +-- src/coins.h | 3 +-- src/support/allocators/pool.h | 2 +- src/test/pool_tests.cpp | 3 +-- 4 files changed, 4 insertions(+), 7 deletions(-) diff --git a/src/bench/pool.cpp b/src/bench/pool.cpp index b3e54d85a25..b2a5f8debf9 100644 --- a/src/bench/pool.cpp +++ b/src/bench/pool.cpp @@ -37,8 +37,7 @@ static void PoolAllocator_StdUnorderedMapWithPoolResource(benchmark::Bench& benc std::hash, std::equal_to, PoolAllocator, - sizeof(std::pair) + 4 * sizeof(void*), - alignof(void*)>>; + sizeof(std::pair) + 4 * sizeof(void*)>>; // make sure the resource supports large enough pools to hold the node. We do this by adding the size of a few pointers to it. auto pool_resource = Map::allocator_type::ResourceType(); diff --git a/src/coins.h b/src/coins.h index a6cbb031339..bbf9e3895fa 100644 --- a/src/coins.h +++ b/src/coins.h @@ -145,8 +145,7 @@ using CCoinsMap = std::unordered_map, PoolAllocator, - sizeof(std::pair) + sizeof(void*) * 4, - alignof(void*)>>; + sizeof(std::pair) + sizeof(void*) * 4>>; using CCoinsMapMemoryResource = CCoinsMap::allocator_type::ResourceType; diff --git a/src/support/allocators/pool.h b/src/support/allocators/pool.h index c8e70ebacff..873e260b651 100644 --- a/src/support/allocators/pool.h +++ b/src/support/allocators/pool.h @@ -272,7 +272,7 @@ public: /** * Forwards all allocations/deallocations to the PoolResource. */ -template +template class PoolAllocator { PoolResource* m_resource; diff --git a/src/test/pool_tests.cpp b/src/test/pool_tests.cpp index 8a07e09a442..2663e088eb9 100644 --- a/src/test/pool_tests.cpp +++ b/src/test/pool_tests.cpp @@ -163,8 +163,7 @@ BOOST_AUTO_TEST_CASE(memusage_test) std::hash, std::equal_to, PoolAllocator, - sizeof(std::pair) + sizeof(void*) * 4, - alignof(void*)>>; + sizeof(std::pair) + sizeof(void*) * 4>>; auto resource = Map::allocator_type::ResourceType(1024); PoolResourceTester::CheckAllDataAccountedFor(resource);