From f8be662d357be9dcbb4b298d93b43d29ae93ce04 Mon Sep 17 00:00:00 2001 From: assiduous Date: Tue, 27 Oct 2020 19:08:28 -0700 Subject: A number of updates/fixes to PSO refactor merge --- Common/interface/CompilerDefinitions.h | 52 ++++++++++++++++++ Common/interface/Definitions.hpp | 52 ------------------ Common/interface/DynamicLinearAllocator.hpp | 83 +++++++++++++++-------------- Common/interface/LinearAllocator.hpp | 29 +++++----- 4 files changed, 110 insertions(+), 106 deletions(-) create mode 100644 Common/interface/CompilerDefinitions.h delete mode 100644 Common/interface/Definitions.hpp (limited to 'Common/interface') diff --git a/Common/interface/CompilerDefinitions.h b/Common/interface/CompilerDefinitions.h new file mode 100644 index 00000000..83ef8842 --- /dev/null +++ b/Common/interface/CompilerDefinitions.h @@ -0,0 +1,52 @@ +/* + * Copyright 2019-2020 Diligent Graphics LLC + * Copyright 2015-2019 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 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * 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. + */ + +#pragma once + +#ifdef _MSC_VER +# if _MSC_VER >= 1917 +# define NODISCARD [[nodiscard]] +# else +# define NODISCARD +# endif +#endif // _MSC_VER + +#ifdef __clang__ +# if __has_feature(cxx_attributes) +# define NODISCARD [[nodiscard]] +# else +# define NODISCARD +# endif +#endif // __clang__ + +#ifdef __GNUC__ +# if __has_cpp_attribute(nodiscard) +# define NODISCARD [[nodiscard]] +# else +# define NODISCARD +# endif +#endif // __GNUC__ diff --git a/Common/interface/Definitions.hpp b/Common/interface/Definitions.hpp deleted file mode 100644 index 72a09524..00000000 --- a/Common/interface/Definitions.hpp +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2019-2020 Diligent Graphics LLC - * Copyright 2015-2019 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * 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. - */ - -#pragma once - -#ifdef _MSC_VER -# if _MSC_VER >= 1917 -# define NDDISCARD [[nodiscard]] -# else -# define NDDISCARD -# endif -#endif // _MSC_VER - -#ifdef __clang__ -# if __has_feature(cxx_attributes) -# define NDDISCARD [[nodiscard]] -# else -# define NDDISCARD -# endif -#endif // __clang__ - -#ifdef __GNUC__ -# if __has_cpp_attribute(nodiscard) -# define NDDISCARD [[nodiscard]] -# else -# define NDDISCARD -# endif -#endif // __GNUC__ diff --git a/Common/interface/DynamicLinearAllocator.hpp b/Common/interface/DynamicLinearAllocator.hpp index 9ddffbb3..f57eaf10 100644 --- a/Common/interface/DynamicLinearAllocator.hpp +++ b/Common/interface/DynamicLinearAllocator.hpp @@ -35,13 +35,13 @@ #include "../../Primitives/interface/BasicTypes.h" #include "../../Primitives/interface/MemoryAllocator.h" #include "../../Platforms/Basic/interface/DebugUtilities.hpp" -#include "Definitions.hpp" +#include "CompilerDefinitions.h" #include "Align.hpp" namespace Diligent { -/// Implementation of a linear allocator on a fixed memory pages +/// Implementation of a linear allocator on fixed memory pages class DynamicLinearAllocator { public: @@ -55,7 +55,9 @@ public: explicit DynamicLinearAllocator(IMemoryAllocator& Allocator, Uint32 BlockSize = 4 << 10) : m_pAllocator{&Allocator}, m_BlockSize{BlockSize} - {} + { + VERIFY(IsPowerOfTwo(BlockSize), "Block size (", BlockSize, ") is not power of two"); + } ~DynamicLinearAllocator() { @@ -66,7 +68,7 @@ public: { for (auto& block : m_Blocks) { - m_pAllocator->Free(block.Page); + m_pAllocator->Free(block.Data); } m_Blocks.clear(); @@ -77,45 +79,46 @@ public: { for (auto& block : m_Blocks) { - block.Size = 0; + block.CurrPtr = block.Data; } } - NDDISCARD void* Allocate(size_t size, size_t align) + NODISCARD void* Allocate(size_t size, size_t align) { if (size == 0) return nullptr; for (auto& block : m_Blocks) { - size_t offset = Align(reinterpret_cast(block.Page) + block.Size, align) - reinterpret_cast(block.Page); - - if (size <= (block.Capacity - offset)) + auto* Ptr = Align(block.CurrPtr, align); + if (Ptr + size <= block.Data + block.Size) { - block.Size = offset + size; - return block.Page + offset; + block.CurrPtr = Ptr + size; + return Ptr; } } - // create new block + // Create a new block size_t BlockSize = m_BlockSize; - BlockSize = size * 2 < BlockSize ? BlockSize : size * 2; - m_Blocks.emplace_back(m_pAllocator->Allocate(BlockSize, "dynamic linear allocator page", __FILE__, __LINE__), 0, BlockSize); - - auto& block = m_Blocks.back(); - size_t offset = Align(reinterpret_cast(block.Page), align) - reinterpret_cast(block.Page); - block.Size = offset + size; - return block.Page + offset; + while (BlockSize < size + align - 1) + BlockSize *= 2; + m_Blocks.emplace_back(m_pAllocator->Allocate(BlockSize, "dynamic linear allocator page", __FILE__, __LINE__), BlockSize); + + auto& block = m_Blocks.back(); + auto* Ptr = Align(block.Data, align); + VERIFY(Ptr + size <= block.Data + block.Size, "Not enough space in the new block - this is a bug"); + block.CurrPtr = Ptr + size; + return Ptr; } template - NDDISCARD T* Allocate(size_t count = 1) + NODISCARD T* Allocate(size_t count = 1) { return reinterpret_cast(Allocate(sizeof(T) * count, alignof(T))); } template - NDDISCARD T* Construct(Args&&... args) + NODISCARD T* Construct(Args&&... args) { T* Ptr = Allocate(1); new (Ptr) T{std::forward(args)...}; @@ -123,7 +126,7 @@ public: } template - NDDISCARD T* ConstructArray(size_t count, const Args&... args) + NODISCARD T* ConstructArray(size_t count, const Args&... args) { T* Ptr = Allocate(count); for (size_t i = 0; i < count; ++i) @@ -134,7 +137,7 @@ public: } template - NDDISCARD T* CopyArray(const T* Src, size_t count) + NODISCARD T* CopyArray(const T* Src, size_t count) { T* Dst = Allocate(count); for (size_t i = 0; i < count; ++i) @@ -144,24 +147,28 @@ public: return Dst; } - NDDISCARD Char* CopyString(const Char* Str) + NODISCARD Char* CopyString(const Char* Str, size_t len = 0) { if (Str == nullptr) return nullptr; - size_t len = strlen(Str) + 1; - Char* Dst = Allocate(len + 1); + if (len == 0) + len = strlen(Str); + else + VERIFY_EXPR(len <= strlen(Str)); + + Char* Dst = Allocate(len + 1); std::memcpy(Dst, Str, sizeof(Char) * len); Dst[len] = 0; return Dst; } - NDDISCARD wchar_t* CopyWString(const char* Str) + NODISCARD wchar_t* CopyWString(const char* Str) { if (Str == nullptr) return nullptr; - size_t len = strlen(Str) + 1; + size_t len = strlen(Str); auto* Dst = Allocate(len + 1); for (size_t i = 0; i < len; ++i) { @@ -171,28 +178,24 @@ public: return Dst; } - NDDISCARD Char* CopyString(const String& Str) + NODISCARD Char* CopyString(const String& Str) { - size_t len = Str.length() + 1; - Char* Dst = Allocate(len + 1); - std::memcpy(Dst, Str.c_str(), sizeof(Char) * len); - Dst[len] = 0; - return Dst; + return CopyString(Str.c_str(), Str.length()); } private: struct Block { - uint8_t* Page = nullptr; - size_t Size = 0; - size_t Capacity = 0; + uint8_t* const Data = nullptr; + size_t const Size = 0; + uint8_t* CurrPtr = nullptr; - Block(void* _Page, size_t _Size, size_t _Capacity) : - Page{static_cast(_Page)}, Size{_Size}, Capacity{_Capacity} {} + Block(void* _Data, size_t _Size) : + Data{static_cast(_Data)}, Size{_Size}, CurrPtr{Data} {} }; std::vector m_Blocks; - Uint32 m_BlockSize = 4 << 10; + const Uint32 m_BlockSize = 4 << 10; IMemoryAllocator* m_pAllocator = nullptr; }; diff --git a/Common/interface/LinearAllocator.hpp b/Common/interface/LinearAllocator.hpp index 05049dff..34276aea 100644 --- a/Common/interface/LinearAllocator.hpp +++ b/Common/interface/LinearAllocator.hpp @@ -35,7 +35,7 @@ #include "../../Primitives/interface/BasicTypes.h" #include "../../Primitives/interface/MemoryAllocator.h" #include "../../Platforms/Basic/interface/DebugUtilities.hpp" -#include "Definitions.hpp" +#include "CompilerDefinitions.h" #include "Align.hpp" namespace Diligent @@ -81,20 +81,20 @@ public: Reset(); } - NDDISCARD void* Release() + NODISCARD void* Release() { void* Ptr = m_pDataStart; Reset(); return Ptr; } - NDDISCARD void* ReleaseOwnership() noexcept + NODISCARD void* ReleaseOwnership() noexcept { m_pAllocator = nullptr; return GetDataPtr(); } - NDDISCARD void* GetDataPtr() const noexcept + NODISCARD void* GetDataPtr() const noexcept { return m_pDataStart; } @@ -169,7 +169,7 @@ public: m_CurrAlignment = sizeof(void*); } - NDDISCARD void* Allocate(size_t size, size_t alignment) + NODISCARD void* Allocate(size_t size, size_t alignment) { VERIFY(size == 0 || m_pDataStart != nullptr, "Memory has not been allocated"); VERIFY(IsPowerOfTwo(alignment), "Alignment is not a power of two!"); @@ -202,13 +202,13 @@ public: } template - NDDISCARD T* Allocate(size_t count = 1) + NODISCARD T* Allocate(size_t count = 1) { return reinterpret_cast(Allocate(sizeof(T) * count, alignof(T))); } template - NDDISCARD T* Construct(Args&&... args) + NODISCARD T* Construct(Args&&... args) { T* Ptr = Allocate(); new (Ptr) T{std::forward(args)...}; @@ -216,7 +216,7 @@ public: } template - NDDISCARD T* ConstructArray(size_t count, const Args&... args) + NODISCARD T* ConstructArray(size_t count, const Args&... args) { T* Ptr = Allocate(count); for (size_t i = 0; i < count; ++i) @@ -227,13 +227,13 @@ public: } template - NDDISCARD T* Copy(const T& Src) + NODISCARD T* Copy(const T& Src) { return Construct(Src); } template - NDDISCARD T* CopyArray(const T* Src, size_t count) + NODISCARD T* CopyArray(const T* Src, size_t count) { T* Dst = Allocate(count); for (size_t i = 0; i < count; ++i) @@ -243,7 +243,7 @@ public: return Dst; } - NDDISCARD Char* CopyString(const char* Str) + NODISCARD Char* CopyString(const char* Str) { if (Str == nullptr) return nullptr; @@ -265,17 +265,18 @@ public: return Ptr; } - NDDISCARD Char* CopyString(const std::string& Str) + NODISCARD Char* CopyString(const std::string& Str) { return CopyString(Str.c_str()); } - NDDISCARD size_t GetCurrentSize() const + NODISCARD size_t GetCurrentSize() const { + VERIFY(m_pDataStart != nullptr, "Memory has not been allocated"); return static_cast(m_pCurrPtr - m_pDataStart); } - NDDISCARD size_t GetReservedSize() const + NODISCARD size_t GetReservedSize() const { return m_ReservedSize; } -- cgit v1.2.3