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/src/FixedBlockMemoryAllocator.cpp | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'Common/src') 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); -- cgit v1.2.3