From 2b64f003622ee7871fd7aec041622427dd2fc88f Mon Sep 17 00:00:00 2001 From: Egor Yusov Date: Fri, 19 Aug 2016 23:35:47 -0700 Subject: Updated to Diligent Engine 2.0 --- Common/src/BasicFileStream.cpp | 2 +- Common/src/DataBlobImpl.cpp | 2 +- Common/src/DefaultRawMemoryAllocator.cpp | 57 +++++++++++++++ Common/src/FixedBlockMemoryAllocator.cpp | 116 +++++++++++++++++++++++++++++++ Common/src/Timer.cpp | 2 +- Common/src/pch.cpp | 2 +- 6 files changed, 177 insertions(+), 4 deletions(-) create mode 100644 Common/src/DefaultRawMemoryAllocator.cpp create mode 100644 Common/src/FixedBlockMemoryAllocator.cpp (limited to 'Common/src') diff --git a/Common/src/BasicFileStream.cpp b/Common/src/BasicFileStream.cpp index 09719821..4a7efb68 100644 --- a/Common/src/BasicFileStream.cpp +++ b/Common/src/BasicFileStream.cpp @@ -1,4 +1,4 @@ -/* Copyright 2015 Egor Yusov +/* Copyright 2015-2016 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/Common/src/DataBlobImpl.cpp b/Common/src/DataBlobImpl.cpp index 9255f2e3..614a0be4 100644 --- a/Common/src/DataBlobImpl.cpp +++ b/Common/src/DataBlobImpl.cpp @@ -1,4 +1,4 @@ -/* Copyright 2015 Egor Yusov +/* Copyright 2015-2016 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/Common/src/DefaultRawMemoryAllocator.cpp b/Common/src/DefaultRawMemoryAllocator.cpp new file mode 100644 index 00000000..503a1d5b --- /dev/null +++ b/Common/src/DefaultRawMemoryAllocator.cpp @@ -0,0 +1,57 @@ +/* Copyright 2015-2016 Egor Yusov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. + * + * In no event and under no legal theory, whether in tort (including negligence), + * contract, or otherwise, unless required by applicable law (such as deliberate + * and grossly negligent acts) or agreed to in writing, shall any Contributor be + * liable for any damages, including any direct, indirect, special, incidental, + * or consequential damages of any character arising as a result of this License or + * out of the use or inability to use the software (including but not limited to damages + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and + * all other commercial damages or losses), even if such Contributor has been advised + * of the possibility of such damages. + */ + +#include "pch.h" +#include "DefaultRawMemoryAllocator.h" + +namespace Diligent +{ + DefaultRawMemoryAllocator::DefaultRawMemoryAllocator() + { + + } + + void* DefaultRawMemoryAllocator::Allocate( size_t Size, const Char* dbgDescription, const char* dbgFileName, const Int32 dbgLineNumber) + { +#ifdef _DEBUG + return new Uint8[Size+16]+16; +#else + return new Uint8[Size]; +#endif + } + + void DefaultRawMemoryAllocator::Free(void *Ptr) + { +#ifdef _DEBUG + delete[] (reinterpret_cast(Ptr)-16); +#else + delete[] Ptr; +#endif + } + + DefaultRawMemoryAllocator& DefaultRawMemoryAllocator::GetAllocator() + { + static DefaultRawMemoryAllocator Allocator; + return Allocator; + } +} diff --git a/Common/src/FixedBlockMemoryAllocator.cpp b/Common/src/FixedBlockMemoryAllocator.cpp new file mode 100644 index 00000000..72842fe4 --- /dev/null +++ b/Common/src/FixedBlockMemoryAllocator.cpp @@ -0,0 +1,116 @@ +/* Copyright 2015-2016 Egor Yusov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PROPRIETARY RIGHTS. + * + * In no event and under no legal theory, whether in tort (including negligence), + * contract, or otherwise, unless required by applicable law (such as deliberate + * and grossly negligent acts) or agreed to in writing, shall any Contributor be + * liable for any damages, including any direct, indirect, special, incidental, + * or consequential damages of any character arising as a result of this License or + * out of the use or inability to use the software (including but not limited to damages + * for loss of goodwill, work stoppage, computer failure or malfunction, or any and + * all other commercial damages or losses), even if such Contributor has been advised + * of the possibility of such damages. + */ + +#include "pch.h" +#include "FixedBlockMemoryAllocator.h" + +namespace Diligent +{ + FixedBlockMemoryAllocator::FixedBlockMemoryAllocator(IMemoryAllocator &RawMemoryAllocator, size_t BlockSize, Uint32 NumBlocksInPage) : + m_RawMemoryAllocator(RawMemoryAllocator), + m_BlockSize(BlockSize), + m_NumBlocksInPage(NumBlocksInPage), + 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")) + { + //tmpLargeBuffer = new Uint8[100 << 20]; + //tmpCurrPtr = tmpLargeBuffer; + + // Allocate one page + CreateNewPage(); + } + + FixedBlockMemoryAllocator::~FixedBlockMemoryAllocator() + { + //delete[] tmpLargeBuffer; + +#ifdef _DEBUG + for (size_t p = 0; p < m_PagePool.size(); ++p) + { + VERIFY(!m_PagePool[p].HasAllocations(), "Memory leak detected: memory page has allocated block") + VERIFY(m_AvailablePages.find(p) != m_AvailablePages.end(), "Memory page is not in the available page pool") + } +#endif + } + + void FixedBlockMemoryAllocator::CreateNewPage() + { + m_PagePool.emplace_back( *this ); + m_AvailablePages.insert( m_PagePool.size()-1 ); + m_AddrToPageId.reserve( m_PagePool.size()*m_NumBlocksInPage ); + } + + void* FixedBlockMemoryAllocator::Allocate( size_t Size, const Char* dbgDescription, const char* dbgFileName, const Int32 dbgLineNumber) + { + //auto *ptr = tmpCurrPtr; + //tmpCurrPtr += Size; + //return ptr; + + VERIFY(m_BlockSize == Size, "Requested size (", Size, ") does not match the block size (", m_BlockSize, ")"); + + std::lock_guard LockGuard(m_Mutex); + + if (m_AvailablePages.empty()) + { + CreateNewPage(); + } + + auto PageId = *m_AvailablePages.begin(); + auto &Page = m_PagePool[PageId]; + auto *Ptr = Page.Allocate(); + m_AddrToPageId.insert( std::make_pair(Ptr, PageId) ); + if (!Page.HasSpace()) + { + m_AvailablePages.erase(m_AvailablePages.begin()); + } + + return Ptr; + } + + void FixedBlockMemoryAllocator::Free(void *Ptr) + { + std::lock_guard LockGuard(m_Mutex); + auto PageIdIt = m_AddrToPageId.find(Ptr); + if (PageIdIt != m_AddrToPageId.end()) + { + auto PageId = PageIdIt->second; + VERIFY_EXPR(PageId >= 0 && PageId < m_PagePool.size()); + m_PagePool[PageId].DeAllocate(Ptr); + m_AvailablePages.insert(PageId); + m_AddrToPageId.erase(PageIdIt); + if (m_AvailablePages.size() > 1 && !m_PagePool[PageId].HasAllocations()) + { + // In current implementation pages are never released! + // Note that if we delete a page, all indices past it will be invalid + + //m_PagePool.erase(m_PagePool.begin() + PageId); + //m_AvailablePages.erase(PageId); + } + } + else + { + UNEXPECTED("Address not found in the allocations list - double freeing memory?") + } + } +} diff --git a/Common/src/Timer.cpp b/Common/src/Timer.cpp index caf6ffdb..1673333a 100644 --- a/Common/src/Timer.cpp +++ b/Common/src/Timer.cpp @@ -1,4 +1,4 @@ -/* Copyright 2015 Egor Yusov +/* Copyright 2015-2016 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/Common/src/pch.cpp b/Common/src/pch.cpp index 1a94193e..4502a41d 100644 --- a/Common/src/pch.cpp +++ b/Common/src/pch.cpp @@ -1,4 +1,4 @@ -/* Copyright 2015 Egor Yusov +/* Copyright 2015-2016 Egor Yusov * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. -- cgit v1.2.3