From 208ca9fafdbf53bb41213834bba697cad2b4ff5d Mon Sep 17 00:00:00 2001 From: assiduous Date: Thu, 13 Feb 2020 11:29:06 -0800 Subject: FixedBlockMemoryAllocator: fixed issue with block size alignment by sizeof(void*) --- Common/interface/FixedBlockMemoryAllocator.hpp | 4 +-- Common/src/FixedBlockMemoryAllocator.cpp | 21 +++++++----- .../DiligentCoreTest/src/Common/AllocatorTest.cpp | 40 ++++++++++++++++++++++ 3 files changed, 55 insertions(+), 10 deletions(-) diff --git a/Common/interface/FixedBlockMemoryAllocator.hpp b/Common/interface/FixedBlockMemoryAllocator.hpp index a21e41d1..89c9e355 100644 --- a/Common/interface/FixedBlockMemoryAllocator.hpp +++ b/Common/interface/FixedBlockMemoryAllocator.hpp @@ -229,8 +229,8 @@ private: std::mutex m_Mutex; IMemoryAllocator& m_RawMemoryAllocator; - size_t m_BlockSize; - Uint32 m_NumBlocksInPage; + const size_t m_BlockSize; + const Uint32 m_NumBlocksInPage; }; IMemoryAllocator& GetRawAllocator(); diff --git a/Common/src/FixedBlockMemoryAllocator.cpp b/Common/src/FixedBlockMemoryAllocator.cpp index 8235edc0..bca872fa 100644 --- a/Common/src/FixedBlockMemoryAllocator.cpp +++ b/Common/src/FixedBlockMemoryAllocator.cpp @@ -26,11 +26,18 @@ */ #include "pch.h" +#include #include "FixedBlockMemoryAllocator.hpp" +#include "Align.hpp" namespace Diligent { +static size_t AdjustBlockSize(size_t BlockSize) +{ + return Align(std::max(BlockSize, size_t{1}), sizeof(void*)); +} + FixedBlockMemoryAllocator::FixedBlockMemoryAllocator(IMemoryAllocator& RawMemoryAllocator, size_t BlockSize, Uint32 NumBlocksInPage) : @@ -38,16 +45,13 @@ FixedBlockMemoryAllocator::FixedBlockMemoryAllocator(IMemoryAllocator& RawMemory m_PagePool (STD_ALLOCATOR_RAW_MEM(MemoryPage, RawMemoryAllocator, "Allocator for vector")), m_AvailablePages (STD_ALLOCATOR_RAW_MEM(size_t, RawMemoryAllocator, "Allocator for unordered_set") ), m_AddrToPageId (STD_ALLOCATOR_RAW_MEM(AddrToPageIdMapElem, RawMemoryAllocator, "Allocator for unordered_map")), - m_RawMemoryAllocator{RawMemoryAllocator}, - m_BlockSize {BlockSize }, - m_NumBlocksInPage {NumBlocksInPage } + m_RawMemoryAllocator{RawMemoryAllocator }, + m_BlockSize {AdjustBlockSize(BlockSize)}, + m_NumBlocksInPage {NumBlocksInPage } // clang-format on { - if (BlockSize > 0) - { - // Allocate one page - CreateNewPage(); - } + // Allocate one page + CreateNewPage(); } FixedBlockMemoryAllocator::~FixedBlockMemoryAllocator() @@ -70,6 +74,7 @@ void FixedBlockMemoryAllocator::CreateNewPage() void* FixedBlockMemoryAllocator::Allocate(size_t Size, const Char* dbgDescription, const char* dbgFileName, const Int32 dbgLineNumber) { + Size = AdjustBlockSize(Size); VERIFY(m_BlockSize == Size, "Requested size (", Size, ") does not match the block size (", m_BlockSize, ")"); std::lock_guard LockGuard(m_Mutex); diff --git a/Tests/DiligentCoreTest/src/Common/AllocatorTest.cpp b/Tests/DiligentCoreTest/src/Common/AllocatorTest.cpp index 3d074246..00df04f2 100644 --- a/Tests/DiligentCoreTest/src/Common/AllocatorTest.cpp +++ b/Tests/DiligentCoreTest/src/Common/AllocatorTest.cpp @@ -80,4 +80,44 @@ TEST(Common_FixedBlockMemoryAllocator, AllocDealloc) TestAllocator.Free(Allocations[i][p]); } +TEST(Common_FixedBlockMemoryAllocator, SmallObject) +{ + constexpr Uint32 AllocSize = 4; + constexpr Uint32 NumAllocationsPerPage = 1; + + FixedBlockMemoryAllocator TestAllocator(DefaultRawMemoryAllocator::GetAllocator(), AllocSize, NumAllocationsPerPage); + + { + void* pRawMem0 = TestAllocator.Allocate(AllocSize, "Small object allocation test", __FILE__, __LINE__); + TestAllocator.Free(pRawMem0); + } + + { + void* pRawMem0 = TestAllocator.Allocate(AllocSize, "Small object allocation test", __FILE__, __LINE__); + void* pRawMem1 = TestAllocator.Allocate(AllocSize, "Small object allocation test", __FILE__, __LINE__); + TestAllocator.Free(pRawMem0); + TestAllocator.Free(pRawMem1); + } +} + +TEST(Common_FixedBlockMemoryAllocator, UnalignedSize) +{ + constexpr Uint32 AllocSize = 10; + constexpr Uint32 NumAllocationsPerPage = 1; + + FixedBlockMemoryAllocator TestAllocator(DefaultRawMemoryAllocator::GetAllocator(), AllocSize, NumAllocationsPerPage); + + { + void* pRawMem0 = TestAllocator.Allocate(AllocSize, "Unaligned-size object allocation test", __FILE__, __LINE__); + TestAllocator.Free(pRawMem0); + } + + { + void* pRawMem0 = TestAllocator.Allocate(AllocSize, "Unaligned-size object allocation test", __FILE__, __LINE__); + void* pRawMem1 = TestAllocator.Allocate(AllocSize, "Unaligned-size object allocation test", __FILE__, __LINE__); + TestAllocator.Free(pRawMem0); + TestAllocator.Free(pRawMem1); + } +} + } // namespace -- cgit v1.2.3