Skip to content

Commit

Permalink
pool: make sure PoolAllocator uses the correct alignment
Browse files Browse the repository at this point in the history
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: bitcoin#28913
Rebased-From: ce881bf
  • Loading branch information
martinus authored and fanquake committed Nov 22, 2023
1 parent 7dda499 commit bcc183c
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 7 deletions.
3 changes: 1 addition & 2 deletions src/bench/pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ static void PoolAllocator_StdUnorderedMapWithPoolResource(benchmark::Bench& benc
std::hash<uint64_t>,
std::equal_to<uint64_t>,
PoolAllocator<std::pair<const uint64_t, uint64_t>,
sizeof(std::pair<const uint64_t, uint64_t>) + 4 * sizeof(void*),
alignof(void*)>>;
sizeof(std::pair<const uint64_t, uint64_t>) + 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();
Expand Down
3 changes: 1 addition & 2 deletions src/coins.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ using CCoinsMap = std::unordered_map<COutPoint,
SaltedOutpointHasher,
std::equal_to<COutPoint>,
PoolAllocator<std::pair<const COutPoint, CCoinsCacheEntry>,
sizeof(std::pair<const COutPoint, CCoinsCacheEntry>) + sizeof(void*) * 4,
alignof(void*)>>;
sizeof(std::pair<const COutPoint, CCoinsCacheEntry>) + sizeof(void*) * 4>>;

using CCoinsMapMemoryResource = CCoinsMap::allocator_type::ResourceType;

Expand Down
2 changes: 1 addition & 1 deletion src/support/allocators/pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ class PoolResource final
/**
* Forwards all allocations/deallocations to the PoolResource.
*/
template <class T, std::size_t MAX_BLOCK_SIZE_BYTES, std::size_t ALIGN_BYTES>
template <class T, std::size_t MAX_BLOCK_SIZE_BYTES, std::size_t ALIGN_BYTES = alignof(T)>
class PoolAllocator
{
PoolResource<MAX_BLOCK_SIZE_BYTES, ALIGN_BYTES>* m_resource;
Expand Down
3 changes: 1 addition & 2 deletions src/test/pool_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@ BOOST_AUTO_TEST_CASE(memusage_test)
std::hash<int>,
std::equal_to<int>,
PoolAllocator<std::pair<const int, int>,
sizeof(std::pair<const int, int>) + sizeof(void*) * 4,
alignof(void*)>>;
sizeof(std::pair<const int, int>) + sizeof(void*) * 4>>;
auto resource = Map::allocator_type::ResourceType(1024);

PoolResourceTester::CheckAllDataAccountedFor(resource);
Expand Down

0 comments on commit bcc183c

Please sign in to comment.