summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorassiduous <assiduous@diligentgraphics.com>2020-02-13 19:29:06 +0000
committerassiduous <assiduous@diligentgraphics.com>2020-02-13 19:29:06 +0000
commit208ca9fafdbf53bb41213834bba697cad2b4ff5d (patch)
treed8739487826900bf9de331d100f6a26ffed27f94
parentUpdated TraceLineThroughGrid test plus some minor comments clean-up (diff)
downloadDiligentCore-208ca9fafdbf53bb41213834bba697cad2b4ff5d.tar.gz
DiligentCore-208ca9fafdbf53bb41213834bba697cad2b4ff5d.zip
FixedBlockMemoryAllocator: fixed issue with block size alignment by sizeof(void*)
-rw-r--r--Common/interface/FixedBlockMemoryAllocator.hpp4
-rw-r--r--Common/src/FixedBlockMemoryAllocator.cpp21
-rw-r--r--Tests/DiligentCoreTest/src/Common/AllocatorTest.cpp40
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 <algorithm>
#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<MemoryPage>")),
m_AvailablePages (STD_ALLOCATOR_RAW_MEM(size_t, RawMemoryAllocator, "Allocator for unordered_set<size_t>") ),
m_AddrToPageId (STD_ALLOCATOR_RAW_MEM(AddrToPageIdMapElem, RawMemoryAllocator, "Allocator for unordered_map<void*, size_t>")),
- 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<std::mutex> 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