diff options
| author | Egor Yusov <egor.yusov@gmail.com> | 2019-11-24 08:03:16 +0000 |
|---|---|---|
| committer | Egor Yusov <egor.yusov@gmail.com> | 2019-11-24 08:03:16 +0000 |
| commit | 0cc615c573a132489f7ea101daaea22e14b45037 (patch) | |
| tree | 9c7985d2b381261a9b15b5a29f30075bca172179 /Common/src | |
| parent | Fixed few formatting issues; updated format validation script to make validat... (diff) | |
| download | DiligentCore-0cc615c573a132489f7ea101daaea22e14b45037.tar.gz DiligentCore-0cc615c573a132489f7ea101daaea22e14b45037.zip | |
clang-formatted Common project
Diffstat (limited to 'Common/src')
| -rw-r--r-- | Common/src/BasicFileStream.cpp | 70 | ||||
| -rw-r--r-- | Common/src/DataBlobImpl.cpp | 14 | ||||
| -rw-r--r-- | Common/src/DefaultRawMemoryAllocator.cpp | 37 | ||||
| -rw-r--r-- | Common/src/FixedBlockMemoryAllocator.cpp | 136 | ||||
| -rw-r--r-- | Common/src/LockHelper.cpp | 4 | ||||
| -rw-r--r-- | Common/src/MemoryFileStream.cpp | 85 | ||||
| -rw-r--r-- | Common/src/Timer.cpp | 58 |
7 files changed, 208 insertions, 196 deletions
diff --git a/Common/src/BasicFileStream.cpp b/Common/src/BasicFileStream.cpp index b309e338..544095c6 100644 --- a/Common/src/BasicFileStream.cpp +++ b/Common/src/BasicFileStream.cpp @@ -26,38 +26,40 @@ namespace Diligent { - BasicFileStream::BasicFileStream(IReferenceCounters* pRefCounters, - const Char* Path, - EFileAccessMode Access/* = EFileAccessMode::Read*/) : - TBase(pRefCounters), - m_FileWrpr(Path, Access) - { - } - - IMPLEMENT_QUERY_INTERFACE(BasicFileStream, IID_FileStream, TBase) - - bool BasicFileStream::Read(void* Data, size_t Size) - { - return m_FileWrpr->Read( Data, Size ); - } - - void BasicFileStream::Read( Diligent::IDataBlob* pData ) - { - return m_FileWrpr->Read( pData ); - } - - bool BasicFileStream::Write(const void* Data, size_t Size) - { - return m_FileWrpr->Write( Data, Size ); - } - - bool BasicFileStream::IsValid() - { - return !!m_FileWrpr; - } - - size_t BasicFileStream::GetSize() - { - return m_FileWrpr->GetSize(); - } + +BasicFileStream::BasicFileStream(IReferenceCounters* pRefCounters, + const Char* Path, + EFileAccessMode Access /* = EFileAccessMode::Read*/) : + TBase{pRefCounters}, + m_FileWrpr{Path, Access} +{ +} + +IMPLEMENT_QUERY_INTERFACE(BasicFileStream, IID_FileStream, TBase) + +bool BasicFileStream::Read(void* Data, size_t Size) +{ + return m_FileWrpr->Read(Data, Size); } + +void BasicFileStream::Read(Diligent::IDataBlob* pData) +{ + return m_FileWrpr->Read(pData); +} + +bool BasicFileStream::Write(const void* Data, size_t Size) +{ + return m_FileWrpr->Write(Data, Size); +} + +bool BasicFileStream::IsValid() +{ + return !!m_FileWrpr; +} + +size_t BasicFileStream::GetSize() +{ + return m_FileWrpr->GetSize(); +} + +} // namespace Diligent diff --git a/Common/src/DataBlobImpl.cpp b/Common/src/DataBlobImpl.cpp index 92428a18..7d701471 100644 --- a/Common/src/DataBlobImpl.cpp +++ b/Common/src/DataBlobImpl.cpp @@ -28,16 +28,16 @@ namespace Diligent { - -DataBlobImpl::DataBlobImpl( IReferenceCounters *pRefCounters, size_t InitialSize ) : - TBase(pRefCounters), + +DataBlobImpl::DataBlobImpl(IReferenceCounters* pRefCounters, size_t InitialSize) : + TBase{pRefCounters}, m_DataBuff(InitialSize) {} /// Sets the size of the internal data buffer -void DataBlobImpl::Resize( size_t NewSize ) +void DataBlobImpl::Resize(size_t NewSize) { - m_DataBuff.resize( NewSize ); + m_DataBuff.resize(NewSize); } /// Returns the size of the internal data buffer @@ -46,7 +46,7 @@ size_t DataBlobImpl::GetSize() return m_DataBuff.size(); } - /// Returns the pointer to the internal data buffer +/// Returns the pointer to the internal data buffer void* DataBlobImpl::GetDataPtr() { return m_DataBuff.data(); @@ -54,4 +54,4 @@ void* DataBlobImpl::GetDataPtr() IMPLEMENT_QUERY_INTERFACE(DataBlobImpl, IID_DataBlob, TBase) -} +} // namespace Diligent diff --git a/Common/src/DefaultRawMemoryAllocator.cpp b/Common/src/DefaultRawMemoryAllocator.cpp index 8e792c12..ea5b0149 100644 --- a/Common/src/DefaultRawMemoryAllocator.cpp +++ b/Common/src/DefaultRawMemoryAllocator.cpp @@ -26,32 +26,33 @@ namespace Diligent { - DefaultRawMemoryAllocator::DefaultRawMemoryAllocator() - { - } +DefaultRawMemoryAllocator::DefaultRawMemoryAllocator() +{ +} - void* DefaultRawMemoryAllocator::Allocate( size_t Size, const Char* dbgDescription, const char* dbgFileName, const Int32 dbgLineNumber) - { +void* DefaultRawMemoryAllocator::Allocate(size_t Size, const Char* dbgDescription, const char* dbgFileName, const Int32 dbgLineNumber) +{ #ifdef _DEBUG - return new Uint8[Size+16]+16; + return new Uint8[Size + 16] + 16; #else - return new Uint8[Size]; + return new Uint8[Size]; #endif - } +} - void DefaultRawMemoryAllocator::Free(void *Ptr) - { +void DefaultRawMemoryAllocator::Free(void* Ptr) +{ #ifdef _DEBUG - delete[] (reinterpret_cast<Uint8*>(Ptr)-16); + delete[](reinterpret_cast<Uint8*>(Ptr) - 16); #else - delete[] reinterpret_cast<Uint8*>(Ptr); + delete[] reinterpret_cast<Uint8*>(Ptr); #endif - } +} - DefaultRawMemoryAllocator& DefaultRawMemoryAllocator::GetAllocator() - { - static DefaultRawMemoryAllocator Allocator; - return Allocator; - } +DefaultRawMemoryAllocator& DefaultRawMemoryAllocator::GetAllocator() +{ + static DefaultRawMemoryAllocator Allocator; + return Allocator; } + +} // namespace Diligent diff --git a/Common/src/FixedBlockMemoryAllocator.cpp b/Common/src/FixedBlockMemoryAllocator.cpp index 6d74719b..22095f8d 100644 --- a/Common/src/FixedBlockMemoryAllocator.cpp +++ b/Common/src/FixedBlockMemoryAllocator.cpp @@ -26,87 +26,91 @@ namespace Diligent { - FixedBlockMemoryAllocator::FixedBlockMemoryAllocator(IMemoryAllocator& RawMemoryAllocator, - size_t BlockSize, - Uint32 NumBlocksInPage) : - 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) + +FixedBlockMemoryAllocator::FixedBlockMemoryAllocator(IMemoryAllocator& RawMemoryAllocator, + size_t BlockSize, + Uint32 NumBlocksInPage) : + // clang-format off + 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 } +// clang-format on +{ + if (BlockSize > 0) { - if (BlockSize > 0) - { - // Allocate one page - CreateNewPage(); - } + // Allocate one page + CreateNewPage(); } +} - FixedBlockMemoryAllocator::~FixedBlockMemoryAllocator() - { +FixedBlockMemoryAllocator::~FixedBlockMemoryAllocator() +{ #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 + 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() +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) +{ + VERIFY(m_BlockSize == Size, "Requested size (", Size, ") does not match the block size (", m_BlockSize, ")"); + + std::lock_guard<std::mutex> LockGuard(m_Mutex); + + if (m_AvailablePages.empty()) { - m_PagePool.emplace_back( *this ); - m_AvailablePages.insert( m_PagePool.size()-1 ); - m_AddrToPageId.reserve( m_PagePool.size()*m_NumBlocksInPage ); + CreateNewPage(); } - void* FixedBlockMemoryAllocator::Allocate( size_t Size, const Char* dbgDescription, const char* dbgFileName, const Int32 dbgLineNumber) + 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()) { - VERIFY(m_BlockSize == Size, "Requested size (", Size, ") does not match the block size (", m_BlockSize, ")"); - - std::lock_guard<std::mutex> 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; + m_AvailablePages.erase(m_AvailablePages.begin()); } - void FixedBlockMemoryAllocator::Free(void *Ptr) + return Ptr; +} + +void FixedBlockMemoryAllocator::Free(void* Ptr) +{ + std::lock_guard<std::mutex> LockGuard(m_Mutex); + auto PageIdIt = m_AddrToPageId.find(Ptr); + if (PageIdIt != m_AddrToPageId.end()) { - std::lock_guard<std::mutex> 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()) { - 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 + // 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?"); + //m_PagePool.erase(m_PagePool.begin() + PageId); + //m_AvailablePages.erase(PageId); } } + else + { + UNEXPECTED("Address not found in the allocations list - double freeing memory?"); + } } + +} // namespace Diligent diff --git a/Common/src/LockHelper.cpp b/Common/src/LockHelper.cpp index 30206b71..5b072ded 100644 --- a/Common/src/LockHelper.cpp +++ b/Common/src/LockHelper.cpp @@ -27,9 +27,9 @@ namespace ThreadingTools { -void LockHelper::YieldThread()noexcept +void LockHelper::YieldThread() noexcept { std::this_thread::yield(); } -} +} // namespace ThreadingTools diff --git a/Common/src/MemoryFileStream.cpp b/Common/src/MemoryFileStream.cpp index 704821bf..1e85590f 100644 --- a/Common/src/MemoryFileStream.cpp +++ b/Common/src/MemoryFileStream.cpp @@ -28,53 +28,56 @@ namespace Diligent { - MemoryFileStream::MemoryFileStream(IReferenceCounters* pRefCounters, - IDataBlob* pData) : - TBase(pRefCounters), - m_DataBlob(pData) - { - } - IMPLEMENT_QUERY_INTERFACE(MemoryFileStream, IID_FileStream, TBase) +MemoryFileStream::MemoryFileStream(IReferenceCounters* pRefCounters, + IDataBlob* pData) : + TBase{pRefCounters}, + m_DataBlob{pData} +{ +} - bool MemoryFileStream::Read(void* Data, size_t Size) - { - VERIFY_EXPR(m_CurrentOffset <= m_DataBlob->GetSize()); - auto BytesLeft = m_DataBlob->GetSize() - m_CurrentOffset; - auto BytesToRead = std::min(BytesLeft, Size); - auto* pSrcData = reinterpret_cast<const Uint8*>(m_DataBlob->GetDataPtr()) + m_CurrentOffset; - memcpy(Data, pSrcData, BytesToRead); - m_CurrentOffset += BytesToRead; - return Size == BytesToRead; - } +IMPLEMENT_QUERY_INTERFACE(MemoryFileStream, IID_FileStream, TBase) - void MemoryFileStream::Read( IDataBlob* pData ) - { - auto BytesLeft = m_DataBlob->GetSize() - m_CurrentOffset; - pData->Resize(BytesLeft); - auto res = Read(pData->GetDataPtr(), pData->GetSize()); - VERIFY_EXPR(res); (void)res; - } +bool MemoryFileStream::Read(void* Data, size_t Size) +{ + VERIFY_EXPR(m_CurrentOffset <= m_DataBlob->GetSize()); + auto BytesLeft = m_DataBlob->GetSize() - m_CurrentOffset; + auto BytesToRead = std::min(BytesLeft, Size); + auto* pSrcData = reinterpret_cast<const Uint8*>(m_DataBlob->GetDataPtr()) + m_CurrentOffset; + memcpy(Data, pSrcData, BytesToRead); + m_CurrentOffset += BytesToRead; + return Size == BytesToRead; +} - bool MemoryFileStream::Write(const void* Data, size_t Size) - { - if (m_CurrentOffset + Size > m_DataBlob->GetSize()) - { - m_DataBlob->Resize(m_CurrentOffset + Size); - } - auto* DstData = reinterpret_cast<Uint8*>(m_DataBlob->GetDataPtr()) + m_CurrentOffset; - memcpy(DstData, Data, Size); - m_CurrentOffset += Size; - return true; - } +void MemoryFileStream::Read(IDataBlob* pData) +{ + auto BytesLeft = m_DataBlob->GetSize() - m_CurrentOffset; + pData->Resize(BytesLeft); + auto res = Read(pData->GetDataPtr(), pData->GetSize()); + VERIFY_EXPR(res); + (void)res; +} - bool MemoryFileStream::IsValid() +bool MemoryFileStream::Write(const void* Data, size_t Size) +{ + if (m_CurrentOffset + Size > m_DataBlob->GetSize()) { - return !!m_DataBlob; + m_DataBlob->Resize(m_CurrentOffset + Size); } + auto* DstData = reinterpret_cast<Uint8*>(m_DataBlob->GetDataPtr()) + m_CurrentOffset; + memcpy(DstData, Data, Size); + m_CurrentOffset += Size; + return true; +} - size_t MemoryFileStream::GetSize() - { - return m_DataBlob->GetSize(); - } +bool MemoryFileStream::IsValid() +{ + return !!m_DataBlob; } + +size_t MemoryFileStream::GetSize() +{ + return m_DataBlob->GetSize(); +} + +} // namespace Diligent diff --git a/Common/src/Timer.cpp b/Common/src/Timer.cpp index ba3b7919..7c1cd86b 100644 --- a/Common/src/Timer.cpp +++ b/Common/src/Timer.cpp @@ -27,32 +27,34 @@ using namespace std::chrono; namespace Diligent { - Timer::Timer() - { - Restart(); - } - - void Timer::Restart() - { - m_StartTime = high_resolution_clock().now(); - } - - template<typename T> - T GetElapsedTime( high_resolution_clock::time_point StartTime ) - { - auto CurrTime = high_resolution_clock::now(); - auto time_span = duration_cast<duration<T>>(CurrTime - StartTime); - return time_span.count(); - } - - double Timer::GetElapsedTime()const - { - return Diligent::GetElapsedTime<double>( m_StartTime ); - } - - float Timer::GetElapsedTimef()const - { - - return Diligent::GetElapsedTime<float>( m_StartTime ); - } + +Timer::Timer() +{ + Restart(); +} + +void Timer::Restart() +{ + m_StartTime = high_resolution_clock().now(); +} + +template <typename T> +T GetElapsedTime(high_resolution_clock::time_point StartTime) +{ + auto CurrTime = high_resolution_clock::now(); + auto time_span = duration_cast<duration<T>>(CurrTime - StartTime); + return time_span.count(); } + +double Timer::GetElapsedTime() const +{ + return Diligent::GetElapsedTime<double>(m_StartTime); +} + +float Timer::GetElapsedTimef() const +{ + + return Diligent::GetElapsedTime<float>(m_StartTime); +} + +} // namespace Diligent |
