diff --git a/include/gpgmm_d3d12.h b/include/gpgmm_d3d12.h index 9b2734ef0..7da387cb8 100644 --- a/include/gpgmm_d3d12.h +++ b/include/gpgmm_d3d12.h @@ -671,7 +671,7 @@ namespace gpgmm::d3d12 { ALLOCATION_METHOD Method; }; - /** \brief ResourceAllocation is MemoryAllocation that contains a ID3D12Resource. + /** \brief ResourceAllocation is an allocation that contains a ID3D12Resource. It can represent a allocation using a resource in one of three ways: 1) ID3D12Resource "placed" in a ID3D12Heap, 2) a ID3D12Resource at a specific offset, or 3) a ID3D12Resource without a diff --git a/src/gpgmm/common/Backend.h b/src/gpgmm/common/Backend.h index 47bb70200..fc7fc19ad 100644 --- a/src/gpgmm/common/Backend.h +++ b/src/gpgmm/common/Backend.h @@ -18,14 +18,14 @@ namespace gpgmm { // Forward declare common types. - class MemoryAllocation; + class MemoryAllocationBase; template struct CommonTrait; // Define common types. template - struct CommonTrait { + struct CommonTrait { using CommonType = typename BackendTrait::AllocationType; }; diff --git a/src/gpgmm/common/BuddyMemoryAllocator.cpp b/src/gpgmm/common/BuddyMemoryAllocator.cpp index 6c54f4f81..79be5b25c 100644 --- a/src/gpgmm/common/BuddyMemoryAllocator.cpp +++ b/src/gpgmm/common/BuddyMemoryAllocator.cpp @@ -41,7 +41,7 @@ namespace gpgmm { return static_cast(SafeDivide(offset, mMemorySize)); } - ResultOrError> BuddyMemoryAllocator::TryAllocateMemory( + ResultOrError> BuddyMemoryAllocator::TryAllocateMemory( const MemoryAllocationRequest& request) { GPGMM_TRACE_EVENT_DURATION(TraceEventCategory::kDefault, "BuddyMemoryAllocator.TryAllocateMemory"); @@ -57,13 +57,13 @@ namespace gpgmm { GPGMM_RETURN_INVALID_IF(allocationSize > mMemorySize); // Attempt to sub-allocate a block of the requested size. - std::unique_ptr subAllocation; + std::unique_ptr subAllocation; GPGMM_TRY_ASSIGN( TrySubAllocateMemory( &mBuddyBlockAllocator, allocationSize, request.Alignment, request.NeverAllocate, [&](const auto& block) -> ResultOrError { const uint64_t memoryIndex = GetMemoryIndex(block->Offset); - MemoryAllocation memoryAllocation = mUsedPool.AcquireFromPool(memoryIndex); + MemoryAllocationBase memoryAllocation = mUsedPool.AcquireFromPool(memoryIndex); // No existing, allocate new memory for the block. if (memoryAllocation == GPGMM_INVALID_ALLOCATION) { @@ -71,8 +71,9 @@ namespace gpgmm { newRequest.SizeInBytes = mMemorySize; newRequest.Alignment = mMemoryAlignment; - ResultOrError> memoryAllocationResult = - GetNextInChain()->TryAllocateMemory(newRequest); + ResultOrError> + memoryAllocationResult = + GetNextInChain()->TryAllocateMemory(newRequest); if (!memoryAllocationResult.IsSuccess()) { return memoryAllocationResult.GetErrorCode(); } @@ -94,12 +95,13 @@ namespace gpgmm { // Memory allocation offset is always memory-relative. const uint64_t memoryOffset = block->Offset % mMemorySize; - return std::make_unique(/*allocator*/ this, subAllocation->GetMemory(), - memoryOffset, AllocationMethod::kSubAllocated, - block, request.SizeInBytes); + return std::make_unique( + /*allocator*/ this, subAllocation->GetMemory(), memoryOffset, + AllocationMethod::kSubAllocated, block, request.SizeInBytes); } - void BuddyMemoryAllocator::DeallocateMemory(std::unique_ptr subAllocation) { + void BuddyMemoryAllocator::DeallocateMemory( + std::unique_ptr subAllocation) { std::lock_guard lock(mMutex); GPGMM_TRACE_EVENT_DURATION(TraceEventCategory::kDefault, @@ -114,14 +116,14 @@ namespace gpgmm { mBuddyBlockAllocator.DeallocateBlock(subAllocation->GetBlock()); - MemoryAllocation memoryAllocation = mUsedPool.AcquireFromPool(memoryIndex); + MemoryAllocationBase memoryAllocation = mUsedPool.AcquireFromPool(memoryIndex); MemoryBase* memory = memoryAllocation.GetMemory(); ASSERT(memory != nullptr); if (memory->Unref()) { GetNextInChain()->DeallocateMemory( - std::make_unique(memoryAllocation)); + std::make_unique(memoryAllocation)); } else { mUsedPool.ReturnToPool(memoryAllocation, memoryIndex); } diff --git a/src/gpgmm/common/BuddyMemoryAllocator.h b/src/gpgmm/common/BuddyMemoryAllocator.h index e1766dfe7..37ca3bffd 100644 --- a/src/gpgmm/common/BuddyMemoryAllocator.h +++ b/src/gpgmm/common/BuddyMemoryAllocator.h @@ -43,9 +43,9 @@ namespace gpgmm { std::unique_ptr memoryAllocator); // MemoryAllocatorBase interface - ResultOrError> TryAllocateMemory( + ResultOrError> TryAllocateMemory( const MemoryAllocationRequest& request) override; - void DeallocateMemory(std::unique_ptr subAllocation) override; + void DeallocateMemory(std::unique_ptr subAllocation) override; uint64_t GetMemorySize() const override; uint64_t GetMemoryAlignment() const override; diff --git a/src/gpgmm/common/ConditionalMemoryAllocator.cpp b/src/gpgmm/common/ConditionalMemoryAllocator.cpp index d395f9dae..d19f1a505 100644 --- a/src/gpgmm/common/ConditionalMemoryAllocator.cpp +++ b/src/gpgmm/common/ConditionalMemoryAllocator.cpp @@ -28,8 +28,8 @@ namespace gpgmm { mConditionalSize(conditionalSize) { } - ResultOrError> ConditionalMemoryAllocator::TryAllocateMemory( - const MemoryAllocationRequest& request) { + ResultOrError> + ConditionalMemoryAllocator::TryAllocateMemory(const MemoryAllocationRequest& request) { GPGMM_TRACE_EVENT_DURATION(TraceEventCategory::kDefault, "ConditionalMemoryAllocator.TryAllocateMemory"); if (request.SizeInBytes <= mConditionalSize) { @@ -40,7 +40,7 @@ namespace gpgmm { } void ConditionalMemoryAllocator::DeallocateMemory( - std::unique_ptr allocation) { + std::unique_ptr allocation) { // ConditionalMemoryAllocator cannot allocate memory itself, so it must not deallocate. allocation->GetAllocator()->DeallocateMemory(std::move(allocation)); } diff --git a/src/gpgmm/common/ConditionalMemoryAllocator.h b/src/gpgmm/common/ConditionalMemoryAllocator.h index 0aad2977b..99b2b97a1 100644 --- a/src/gpgmm/common/ConditionalMemoryAllocator.h +++ b/src/gpgmm/common/ConditionalMemoryAllocator.h @@ -30,9 +30,9 @@ namespace gpgmm { ~ConditionalMemoryAllocator() override = default; // MemoryAllocatorBase interface - ResultOrError> TryAllocateMemory( + ResultOrError> TryAllocateMemory( const MemoryAllocationRequest& request) override; - void DeallocateMemory(std::unique_ptr allocation) override; + void DeallocateMemory(std::unique_ptr allocation) override; MemoryAllocatorStats GetStats() const override; diff --git a/src/gpgmm/common/DedicatedMemoryAllocator.cpp b/src/gpgmm/common/DedicatedMemoryAllocator.cpp index dd630a087..eed001ec2 100644 --- a/src/gpgmm/common/DedicatedMemoryAllocator.cpp +++ b/src/gpgmm/common/DedicatedMemoryAllocator.cpp @@ -26,8 +26,8 @@ namespace gpgmm { : MemoryAllocatorBase(std::move(memoryAllocator)), mMemoryAlignment(memoryAlignment) { } - ResultOrError> DedicatedMemoryAllocator::TryAllocateMemory( - const MemoryAllocationRequest& request) { + ResultOrError> + DedicatedMemoryAllocator::TryAllocateMemory(const MemoryAllocationRequest& request) { GPGMM_TRACE_EVENT_DURATION(TraceEventCategory::kDefault, "DedicatedMemoryAllocator.TryAllocateMemory"); @@ -39,7 +39,7 @@ namespace gpgmm { memoryRequest.Alignment = mMemoryAlignment; memoryRequest.SizeInBytes = AlignTo(request.SizeInBytes, mMemoryAlignment); - std::unique_ptr allocation; + std::unique_ptr allocation; GPGMM_TRY_ASSIGN(GetNextInChain()->TryAllocateMemory(memoryRequest), allocation); if (memoryRequest.SizeInBytes > request.SizeInBytes) { @@ -51,12 +51,13 @@ namespace gpgmm { mStats.UsedBlockCount++; mStats.UsedBlockUsage += allocation->GetSize(); - return std::make_unique( + return std::make_unique( this, allocation->GetMemory(), /*offset*/ 0, allocation->GetMethod(), new MemoryBlock{0, allocation->GetSize()}, request.SizeInBytes); } - void DedicatedMemoryAllocator::DeallocateMemory(std::unique_ptr allocation) { + void DedicatedMemoryAllocator::DeallocateMemory( + std::unique_ptr allocation) { GPGMM_TRACE_EVENT_DURATION(TraceEventCategory::kDefault, "DedicatedMemoryAllocator.DeallocateMemory"); diff --git a/src/gpgmm/common/DedicatedMemoryAllocator.h b/src/gpgmm/common/DedicatedMemoryAllocator.h index b39106ef7..bf5ce46d2 100644 --- a/src/gpgmm/common/DedicatedMemoryAllocator.h +++ b/src/gpgmm/common/DedicatedMemoryAllocator.h @@ -29,9 +29,9 @@ namespace gpgmm { uint64_t memoryAlignment); // MemoryAllocatorBase interface - ResultOrError> TryAllocateMemory( + ResultOrError> TryAllocateMemory( const MemoryAllocationRequest& request) override; - void DeallocateMemory(std::unique_ptr subAllocation) override; + void DeallocateMemory(std::unique_ptr subAllocation) override; uint64_t GetMemoryAlignment() const override; MemoryAllocatorStats GetStats() const override; diff --git a/src/gpgmm/common/IndexedMemoryPool.cpp b/src/gpgmm/common/IndexedMemoryPool.cpp index 8d4d020f4..2918d15f8 100644 --- a/src/gpgmm/common/IndexedMemoryPool.cpp +++ b/src/gpgmm/common/IndexedMemoryPool.cpp @@ -22,16 +22,16 @@ namespace gpgmm { IndexedMemoryPool::IndexedMemoryPool(uint64_t memorySize) : MemoryPoolBase(memorySize) { } - MemoryAllocation IndexedMemoryPool::AcquireFromPool(uint64_t indexInPool) { + MemoryAllocationBase IndexedMemoryPool::AcquireFromPool(uint64_t indexInPool) { if (indexInPool >= mPool.size()) { mPool.resize(indexInPool + 1); } - MemoryAllocation tmp = mPool[indexInPool]; + MemoryAllocationBase tmp = mPool[indexInPool]; mPool[indexInPool] = {}; // invalidate it return tmp; } - void IndexedMemoryPool::ReturnToPool(MemoryAllocation allocation, uint64_t indexInPool) { + void IndexedMemoryPool::ReturnToPool(MemoryAllocationBase allocation, uint64_t indexInPool) { ASSERT(indexInPool < mPool.size()); ASSERT(allocation.GetSize() == GetMemorySize()); mPool[indexInPool] = std::move(allocation); diff --git a/src/gpgmm/common/IndexedMemoryPool.h b/src/gpgmm/common/IndexedMemoryPool.h index 9f5106eb0..a30429fbf 100644 --- a/src/gpgmm/common/IndexedMemoryPool.h +++ b/src/gpgmm/common/IndexedMemoryPool.h @@ -27,14 +27,14 @@ namespace gpgmm { ~IndexedMemoryPool() override = default; // MemoryPoolBase interface - MemoryAllocation AcquireFromPool(uint64_t indexInPool) override; - void ReturnToPool(MemoryAllocation allocation, uint64_t indexInPool) override; + MemoryAllocationBase AcquireFromPool(uint64_t indexInPool) override; + void ReturnToPool(MemoryAllocationBase allocation, uint64_t indexInPool) override; uint64_t ReleasePool(uint64_t bytesToRelease) override; uint64_t GetPoolSize() const override; private: - std::vector mPool; + std::vector mPool; }; } // namespace gpgmm diff --git a/src/gpgmm/common/LIFOMemoryPool.cpp b/src/gpgmm/common/LIFOMemoryPool.cpp index 55bc44ab2..f6adb4452 100644 --- a/src/gpgmm/common/LIFOMemoryPool.cpp +++ b/src/gpgmm/common/LIFOMemoryPool.cpp @@ -24,10 +24,10 @@ namespace gpgmm { LIFOMemoryPool::LIFOMemoryPool(uint64_t memorySize) : MemoryPoolBase(memorySize) { } - MemoryAllocation LIFOMemoryPool::AcquireFromPool(uint64_t indexInPool) { + MemoryAllocationBase LIFOMemoryPool::AcquireFromPool(uint64_t indexInPool) { ASSERT(indexInPool == kInvalidIndex); - MemoryAllocation allocation = {}; + MemoryAllocationBase allocation = {}; if (!mPool.empty()) { allocation = std::move(mPool.front()); mPool.pop_front(); @@ -35,7 +35,7 @@ namespace gpgmm { return allocation; } - void LIFOMemoryPool::ReturnToPool(MemoryAllocation allocation, uint64_t indexInPool) { + void LIFOMemoryPool::ReturnToPool(MemoryAllocationBase allocation, uint64_t indexInPool) { ASSERT(indexInPool == kInvalidIndex); ASSERT(allocation.GetSize() == GetMemorySize()); diff --git a/src/gpgmm/common/LIFOMemoryPool.h b/src/gpgmm/common/LIFOMemoryPool.h index 703fff554..ffa948d38 100644 --- a/src/gpgmm/common/LIFOMemoryPool.h +++ b/src/gpgmm/common/LIFOMemoryPool.h @@ -28,15 +28,15 @@ namespace gpgmm { ~LIFOMemoryPool() override = default; // MemoryPoolBase interface - MemoryAllocation AcquireFromPool(uint64_t indexInPool = kInvalidIndex) override; - void ReturnToPool(MemoryAllocation allocation, + MemoryAllocationBase AcquireFromPool(uint64_t indexInPool = kInvalidIndex) override; + void ReturnToPool(MemoryAllocationBase allocation, uint64_t indexInPool = kInvalidIndex) override; uint64_t ReleasePool(uint64_t bytesToFree = kInvalidSize) override; uint64_t GetPoolSize() const override; private: - std::deque mPool; + std::deque mPool; }; } // namespace gpgmm diff --git a/src/gpgmm/common/MemoryAllocation.cpp b/src/gpgmm/common/MemoryAllocation.cpp index c550c32e8..0800528b9 100644 --- a/src/gpgmm/common/MemoryAllocation.cpp +++ b/src/gpgmm/common/MemoryAllocation.cpp @@ -21,7 +21,7 @@ namespace gpgmm { - MemoryAllocation::MemoryAllocation() + MemoryAllocationBase::MemoryAllocationBase() : mAllocator(nullptr), mMemory(nullptr), mOffset(kInvalidOffset), @@ -32,12 +32,12 @@ namespace gpgmm { mBlock(nullptr) { } - MemoryAllocation::MemoryAllocation(MemoryAllocatorBase* allocator, - MemoryBase* memory, - uint64_t offset, - AllocationMethod method, - MemoryBlock* block, - uint64_t requestSize) + MemoryAllocationBase::MemoryAllocationBase(MemoryAllocatorBase* allocator, + MemoryBase* memory, + uint64_t offset, + AllocationMethod method, + MemoryBlock* block, + uint64_t requestSize) : mAllocator(allocator), mMemory(memory), mOffset(offset), @@ -48,9 +48,9 @@ namespace gpgmm { mBlock(block) { } - MemoryAllocation::MemoryAllocation(MemoryAllocatorBase* allocator, - MemoryBase* memory, - uint64_t requestSize) + MemoryAllocationBase::MemoryAllocationBase(MemoryAllocatorBase* allocator, + MemoryBase* memory, + uint64_t requestSize) : mAllocator(allocator), mMemory(memory), mOffset(0), @@ -61,24 +61,24 @@ namespace gpgmm { mBlock(nullptr) { } - bool MemoryAllocation::operator==(const MemoryAllocation& other) const { + bool MemoryAllocationBase::operator==(const MemoryAllocationBase& other) const { return (other.mAllocator == mAllocator && other.mMemory == mMemory && other.mOffset == mOffset && other.mMethod == mMethod && other.mBlock == mBlock); } - bool MemoryAllocation::operator!=(const MemoryAllocation& other) const { + bool MemoryAllocationBase::operator!=(const MemoryAllocationBase& other) const { return !operator==(other); } - MemoryBase* MemoryAllocation::GetMemory() const { + MemoryBase* MemoryAllocationBase::GetMemory() const { return mMemory; } - MemoryAllocatorBase* MemoryAllocation::GetAllocator() const { + MemoryAllocatorBase* MemoryAllocationBase::GetAllocator() const { return mAllocator; } - uint64_t MemoryAllocation::GetSize() const { + uint64_t MemoryAllocationBase::GetSize() const { switch (mMethod) { case gpgmm::AllocationMethod::kStandalone: ASSERT(mMemory != nullptr); @@ -95,7 +95,7 @@ namespace gpgmm { } } - uint64_t MemoryAllocation::GetRequestSize() const { + uint64_t MemoryAllocationBase::GetRequestSize() const { #ifdef GPGMM_ENABLE_MEMORY_ALIGN_CHECKS return mRequestSize; #else @@ -103,7 +103,7 @@ namespace gpgmm { #endif } - uint64_t MemoryAllocation::GetAlignment() const { + uint64_t MemoryAllocationBase::GetAlignment() const { switch (mMethod) { case gpgmm::AllocationMethod::kStandalone: ASSERT(mMemory != nullptr); @@ -122,15 +122,15 @@ namespace gpgmm { } } - uint64_t MemoryAllocation::GetOffset() const { + uint64_t MemoryAllocationBase::GetOffset() const { return mOffset; } - AllocationMethod MemoryAllocation::GetMethod() const { + AllocationMethod MemoryAllocationBase::GetMethod() const { return mMethod; } - MemoryBlock* MemoryAllocation::GetBlock() const { + MemoryBlock* MemoryAllocationBase::GetBlock() const { return mBlock; } diff --git a/src/gpgmm/common/MemoryAllocation.h b/src/gpgmm/common/MemoryAllocation.h index 1004ddbe8..dbb41f624 100644 --- a/src/gpgmm/common/MemoryAllocation.h +++ b/src/gpgmm/common/MemoryAllocation.h @@ -20,7 +20,7 @@ #include "gpgmm/utils/Limits.h" #define GPGMM_INVALID_ALLOCATION \ - MemoryAllocation { \ + MemoryAllocationBase { \ } namespace gpgmm { @@ -46,28 +46,30 @@ namespace gpgmm { class MemoryAllocatorBase; // Represents a location and range in memory. - class MemoryAllocation : public ObjectBase { + class MemoryAllocationBase : public ObjectBase { public: // Contructs an invalid memory allocation. - MemoryAllocation(); + MemoryAllocationBase(); // Constructs a "sub-allocated" memory allocation. - MemoryAllocation(MemoryAllocatorBase* allocator, - MemoryBase* memory, - uint64_t offset, - AllocationMethod method, - MemoryBlock* block, - uint64_t requestSize); + MemoryAllocationBase(MemoryAllocatorBase* allocator, + MemoryBase* memory, + uint64_t offset, + AllocationMethod method, + MemoryBlock* block, + uint64_t requestSize); // Constructs a "standalone" memory allocation. - MemoryAllocation(MemoryAllocatorBase* allocator, MemoryBase* memory, uint64_t requestSize); + MemoryAllocationBase(MemoryAllocatorBase* allocator, + MemoryBase* memory, + uint64_t requestSize); - virtual ~MemoryAllocation() override = default; + virtual ~MemoryAllocationBase() override = default; - MemoryAllocation(const MemoryAllocation&) = default; - MemoryAllocation& operator=(const MemoryAllocation&) = default; - bool operator==(const MemoryAllocation&) const; - bool operator!=(const MemoryAllocation& other) const; + MemoryAllocationBase(const MemoryAllocationBase&) = default; + MemoryAllocationBase& operator=(const MemoryAllocationBase&) = default; + bool operator==(const MemoryAllocationBase&) const; + bool operator!=(const MemoryAllocationBase& other) const; MemoryBase* GetMemory() const; MemoryAllocatorBase* GetAllocator() const; @@ -85,7 +87,7 @@ namespace gpgmm { private: // ObjectBase interface - DEFINE_OBJECT_BASE_OVERRIDES(MemoryAllocation) + DEFINE_OBJECT_BASE_OVERRIDES(MemoryAllocationBase) MemoryBase* mMemory; uint64_t mOffset; // Offset always local to the memory. diff --git a/src/gpgmm/common/MemoryAllocator.cpp b/src/gpgmm/common/MemoryAllocator.cpp index 7b13c8f2e..6b15d58ee 100644 --- a/src/gpgmm/common/MemoryAllocator.cpp +++ b/src/gpgmm/common/MemoryAllocator.cpp @@ -31,7 +31,7 @@ namespace gpgmm { mAllocation = mAllocator->TryAllocateMemory(mRequest); } - ResultOrError> AcquireAllocation() { + ResultOrError> AcquireAllocation() { std::lock_guard lock(mAllocationMutex); return std::move(mAllocation); } @@ -41,7 +41,7 @@ namespace gpgmm { const MemoryAllocationRequest mRequest; std::mutex mAllocationMutex; - ResultOrError> mAllocation; + ResultOrError> mAllocation; }; // MemoryAllocatorStats @@ -81,7 +81,7 @@ namespace gpgmm { return mEvent->Signal(); } - ResultOrError> MemoryAllocationEvent::AcquireAllocation() + ResultOrError> MemoryAllocationEvent::AcquireAllocation() const { return mTask->AcquireAllocation(); } @@ -116,13 +116,13 @@ namespace gpgmm { } } - ResultOrError> MemoryAllocatorBase::TryAllocateMemory( + ResultOrError> MemoryAllocatorBase::TryAllocateMemory( const MemoryAllocationRequest& request) { ASSERT(false); return {}; } - std::unique_ptr MemoryAllocatorBase::TryAllocateMemoryForTesting( + std::unique_ptr MemoryAllocatorBase::TryAllocateMemoryForTesting( const MemoryAllocationRequest& request) { return TryAllocateMemory(request).AcquireResult(); } @@ -205,7 +205,7 @@ namespace gpgmm { } void MemoryAllocatorBase::CheckAndReportAllocationMisalignment( - const MemoryAllocation& allocation) { + const MemoryAllocationBase& allocation) { if (allocation.GetSize() > allocation.GetRequestSize()) { WarnLog(MessageId::kAlignmentMismatch) << "Allocation is larger then the requested size (" + diff --git a/src/gpgmm/common/MemoryAllocator.h b/src/gpgmm/common/MemoryAllocator.h index 749f86e48..76d716a66 100644 --- a/src/gpgmm/common/MemoryAllocator.h +++ b/src/gpgmm/common/MemoryAllocator.h @@ -42,7 +42,7 @@ namespace gpgmm { // Event overrides void Wait() override; - ResultOrError> AcquireAllocation() const; + ResultOrError> AcquireAllocation() const; private: void Signal() override; @@ -143,14 +143,14 @@ namespace gpgmm { // Attempts creation of a memory allocation. // - // The returned MemoryAllocation is only valid for the lifetime of |this| + // The returned MemoryAllocationBase is only valid for the lifetime of |this| // MemoryAllocatorBase. - virtual ResultOrError> TryAllocateMemory( + virtual ResultOrError> TryAllocateMemory( const MemoryAllocationRequest& request); // Same as TryAllocateMemory above but leaves the result unwrapped for testing the result // directly. - std::unique_ptr TryAllocateMemoryForTesting( + std::unique_ptr TryAllocateMemoryForTesting( const MemoryAllocationRequest& request); // Non-blocking version of TryAllocateMemory. @@ -161,8 +161,8 @@ namespace gpgmm { // Free a memory allocation. // - // After DeallocateMemory is called, the MemoryAllocation is longer valid. - virtual void DeallocateMemory(std::unique_ptr allocation) = 0; + // After DeallocateMemory is called, the MemoryAllocationBase is longer valid. + virtual void DeallocateMemory(std::unique_ptr allocation) = 0; // Return free memory back to the OS. // @@ -207,7 +207,7 @@ namespace gpgmm { // or uninitalized memory allocation cannot be created. If memory cannot be allocated for // the block, the block will be deallocated instead of allowing it to leak. template - static ResultOrError> TrySubAllocateMemory( + static ResultOrError> TrySubAllocateMemory( BlockAllocator* allocator, uint64_t requestSize, uint64_t alignment, @@ -240,13 +240,13 @@ namespace gpgmm { // Caller is be responsible in fully initializing the memory allocation. // This is because TrySubAllocateMemory() does not necessarily know how to map the // final sub-allocated block to created memory. - return std::make_unique( + return std::make_unique( nullptr, memory, kInvalidOffset, AllocationMethod::kUndefined, block, requestSize); } void InsertIntoChain(std::unique_ptr next); - void CheckAndReportAllocationMisalignment(const MemoryAllocation& allocation); + void CheckAndReportAllocationMisalignment(const MemoryAllocationBase& allocation); MemoryAllocatorStats mStats = {}; diff --git a/src/gpgmm/common/MemoryPool.h b/src/gpgmm/common/MemoryPool.h index 19d57ee71..fb1329d8c 100644 --- a/src/gpgmm/common/MemoryPool.h +++ b/src/gpgmm/common/MemoryPool.h @@ -30,10 +30,10 @@ namespace gpgmm { virtual ~MemoryPoolBase(); // Retrieves a memory allocation from the pool using an optional index. - virtual MemoryAllocation AcquireFromPool(uint64_t indexInPool = kInvalidIndex) = 0; + virtual MemoryAllocationBase AcquireFromPool(uint64_t indexInPool = kInvalidIndex) = 0; // Returns a memory allocation back to the pool using an optional index. - virtual void ReturnToPool(MemoryAllocation allocation, + virtual void ReturnToPool(MemoryAllocationBase allocation, uint64_t indexInPool = kInvalidIndex) = 0; // Deallocate or shrink the pool. @@ -54,7 +54,7 @@ namespace gpgmm { for (auto& allocation : pool) { totalBytesReleased += allocation.GetSize(); allocation.GetAllocator()->DeallocateMemory( - std::make_unique(allocation)); + std::make_unique(allocation)); lastIndex++; if (totalBytesReleased >= bytesToRelease) { break; diff --git a/src/gpgmm/common/PooledMemoryAllocator.cpp b/src/gpgmm/common/PooledMemoryAllocator.cpp index c6e5a91e6..5a576f311 100644 --- a/src/gpgmm/common/PooledMemoryAllocator.cpp +++ b/src/gpgmm/common/PooledMemoryAllocator.cpp @@ -35,7 +35,7 @@ namespace gpgmm { mPool->ReleasePool(kInvalidSize); } - ResultOrError> PooledMemoryAllocator::TryAllocateMemory( + ResultOrError> PooledMemoryAllocator::TryAllocateMemory( const MemoryAllocationRequest& request) { GPGMM_TRACE_EVENT_DURATION(TraceEventCategory::kDefault, "PooledMemoryAllocator.TryAllocateMemory"); @@ -44,13 +44,13 @@ namespace gpgmm { GPGMM_RETURN_INVALID_IF(!ValidateRequest(request)); - MemoryAllocation allocation = mPool->AcquireFromPool(); + MemoryAllocationBase allocation = mPool->AcquireFromPool(); if (allocation == GPGMM_INVALID_ALLOCATION) { MemoryAllocationRequest memoryRequest = request; memoryRequest.Alignment = mMemoryAlignment; memoryRequest.SizeInBytes = AlignTo(request.SizeInBytes, mMemoryAlignment); - std::unique_ptr allocationPtr; + std::unique_ptr allocationPtr; GPGMM_TRY_ASSIGN(GetNextInChain()->TryAllocateMemory(memoryRequest), allocationPtr); allocation = *allocationPtr; } else { @@ -63,10 +63,10 @@ namespace gpgmm { MemoryBase* memory = allocation.GetMemory(); ASSERT(memory != nullptr); - return std::make_unique(this, memory, allocation.GetRequestSize()); + return std::make_unique(this, memory, allocation.GetRequestSize()); } - void PooledMemoryAllocator::DeallocateMemory(std::unique_ptr allocation) { + void PooledMemoryAllocator::DeallocateMemory(std::unique_ptr allocation) { GPGMM_TRACE_EVENT_DURATION(TraceEventCategory::kDefault, "PooledMemoryAllocator.DeallocateMemory"); @@ -81,7 +81,7 @@ namespace gpgmm { ASSERT(memory != nullptr); mPool->ReturnToPool( - MemoryAllocation(GetNextInChain(), memory, allocation->GetRequestSize())); + MemoryAllocationBase(GetNextInChain(), memory, allocation->GetRequestSize())); } uint64_t PooledMemoryAllocator::ReleaseMemory(uint64_t bytesToRelease) { diff --git a/src/gpgmm/common/PooledMemoryAllocator.h b/src/gpgmm/common/PooledMemoryAllocator.h index bdd76883a..a01f4ee94 100644 --- a/src/gpgmm/common/PooledMemoryAllocator.h +++ b/src/gpgmm/common/PooledMemoryAllocator.h @@ -30,9 +30,9 @@ namespace gpgmm { ~PooledMemoryAllocator() override; // MemoryAllocatorBase interface - ResultOrError> TryAllocateMemory( + ResultOrError> TryAllocateMemory( const MemoryAllocationRequest& request) override; - void DeallocateMemory(std::unique_ptr allocation) override; + void DeallocateMemory(std::unique_ptr allocation) override; uint64_t ReleaseMemory(uint64_t bytesToRelease = kInvalidSize) override; uint64_t GetMemorySize() const override; uint64_t GetMemoryAlignment() const override; diff --git a/src/gpgmm/common/SegmentedMemoryAllocator.cpp b/src/gpgmm/common/SegmentedMemoryAllocator.cpp index 3d9370801..eb3a28215 100644 --- a/src/gpgmm/common/SegmentedMemoryAllocator.cpp +++ b/src/gpgmm/common/SegmentedMemoryAllocator.cpp @@ -119,8 +119,8 @@ namespace gpgmm { return newFreeSegment; } - ResultOrError> SegmentedMemoryAllocator::TryAllocateMemory( - const MemoryAllocationRequest& request) { + ResultOrError> + SegmentedMemoryAllocator::TryAllocateMemory(const MemoryAllocationRequest& request) { GPGMM_TRACE_EVENT_DURATION(TraceEventCategory::kDefault, "SegmentedMemoryAllocator.TryAllocateMemory"); @@ -132,13 +132,13 @@ namespace gpgmm { MemorySegment* segment = GetOrCreateFreeSegment(memorySize); ASSERT(segment != nullptr); - MemoryAllocation allocation = segment->AcquireFromPool(); + MemoryAllocationBase allocation = segment->AcquireFromPool(); if (allocation == GPGMM_INVALID_ALLOCATION) { MemoryAllocationRequest memoryRequest = request; memoryRequest.Alignment = mMemoryAlignment; memoryRequest.SizeInBytes = AlignTo(request.SizeInBytes, mMemoryAlignment); - std::unique_ptr allocationPtr; + std::unique_ptr allocationPtr; GPGMM_TRY_ASSIGN(GetNextInChain()->TryAllocateMemory(memoryRequest), allocationPtr); allocation = *allocationPtr; } else { @@ -152,10 +152,11 @@ namespace gpgmm { ASSERT(memory != nullptr); memory->SetPool(segment); - return std::make_unique(this, memory, request.SizeInBytes); + return std::make_unique(this, memory, request.SizeInBytes); } - void SegmentedMemoryAllocator::DeallocateMemory(std::unique_ptr allocation) { + void SegmentedMemoryAllocator::DeallocateMemory( + std::unique_ptr allocation) { GPGMM_TRACE_EVENT_DURATION(TraceEventCategory::kDefault, "SegmentedMemoryAllocator.DeallocateMemory"); @@ -175,7 +176,7 @@ namespace gpgmm { ASSERT(pool != nullptr); static_cast(pool)->ReturnToPool( - MemoryAllocation(GetNextInChain(), memory, allocation->GetRequestSize())); + MemoryAllocationBase(GetNextInChain(), memory, allocation->GetRequestSize())); } uint64_t SegmentedMemoryAllocator::ReleaseMemory(uint64_t bytesToRelease) { diff --git a/src/gpgmm/common/SegmentedMemoryAllocator.h b/src/gpgmm/common/SegmentedMemoryAllocator.h index 8d1dcf90f..d728c3e06 100644 --- a/src/gpgmm/common/SegmentedMemoryAllocator.h +++ b/src/gpgmm/common/SegmentedMemoryAllocator.h @@ -39,9 +39,9 @@ namespace gpgmm { ~SegmentedMemoryAllocator() override; // MemoryAllocatorBase interface - ResultOrError> TryAllocateMemory( + ResultOrError> TryAllocateMemory( const MemoryAllocationRequest& request) override; - void DeallocateMemory(std::unique_ptr allocation) override; + void DeallocateMemory(std::unique_ptr allocation) override; uint64_t ReleaseMemory(uint64_t bytesToRelease = kInvalidSize) override; uint64_t GetMemoryAlignment() const override; diff --git a/src/gpgmm/common/SlabMemoryAllocator.cpp b/src/gpgmm/common/SlabMemoryAllocator.cpp index 494021a70..bf87a8a8c 100644 --- a/src/gpgmm/common/SlabMemoryAllocator.cpp +++ b/src/gpgmm/common/SlabMemoryAllocator.cpp @@ -71,7 +71,7 @@ namespace gpgmm { SlabBlockAllocator Allocator; uint64_t UsedBlocksPerSlab = 0; - MemoryAllocation Allocation; + MemoryAllocationBase Allocation; uint64_t IndexInList = kInvalidIndex; uint64_t IndexInCache = kInvalidIndex; }; @@ -106,7 +106,7 @@ namespace gpgmm { SlabMemoryAllocator::~SlabMemoryAllocator() { if (mNextSlabAllocationEvent != nullptr) { mNextSlabAllocationEvent->Wait(); - ResultOrError> result = + ResultOrError> result = mNextSlabAllocationEvent->AcquireAllocation(); if (result.IsSuccess()) { mMemoryAllocator->DeallocateMemory(result.AcquireResult()); @@ -195,7 +195,7 @@ namespace gpgmm { return cache; } - ResultOrError> SlabMemoryAllocator::TryAllocateMemory( + ResultOrError> SlabMemoryAllocator::TryAllocateMemory( const MemoryAllocationRequest& request) { GPGMM_TRACE_EVENT_DURATION(TraceEventCategory::kDefault, "SlabMemoryAllocator.TryAllocateMemory"); @@ -262,7 +262,7 @@ namespace gpgmm { ASSERT(pFreeSlab != nullptr); ASSERT(!pFreeSlab->IsFull()); - std::unique_ptr subAllocation; + std::unique_ptr subAllocation; GPGMM_TRY_ASSIGN( TrySubAllocateMemory( &pFreeSlab->Allocator, mBlockSize, request.Alignment, request.NeverAllocate, @@ -277,13 +277,13 @@ namespace gpgmm { if (mNextSlabAllocationEvent != nullptr) { // Resolve the pending pre-fetched allocation. mNextSlabAllocationEvent->Wait(); - ResultOrError> memoryAllocationResult = - mNextSlabAllocationEvent->AcquireAllocation(); + ResultOrError> + memoryAllocationResult = mNextSlabAllocationEvent->AcquireAllocation(); if (!memoryAllocationResult.IsSuccess()) { return memoryAllocationResult.GetErrorCode(); } - std::unique_ptr prefetchedSlabAllocation = + std::unique_ptr prefetchedSlabAllocation = memoryAllocationResult.AcquireResult(); mNextSlabAllocationEvent.reset(); @@ -313,7 +313,7 @@ namespace gpgmm { newSlabRequest.SizeInBytes = slabSize; newSlabRequest.Alignment = mSlabAlignment; - ResultOrError> slabAllocationResult = + ResultOrError> slabAllocationResult = mMemoryAllocator->TryAllocateMemory(newSlabRequest); if (!slabAllocationResult.IsSuccess()) { return slabAllocationResult.GetErrorCode(); @@ -395,12 +395,13 @@ namespace gpgmm { mStats.UsedBlockCount++; mStats.UsedBlockUsage += blockInSlab->Size; - return std::make_unique(this, subAllocation->GetMemory(), - offsetFromMemory, AllocationMethod::kSubAllocated, - blockInSlab, request.SizeInBytes); + return std::make_unique( + this, subAllocation->GetMemory(), offsetFromMemory, AllocationMethod::kSubAllocated, + blockInSlab, request.SizeInBytes); } - void SlabMemoryAllocator::DeallocateMemory(std::unique_ptr subAllocation) { + void SlabMemoryAllocator::DeallocateMemory( + std::unique_ptr subAllocation) { GPGMM_TRACE_EVENT_DURATION(TraceEventCategory::kDefault, "SlabMemoryAllocator.DeallocateMemory"); @@ -434,7 +435,7 @@ namespace gpgmm { if (pSlab->IsEmpty()) { mMemoryAllocator->DeallocateMemory( - std::make_unique(pSlab->Allocation)); + std::make_unique(pSlab->Allocation)); pSlab->Allocation = {}; // Invalidate it } } @@ -507,7 +508,7 @@ namespace gpgmm { mSizeCache.clear(); } - ResultOrError> SlabCacheAllocator::TryAllocateMemory( + ResultOrError> SlabCacheAllocator::TryAllocateMemory( const MemoryAllocationRequest& request) { GPGMM_TRACE_EVENT_DURATION(TraceEventCategory::kDefault, "SlabCacheAllocator.TryAllocateMemory"); @@ -532,18 +533,18 @@ namespace gpgmm { ASSERT(slabAllocator != nullptr); - std::unique_ptr subAllocation; + std::unique_ptr subAllocation; GPGMM_TRY_ASSIGN(slabAllocator->TryAllocateMemory(request), subAllocation); // Hold onto the cached allocator until the last allocation gets deallocated. entry->Ref(); - return std::make_unique( + return std::make_unique( this, subAllocation->GetMemory(), subAllocation->GetOffset(), subAllocation->GetMethod(), subAllocation->GetBlock(), request.SizeInBytes); } - void SlabCacheAllocator::DeallocateMemory(std::unique_ptr subAllocation) { + void SlabCacheAllocator::DeallocateMemory(std::unique_ptr subAllocation) { GPGMM_TRACE_EVENT_DURATION(TraceEventCategory::kDefault, "SlabCacheAllocator.DeallocateMemory"); diff --git a/src/gpgmm/common/SlabMemoryAllocator.h b/src/gpgmm/common/SlabMemoryAllocator.h index f704bc1c6..00400fc82 100644 --- a/src/gpgmm/common/SlabMemoryAllocator.h +++ b/src/gpgmm/common/SlabMemoryAllocator.h @@ -55,9 +55,9 @@ namespace gpgmm { ~SlabMemoryAllocator() override; // MemoryAllocatorBase interface - ResultOrError> TryAllocateMemory( + ResultOrError> TryAllocateMemory( const MemoryAllocationRequest& request) override; - void DeallocateMemory(std::unique_ptr allocation) override; + void DeallocateMemory(std::unique_ptr allocation) override; MemoryAllocatorStats GetStats() const override; @@ -122,9 +122,9 @@ namespace gpgmm { ~SlabCacheAllocator() override; // MemoryAllocatorBase interface - ResultOrError> TryAllocateMemory( + ResultOrError> TryAllocateMemory( const MemoryAllocationRequest& request) override; - void DeallocateMemory(std::unique_ptr allocation) override; + void DeallocateMemory(std::unique_ptr allocation) override; MemoryAllocatorStats GetStats() const override; diff --git a/src/gpgmm/d3d12/BufferAllocatorD3D12.cpp b/src/gpgmm/d3d12/BufferAllocatorD3D12.cpp index 5c09197a3..4ec72f44f 100644 --- a/src/gpgmm/d3d12/BufferAllocatorD3D12.cpp +++ b/src/gpgmm/d3d12/BufferAllocatorD3D12.cpp @@ -36,7 +36,7 @@ namespace gpgmm::d3d12 { mInitialResourceState(initialResourceState) { } - ResultOrError> BufferAllocator::TryAllocateMemory( + ResultOrError> BufferAllocator::TryAllocateMemory( const MemoryAllocationRequest& request) { GPGMM_TRACE_EVENT_DURATION(TraceEventCategory::kDefault, "BufferAllocator.TryAllocateMemory"); @@ -84,10 +84,11 @@ namespace gpgmm::d3d12 { mStats.UsedMemoryUsage += resourceHeap->GetSize(); mStats.UsedMemoryCount++; - return std::make_unique(this, resourceHeap.Detach(), request.SizeInBytes); + return std::make_unique(this, resourceHeap.Detach(), + request.SizeInBytes); } - void BufferAllocator::DeallocateMemory(std::unique_ptr allocation) { + void BufferAllocator::DeallocateMemory(std::unique_ptr allocation) { GPGMM_TRACE_EVENT_DURATION(TraceEventCategory::kDefault, "BufferAllocator.DeallocateMemory"); std::lock_guard lock(mMutex); diff --git a/src/gpgmm/d3d12/BufferAllocatorD3D12.h b/src/gpgmm/d3d12/BufferAllocatorD3D12.h index 0fc29c3ab..136076af8 100644 --- a/src/gpgmm/d3d12/BufferAllocatorD3D12.h +++ b/src/gpgmm/d3d12/BufferAllocatorD3D12.h @@ -33,9 +33,9 @@ namespace gpgmm::d3d12 { ~BufferAllocator() override = default; // MemoryAllocatorBase interface - ResultOrError> TryAllocateMemory( + ResultOrError> TryAllocateMemory( const MemoryAllocationRequest& request) override; - void DeallocateMemory(std::unique_ptr allocation) override; + void DeallocateMemory(std::unique_ptr allocation) override; private: ResourceAllocator* const mResourceAllocator; diff --git a/src/gpgmm/d3d12/ResourceAllocationD3D12.cpp b/src/gpgmm/d3d12/ResourceAllocationD3D12.cpp index daf14e330..b7d69c3e5 100644 --- a/src/gpgmm/d3d12/ResourceAllocationD3D12.cpp +++ b/src/gpgmm/d3d12/ResourceAllocationD3D12.cpp @@ -48,12 +48,12 @@ namespace gpgmm::d3d12 { ResidencyHeap* resourceHeap, MemoryBlock* block, ComPtr resource) - : MemoryAllocation(allocator, - resourceHeap, - desc.HeapOffset, - static_cast(desc.Method), - block, - desc.SizeInBytes), + : MemoryAllocationBase(allocator, + resourceHeap, + desc.HeapOffset, + static_cast(desc.Method), + block, + desc.SizeInBytes), mResidencyManager(residencyManager), mResource(std::move(resource)), mOffsetFromResource(desc.OffsetFromResource) { @@ -66,7 +66,7 @@ namespace gpgmm::d3d12 { } void ResourceAllocation::DeleteThis() { - GetAllocator()->DeallocateMemory(std::unique_ptr(this)); + GetAllocator()->DeallocateMemory(std::unique_ptr(this)); } ID3D12Resource* ResourceAllocation::GetResource() const { @@ -152,7 +152,7 @@ namespace gpgmm::d3d12 { } IResidencyHeap* ResourceAllocation::GetMemory() const { - return static_cast(MemoryAllocation::GetMemory()); + return static_cast(MemoryAllocationBase::GetMemory()); } void ResourceAllocation::SetDebugAllocator(MemoryAllocatorBase* allocator) { diff --git a/src/gpgmm/d3d12/ResourceAllocationD3D12.h b/src/gpgmm/d3d12/ResourceAllocationD3D12.h index 6ce1cb1ae..18b53a329 100644 --- a/src/gpgmm/d3d12/ResourceAllocationD3D12.h +++ b/src/gpgmm/d3d12/ResourceAllocationD3D12.h @@ -36,7 +36,7 @@ namespace gpgmm::d3d12 { LPCWSTR DebugName; }; - class ResourceAllocation final : public MemoryAllocation, + class ResourceAllocation final : public MemoryAllocationBase, public DebugObject, public IResourceAllocation { public: diff --git a/src/gpgmm/d3d12/ResourceAllocationTrackingAllocatorD3D12.cpp b/src/gpgmm/d3d12/ResourceAllocationTrackingAllocatorD3D12.cpp index e9bfc604d..12007aa57 100644 --- a/src/gpgmm/d3d12/ResourceAllocationTrackingAllocatorD3D12.cpp +++ b/src/gpgmm/d3d12/ResourceAllocationTrackingAllocatorD3D12.cpp @@ -61,7 +61,7 @@ namespace gpgmm::d3d12 { std::lock_guard lock(mMutex); for (auto allocationEntry : mLiveAllocations) { allocationEntry->GetValue().GetAllocator()->DeallocateMemory( - std::unique_ptr(allocationEntry->GetValue().GetAllocation())); + std::unique_ptr(allocationEntry->GetValue().GetAllocation())); } mLiveAllocations.clear(); @@ -78,7 +78,7 @@ namespace gpgmm::d3d12 { } void ResourceAllocationTrackingAllocator::DeallocateMemory( - std::unique_ptr allocation) { + std::unique_ptr allocation) { std::lock_guard lock(mMutex); // KeepAlive must be false so |mLiveAllocations| cache will shrink by 1 entry once |entry| diff --git a/src/gpgmm/d3d12/ResourceAllocationTrackingAllocatorD3D12.h b/src/gpgmm/d3d12/ResourceAllocationTrackingAllocatorD3D12.h index 869bc4422..f69b81545 100644 --- a/src/gpgmm/d3d12/ResourceAllocationTrackingAllocatorD3D12.h +++ b/src/gpgmm/d3d12/ResourceAllocationTrackingAllocatorD3D12.h @@ -37,7 +37,7 @@ namespace gpgmm::d3d12 { // ObjectBase interface DEFINE_OBJECT_BASE_OVERRIDES(DebugResourceAllocator) - void DeallocateMemory(std::unique_ptr allocation) override; + void DeallocateMemory(std::unique_ptr allocation) override; class ResourceAllocationEntry { public: diff --git a/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp b/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp index e7a2593f4..3ee59ceb4 100644 --- a/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp +++ b/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp @@ -268,7 +268,7 @@ namespace gpgmm::d3d12 { HRESULT TryAllocateResource(MemoryAllocatorBase* allocator, const MemoryAllocationRequest& request, CreateResourceFn&& createResourceFn) { - ResultOrError> result = + ResultOrError> result = allocator->TryAllocateMemory(request); if (FAILED(result.GetErrorCode())) { // NeverAllocate always fails, so suppress it. @@ -280,7 +280,7 @@ namespace gpgmm::d3d12 { return static_cast(result.GetErrorCode()); } - std::unique_ptr allocation = result.AcquireResult(); + std::unique_ptr allocation = result.AcquireResult(); ASSERT(allocation != nullptr); HRESULT hr = createResourceFn(*allocation); @@ -1647,7 +1647,7 @@ namespace gpgmm::d3d12 { return S_OK; } - void ResourceAllocator::DeallocateMemory(std::unique_ptr allocation) { + void ResourceAllocator::DeallocateMemory(std::unique_ptr allocation) { GPGMM_TRACE_EVENT_DURATION(TraceEventCategory::kDefault, "ResourceAllocator.DeallocateMemory"); diff --git a/src/gpgmm/d3d12/ResourceAllocatorD3D12.h b/src/gpgmm/d3d12/ResourceAllocatorD3D12.h index f5e6b2d86..f23501392 100644 --- a/src/gpgmm/d3d12/ResourceAllocatorD3D12.h +++ b/src/gpgmm/d3d12/ResourceAllocatorD3D12.h @@ -146,7 +146,7 @@ namespace gpgmm::d3d12 { D3D12_RESOURCE_DESC& resourceDescriptor) const; // MemoryAllocatorBase interface - void DeallocateMemory(std::unique_ptr allocation) override; + void DeallocateMemory(std::unique_ptr allocation) override; HRESULT QueryStatsInternal(ALLOCATOR_STATS* pResourceAllocatorStats); diff --git a/src/gpgmm/d3d12/ResourceHeapAllocatorD3D12.cpp b/src/gpgmm/d3d12/ResourceHeapAllocatorD3D12.cpp index 7808c06f1..b8d2905a0 100644 --- a/src/gpgmm/d3d12/ResourceHeapAllocatorD3D12.cpp +++ b/src/gpgmm/d3d12/ResourceHeapAllocatorD3D12.cpp @@ -39,7 +39,7 @@ namespace gpgmm::d3d12 { mIsAlwaysCreatedInBudget(alwaysCreatedInBudget) { } - ResultOrError> ResourceHeapAllocator::TryAllocateMemory( + ResultOrError> ResourceHeapAllocator::TryAllocateMemory( const MemoryAllocationRequest& request) { GPGMM_TRACE_EVENT_DURATION(TraceEventCategory::kDefault, "ResourceHeapAllocator.TryAllocateMemory"); @@ -83,11 +83,11 @@ namespace gpgmm::d3d12 { mStats.UsedMemoryUsage += resourceHeapDesc.SizeInBytes; mStats.UsedMemoryCount++; - return std::make_unique( + return std::make_unique( this, static_cast(resourceHeap.Detach()), request.SizeInBytes); } - void ResourceHeapAllocator::DeallocateMemory(std::unique_ptr allocation) { + void ResourceHeapAllocator::DeallocateMemory(std::unique_ptr allocation) { std::lock_guard lock(mMutex); GPGMM_TRACE_EVENT_DURATION(TraceEventCategory::kDefault, diff --git a/src/gpgmm/d3d12/ResourceHeapAllocatorD3D12.h b/src/gpgmm/d3d12/ResourceHeapAllocatorD3D12.h index 4b81da460..0f517a927 100644 --- a/src/gpgmm/d3d12/ResourceHeapAllocatorD3D12.h +++ b/src/gpgmm/d3d12/ResourceHeapAllocatorD3D12.h @@ -46,9 +46,9 @@ namespace gpgmm::d3d12 { ~ResourceHeapAllocator() override = default; // MemoryAllocatorBase interface - ResultOrError> TryAllocateMemory( + ResultOrError> TryAllocateMemory( const MemoryAllocationRequest& request) override; - void DeallocateMemory(std::unique_ptr allocation) override; + void DeallocateMemory(std::unique_ptr allocation) override; private: // ObjectBase interface diff --git a/src/gpgmm/vk/DeviceMemoryAllocatorVk.cpp b/src/gpgmm/vk/DeviceMemoryAllocatorVk.cpp index d2b8a2f66..b23d63559 100644 --- a/src/gpgmm/vk/DeviceMemoryAllocatorVk.cpp +++ b/src/gpgmm/vk/DeviceMemoryAllocatorVk.cpp @@ -28,7 +28,7 @@ namespace gpgmm::vk { : mResourceAllocator(resourceAllocator), mMemoryTypeIndex(memoryTypeIndex) { } - ResultOrError> DeviceMemoryAllocator::TryAllocateMemory( + ResultOrError> DeviceMemoryAllocator::TryAllocateMemory( const MemoryAllocationRequest& request) { GPGMM_TRACE_EVENT_DURATION(TraceEventCategory::kDefault, "DeviceMemoryAllocator.TryAllocateMemory"); @@ -65,14 +65,14 @@ namespace gpgmm::vk { mStats.UsedMemoryUsage += request.SizeInBytes; mStats.UsedMemoryCount++; - return std::make_unique( + return std::make_unique( this, new DeviceMemory(deviceMemory, mMemoryTypeIndex, request.SizeInBytes, request.Alignment), request.SizeInBytes); } - void DeviceMemoryAllocator::DeallocateMemory(std::unique_ptr allocation) { + void DeviceMemoryAllocator::DeallocateMemory(std::unique_ptr allocation) { GPGMM_TRACE_EVENT_DURATION(TraceEventCategory::kDefault, "DeviceMemoryAllocator.DeallocateMemory"); diff --git a/src/gpgmm/vk/DeviceMemoryAllocatorVk.h b/src/gpgmm/vk/DeviceMemoryAllocatorVk.h index 2a9f226c6..ed06f7ae0 100644 --- a/src/gpgmm/vk/DeviceMemoryAllocatorVk.h +++ b/src/gpgmm/vk/DeviceMemoryAllocatorVk.h @@ -24,9 +24,9 @@ namespace gpgmm::vk { ~DeviceMemoryAllocator() override = default; // MemoryAllocatorBase interface - ResultOrError> TryAllocateMemory( + ResultOrError> TryAllocateMemory( const MemoryAllocationRequest& request) override; - void DeallocateMemory(std::unique_ptr allocation) override; + void DeallocateMemory(std::unique_ptr allocation) override; private: GpResourceAllocator mResourceAllocator; diff --git a/src/gpgmm/vk/ResourceAllocatorVk.cpp b/src/gpgmm/vk/ResourceAllocatorVk.cpp index e1c2c1d61..cb30acf12 100644 --- a/src/gpgmm/vk/ResourceAllocatorVk.cpp +++ b/src/gpgmm/vk/ResourceAllocatorVk.cpp @@ -182,8 +182,8 @@ namespace gpgmm::vk { // GpResourceAllocation_T - GpResourceAllocation_T::GpResourceAllocation_T(const MemoryAllocation& allocation) - : MemoryAllocation(allocation) { + GpResourceAllocation_T::GpResourceAllocation_T(const MemoryAllocationBase& allocation) + : MemoryAllocationBase(allocation) { } // GpResourceAllocator_T @@ -342,7 +342,7 @@ namespace gpgmm::vk { // Attempt to allocate using the most effective allocator. MemoryAllocatorBase* allocator = nullptr; - ResultOrError> result; + ResultOrError> result; if (!neverSubAllocate) { allocator = mResourceAllocatorsPerType[memoryTypeIndex].get(); result = allocator->TryAllocateMemory(request); @@ -368,7 +368,8 @@ namespace gpgmm::vk { if (allocation == VK_NULL_HANDLE) { return; } - allocation->GetAllocator()->DeallocateMemory(std::unique_ptr(allocation)); + allocation->GetAllocator()->DeallocateMemory( + std::unique_ptr(allocation)); } VkDevice GpResourceAllocator_T::GetDevice() const { diff --git a/src/gpgmm/vk/ResourceAllocatorVk.h b/src/gpgmm/vk/ResourceAllocatorVk.h index 4e9077d31..b7b34c5b6 100644 --- a/src/gpgmm/vk/ResourceAllocatorVk.h +++ b/src/gpgmm/vk/ResourceAllocatorVk.h @@ -24,8 +24,8 @@ namespace gpgmm::vk { - struct GpResourceAllocation_T final : public MemoryAllocation { - GpResourceAllocation_T(const MemoryAllocation& allocation); + struct GpResourceAllocation_T final : public MemoryAllocationBase { + GpResourceAllocation_T(const MemoryAllocationBase& allocation); }; class Caps; diff --git a/src/mvi/gpgmm.cpp b/src/mvi/gpgmm.cpp index cc407a199..423397796 100644 --- a/src/mvi/gpgmm.cpp +++ b/src/mvi/gpgmm.cpp @@ -35,25 +35,25 @@ namespace gpgmm { return 0; } - // MemoryAllocation + // MemoryAllocationBase - MemoryAllocation::MemoryAllocation(MemoryAllocatorBase* allocator, MemoryBase* memory) + MemoryAllocationBase::MemoryAllocationBase(MemoryAllocatorBase* allocator, MemoryBase* memory) : mAllocator(allocator), mMemory(memory) { } - MemoryBase* MemoryAllocation::GetMemory() const { + MemoryBase* MemoryAllocationBase::GetMemory() const { return mMemory; } - MemoryAllocatorBase* MemoryAllocation::GetAllocator() const { + MemoryAllocatorBase* MemoryAllocationBase::GetAllocator() const { return mAllocator; } - uint64_t MemoryAllocation::GetSize() const { + uint64_t MemoryAllocationBase::GetSize() const { return mMemory->GetSize(); } - uint64_t MemoryAllocation::GetAlignment() const { + uint64_t MemoryAllocationBase::GetAlignment() const { return mMemory->GetAlignment(); } diff --git a/src/mvi/gpgmm.h b/src/mvi/gpgmm.h index 92604e7b5..0175bbece 100644 --- a/src/mvi/gpgmm.h +++ b/src/mvi/gpgmm.h @@ -32,20 +32,20 @@ namespace gpgmm { const uint64_t mAlignment; }; - class MemoryAllocation; + class MemoryAllocationBase; class MemoryAllocatorBase { public: - virtual void DeallocateMemory(std::unique_ptr allocation) = 0; + virtual void DeallocateMemory(std::unique_ptr allocation) = 0; virtual uint64_t ReleaseMemory(uint64_t bytesToRelease); }; - // MemoryAllocation represents a range of memory. A MemoryAllocation object will be held alive - // until MemoryAllocatorBase::DeallocateMemory is called on the MemoryAllocatorBase object from - // which it was originally created. - class MemoryAllocation { + // MemoryAllocationBase represents a range of memory. A MemoryAllocationBase object will be held + // alive until MemoryAllocatorBase::DeallocateMemory is called on the MemoryAllocatorBase object + // from which it was originally created. + class MemoryAllocationBase { public: - MemoryAllocation(MemoryAllocatorBase* allocator, MemoryBase* memory); + MemoryAllocationBase(MemoryAllocatorBase* allocator, MemoryBase* memory); MemoryAllocatorBase* GetAllocator() const; uint64_t GetSize() const; diff --git a/src/mvi/gpgmm_d3d12.cpp b/src/mvi/gpgmm_d3d12.cpp index eddd3df73..1b3334d7c 100644 --- a/src/mvi/gpgmm_d3d12.cpp +++ b/src/mvi/gpgmm_d3d12.cpp @@ -269,13 +269,13 @@ namespace gpgmm::d3d12 { } IResidencyHeap* ResourceAllocation::GetMemory() const { - return static_cast(MemoryAllocation::GetMemory()); + return static_cast(MemoryAllocationBase::GetMemory()); } ResourceAllocation::ResourceAllocation(MemoryAllocatorBase* allocator, ResidencyHeap* resourceHeap, Microsoft::WRL::ComPtr resource) - : MemoryAllocation(allocator, resourceHeap), mResource(std::move(resource)) { + : MemoryAllocationBase(allocator, resourceHeap), mResource(std::move(resource)) { } HRESULT STDMETHODCALLTYPE ResourceAllocation::QueryInterface(REFIID riid, void** ppvObject) { @@ -419,7 +419,7 @@ namespace gpgmm::d3d12 { : mDevice(pDevice), mResidencyManager(pResidencyManager) { } - void ResourceAllocator::DeallocateMemory(std::unique_ptr allocation) { + void ResourceAllocator::DeallocateMemory(std::unique_ptr allocation) { delete allocation->GetMemory(); } diff --git a/src/mvi/gpgmm_d3d12.h b/src/mvi/gpgmm_d3d12.h index 6ccdc4cac..3e35e0264 100644 --- a/src/mvi/gpgmm_d3d12.h +++ b/src/mvi/gpgmm_d3d12.h @@ -148,7 +148,7 @@ namespace gpgmm::d3d12 { class ResourceAllocator; - class ResourceAllocation final : public MemoryAllocation, + class ResourceAllocation final : public MemoryAllocationBase, public Unknown, public IResourceAllocation { public: @@ -250,7 +250,7 @@ namespace gpgmm::d3d12 { ID3D12Device* pDevice, ResidencyManager* pResidencyManager); - void DeallocateMemory(std::unique_ptr allocation) override; + void DeallocateMemory(std::unique_ptr allocation) override; Microsoft::WRL::ComPtr mDevice; Microsoft::WRL::ComPtr mResidencyManager; diff --git a/src/tests/DummyMemoryAllocator.h b/src/tests/DummyMemoryAllocator.h index 1f9369bba..425c75731 100644 --- a/src/tests/DummyMemoryAllocator.h +++ b/src/tests/DummyMemoryAllocator.h @@ -35,7 +35,7 @@ namespace gpgmm { : MemoryAllocatorBase(std::move(next)) { } - ResultOrError> TryAllocateMemory( + ResultOrError> TryAllocateMemory( const MemoryAllocationRequest& request) override { GPGMM_TRACE_EVENT_DURATION(TraceEventCategory::kDefault, "DummyMemoryAllocator.TryAllocateMemory"); @@ -49,11 +49,11 @@ namespace gpgmm { mStats.UsedMemoryCount++; mStats.UsedMemoryUsage += request.SizeInBytes; - return std::make_unique( + return std::make_unique( this, new DummyMemory(request.SizeInBytes, request.Alignment), request.SizeInBytes); } - void DeallocateMemory(std::unique_ptr allocation) override { + void DeallocateMemory(std::unique_ptr allocation) override { GPGMM_TRACE_EVENT_DURATION(TraceEventCategory::kDefault, "DummyMemoryAllocator.DeallocateMemory"); diff --git a/src/tests/perftests/MemoryAllocatorPerfTests.cpp b/src/tests/perftests/MemoryAllocatorPerfTests.cpp index 4dc48bd99..24ad18189 100644 --- a/src/tests/perftests/MemoryAllocatorPerfTests.cpp +++ b/src/tests/perftests/MemoryAllocatorPerfTests.cpp @@ -48,7 +48,7 @@ class SingleSizeAllocationPerfTests : public MemoryAllocatorPerfTests { void SingleStep(benchmark::State& state, MemoryAllocatorBase* allocator, const MemoryAllocationRequest& request) const { - std::vector> allocations; + std::vector> allocations; for (int i = 0; i < state.range(3); i++) { auto allocation = allocator->TryAllocateMemoryForTesting(request); if (allocation == nullptr) { diff --git a/src/tests/unittests/BuddyMemoryAllocatorTests.cpp b/src/tests/unittests/BuddyMemoryAllocatorTests.cpp index 636e1f5fd..fb5d06f99 100644 --- a/src/tests/unittests/BuddyMemoryAllocatorTests.cpp +++ b/src/tests/unittests/BuddyMemoryAllocatorTests.cpp @@ -56,13 +56,14 @@ TEST_F(BuddyMemoryAllocatorTests, SingleHeap) { // Cannot allocate greater than heap size. { - std::unique_ptr invalidAllocation = allocator.TryAllocateMemoryForTesting( - CreateBasicRequest(kDefaultMemorySize * 2, kDefaultMemoryAlignment)); + std::unique_ptr invalidAllocation = + allocator.TryAllocateMemoryForTesting( + CreateBasicRequest(kDefaultMemorySize * 2, kDefaultMemoryAlignment)); ASSERT_EQ(invalidAllocation, nullptr); } // Allocate one 128 byte allocation (same size as heap). - std::unique_ptr allocation1 = + std::unique_ptr allocation1 = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(128, kDefaultMemoryAlignment)); ASSERT_NE(allocation1, nullptr); ASSERT_EQ(allocation1->GetBlock()->Offset, 0u); @@ -73,7 +74,7 @@ TEST_F(BuddyMemoryAllocatorTests, SingleHeap) { // Cannot allocate when allocator is full. { - std::unique_ptr invalidAllocation = + std::unique_ptr invalidAllocation = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(128, kDefaultMemoryAlignment)); ASSERT_EQ(invalidAllocation, nullptr); } @@ -98,20 +99,22 @@ TEST_F(BuddyMemoryAllocatorTests, MultipleHeaps) { // Cannot allocate greater than heap size. { - std::unique_ptr invalidAllocation = allocator.TryAllocateMemoryForTesting( - CreateBasicRequest(kDefaultMemorySize * 2, kDefaultMemoryAlignment)); + std::unique_ptr invalidAllocation = + allocator.TryAllocateMemoryForTesting( + CreateBasicRequest(kDefaultMemorySize * 2, kDefaultMemoryAlignment)); ASSERT_EQ(invalidAllocation, nullptr); } // Cannot allocate greater than max block size. { - std::unique_ptr invalidAllocation = allocator.TryAllocateMemoryForTesting( - CreateBasicRequest(maxBlockSize * 2, kDefaultMemoryAlignment)); + std::unique_ptr invalidAllocation = + allocator.TryAllocateMemoryForTesting( + CreateBasicRequest(maxBlockSize * 2, kDefaultMemoryAlignment)); ASSERT_EQ(invalidAllocation, nullptr); } // Allocate two 128 byte allocations. - std::unique_ptr allocation1 = allocator.TryAllocateMemoryForTesting( + std::unique_ptr allocation1 = allocator.TryAllocateMemoryForTesting( CreateBasicRequest(kDefaultMemorySize, kDefaultMemoryAlignment)); ASSERT_NE(allocation1, nullptr); ASSERT_EQ(allocation1->GetSize(), kDefaultMemorySize); @@ -121,7 +124,7 @@ TEST_F(BuddyMemoryAllocatorTests, MultipleHeaps) { // First allocation creates first heap. ASSERT_EQ(allocator.GetStats().UsedMemoryCount, 1u); - std::unique_ptr allocation2 = allocator.TryAllocateMemoryForTesting( + std::unique_ptr allocation2 = allocator.TryAllocateMemoryForTesting( CreateBasicRequest(kDefaultMemorySize, kDefaultMemoryAlignment)); ASSERT_NE(allocation2, nullptr); ASSERT_EQ(allocation2->GetSize(), kDefaultMemorySize); @@ -157,7 +160,7 @@ TEST_F(BuddyMemoryAllocatorTests, MultipleSplitHeaps) { std::make_unique()); // Allocate two 64 byte sub-allocations. - std::unique_ptr allocation1 = + std::unique_ptr allocation1 = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kDefaultMemorySize / 2, 1)); ASSERT_NE(allocation1, nullptr); ASSERT_EQ(allocation1->GetSize(), kDefaultMemorySize / 2); @@ -167,7 +170,7 @@ TEST_F(BuddyMemoryAllocatorTests, MultipleSplitHeaps) { // First sub-allocation creates first heap. ASSERT_EQ(allocator.GetStats().UsedMemoryCount, 1u); - std::unique_ptr allocation2 = + std::unique_ptr allocation2 = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kDefaultMemorySize / 2, 1)); ASSERT_NE(allocation2, nullptr); ASSERT_EQ(allocation2->GetSize(), kDefaultMemorySize / 2); @@ -178,7 +181,7 @@ TEST_F(BuddyMemoryAllocatorTests, MultipleSplitHeaps) { ASSERT_EQ(allocator.GetStats().UsedMemoryCount, 1u); ASSERT_EQ(allocation1->GetMemory(), allocation2->GetMemory()); - std::unique_ptr allocation3 = + std::unique_ptr allocation3 = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kDefaultMemorySize / 2, 1)); ASSERT_NE(allocation3, nullptr); ASSERT_EQ(allocation3->GetSize(), kDefaultMemorySize / 2); @@ -220,7 +223,7 @@ TEST_F(BuddyMemoryAllocatorTests, MultiplSplitHeapsVariableSizes) { std::make_unique()); // Allocate two 64-byte allocations. - std::unique_ptr allocation1 = + std::unique_ptr allocation1 = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(64, 1)); ASSERT_NE(allocation1, nullptr); ASSERT_EQ(allocation1->GetSize(), 64u); @@ -228,7 +231,7 @@ TEST_F(BuddyMemoryAllocatorTests, MultiplSplitHeapsVariableSizes) { ASSERT_EQ(allocation1->GetOffset(), 0u); ASSERT_EQ(allocation1->GetMethod(), AllocationMethod::kSubAllocated); - std::unique_ptr allocation2 = + std::unique_ptr allocation2 = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(64, 1)); ASSERT_NE(allocation2, nullptr); ASSERT_EQ(allocation2->GetSize(), 64u); @@ -240,7 +243,7 @@ TEST_F(BuddyMemoryAllocatorTests, MultiplSplitHeapsVariableSizes) { ASSERT_EQ(allocator.GetStats().UsedMemoryCount, 1u); ASSERT_EQ(allocation1->GetMemory(), allocation2->GetMemory()); - std::unique_ptr allocation3 = + std::unique_ptr allocation3 = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(128, 1)); ASSERT_NE(allocation3, nullptr); ASSERT_EQ(allocation3->GetSize(), 128u); @@ -252,7 +255,7 @@ TEST_F(BuddyMemoryAllocatorTests, MultiplSplitHeapsVariableSizes) { ASSERT_EQ(allocator.GetStats().UsedMemoryCount, 2u); ASSERT_NE(allocation2->GetMemory(), allocation3->GetMemory()); - std::unique_ptr allocation4 = + std::unique_ptr allocation4 = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(64, 1)); ASSERT_NE(allocation4, nullptr); ASSERT_EQ(allocation4->GetSize(), 64u); @@ -264,7 +267,7 @@ TEST_F(BuddyMemoryAllocatorTests, MultiplSplitHeapsVariableSizes) { ASSERT_NE(allocation3->GetMemory(), allocation4->GetMemory()); // R5 size forms 64 byte hole after R4. - std::unique_ptr allocation5 = + std::unique_ptr allocation5 = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(128, 1)); ASSERT_NE(allocation5, nullptr); ASSERT_EQ(allocation5->GetSize(), 128u); @@ -310,7 +313,7 @@ TEST_F(BuddyMemoryAllocatorTests, SameSizeVariousAlignment) { BuddyMemoryAllocator allocator(maxBlockSize, kDefaultMemorySize, kDefaultMemoryAlignment, std::make_unique()); - std::unique_ptr allocation1 = + std::unique_ptr allocation1 = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(64, 128)); ASSERT_NE(allocation1, nullptr); ASSERT_EQ(allocation1->GetSize(), 64u); @@ -320,7 +323,7 @@ TEST_F(BuddyMemoryAllocatorTests, SameSizeVariousAlignment) { ASSERT_EQ(allocator.GetStats().UsedMemoryCount, 1u); - std::unique_ptr allocation2 = + std::unique_ptr allocation2 = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(64, 128)); ASSERT_NE(allocation2, nullptr); ASSERT_EQ(allocation2->GetSize(), 64u); @@ -331,7 +334,7 @@ TEST_F(BuddyMemoryAllocatorTests, SameSizeVariousAlignment) { ASSERT_EQ(allocator.GetStats().UsedMemoryCount, 2u); ASSERT_NE(allocation1->GetMemory(), allocation2->GetMemory()); - std::unique_ptr allocation3 = + std::unique_ptr allocation3 = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(64, 128)); ASSERT_NE(allocation3, nullptr); ASSERT_EQ(allocation3->GetSize(), 64u); @@ -342,7 +345,7 @@ TEST_F(BuddyMemoryAllocatorTests, SameSizeVariousAlignment) { ASSERT_EQ(allocator.GetStats().UsedMemoryCount, 3u); ASSERT_NE(allocation2->GetMemory(), allocation3->GetMemory()); - std::unique_ptr allocation4 = + std::unique_ptr allocation4 = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(64, 64)); ASSERT_NE(allocation4, nullptr); ASSERT_EQ(allocation4->GetSize(), 64u); @@ -386,7 +389,7 @@ TEST_F(BuddyMemoryAllocatorTests, VariousSizeSameAlignment) { constexpr uint64_t alignment = 64; - std::unique_ptr allocation1 = + std::unique_ptr allocation1 = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(64, alignment)); ASSERT_NE(allocation1, nullptr); ASSERT_EQ(allocation1->GetSize(), 64u); @@ -395,7 +398,7 @@ TEST_F(BuddyMemoryAllocatorTests, VariousSizeSameAlignment) { ASSERT_EQ(allocator.GetStats().UsedMemoryCount, 1u); - std::unique_ptr allocation2 = + std::unique_ptr allocation2 = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(64, alignment)); ASSERT_NE(allocation2, nullptr); ASSERT_EQ(allocation2->GetSize(), 64u); @@ -406,7 +409,7 @@ TEST_F(BuddyMemoryAllocatorTests, VariousSizeSameAlignment) { ASSERT_EQ(allocator.GetStats().UsedMemoryCount, 1u); // Reuses H0 ASSERT_EQ(allocation1->GetMemory(), allocation2->GetMemory()); - std::unique_ptr allocation3 = + std::unique_ptr allocation3 = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(128, alignment)); ASSERT_NE(allocation3, nullptr); ASSERT_EQ(allocation3->GetSize(), 128u); @@ -417,7 +420,7 @@ TEST_F(BuddyMemoryAllocatorTests, VariousSizeSameAlignment) { ASSERT_EQ(allocator.GetStats().UsedMemoryCount, 2u); ASSERT_NE(allocation2->GetMemory(), allocation3->GetMemory()); - std::unique_ptr allocation4 = + std::unique_ptr allocation4 = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(128, alignment)); ASSERT_NE(allocation4, nullptr); ASSERT_EQ(allocation4->GetSize(), 128u); @@ -448,7 +451,7 @@ TEST_F(BuddyMemoryAllocatorTests, AllocationOverflow) { std::make_unique()); constexpr uint64_t largeBlock = (1ull << 63) + 1; - std::unique_ptr invalidAllocation = allocator.TryAllocateMemoryForTesting( + std::unique_ptr invalidAllocation = allocator.TryAllocateMemoryForTesting( CreateBasicRequest(largeBlock, kDefaultMemoryAlignment)); ASSERT_EQ(invalidAllocation, nullptr); } @@ -464,13 +467,13 @@ TEST_F(BuddyMemoryAllocatorTests, ReuseFreedHeaps) { std::move(poolAllocator)); std::set heaps = {}; - std::vector> allocations = {}; + std::vector> allocations = {}; constexpr uint32_t kNumOfAllocations = 100; // Allocate |kNumOfAllocations|. for (uint32_t i = 0; i < kNumOfAllocations; i++) { - std::unique_ptr allocation = + std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(4, 1)); ASSERT_NE(allocation, nullptr); ASSERT_EQ(allocation->GetSize(), 4u); @@ -493,7 +496,7 @@ TEST_F(BuddyMemoryAllocatorTests, ReuseFreedHeaps) { // Allocate again reusing the same heaps. for (uint32_t i = 0; i < kNumOfAllocations; i++) { - std::unique_ptr allocation = + std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(4, 1)); ASSERT_NE(allocation, nullptr); ASSERT_EQ(allocation->GetSize(), 4u); @@ -524,7 +527,7 @@ TEST_F(BuddyMemoryAllocatorTests, DestroyHeaps) { std::move(poolAllocator)); std::set heaps = {}; - std::vector> allocations = {}; + std::vector> allocations = {}; // Count by heap (vs number of allocations) to ensure there are exactly |kNumOfHeaps| worth of // buffers. Otherwise, the heap may be reused if not full. @@ -532,7 +535,7 @@ TEST_F(BuddyMemoryAllocatorTests, DestroyHeaps) { // Allocate |kNumOfHeaps| worth. while (heaps.size() < kNumOfHeaps) { - std::unique_ptr allocation = + std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(4, 1)); ASSERT_NE(allocation, nullptr); ASSERT_EQ(allocation->GetSize(), 4u); diff --git a/src/tests/unittests/ConditionalMemoryAllocatorTests.cpp b/src/tests/unittests/ConditionalMemoryAllocatorTests.cpp index 19eb7719e..8aeba17e3 100644 --- a/src/tests/unittests/ConditionalMemoryAllocatorTests.cpp +++ b/src/tests/unittests/ConditionalMemoryAllocatorTests.cpp @@ -39,7 +39,7 @@ TEST_F(ConditionalMemoryAllocatorTests, Basic) { // Smaller allocation uses firstAllocator. { - std::unique_ptr allocation = + std::unique_ptr allocation = alloc.TryAllocateMemoryForTesting(CreateBasicRequest(4, 1)); ASSERT_EQ(alloc.GetFirstAllocatorForTesting()->GetStats().UsedMemoryUsage, 4u); ASSERT_NE(allocation, nullptr); @@ -48,7 +48,7 @@ TEST_F(ConditionalMemoryAllocatorTests, Basic) { // Equal size allocation uses firstAllocator. { - std::unique_ptr allocation = + std::unique_ptr allocation = alloc.TryAllocateMemoryForTesting(CreateBasicRequest(16, 1)); ASSERT_EQ(alloc.GetFirstAllocatorForTesting()->GetStats().UsedMemoryUsage, 16u); ASSERT_NE(allocation, nullptr); @@ -57,7 +57,7 @@ TEST_F(ConditionalMemoryAllocatorTests, Basic) { // Larger allocation uses secondAllocator. { - std::unique_ptr allocation = + std::unique_ptr allocation = alloc.TryAllocateMemoryForTesting(CreateBasicRequest(24, 1)); ASSERT_EQ(alloc.GetSecondAllocatorForTesting()->GetStats().UsedMemoryUsage, 24u); ASSERT_NE(allocation, nullptr); @@ -66,7 +66,7 @@ TEST_F(ConditionalMemoryAllocatorTests, Basic) { // Smaller allocation again uses firstAllocator. { - std::unique_ptr allocation = + std::unique_ptr allocation = alloc.TryAllocateMemoryForTesting(CreateBasicRequest(4, 1)); ASSERT_EQ(alloc.GetFirstAllocatorForTesting()->GetStats().UsedMemoryUsage, 4u); ASSERT_NE(allocation, nullptr); @@ -75,7 +75,7 @@ TEST_F(ConditionalMemoryAllocatorTests, Basic) { // Larger allocation again uses secondAllocator. { - std::unique_ptr allocation = + std::unique_ptr allocation = alloc.TryAllocateMemoryForTesting(CreateBasicRequest(24, 1)); ASSERT_EQ(alloc.GetSecondAllocatorForTesting()->GetStats().UsedMemoryUsage, 24u); ASSERT_NE(allocation, nullptr); diff --git a/src/tests/unittests/PooledMemoryAllocatorTests.cpp b/src/tests/unittests/PooledMemoryAllocatorTests.cpp index 5ceba6914..1c4732e4f 100644 --- a/src/tests/unittests/PooledMemoryAllocatorTests.cpp +++ b/src/tests/unittests/PooledMemoryAllocatorTests.cpp @@ -42,7 +42,7 @@ TEST_F(PooledMemoryAllocatorTests, SingleHeap) { PooledMemoryAllocator allocator(kDefaultMemorySize, kDefaultMemoryAlignment, std::make_unique()); - std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting( + std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting( CreateBasicRequest(kDefaultMemorySize, kDefaultMemoryAlignment)); ASSERT_NE(allocation, nullptr); EXPECT_EQ(allocation->GetSize(), kDefaultMemorySize); @@ -60,12 +60,12 @@ TEST_F(PooledMemoryAllocatorTests, MultipleHeaps) { PooledMemoryAllocator allocator(kDefaultMemorySize, kDefaultMemoryAlignment, std::make_unique()); - std::unique_ptr firstAllocation = allocator.TryAllocateMemoryForTesting( + std::unique_ptr firstAllocation = allocator.TryAllocateMemoryForTesting( CreateBasicRequest(kDefaultMemorySize, kDefaultMemoryAlignment)); ASSERT_NE(firstAllocation, nullptr); EXPECT_EQ(firstAllocation->GetSize(), kDefaultMemorySize); - std::unique_ptr secondAllocation = allocator.TryAllocateMemoryForTesting( + std::unique_ptr secondAllocation = allocator.TryAllocateMemoryForTesting( CreateBasicRequest(kDefaultMemorySize, kDefaultMemoryAlignment)); ASSERT_NE(secondAllocation, nullptr); EXPECT_EQ(secondAllocation->GetSize(), kDefaultMemorySize); @@ -87,7 +87,7 @@ TEST_F(PooledMemoryAllocatorTests, ReuseFreedHeaps) { PooledMemoryAllocator allocator(kDefaultMemorySize, kDefaultMemoryAlignment, std::make_unique()); { - std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting( + std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting( CreateBasicRequest(kDefaultMemorySize, kDefaultMemoryAlignment)); ASSERT_NE(allocation, nullptr); EXPECT_EQ(allocation->GetSize(), kDefaultMemorySize); @@ -98,7 +98,7 @@ TEST_F(PooledMemoryAllocatorTests, ReuseFreedHeaps) { EXPECT_EQ(allocator.GetStats().FreeMemoryUsage, kDefaultMemorySize); { - std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting( + std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting( CreateBasicRequest(kDefaultMemorySize, kDefaultMemoryAlignment)); ASSERT_NE(allocation, nullptr); EXPECT_EQ(allocation->GetSize(), kDefaultMemorySize); @@ -113,7 +113,7 @@ TEST_F(PooledMemoryAllocatorTests, GetInfo) { PooledMemoryAllocator allocator(kDefaultMemorySize, kDefaultMemoryAlignment, std::make_unique()); - std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting( + std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting( CreateBasicRequest(kDefaultMemorySize, kDefaultMemoryAlignment)); EXPECT_NE(allocation, nullptr); diff --git a/src/tests/unittests/SegmentedMemoryAllocatorTests.cpp b/src/tests/unittests/SegmentedMemoryAllocatorTests.cpp index 3a520ed00..5ed20695f 100644 --- a/src/tests/unittests/SegmentedMemoryAllocatorTests.cpp +++ b/src/tests/unittests/SegmentedMemoryAllocatorTests.cpp @@ -36,7 +36,7 @@ TEST(SegmentedMemoryAllocatorTests, SingleHeap) { SegmentedMemoryAllocator allocator(std::make_unique(), kDefaultMemoryAlignment); - std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting( + std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting( CreateBasicRequest(kDefaultMemorySize, kDefaultMemoryAlignment)); ASSERT_NE(allocation, nullptr); EXPECT_EQ(allocation->GetSize(), kDefaultMemorySize); @@ -54,12 +54,12 @@ TEST(SegmentedMemoryAllocatorTests, MultipleHeaps) { SegmentedMemoryAllocator allocator(std::make_unique(), kDefaultMemoryAlignment); - std::unique_ptr firstAllocation = allocator.TryAllocateMemoryForTesting( + std::unique_ptr firstAllocation = allocator.TryAllocateMemoryForTesting( CreateBasicRequest(kDefaultMemorySize, kDefaultMemoryAlignment)); ASSERT_NE(firstAllocation, nullptr); EXPECT_EQ(firstAllocation->GetSize(), kDefaultMemorySize); - std::unique_ptr secondAllocation = allocator.TryAllocateMemoryForTesting( + std::unique_ptr secondAllocation = allocator.TryAllocateMemoryForTesting( CreateBasicRequest(kDefaultMemorySize, kDefaultMemoryAlignment)); ASSERT_NE(secondAllocation, nullptr); EXPECT_EQ(secondAllocation->GetSize(), kDefaultMemorySize); @@ -83,14 +83,14 @@ TEST(SegmentedMemoryAllocatorTests, MultipleHeapsVariousSizes) { // Append the 1st and 3rd segment, in sequence. uint64_t firstMemorySize = kDefaultMemorySize / 2; - std::unique_ptr firstAllocation = allocator.TryAllocateMemoryForTesting( + std::unique_ptr firstAllocation = allocator.TryAllocateMemoryForTesting( CreateBasicRequest(firstMemorySize, kDefaultMemoryAlignment)); EXPECT_EQ(firstAllocation->GetMethod(), AllocationMethod::kStandalone); ASSERT_NE(firstAllocation, nullptr); EXPECT_EQ(firstAllocation->GetSize(), firstMemorySize); uint64_t secondMemorySize = kDefaultMemorySize / 8; - std::unique_ptr secondAllocation = allocator.TryAllocateMemoryForTesting( + std::unique_ptr secondAllocation = allocator.TryAllocateMemoryForTesting( CreateBasicRequest(secondMemorySize, kDefaultMemoryAlignment)); ASSERT_NE(secondAllocation, nullptr); EXPECT_EQ(secondAllocation->GetMethod(), AllocationMethod::kStandalone); @@ -98,7 +98,7 @@ TEST(SegmentedMemoryAllocatorTests, MultipleHeapsVariousSizes) { // Insert a 3rd segment in the middle or between the 1st and 2nd segment. uint64_t thirdMemorySize = kDefaultMemorySize / 4; - std::unique_ptr thirdAllocation = allocator.TryAllocateMemoryForTesting( + std::unique_ptr thirdAllocation = allocator.TryAllocateMemoryForTesting( CreateBasicRequest(thirdMemorySize, kDefaultMemoryAlignment)); ASSERT_NE(thirdAllocation, nullptr); EXPECT_EQ(thirdAllocation->GetMethod(), AllocationMethod::kStandalone); @@ -106,28 +106,28 @@ TEST(SegmentedMemoryAllocatorTests, MultipleHeapsVariousSizes) { // Insert a 4th segment at the end. uint64_t fourthMemorySize = kDefaultMemorySize; - std::unique_ptr fourthAllocation = allocator.TryAllocateMemoryForTesting( + std::unique_ptr fourthAllocation = allocator.TryAllocateMemoryForTesting( CreateBasicRequest(fourthMemorySize, kDefaultMemoryAlignment)); ASSERT_NE(fourthAllocation, nullptr); EXPECT_EQ(fourthAllocation->GetSize(), fourthMemorySize); // Insert a 5th segment at the start. uint64_t fifthMemorySize = kDefaultMemorySize / 16; - std::unique_ptr fifthAllocation = allocator.TryAllocateMemoryForTesting( + std::unique_ptr fifthAllocation = allocator.TryAllocateMemoryForTesting( CreateBasicRequest(fifthMemorySize, kDefaultMemoryAlignment)); ASSERT_NE(fifthAllocation, nullptr); EXPECT_EQ(fifthAllocation->GetMethod(), AllocationMethod::kStandalone); EXPECT_EQ(fifthAllocation->GetSize(), fifthMemorySize); // Reuse the 3rd segment. - std::unique_ptr sixthAllocation = allocator.TryAllocateMemoryForTesting( + std::unique_ptr sixthAllocation = allocator.TryAllocateMemoryForTesting( CreateBasicRequest(thirdMemorySize, kDefaultMemoryAlignment)); ASSERT_NE(sixthAllocation, nullptr); EXPECT_EQ(sixthAllocation->GetMethod(), AllocationMethod::kStandalone); EXPECT_EQ(sixthAllocation->GetSize(), thirdMemorySize); // Reuse the 1st segment. - std::unique_ptr seventhAllocation = allocator.TryAllocateMemoryForTesting( + std::unique_ptr seventhAllocation = allocator.TryAllocateMemoryForTesting( CreateBasicRequest(firstMemorySize, kDefaultMemoryAlignment)); ASSERT_NE(seventhAllocation, nullptr); EXPECT_EQ(seventhAllocation->GetMethod(), AllocationMethod::kStandalone); @@ -164,7 +164,7 @@ TEST(SegmentedMemoryAllocatorTests, ReuseFreedHeaps) { SegmentedMemoryAllocator allocator(std::make_unique(), kDefaultMemoryAlignment); { - std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting( + std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting( CreateBasicRequest(kDefaultMemorySize, kDefaultMemoryAlignment)); ASSERT_NE(allocation, nullptr); EXPECT_EQ(allocation->GetSize(), kDefaultMemorySize); @@ -175,7 +175,7 @@ TEST(SegmentedMemoryAllocatorTests, ReuseFreedHeaps) { EXPECT_EQ(allocator.GetSegmentSizeForTesting(), 1u); { - std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting( + std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting( CreateBasicRequest(kDefaultMemorySize, kDefaultMemoryAlignment)); ASSERT_NE(allocation, nullptr); EXPECT_EQ(allocation->GetSize(), kDefaultMemorySize); @@ -190,7 +190,7 @@ TEST(SegmentedMemoryAllocatorTests, GetInfo) { SegmentedMemoryAllocator allocator(std::make_unique(), kDefaultMemoryAlignment); - std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting( + std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting( CreateBasicRequest(kDefaultMemorySize, kDefaultMemoryAlignment)); EXPECT_NE(allocation, nullptr); diff --git a/src/tests/unittests/SlabMemoryAllocatorTests.cpp b/src/tests/unittests/SlabMemoryAllocatorTests.cpp index 84c7300f6..e22ca63d4 100644 --- a/src/tests/unittests/SlabMemoryAllocatorTests.cpp +++ b/src/tests/unittests/SlabMemoryAllocatorTests.cpp @@ -91,7 +91,7 @@ TEST_F(SlabMemoryAllocatorTests, SingleSlab) { kDefaultSlabFragmentationLimit, kNoSlabPrefetchAllowed, kDisableSlabGrowth, dummyMemoryAllocator.get()); - std::unique_ptr allocation = + std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocation, nullptr); EXPECT_EQ(allocation->GetOffset(), 0u); @@ -112,7 +112,7 @@ TEST_F(SlabMemoryAllocatorTests, SingleSlab) { // Max allocation cannot be more than 1/8th the max slab size or 4 bytes. // Since a 10 byte allocation requires a 128 byte slab, allocation should always fail. - std::unique_ptr allocation = + std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(10, 1)); ASSERT_EQ(allocation, nullptr); @@ -135,7 +135,7 @@ TEST_F(SlabMemoryAllocatorTests, SingleSlab) { kDefaultSlabFragmentationLimit, kNoSlabPrefetchAllowed, kDisableSlabGrowth, dummyMemoryAllocator.get()); - std::unique_ptr allocation = + std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocation, nullptr); EXPECT_GE(allocation->GetSize(), kBlockSize); @@ -153,7 +153,7 @@ TEST_F(SlabMemoryAllocatorTests, SingleSlab) { kDefaultSlabFragmentationLimit, kNoSlabPrefetchAllowed, kDisableSlabGrowth, dummyMemoryAllocator.get()); - std::unique_ptr allocation = + std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocation, nullptr); EXPECT_GE(allocation->GetSize(), kBlockSize); @@ -197,9 +197,9 @@ TEST_F(SlabMemoryAllocatorTests, MultipleSlabs) { kNoSlabPrefetchAllowed, kDisableSlabGrowth, dummyMemoryAllocator.get()); const uint64_t kNumOfSlabs = 12; - std::vector> allocations = {}; + std::vector> allocations = {}; for (uint32_t slabi = 0; slabi < kNumOfSlabs; slabi++) { - std::unique_ptr allocation = + std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocation, nullptr); allocations.push_back(std::move(allocation)); @@ -224,9 +224,9 @@ TEST_F(SlabMemoryAllocatorTests, MultipleSlabs) { kNoSlabPrefetchAllowed, kDisableSlabGrowth, dummyMemoryAllocator.get()); // Fill up exactly two 128B slabs. - std::vector> allocations = {}; + std::vector> allocations = {}; for (uint32_t blocki = 0; blocki < (kDefaultSlabSize * 2 / kBlockSize); blocki++) { - std::unique_ptr allocation = + std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(22, 1)); ASSERT_NE(allocation, nullptr); allocations.push_back(std::move(allocation)); @@ -252,37 +252,37 @@ TEST_F(SlabMemoryAllocatorTests, MultipleSlabs) { dummyMemoryAllocator.get()); // Both allocation A and B go in Slab A, which will become full. - std::unique_ptr allocationAinSlabA = + std::unique_ptr allocationAinSlabA = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocationAinSlabA, nullptr); - std::unique_ptr allocationBInSlabA = + std::unique_ptr allocationBInSlabA = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocationAinSlabA, nullptr); EXPECT_EQ(allocationAinSlabA->GetMemory(), allocationBInSlabA->GetMemory()); // Allocation C and D go in Slab B, which will become full. - std::unique_ptr allocationCInSlabB = + std::unique_ptr allocationCInSlabB = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocationCInSlabB, nullptr); EXPECT_NE(allocationBInSlabA->GetMemory(), allocationCInSlabB->GetMemory()); - std::unique_ptr allocationDInSlabB = + std::unique_ptr allocationDInSlabB = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocationDInSlabB, nullptr); EXPECT_EQ(allocationCInSlabB->GetMemory(), allocationDInSlabB->GetMemory()); // Allocation E and F goes in Slab C, which will become full. - std::unique_ptr allocationEInSlabC = + std::unique_ptr allocationEInSlabC = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocationEInSlabC, nullptr); EXPECT_NE(allocationDInSlabB->GetMemory(), allocationEInSlabC->GetMemory()); - std::unique_ptr allocationFInSlabC = + std::unique_ptr allocationFInSlabC = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocationFInSlabC, nullptr); @@ -297,7 +297,7 @@ TEST_F(SlabMemoryAllocatorTests, MultipleSlabs) { // Free list: B -> A. // Full list: C. - std::unique_ptr allocationGInSlabB = + std::unique_ptr allocationGInSlabB = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocationGInSlabB, nullptr); EXPECT_EQ(allocationDInSlabB->GetMemory(), allocationGInSlabB->GetMemory()); @@ -305,7 +305,7 @@ TEST_F(SlabMemoryAllocatorTests, MultipleSlabs) { // Free list: A. // Full list: B -> C. - std::unique_ptr allocationHInSlabA = + std::unique_ptr allocationHInSlabA = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocationGInSlabB, nullptr); @@ -339,7 +339,7 @@ TEST_F(SlabMemoryAllocatorTests, AllocationOverflow) { kDisableSlabGrowth, dummyMemoryAllocator.get()); constexpr uint64_t largeBlock = (1ull << 63) + 1; - std::unique_ptr invalidAllocation = allocator.TryAllocateMemoryForTesting( + std::unique_ptr invalidAllocation = allocator.TryAllocateMemoryForTesting( CreateBasicRequest(largeBlock, kDefaultSlabAlignment, true)); ASSERT_EQ(invalidAllocation, nullptr); } @@ -356,7 +356,7 @@ TEST_F(SlabMemoryAllocatorTests, ReuseSlabs) { kDisableSlabGrowth, poolAllocator.get()); std::set slabMemory = {}; - std::vector> allocations = {}; + std::vector> allocations = {}; // Count by slabs (vs number of allocations) to ensure there are exactly |kNumOfSlabs| worth of // allocations. Otherwise, the slab may be reused if not full. @@ -364,7 +364,7 @@ TEST_F(SlabMemoryAllocatorTests, ReuseSlabs) { // Allocate |kNumOfSlabs| worth. while (slabMemory.size() < kNumOfSlabs) { - std::unique_ptr allocation = + std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocation, nullptr); EXPECT_EQ(allocation->GetSize(), kBlockSize); @@ -399,7 +399,7 @@ TEST_F(SlabMemoryAllocatorTests, GetInfo) { kNoSlabPrefetchAllowed, kDisableSlabGrowth, dummyMemoryAllocator.get()); - std::unique_ptr allocation = + std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_NE(allocation, nullptr); @@ -433,7 +433,7 @@ TEST_F(SlabMemoryAllocatorTests, GetInfo) { kNoSlabPrefetchAllowed, kDisableSlabGrowth, poolAllocator.get()); - std::unique_ptr allocation = + std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_NE(allocation, nullptr); @@ -467,35 +467,35 @@ TEST_F(SlabMemoryAllocatorTests, SlabGrowth) { /*slabGrowthFactor*/ 2, &dummyAllocator); // Slab A holds 1 allocation. - std::unique_ptr allocationAInSlabA = + std::unique_ptr allocationAInSlabA = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_EQ(allocationAInSlabA->GetSize(), kBlockSize); EXPECT_EQ(allocationAInSlabA->GetMemory()->GetSize(), kBlockSize); // Slab B holds 1 allocation. - std::unique_ptr allocationAInSlabB = + std::unique_ptr allocationAInSlabB = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_EQ(allocationAInSlabB->GetSize(), kBlockSize); EXPECT_EQ(allocationAInSlabB->GetMemory()->GetSize(), kBlockSize); // Slab C grows 2x and holds 2 allocations per slab. - std::unique_ptr allocationAInSlabC = + std::unique_ptr allocationAInSlabC = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_EQ(allocationAInSlabC->GetSize(), kBlockSize); EXPECT_EQ(allocationAInSlabC->GetMemory()->GetSize(), kBlockSize * 2); - std::unique_ptr allocationBInSlabC = + std::unique_ptr allocationBInSlabC = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_EQ(allocationBInSlabC->GetSize(), kBlockSize); EXPECT_EQ(allocationBInSlabC->GetMemory()->GetSize(), kBlockSize * 2); // Slab D still holds 2 allocations per slab. - std::unique_ptr allocationAInSlabD = + std::unique_ptr allocationAInSlabD = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_EQ(allocationAInSlabD->GetSize(), kBlockSize); EXPECT_EQ(allocationAInSlabD->GetMemory()->GetSize(), kBlockSize * 2); - std::unique_ptr allocationBInSlabD = + std::unique_ptr allocationBInSlabD = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_EQ(allocationBInSlabD->GetSize(), kBlockSize); EXPECT_EQ(allocationBInSlabD->GetMemory()->GetSize(), kBlockSize * 2); @@ -522,12 +522,12 @@ TEST_F(SlabMemoryAllocatorTests, SlabGrowth) { /*slabGrowthFactor*/ 2, &dummyAllocator); // Slab A holds 2 allocations per slab. - std::unique_ptr allocationAInSlabA = + std::unique_ptr allocationAInSlabA = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_EQ(allocationAInSlabA->GetSize(), kBlockSize); EXPECT_EQ(allocationAInSlabA->GetMemory()->GetSize(), kMinSlabSize); - std::unique_ptr allocationBInSlabA = + std::unique_ptr allocationBInSlabA = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_EQ(allocationBInSlabA->GetSize(), kBlockSize); EXPECT_EQ(allocationBInSlabA->GetMemory()->GetSize(), kMinSlabSize); @@ -535,12 +535,12 @@ TEST_F(SlabMemoryAllocatorTests, SlabGrowth) { EXPECT_EQ(allocationAInSlabA->GetMemory(), allocationBInSlabA->GetMemory()); // Slab B holds 2 allocations per slab. - std::unique_ptr allocationAInSlabB = + std::unique_ptr allocationAInSlabB = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_EQ(allocationAInSlabB->GetSize(), kBlockSize); EXPECT_EQ(allocationAInSlabB->GetMemory()->GetSize(), kMinSlabSize); - std::unique_ptr allocationBInSlabB = + std::unique_ptr allocationBInSlabB = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_EQ(allocationBInSlabB->GetSize(), kBlockSize); EXPECT_EQ(allocationBInSlabB->GetMemory()->GetSize(), kMinSlabSize); @@ -548,7 +548,7 @@ TEST_F(SlabMemoryAllocatorTests, SlabGrowth) { EXPECT_EQ(allocationAInSlabB->GetMemory(), allocationBInSlabB->GetMemory()); // Slab C grows 2x and holds 4 allocations per slab. - std::unique_ptr allocationAInSlabC = + std::unique_ptr allocationAInSlabC = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_EQ(allocationAInSlabC->GetSize(), kBlockSize); EXPECT_EQ(allocationAInSlabC->GetMemory()->GetSize(), kMinSlabSize * 2); @@ -579,12 +579,12 @@ TEST_F(SlabMemoryAllocatorTests, SlabGrowthLimit) { /*slabGrowthFactor*/ 2, &dummyAllocator); // Slab A holds 2 allocations per slab. - std::unique_ptr allocationAInSlabA = + std::unique_ptr allocationAInSlabA = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_EQ(allocationAInSlabA->GetSize(), kBlockSize); EXPECT_EQ(allocationAInSlabA->GetMemory()->GetSize(), kMinSlabSize); - std::unique_ptr allocationBInSlabA = + std::unique_ptr allocationBInSlabA = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_EQ(allocationBInSlabA->GetSize(), kBlockSize); EXPECT_EQ(allocationBInSlabA->GetMemory()->GetSize(), kMinSlabSize); @@ -592,12 +592,12 @@ TEST_F(SlabMemoryAllocatorTests, SlabGrowthLimit) { EXPECT_EQ(allocationAInSlabA->GetMemory(), allocationBInSlabA->GetMemory()); // Slab B holds 2 allocations per slab. - std::unique_ptr allocationAInSlabB = + std::unique_ptr allocationAInSlabB = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_EQ(allocationAInSlabB->GetSize(), kBlockSize); EXPECT_EQ(allocationAInSlabB->GetMemory()->GetSize(), kMinSlabSize); - std::unique_ptr allocationBInSlabB = + std::unique_ptr allocationBInSlabB = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_EQ(allocationBInSlabB->GetSize(), kBlockSize); EXPECT_EQ(allocationBInSlabB->GetMemory()->GetSize(), kMinSlabSize); @@ -605,7 +605,7 @@ TEST_F(SlabMemoryAllocatorTests, SlabGrowthLimit) { EXPECT_EQ(allocationAInSlabB->GetMemory(), allocationBInSlabB->GetMemory()); // Slab C grows 2x and holds 4 allocations per slab. - std::unique_ptr allocationAInSlabC = + std::unique_ptr allocationAInSlabC = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_EQ(allocationAInSlabC->GetSize(), kBlockSize); EXPECT_EQ(allocationAInSlabC->GetMemory()->GetSize(), kMinSlabSize * 2); @@ -631,13 +631,13 @@ TEST_F(SlabMemoryAllocatorTests, SlabGrowthLimit) { /*slabGrowthFactor*/ 2, &dummyAllocator); // Slab A holds 1 allocation per slab. - std::unique_ptr allocationAInSlabA = + std::unique_ptr allocationAInSlabA = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_EQ(allocationAInSlabA->GetSize(), kBlockSize); EXPECT_EQ(allocationAInSlabA->GetMemory()->GetSize(), kBlockSize); // Slab B holds 1 allocation per slab. - std::unique_ptr allocationAInSlabB = + std::unique_ptr allocationAInSlabB = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocationAInSlabB, nullptr); EXPECT_EQ(allocationAInSlabB->GetSize(), kBlockSize); @@ -646,7 +646,7 @@ TEST_F(SlabMemoryAllocatorTests, SlabGrowthLimit) { EXPECT_NE(allocationAInSlabA->GetMemory(), allocationAInSlabB->GetMemory()); // Slab C grows 2x and holds 2 allocation per slab. - std::unique_ptr allocationAInSlabC = + std::unique_ptr allocationAInSlabC = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocationAInSlabC, nullptr); EXPECT_EQ(allocationAInSlabC->GetSize(), kBlockSize); @@ -654,14 +654,14 @@ TEST_F(SlabMemoryAllocatorTests, SlabGrowthLimit) { EXPECT_NE(allocationAInSlabB->GetMemory(), allocationAInSlabC->GetMemory()); - std::unique_ptr allocationBInSlabC = + std::unique_ptr allocationBInSlabC = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocationBInSlabC, nullptr); EXPECT_EQ(allocationBInSlabC->GetSize(), kBlockSize); EXPECT_EQ(allocationBInSlabC->GetMemory()->GetSize(), kBlockSize * 2); // Slab C still holds 2 allocation per slab. - std::unique_ptr allocationAInSlabD = + std::unique_ptr allocationAInSlabD = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocationAInSlabD, nullptr); EXPECT_EQ(allocationAInSlabD->GetSize(), kBlockSize); @@ -669,7 +669,7 @@ TEST_F(SlabMemoryAllocatorTests, SlabGrowthLimit) { EXPECT_NE(allocationBInSlabC->GetMemory(), allocationAInSlabD->GetMemory()); - std::unique_ptr allocationBInSlabD = + std::unique_ptr allocationBInSlabD = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocationBInSlabD, nullptr); EXPECT_EQ(allocationBInSlabD->GetSize(), kBlockSize); @@ -730,12 +730,12 @@ TEST_F(SlabCacheAllocatorTests, SingleSlabMultipleAlignments) { // Verify requesting an allocation of same size using multiple alignment succeeds. { constexpr uint64_t kBlockSize = 4; - std::unique_ptr allocationWithAlignmentA = + std::unique_ptr allocationWithAlignmentA = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocationWithAlignmentA, nullptr); EXPECT_EQ(allocationWithAlignmentA->GetSize(), AlignTo(kBlockSize, 1)); - std::unique_ptr allocationWithAlignmentB = + std::unique_ptr allocationWithAlignmentB = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 16)); ASSERT_NE(allocationWithAlignmentB, nullptr); EXPECT_EQ(allocationWithAlignmentB->GetSize(), AlignTo(kBlockSize, 16)); @@ -752,22 +752,22 @@ TEST_F(SlabCacheAllocatorTests, MultipleSlabsSameSize) { kDefaultSlabFragmentationLimit, kNoSlabPrefetchAllowed, kDisableSlabGrowth, std::make_unique()); - std::unique_ptr firstAllocation = + std::unique_ptr firstAllocation = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(22, 1)); ASSERT_NE(firstAllocation, nullptr); - std::unique_ptr secondAllocation = + std::unique_ptr secondAllocation = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(22, 1)); ASSERT_NE(secondAllocation, nullptr); allocator.DeallocateMemory(std::move(firstAllocation)); allocator.DeallocateMemory(std::move(secondAllocation)); - std::unique_ptr thirdAllocation = + std::unique_ptr thirdAllocation = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(44, 1)); ASSERT_NE(thirdAllocation, nullptr); - std::unique_ptr fourthAllocation = + std::unique_ptr fourthAllocation = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(44, 1)); ASSERT_NE(fourthAllocation, nullptr); @@ -783,7 +783,7 @@ TEST_F(SlabCacheAllocatorTests, MultipleSlabsVariableSizes) { kDisableSlabGrowth, std::make_unique()); { constexpr uint64_t allocationSize = 22; - std::unique_ptr allocation = + std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(allocationSize, 1)); ASSERT_NE(allocation, nullptr); EXPECT_EQ(allocation->GetOffset(), 0u); @@ -794,7 +794,7 @@ TEST_F(SlabCacheAllocatorTests, MultipleSlabsVariableSizes) { } { constexpr uint64_t allocationSize = 44; - std::unique_ptr allocation = + std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(allocationSize, 1)); ASSERT_NE(allocation, nullptr); EXPECT_EQ(allocation->GetOffset(), 0u); @@ -805,7 +805,7 @@ TEST_F(SlabCacheAllocatorTests, MultipleSlabsVariableSizes) { } { constexpr uint64_t allocationSize = 88; - std::unique_ptr allocation = + std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(allocationSize, 1)); ASSERT_NE(allocation, nullptr); EXPECT_EQ(allocation->GetOffset(), 0u); @@ -832,7 +832,7 @@ TEST_F(SlabCacheAllocatorTests, SingleSlabInBuddy) { std::make_unique())); constexpr uint64_t kBlockSize = 4; - std::unique_ptr allocation = + std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocation, nullptr); EXPECT_EQ(allocation->GetOffset(), 0u); @@ -858,7 +858,7 @@ TEST_F(SlabCacheAllocatorTests, MultipleSlabsInBuddy) { // Verify multiple slab-buddy sub-allocation in the same slab are allocated contigiously. { constexpr uint64_t allocationSize = 8; - std::unique_ptr firstAllocation = + std::unique_ptr firstAllocation = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(allocationSize, 1)); ASSERT_NE(firstAllocation, nullptr); EXPECT_EQ(firstAllocation->GetOffset(), 0u); @@ -867,7 +867,7 @@ TEST_F(SlabCacheAllocatorTests, MultipleSlabsInBuddy) { EXPECT_EQ(firstAllocation->GetMemory()->GetSize(), kDefaultSlabSize); - std::unique_ptr secondAllocation = + std::unique_ptr secondAllocation = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(allocationSize, 1)); ASSERT_NE(secondAllocation, nullptr); EXPECT_EQ(secondAllocation->GetOffset(), allocationSize); @@ -883,9 +883,9 @@ TEST_F(SlabCacheAllocatorTests, MultipleSlabsInBuddy) { // Verify multiple slab-buddy sub-allocations across buddies are allocated non-contigiously. { // Fill the first buddy up with slabs. - std::vector> allocations = {}; + std::vector> allocations = {}; for (uint32_t i = 0; i < kDefaultSlabSize / kSlabSize; i++) { - std::unique_ptr allocation = + std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kSlabSize, 1)); ASSERT_NE(allocation, nullptr); EXPECT_EQ(allocation->GetOffset(), i * kSlabSize); @@ -895,14 +895,14 @@ TEST_F(SlabCacheAllocatorTests, MultipleSlabsInBuddy) { } // Next slab-buddy sub-allocation must be in the second buddy. - std::unique_ptr firstSlabInSecondBuddy = + std::unique_ptr firstSlabInSecondBuddy = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kSlabSize, 1)); ASSERT_NE(firstSlabInSecondBuddy, nullptr); EXPECT_EQ(firstSlabInSecondBuddy->GetOffset(), 0u); EXPECT_EQ(firstSlabInSecondBuddy->GetMethod(), AllocationMethod::kSubAllocated); EXPECT_GE(firstSlabInSecondBuddy->GetSize(), kSlabSize); - std::unique_ptr secondSlabInSecondBuddy = + std::unique_ptr secondSlabInSecondBuddy = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kSlabSize, 1)); ASSERT_NE(secondSlabInSecondBuddy, nullptr); EXPECT_EQ(secondSlabInSecondBuddy->GetOffset(), kSlabSize); @@ -929,7 +929,7 @@ TEST_F(SlabCacheAllocatorTests, GetInfo) { kDefaultSlabFragmentationLimit, kNoSlabPrefetchAllowed, kDisableSlabGrowth, std::make_unique()); - std::unique_ptr allocation = + std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_NE(allocation, nullptr); @@ -960,7 +960,7 @@ TEST_F(SlabCacheAllocatorTests, GetInfo) { std::make_unique(kDefaultSlabSize, kDefaultSlabAlignment, std::make_unique())); - std::unique_ptr allocation = + std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_NE(allocation, nullptr); @@ -994,7 +994,7 @@ TEST_F(SlabCacheAllocatorTests, GetInfo) { std::make_unique())); constexpr uint64_t kBlockSize = 4; - std::unique_ptr allocation = + std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_NE(allocation, nullptr); @@ -1026,7 +1026,7 @@ TEST_F(SlabCacheAllocatorTests, SlabPrefetch) { kDisableSlabGrowth, std::make_unique()); constexpr uint64_t kNumOfSlabs = 10u; - std::vector> allocations = {}; + std::vector> allocations = {}; for (size_t i = 0; i < kNumOfSlabs * (kDefaultSlabSize / kBlockSize); i++) { allocations.push_back( allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1))); @@ -1052,7 +1052,7 @@ TEST_F(SlabCacheAllocatorTests, SlabPrefetchDisabled) { alwaysPrefetchRequest.AlwaysPrefetch = true; constexpr uint64_t kNumOfSlabs = 10u; - std::vector> allocations = {}; + std::vector> allocations = {}; for (size_t i = 0; i < kNumOfSlabs * (kDefaultSlabSize / kBlockSize); i++) { allocations.push_back(allocator.TryAllocateMemoryForTesting(alwaysPrefetchRequest)); } @@ -1075,7 +1075,8 @@ TEST_F(SlabCacheAllocatorTests, AlwaysCache) { MemoryAllocationRequest request = CreateBasicRequest(32, 1); request.AlwaysCacheSize = true; - std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting(request); + std::unique_ptr allocation = + allocator.TryAllocateMemoryForTesting(request); ASSERT_NE(allocation, nullptr); allocator.DeallocateMemory(std::move(allocation)); @@ -1094,9 +1095,9 @@ TEST_F(SlabCacheAllocatorTests, OutOfMemory) { MemoryAllocationRequest request = CreateBasicRequest(32, 1); request.AvailableForAllocation = kTotalMemoryAvailable; - std::vector> allocations = {}; + std::vector> allocations = {}; while (true) { - std::unique_ptr allocation = + std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting(request); if (allocation == nullptr) { break;