From 24c36a1295817d3e648e5536c625ddd532f4db0d Mon Sep 17 00:00:00 2001 From: Bryan Bernhart Date: Thu, 21 Apr 2022 15:21:15 -0700 Subject: [PATCH] Add type name information to memory allocators. --- src/gpgmm/BuddyMemoryAllocator.cpp | 4 +++ src/gpgmm/BuddyMemoryAllocator.h | 1 + src/gpgmm/ConditionalMemoryAllocator.cpp | 31 +++++++--------------- src/gpgmm/ConditionalMemoryAllocator.h | 1 + src/gpgmm/MemoryAllocator.cpp | 4 +++ src/gpgmm/MemoryAllocator.h | 2 ++ src/gpgmm/PooledMemoryAllocator.cpp | 4 +++ src/gpgmm/PooledMemoryAllocator.h | 1 + src/gpgmm/SegmentedMemoryAllocator.cpp | 4 +++ src/gpgmm/SegmentedMemoryAllocator.h | 1 + src/gpgmm/SlabMemoryAllocator.cpp | 4 +++ src/gpgmm/SlabMemoryAllocator.h | 2 ++ src/gpgmm/StandaloneMemoryAllocator.cpp | 4 +++ src/gpgmm/StandaloneMemoryAllocator.h | 1 + src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp | 2 +- src/gpgmm/d3d12/ResourceAllocatorD3D12.h | 2 +- 16 files changed, 45 insertions(+), 23 deletions(-) diff --git a/src/gpgmm/BuddyMemoryAllocator.cpp b/src/gpgmm/BuddyMemoryAllocator.cpp index 76eb20ebd..2417b48ea 100644 --- a/src/gpgmm/BuddyMemoryAllocator.cpp +++ b/src/gpgmm/BuddyMemoryAllocator.cpp @@ -152,6 +152,10 @@ namespace gpgmm { return result; } + const char* BuddyMemoryAllocator::GetTypename() const { + return "BuddyMemoryAllocator"; + } + uint64_t BuddyMemoryAllocator::GetBuddyMemorySizeForTesting() const { std::lock_guard lock(mMutex); diff --git a/src/gpgmm/BuddyMemoryAllocator.h b/src/gpgmm/BuddyMemoryAllocator.h index de5f4d8ba..6a71b3757 100644 --- a/src/gpgmm/BuddyMemoryAllocator.h +++ b/src/gpgmm/BuddyMemoryAllocator.h @@ -53,6 +53,7 @@ namespace gpgmm { uint64_t GetMemorySize() const override; uint64_t GetMemoryAlignment() const override; MEMORY_ALLOCATOR_INFO GetInfo() const override; + const char* GetTypename() const override; uint64_t GetBuddyMemorySizeForTesting() const; diff --git a/src/gpgmm/ConditionalMemoryAllocator.cpp b/src/gpgmm/ConditionalMemoryAllocator.cpp index 1f548cca6..729fc2620 100644 --- a/src/gpgmm/ConditionalMemoryAllocator.cpp +++ b/src/gpgmm/ConditionalMemoryAllocator.cpp @@ -45,32 +45,21 @@ namespace gpgmm { } } - MEMORY_ALLOCATOR_INFO ConditionalMemoryAllocator::GetInfo() const { - std::lock_guard lock(mMutex); + void ConditionalMemoryAllocator::DeallocateMemory( + std::unique_ptr allocation) { + // ConditionalMemoryAllocator cannot allocate memory itself, so it must not deallocate. + allocation->GetAllocator()->DeallocateMemory(std::move(allocation)); + } + MEMORY_ALLOCATOR_INFO ConditionalMemoryAllocator::GetInfo() const { MEMORY_ALLOCATOR_INFO result = {}; - { - const MEMORY_ALLOCATOR_INFO& info = mFirstAllocator->GetInfo(); - result.FreeMemoryUsage += info.FreeMemoryUsage; - result.UsedBlockCount += info.UsedBlockCount; - result.UsedMemoryUsage += info.UsedMemoryUsage; - result.UsedMemoryCount += info.UsedMemoryCount; - } - { - const MEMORY_ALLOCATOR_INFO& info = mSecondAllocator->GetInfo(); - result.FreeMemoryUsage += info.FreeMemoryUsage; - result.UsedBlockCount += info.UsedBlockCount; - result.UsedMemoryUsage += info.UsedMemoryUsage; - result.UsedMemoryCount += info.UsedMemoryCount; - } - + result += mFirstAllocator->GetInfo(); + result += mSecondAllocator->GetInfo(); return result; } - void ConditionalMemoryAllocator::DeallocateMemory( - std::unique_ptr allocation) { - // ConditionalMemoryAllocator cannot allocate memory itself, so it must not deallocate. - allocation->GetAllocator()->DeallocateMemory(std::move(allocation)); + const char* ConditionalMemoryAllocator::GetTypename() const { + return "ConditionalMemoryAllocator"; } MemoryAllocator* ConditionalMemoryAllocator::GetFirstAllocatorForTesting() const { diff --git a/src/gpgmm/ConditionalMemoryAllocator.h b/src/gpgmm/ConditionalMemoryAllocator.h index f16b9d9ef..ec788c71d 100644 --- a/src/gpgmm/ConditionalMemoryAllocator.h +++ b/src/gpgmm/ConditionalMemoryAllocator.h @@ -38,6 +38,7 @@ namespace gpgmm { void DeallocateMemory(std::unique_ptr allocation) override; MEMORY_ALLOCATOR_INFO GetInfo() const override; + const char* GetTypename() const override; MemoryAllocator* GetFirstAllocatorForTesting() const; MemoryAllocator* GetSecondAllocatorForTesting() const; diff --git a/src/gpgmm/MemoryAllocator.cpp b/src/gpgmm/MemoryAllocator.cpp index 62cbd919f..cf7e6785f 100644 --- a/src/gpgmm/MemoryAllocator.cpp +++ b/src/gpgmm/MemoryAllocator.cpp @@ -110,4 +110,8 @@ namespace gpgmm { return mInfo; } + const char* MemoryAllocator::GetTypename() const { + return "MemoryAllocator"; + } + } // namespace gpgmm diff --git a/src/gpgmm/MemoryAllocator.h b/src/gpgmm/MemoryAllocator.h index 0340f5d09..c17e48cb7 100644 --- a/src/gpgmm/MemoryAllocator.h +++ b/src/gpgmm/MemoryAllocator.h @@ -125,6 +125,8 @@ namespace gpgmm { // Should be overridden when a child allocator or block allocator is used. virtual MEMORY_ALLOCATOR_INFO GetInfo() const; + virtual const char* GetTypename() const; + protected: // Combine TryAllocateBlock and TryAllocateMemory into a single call so a partial // or uninitalized memory allocation cannot be created. If memory cannot be allocated for diff --git a/src/gpgmm/PooledMemoryAllocator.cpp b/src/gpgmm/PooledMemoryAllocator.cpp index 365b6e144..b330a53d5 100644 --- a/src/gpgmm/PooledMemoryAllocator.cpp +++ b/src/gpgmm/PooledMemoryAllocator.cpp @@ -71,4 +71,8 @@ namespace gpgmm { return mPool->GetMemorySize(); } + const char* PooledMemoryAllocator::GetTypename() const { + return "PooledMemoryAllocator"; + } + } // namespace gpgmm diff --git a/src/gpgmm/PooledMemoryAllocator.h b/src/gpgmm/PooledMemoryAllocator.h index c6364f803..cf11d6450 100644 --- a/src/gpgmm/PooledMemoryAllocator.h +++ b/src/gpgmm/PooledMemoryAllocator.h @@ -35,6 +35,7 @@ namespace gpgmm { bool prefetchMemory) override; void DeallocateMemory(std::unique_ptr allocation) override; uint64_t GetMemorySize() const override; + const char* GetTypename() const override; private: MemoryPool* const mPool; diff --git a/src/gpgmm/SegmentedMemoryAllocator.cpp b/src/gpgmm/SegmentedMemoryAllocator.cpp index 1d9e45df3..6fa0f59a4 100644 --- a/src/gpgmm/SegmentedMemoryAllocator.cpp +++ b/src/gpgmm/SegmentedMemoryAllocator.cpp @@ -212,4 +212,8 @@ namespace gpgmm { return count; } + const char* SegmentedMemoryAllocator::GetTypename() const { + return "SegmentedMemoryAllocator"; + } + } // namespace gpgmm diff --git a/src/gpgmm/SegmentedMemoryAllocator.h b/src/gpgmm/SegmentedMemoryAllocator.h index b72f0dfa2..7f1599fbe 100644 --- a/src/gpgmm/SegmentedMemoryAllocator.h +++ b/src/gpgmm/SegmentedMemoryAllocator.h @@ -47,6 +47,7 @@ namespace gpgmm { void DeallocateMemory(std::unique_ptr allocation) override; void ReleaseMemory() override; uint64_t GetMemoryAlignment() const override; + const char* GetTypename() const override; uint64_t GetSegmentSizeForTesting() const; diff --git a/src/gpgmm/SlabMemoryAllocator.cpp b/src/gpgmm/SlabMemoryAllocator.cpp index 95e639efd..ca1527de8 100644 --- a/src/gpgmm/SlabMemoryAllocator.cpp +++ b/src/gpgmm/SlabMemoryAllocator.cpp @@ -388,6 +388,10 @@ namespace gpgmm { return GetFirstChild()->GetMemorySize(); } + const char* SlabCacheAllocator::GetTypename() const { + return "SlabCacheAllocator"; + } + uint64_t SlabCacheAllocator::GetSlabCacheSizeForTesting() const { std::lock_guard lock(mMutex); diff --git a/src/gpgmm/SlabMemoryAllocator.h b/src/gpgmm/SlabMemoryAllocator.h index 9780e1100..8a65eea9f 100644 --- a/src/gpgmm/SlabMemoryAllocator.h +++ b/src/gpgmm/SlabMemoryAllocator.h @@ -149,6 +149,8 @@ namespace gpgmm { uint64_t GetMemorySize() const override; + const char* GetTypename() const override; + uint64_t GetSlabCacheSizeForTesting() const; private: diff --git a/src/gpgmm/StandaloneMemoryAllocator.cpp b/src/gpgmm/StandaloneMemoryAllocator.cpp index 0844cce89..e2863291b 100644 --- a/src/gpgmm/StandaloneMemoryAllocator.cpp +++ b/src/gpgmm/StandaloneMemoryAllocator.cpp @@ -62,4 +62,8 @@ namespace gpgmm { result += GetFirstChild()->GetInfo(); return result; } + + const char* StandaloneMemoryAllocator::GetTypename() const { + return "StandaloneMemoryAllocator"; + } } // namespace gpgmm diff --git a/src/gpgmm/StandaloneMemoryAllocator.h b/src/gpgmm/StandaloneMemoryAllocator.h index 7db84847c..42943af3d 100644 --- a/src/gpgmm/StandaloneMemoryAllocator.h +++ b/src/gpgmm/StandaloneMemoryAllocator.h @@ -35,6 +35,7 @@ namespace gpgmm { void DeallocateMemory(std::unique_ptr subAllocation) override; MEMORY_ALLOCATOR_INFO GetInfo() const override; + const char* GetTypename() const override; }; } // namespace gpgmm diff --git a/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp b/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp index 53a34a49c..fb9f249b1 100644 --- a/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp +++ b/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp @@ -305,7 +305,7 @@ namespace gpgmm { namespace d3d12 { if (allocation == nullptr) { DebugEvent("ResourceAllocator.TryAllocateResource", ALLOCATOR_MESSAGE_ID_RESOURCE_ALLOCATION_FAILED) - << "Resource memory could not be allocated."; + << std::string(allocator->GetTypename()) + " failed to allocate memory."; return E_FAIL; } diff --git a/src/gpgmm/d3d12/ResourceAllocatorD3D12.h b/src/gpgmm/d3d12/ResourceAllocatorD3D12.h index 53d44a428..b67e8c3a4 100644 --- a/src/gpgmm/d3d12/ResourceAllocatorD3D12.h +++ b/src/gpgmm/d3d12/ResourceAllocatorD3D12.h @@ -282,7 +282,7 @@ namespace gpgmm { namespace d3d12 { // Return the current allocator usage. RESOURCE_ALLOCATOR_INFO GetInfo() const override; - const char* GetTypename() const; + const char* GetTypename() const override; private: friend BufferAllocator;