diff --git a/src/gpgmm/common/BuddyMemoryAllocator.cpp b/src/gpgmm/common/BuddyMemoryAllocator.cpp index 5d6b8eb12..5bafd4f30 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)); } - std::unique_ptr BuddyMemoryAllocator::TryAllocateMemory( + ResultOrError> BuddyMemoryAllocator::TryAllocateMemory( const MemoryAllocationRequest& request) { TRACE_EVENT0(TraceEventCategory::kDefault, "BuddyMemoryAllocator.TryAllocateMemory"); @@ -57,33 +57,34 @@ namespace gpgmm { // Attempt to sub-allocate a block of the requested size. std::unique_ptr subAllocation; - GPGMM_TRY_ASSIGN(TrySubAllocateMemory( - &mBuddyBlockAllocator, allocationSize, request.Alignment, - request.NeverAllocate, - [&](const auto& block) -> MemoryBase* { - const uint64_t memoryIndex = GetMemoryIndex(block->Offset); - MemoryAllocation memoryAllocation = - mUsedPool.AcquireFromPool(memoryIndex); - - // No existing, allocate new memory for the block. - if (memoryAllocation == GPGMM_INVALID_ALLOCATION) { - MemoryAllocationRequest newRequest = request; - newRequest.SizeInBytes = mMemorySize; - newRequest.Alignment = mMemoryAlignment; - - std::unique_ptr memoryAllocationPtr; - GPGMM_TRY_ASSIGN( - GetNextInChain()->TryAllocateMemory(newRequest), - memoryAllocationPtr); - memoryAllocation = *memoryAllocationPtr; - } - - MemoryBase* memory = memoryAllocation.GetMemory(); - mUsedPool.ReturnToPool(memoryAllocation, memoryIndex); - - return memory; - }), - 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); + + // No existing, allocate new memory for the block. + if (memoryAllocation == GPGMM_INVALID_ALLOCATION) { + MemoryAllocationRequest newRequest = request; + newRequest.SizeInBytes = mMemorySize; + newRequest.Alignment = mMemoryAlignment; + + ResultOrError> memoryAllocationResult = + GetNextInChain()->TryAllocateMemory(newRequest); + if (!memoryAllocationResult.IsSuccess()) { + return memoryAllocationResult.GetErrorCode(); + } + + memoryAllocation = *memoryAllocationResult.AcquireResult(); + } + + MemoryBase* memory = memoryAllocation.GetMemory(); + mUsedPool.ReturnToPool(memoryAllocation, memoryIndex); + + return memory; + }), + subAllocation); MemoryBlock* block = subAllocation->GetBlock(); mStats.UsedBlockCount++; diff --git a/src/gpgmm/common/BuddyMemoryAllocator.h b/src/gpgmm/common/BuddyMemoryAllocator.h index ecb9b28ee..62f28ca64 100644 --- a/src/gpgmm/common/BuddyMemoryAllocator.h +++ b/src/gpgmm/common/BuddyMemoryAllocator.h @@ -43,7 +43,7 @@ namespace gpgmm { std::unique_ptr memoryAllocator); // MemoryAllocator interface - std::unique_ptr TryAllocateMemory( + ResultOrError> TryAllocateMemory( const MemoryAllocationRequest& request) override; void DeallocateMemory(std::unique_ptr subAllocation) override; diff --git a/src/gpgmm/common/ConditionalMemoryAllocator.cpp b/src/gpgmm/common/ConditionalMemoryAllocator.cpp index 7f21264cc..813e7cdd4 100644 --- a/src/gpgmm/common/ConditionalMemoryAllocator.cpp +++ b/src/gpgmm/common/ConditionalMemoryAllocator.cpp @@ -28,7 +28,7 @@ namespace gpgmm { mConditionalSize(conditionalSize) { } - std::unique_ptr ConditionalMemoryAllocator::TryAllocateMemory( + ResultOrError> ConditionalMemoryAllocator::TryAllocateMemory( const MemoryAllocationRequest& request) { TRACE_EVENT0(TraceEventCategory::kDefault, "ConditionalMemoryAllocator.TryAllocateMemory"); if (request.SizeInBytes <= mConditionalSize) { diff --git a/src/gpgmm/common/ConditionalMemoryAllocator.h b/src/gpgmm/common/ConditionalMemoryAllocator.h index 43662d18e..18bbdbdbd 100644 --- a/src/gpgmm/common/ConditionalMemoryAllocator.h +++ b/src/gpgmm/common/ConditionalMemoryAllocator.h @@ -30,7 +30,7 @@ namespace gpgmm { ~ConditionalMemoryAllocator() override = default; // MemoryAllocator interface - std::unique_ptr TryAllocateMemory( + ResultOrError> TryAllocateMemory( const MemoryAllocationRequest& request) override; void DeallocateMemory(std::unique_ptr allocation) override; diff --git a/src/gpgmm/common/DedicatedMemoryAllocator.cpp b/src/gpgmm/common/DedicatedMemoryAllocator.cpp index 0399bfd5c..fa97da786 100644 --- a/src/gpgmm/common/DedicatedMemoryAllocator.cpp +++ b/src/gpgmm/common/DedicatedMemoryAllocator.cpp @@ -24,7 +24,7 @@ namespace gpgmm { : MemoryAllocator(std::move(memoryAllocator)) { } - std::unique_ptr DedicatedMemoryAllocator::TryAllocateMemory( + ResultOrError> DedicatedMemoryAllocator::TryAllocateMemory( const MemoryAllocationRequest& request) { TRACE_EVENT0(TraceEventCategory::kDefault, "DedicatedMemoryAllocator.TryAllocateMemory"); diff --git a/src/gpgmm/common/DedicatedMemoryAllocator.h b/src/gpgmm/common/DedicatedMemoryAllocator.h index 6248a8c58..08250d9ab 100644 --- a/src/gpgmm/common/DedicatedMemoryAllocator.h +++ b/src/gpgmm/common/DedicatedMemoryAllocator.h @@ -28,7 +28,7 @@ namespace gpgmm { DedicatedMemoryAllocator(std::unique_ptr memoryAllocator); // MemoryAllocator interface - std::unique_ptr TryAllocateMemory( + ResultOrError> TryAllocateMemory( const MemoryAllocationRequest& request) override; void DeallocateMemory(std::unique_ptr subAllocation) override; uint64_t GetMemoryAlignment() const override; diff --git a/src/gpgmm/common/Error.h b/src/gpgmm/common/Error.h index 1203db1e9..38e456d4c 100644 --- a/src/gpgmm/common/Error.h +++ b/src/gpgmm/common/Error.h @@ -17,21 +17,82 @@ #include "gpgmm/utils/Assert.h" +#include + namespace gpgmm { + enum class ErrorCodeType : uint32_t; + + constexpr ErrorCodeType kInternalFailureResult = static_cast(-1); + constexpr ErrorCodeType kInternalSuccessResult = static_cast(0u); + + // Wraps a backend error code with a result object. + // Use Result::IsSuccess then Result::AcquireResult to use or else, use Result::GetErrorCode to + // return the error for backend-specific handling. + template + class Result { + public: + // Empty result + Result() : mErrorCode(kInternalFailureResult) { + } + + // Error only result + Result(ErrorT&& error) : mErrorCode(std::move(error)) { + } + + // Result but with no error + Result(ResultT&& result) : mErrorCode(kInternalSuccessResult), mResult(std::move(result)) { + } + + // Result with error. + Result(ErrorT&& error, ResultT&& result) + : mErrorCode(std::move(error)), mResult(std::move(result)) { + } + + Result(Result&& other) + : mErrorCode(std::move(other.mErrorCode), mResult(std::move(other.mResult))) { + } + + Result& operator=(Result&& other) { + mResult = std::move(other.mResult); + mErrorCode = std::move(other.mErrorCode); + return *this; + } + + ErrorCodeType GetErrorCode() const { + return mErrorCode; + } + + ResultT&& AcquireResult() { + return std::move(mResult); + } + + bool IsSuccess() const { + return mErrorCode == kInternalSuccessResult; + } + + private: + ErrorT mErrorCode; + ResultT mResult; + }; + + // Alias of Result + error code to avoid having to always specify error type. + template + using ResultOrError = Result; + #define GPGMM_INVALID_ALLOCATION \ MemoryAllocation { \ } -#define GPGMM_TRY_ASSIGN(expr, value) \ - { \ - auto result = expr; \ - if (GPGMM_UNLIKELY(result == nullptr)) { \ - return {}; \ - } \ - value = std::move(result); \ - } \ - for (;;) \ +#define GPGMM_TRY_ASSIGN(expr, value) \ + { \ + auto result = expr; \ + if (GPGMM_UNLIKELY(!result.IsSuccess())) { \ + return result; \ + } \ + value = result.AcquireResult(); \ + } \ + for (;;) \ break #define GPGMM_INVALID_IF(expr) \ diff --git a/src/gpgmm/common/MemoryAllocator.cpp b/src/gpgmm/common/MemoryAllocator.cpp index e544347ae..1c33fe74e 100644 --- a/src/gpgmm/common/MemoryAllocator.cpp +++ b/src/gpgmm/common/MemoryAllocator.cpp @@ -31,7 +31,7 @@ namespace gpgmm { mAllocation = mAllocator->TryAllocateMemory(mRequest); } - std::unique_ptr AcquireAllocation() { + ResultOrError> AcquireAllocation() { std::lock_guard lock(mAllocationMutex); return std::move(mAllocation); } @@ -41,7 +41,7 @@ namespace gpgmm { const MemoryAllocationRequest mRequest; std::mutex mAllocationMutex; - std::unique_ptr mAllocation; + ResultOrError> mAllocation; }; // MemoryAllocatorStats @@ -81,7 +81,8 @@ namespace gpgmm { return mEvent->Signal(); } - std::unique_ptr MemoryAllocationEvent::AcquireAllocation() const { + ResultOrError> MemoryAllocationEvent::AcquireAllocation() + const { return mTask->AcquireAllocation(); } @@ -115,12 +116,17 @@ namespace gpgmm { } } - std::unique_ptr MemoryAllocator::TryAllocateMemory( + ResultOrError> MemoryAllocator::TryAllocateMemory( const MemoryAllocationRequest& request) { ASSERT(false); return {}; } + std::unique_ptr MemoryAllocator::TryAllocateMemoryForTesting( + const MemoryAllocationRequest& request) { + return TryAllocateMemory(request).AcquireResult(); + } + std::shared_ptr MemoryAllocator::TryAllocateMemoryAsync( const MemoryAllocationRequest& request) { std::shared_ptr task = diff --git a/src/gpgmm/common/MemoryAllocator.h b/src/gpgmm/common/MemoryAllocator.h index fec2ccfa8..d9c9b535e 100644 --- a/src/gpgmm/common/MemoryAllocator.h +++ b/src/gpgmm/common/MemoryAllocator.h @@ -56,7 +56,7 @@ namespace gpgmm { \return Pointer to MemoryAllocation that was allocated. */ - std::unique_ptr AcquireAllocation() const; + ResultOrError> AcquireAllocation() const; private: void Signal() override; @@ -152,7 +152,12 @@ namespace gpgmm { \return A pointer to MemoryAllocation. If NULL, the request could not be full-filled. */ - virtual std::unique_ptr 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( const MemoryAllocationRequest& request); /** \brief Non-blocking version of TryAllocateMemory. @@ -243,17 +248,19 @@ 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 std::unique_ptr TrySubAllocateMemory( + static ResultOrError> TrySubAllocateMemory( BlockAllocator* allocator, uint64_t requestSize, uint64_t alignment, bool neverAllocate, GetOrCreateMemoryFn&& GetOrCreateMemory) { - MemoryBlock* block = nullptr; - GPGMM_TRY_ASSIGN(allocator->TryAllocateBlock(requestSize, alignment), block); + MemoryBlock* block = allocator->TryAllocateBlock(requestSize, alignment); + if (block == nullptr) { + return {}; + } - MemoryBase* memory = GetOrCreateMemory(block); - if (memory == nullptr) { + ResultOrError result = GetOrCreateMemory(block); + if (!result.IsSuccess()) { // NeverAllocate always fails, so suppress it. if (!neverAllocate) { DebugLog() << std::string(allocator->GetTypename()) + @@ -262,10 +269,12 @@ namespace gpgmm { << std::to_string(block->Offset + block->Size) << ")."; } allocator->DeallocateBlock(block); - return nullptr; + return result.GetErrorCode(); } + MemoryBase* memory = result.AcquireResult(); ASSERT(memory != nullptr); + memory->AddSubAllocationRef(); // Caller is be responsible in fully initializing the memory allocation. diff --git a/src/gpgmm/common/PooledMemoryAllocator.cpp b/src/gpgmm/common/PooledMemoryAllocator.cpp index 4ab9c6861..5fae41978 100644 --- a/src/gpgmm/common/PooledMemoryAllocator.cpp +++ b/src/gpgmm/common/PooledMemoryAllocator.cpp @@ -34,7 +34,7 @@ namespace gpgmm { mPool->ReleasePool(kInvalidSize); } - std::unique_ptr PooledMemoryAllocator::TryAllocateMemory( + ResultOrError> PooledMemoryAllocator::TryAllocateMemory( const MemoryAllocationRequest& request) { TRACE_EVENT0(TraceEventCategory::kDefault, "PooledMemoryAllocator.TryAllocateMemory"); diff --git a/src/gpgmm/common/PooledMemoryAllocator.h b/src/gpgmm/common/PooledMemoryAllocator.h index 2db088905..ec46cc722 100644 --- a/src/gpgmm/common/PooledMemoryAllocator.h +++ b/src/gpgmm/common/PooledMemoryAllocator.h @@ -30,7 +30,7 @@ namespace gpgmm { ~PooledMemoryAllocator() override; // MemoryAllocator interface - std::unique_ptr TryAllocateMemory( + ResultOrError> TryAllocateMemory( const MemoryAllocationRequest& request) override; void DeallocateMemory(std::unique_ptr allocation) override; uint64_t ReleaseMemory(uint64_t bytesToRelease = kInvalidSize) override; diff --git a/src/gpgmm/common/SegmentedMemoryAllocator.cpp b/src/gpgmm/common/SegmentedMemoryAllocator.cpp index 3f40f7343..75a3518ec 100644 --- a/src/gpgmm/common/SegmentedMemoryAllocator.cpp +++ b/src/gpgmm/common/SegmentedMemoryAllocator.cpp @@ -119,7 +119,7 @@ namespace gpgmm { return newFreeSegment; } - std::unique_ptr SegmentedMemoryAllocator::TryAllocateMemory( + ResultOrError> SegmentedMemoryAllocator::TryAllocateMemory( const MemoryAllocationRequest& request) { TRACE_EVENT0(TraceEventCategory::kDefault, "SegmentedMemoryAllocator.TryAllocateMemory"); diff --git a/src/gpgmm/common/SegmentedMemoryAllocator.h b/src/gpgmm/common/SegmentedMemoryAllocator.h index 98df17a49..6bce3897e 100644 --- a/src/gpgmm/common/SegmentedMemoryAllocator.h +++ b/src/gpgmm/common/SegmentedMemoryAllocator.h @@ -39,7 +39,7 @@ namespace gpgmm { ~SegmentedMemoryAllocator() override; // MemoryAllocator interface - std::unique_ptr TryAllocateMemory( + ResultOrError> TryAllocateMemory( const MemoryAllocationRequest& request) override; void DeallocateMemory(std::unique_ptr allocation) override; uint64_t ReleaseMemory(uint64_t bytesToRelease = kInvalidSize) override; diff --git a/src/gpgmm/common/SlabMemoryAllocator.cpp b/src/gpgmm/common/SlabMemoryAllocator.cpp index 2915100d7..78599be61 100644 --- a/src/gpgmm/common/SlabMemoryAllocator.cpp +++ b/src/gpgmm/common/SlabMemoryAllocator.cpp @@ -106,7 +106,11 @@ namespace gpgmm { SlabMemoryAllocator::~SlabMemoryAllocator() { if (mNextSlabAllocationEvent != nullptr) { mNextSlabAllocationEvent->Wait(); - mMemoryAllocator->DeallocateMemory(mNextSlabAllocationEvent->AcquireAllocation()); + ResultOrError> result = + mNextSlabAllocationEvent->AcquireAllocation(); + if (result.IsSuccess()) { + mMemoryAllocator->DeallocateMemory(result.AcquireResult()); + } } for (SlabCache& cache : mCaches) { @@ -190,7 +194,7 @@ namespace gpgmm { return cache; } - std::unique_ptr SlabMemoryAllocator::TryAllocateMemory( + ResultOrError> SlabMemoryAllocator::TryAllocateMemory( const MemoryAllocationRequest& request) { TRACE_EVENT0(TraceEventCategory::kDefault, "SlabMemoryAllocator.TryAllocateMemory"); @@ -256,7 +260,7 @@ namespace gpgmm { GPGMM_TRY_ASSIGN( TrySubAllocateMemory( &pFreeSlab->Allocator, mBlockSize, request.Alignment, request.NeverAllocate, - [&](const auto& block) -> MemoryBase* { + [&](const auto& block) -> ResultOrError { // Re-use memory from the free slab. if (pFreeSlab->Allocation.GetMemory() != nullptr) { return pFreeSlab->Allocation.GetMemory(); @@ -267,8 +271,14 @@ namespace gpgmm { if (mNextSlabAllocationEvent != nullptr) { // Resolve the pending pre-fetched allocation. mNextSlabAllocationEvent->Wait(); - std::unique_ptr prefetchedSlabAllocation = + ResultOrError> memoryAllocationResult = mNextSlabAllocationEvent->AcquireAllocation(); + if (!memoryAllocationResult.IsSuccess()) { + return memoryAllocationResult.GetErrorCode(); + } + + std::unique_ptr prefetchedSlabAllocation = + memoryAllocationResult.AcquireResult(); mNextSlabAllocationEvent.reset(); // Assign pre-fetched memory to the slab. @@ -295,11 +305,13 @@ namespace gpgmm { newSlabRequest.SizeInBytes = slabSize; newSlabRequest.Alignment = mSlabAlignment; - std::unique_ptr slabAllocation; - GPGMM_TRY_ASSIGN(mMemoryAllocator->TryAllocateMemory(newSlabRequest), - slabAllocation); + ResultOrError> slabAllocationResult = + mMemoryAllocator->TryAllocateMemory(newSlabRequest); + if (!slabAllocationResult.IsSuccess()) { + return slabAllocationResult.GetErrorCode(); + } - pFreeSlab->Allocation = *slabAllocation; + pFreeSlab->Allocation = *slabAllocationResult.AcquireResult(); return pFreeSlab->Allocation.GetMemory(); }), @@ -490,7 +502,7 @@ namespace gpgmm { mSizeCache.clear(); } - std::unique_ptr SlabCacheAllocator::TryAllocateMemory( + ResultOrError> SlabCacheAllocator::TryAllocateMemory( const MemoryAllocationRequest& request) { TRACE_EVENT0(TraceEventCategory::kDefault, "SlabCacheAllocator.TryAllocateMemory"); diff --git a/src/gpgmm/common/SlabMemoryAllocator.h b/src/gpgmm/common/SlabMemoryAllocator.h index a9badb77d..e40f6e346 100644 --- a/src/gpgmm/common/SlabMemoryAllocator.h +++ b/src/gpgmm/common/SlabMemoryAllocator.h @@ -55,7 +55,7 @@ namespace gpgmm { ~SlabMemoryAllocator() override; // MemoryAllocator interface - std::unique_ptr TryAllocateMemory( + ResultOrError> TryAllocateMemory( const MemoryAllocationRequest& request) override; void DeallocateMemory(std::unique_ptr allocation) override; @@ -122,7 +122,7 @@ namespace gpgmm { ~SlabCacheAllocator() override; // MemoryAllocator interface - std::unique_ptr TryAllocateMemory( + ResultOrError> TryAllocateMemory( const MemoryAllocationRequest& request) override; void DeallocateMemory(std::unique_ptr allocation) override; diff --git a/src/gpgmm/d3d12/BufferAllocatorD3D12.cpp b/src/gpgmm/d3d12/BufferAllocatorD3D12.cpp index ddb11edc6..6b8e25938 100644 --- a/src/gpgmm/d3d12/BufferAllocatorD3D12.cpp +++ b/src/gpgmm/d3d12/BufferAllocatorD3D12.cpp @@ -36,7 +36,7 @@ namespace gpgmm::d3d12 { mInitialResourceState(initialResourceState) { } - std::unique_ptr BufferAllocator::TryAllocateMemory( + ResultOrError> BufferAllocator::TryAllocateMemory( const MemoryAllocationRequest& request) { TRACE_EVENT0(TraceEventCategory::kDefault, "BufferAllocator.TryAllocateMemory"); @@ -72,18 +72,19 @@ namespace gpgmm::d3d12 { resourceDescriptor.Flags = mResourceFlags; // Optimized clear is not supported for buffers. - Heap* resourceHeap = nullptr; - if (FAILED(mResourceAllocator->CreateCommittedResource( - mHeapProperties, mHeapFlags, info, &resourceDescriptor, - /*pOptimizedClearValue*/ nullptr, mInitialResourceState, /*resourceOut*/ nullptr, - &resourceHeap))) { - return {}; + ComPtr resourceHeap; + HRESULT hr = mResourceAllocator->CreateCommittedResource( + mHeapProperties, mHeapFlags, info, &resourceDescriptor, + /*pOptimizedClearValue*/ nullptr, mInitialResourceState, /*resourceOut*/ nullptr, + &resourceHeap); + if (FAILED(hr)) { + return {static_cast(hr)}; } mStats.UsedMemoryUsage += resourceHeap->GetSize(); mStats.UsedMemoryCount++; - return std::make_unique(this, resourceHeap, request.SizeInBytes); + return std::make_unique(this, resourceHeap.Detach(), request.SizeInBytes); } void BufferAllocator::DeallocateMemory(std::unique_ptr allocation) { diff --git a/src/gpgmm/d3d12/BufferAllocatorD3D12.h b/src/gpgmm/d3d12/BufferAllocatorD3D12.h index 472fc4315..210d9c137 100644 --- a/src/gpgmm/d3d12/BufferAllocatorD3D12.h +++ b/src/gpgmm/d3d12/BufferAllocatorD3D12.h @@ -33,7 +33,7 @@ namespace gpgmm::d3d12 { ~BufferAllocator() override = default; // MemoryAllocator interface - std::unique_ptr TryAllocateMemory( + ResultOrError> TryAllocateMemory( const MemoryAllocationRequest& request) override; void DeallocateMemory(std::unique_ptr allocation) override; diff --git a/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp b/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp index 4a0bc658c..64daeca41 100644 --- a/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp +++ b/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp @@ -305,16 +305,20 @@ namespace gpgmm::d3d12 { MemoryAllocator* allocator, const MemoryAllocationRequest& request, CreateResourceFn&& createResourceFn) { - std::unique_ptr allocation = allocator->TryAllocateMemory(request); - if (allocation == nullptr) { + ResultOrError> result = + allocator->TryAllocateMemory(request); + if (FAILED(result.GetErrorCode())) { // NeverAllocate always fails, so suppress it. if (!request.NeverAllocate) { DebugEvent(allocator, EventMessageId::kAllocatorFailed) << "Unable to allocate memory for request."; } - return E_FAIL; + return static_cast(result.GetErrorCode()); } + std::unique_ptr allocation = result.AcquireResult(); + ASSERT(allocation != nullptr); + HRESULT hr = createResourceFn(*allocation); if (FAILED(hr)) { InfoEvent(allocator, EventMessageId::kAllocatorFailed) diff --git a/src/gpgmm/d3d12/ResourceHeapAllocatorD3D12.cpp b/src/gpgmm/d3d12/ResourceHeapAllocatorD3D12.cpp index 732b69661..27ac7ff45 100644 --- a/src/gpgmm/d3d12/ResourceHeapAllocatorD3D12.cpp +++ b/src/gpgmm/d3d12/ResourceHeapAllocatorD3D12.cpp @@ -38,7 +38,7 @@ namespace gpgmm::d3d12 { mHeapFlags(heapFlags) { } - std::unique_ptr ResourceHeapAllocator::TryAllocateMemory( + ResultOrError> ResourceHeapAllocator::TryAllocateMemory( const MemoryAllocationRequest& request) { TRACE_EVENT0(TraceEventCategory::kDefault, "ResourceHeapAllocator.TryAllocateMemory"); @@ -72,10 +72,11 @@ namespace gpgmm::d3d12 { CreateResourceHeapCallbackContext createResourceHeapCallbackContext(mDevice, &heapDesc); ComPtr resourceHeap; - if (FAILED(Heap::CreateHeap(resourceHeapDesc, mResidencyManager, - CreateResourceHeapCallbackContext::CreateHeap, - &createResourceHeapCallbackContext, &resourceHeap))) { - return {}; + HRESULT hr = Heap::CreateHeap(resourceHeapDesc, mResidencyManager, + CreateResourceHeapCallbackContext::CreateHeap, + &createResourceHeapCallbackContext, &resourceHeap); + if (FAILED(hr)) { + return {static_cast(hr)}; } if (resourceHeapDesc.SizeInBytes > request.SizeInBytes) { diff --git a/src/gpgmm/d3d12/ResourceHeapAllocatorD3D12.h b/src/gpgmm/d3d12/ResourceHeapAllocatorD3D12.h index fcbfd68ec..02a442915 100644 --- a/src/gpgmm/d3d12/ResourceHeapAllocatorD3D12.h +++ b/src/gpgmm/d3d12/ResourceHeapAllocatorD3D12.h @@ -45,7 +45,7 @@ namespace gpgmm::d3d12 { ~ResourceHeapAllocator() override = default; // MemoryAllocator interface - std::unique_ptr TryAllocateMemory( + ResultOrError> TryAllocateMemory( const MemoryAllocationRequest& request) override; void DeallocateMemory(std::unique_ptr allocation) override; diff --git a/src/gpgmm/vk/DeviceMemoryAllocatorVk.cpp b/src/gpgmm/vk/DeviceMemoryAllocatorVk.cpp index 0cb13f11e..d7b49fb32 100644 --- a/src/gpgmm/vk/DeviceMemoryAllocatorVk.cpp +++ b/src/gpgmm/vk/DeviceMemoryAllocatorVk.cpp @@ -28,7 +28,7 @@ namespace gpgmm::vk { : mResourceAllocator(resourceAllocator), mMemoryTypeIndex(memoryTypeIndex) { } - std::unique_ptr DeviceMemoryAllocator::TryAllocateMemory( + ResultOrError> DeviceMemoryAllocator::TryAllocateMemory( const MemoryAllocationRequest& request) { TRACE_EVENT0(TraceEventCategory::kDefault, "DeviceMemoryAllocator.TryAllocateMemory"); diff --git a/src/gpgmm/vk/DeviceMemoryAllocatorVk.h b/src/gpgmm/vk/DeviceMemoryAllocatorVk.h index db3925313..e7b9a9d60 100644 --- a/src/gpgmm/vk/DeviceMemoryAllocatorVk.h +++ b/src/gpgmm/vk/DeviceMemoryAllocatorVk.h @@ -28,7 +28,7 @@ namespace gpgmm::vk { ~DeviceMemoryAllocator() override = default; // MemoryAllocator interface - std::unique_ptr TryAllocateMemory( + ResultOrError> TryAllocateMemory( const MemoryAllocationRequest& request) override; void DeallocateMemory(std::unique_ptr allocation) override; diff --git a/src/gpgmm/vk/ResourceAllocatorVk.cpp b/src/gpgmm/vk/ResourceAllocatorVk.cpp index 0d7ab3fd2..8082601dc 100644 --- a/src/gpgmm/vk/ResourceAllocatorVk.cpp +++ b/src/gpgmm/vk/ResourceAllocatorVk.cpp @@ -341,25 +341,25 @@ namespace gpgmm::vk { // Attempt to allocate using the most effective allocator. MemoryAllocator* allocator = nullptr; - std::unique_ptr memoryAllocation; + ResultOrError> result; if (!neverSubAllocate) { allocator = mResourceAllocatorsPerType[memoryTypeIndex].get(); - memoryAllocation = allocator->TryAllocateMemory(request); + result = allocator->TryAllocateMemory(request); } - if (memoryAllocation == nullptr) { + if (!result.IsSuccess()) { allocator = mDeviceAllocatorsPerType[memoryTypeIndex].get(); - memoryAllocation = allocator->TryAllocateMemory(request); + result = allocator->TryAllocateMemory(request); } - if (memoryAllocation == nullptr) { + if (!result.IsSuccess()) { ErrorEvent(allocator, EventMessageId::kAllocatorFailed) << "Unable to allocate memory for resource."; return VK_ERROR_UNKNOWN; } - *allocationOut = new GpResourceAllocation_T(*memoryAllocation); + *allocationOut = new GpResourceAllocation_T(*result.AcquireResult()); return VK_SUCCESS; } diff --git a/src/tests/DummyMemoryAllocator.h b/src/tests/DummyMemoryAllocator.h index 2f023e9e2..7390a19de 100644 --- a/src/tests/DummyMemoryAllocator.h +++ b/src/tests/DummyMemoryAllocator.h @@ -35,7 +35,7 @@ namespace gpgmm { : MemoryAllocator(std::move(next)) { } - std::unique_ptr TryAllocateMemory( + ResultOrError> TryAllocateMemory( const MemoryAllocationRequest& request) override { TRACE_EVENT0(TraceEventCategory::kDefault, "DummyMemoryAllocator.TryAllocateMemory"); diff --git a/src/tests/perftests/MemoryAllocatorPerfTests.cpp b/src/tests/perftests/MemoryAllocatorPerfTests.cpp index a723661f7..1af62ba60 100644 --- a/src/tests/perftests/MemoryAllocatorPerfTests.cpp +++ b/src/tests/perftests/MemoryAllocatorPerfTests.cpp @@ -50,7 +50,7 @@ class SingleSizeAllocationPerfTests : public MemoryAllocatorPerfTests { const MemoryAllocationRequest& request) const { std::vector> allocations; for (int i = 0; i < state.range(3); i++) { - auto allocation = allocator->TryAllocateMemory(request); + auto allocation = allocator->TryAllocateMemoryForTesting(request); if (allocation == nullptr) { state.SkipWithError("Unable to allocate. Skipping."); return; diff --git a/src/tests/unittests/BuddyMemoryAllocatorTests.cpp b/src/tests/unittests/BuddyMemoryAllocatorTests.cpp index 545951459..636e1f5fd 100644 --- a/src/tests/unittests/BuddyMemoryAllocatorTests.cpp +++ b/src/tests/unittests/BuddyMemoryAllocatorTests.cpp @@ -56,14 +56,14 @@ TEST_F(BuddyMemoryAllocatorTests, SingleHeap) { // Cannot allocate greater than heap size. { - std::unique_ptr invalidAllocation = allocator.TryAllocateMemory( + 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 = - allocator.TryAllocateMemory(CreateBasicRequest(128, kDefaultMemoryAlignment)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(128, kDefaultMemoryAlignment)); ASSERT_NE(allocation1, nullptr); ASSERT_EQ(allocation1->GetBlock()->Offset, 0u); ASSERT_EQ(allocation1->GetMethod(), AllocationMethod::kSubAllocated); @@ -74,7 +74,7 @@ TEST_F(BuddyMemoryAllocatorTests, SingleHeap) { // Cannot allocate when allocator is full. { std::unique_ptr invalidAllocation = - allocator.TryAllocateMemory(CreateBasicRequest(128, kDefaultMemoryAlignment)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(128, kDefaultMemoryAlignment)); ASSERT_EQ(invalidAllocation, nullptr); } @@ -98,20 +98,20 @@ TEST_F(BuddyMemoryAllocatorTests, MultipleHeaps) { // Cannot allocate greater than heap size. { - std::unique_ptr invalidAllocation = allocator.TryAllocateMemory( + 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.TryAllocateMemory( + std::unique_ptr invalidAllocation = allocator.TryAllocateMemoryForTesting( CreateBasicRequest(maxBlockSize * 2, kDefaultMemoryAlignment)); ASSERT_EQ(invalidAllocation, nullptr); } // Allocate two 128 byte allocations. - std::unique_ptr allocation1 = allocator.TryAllocateMemory( + std::unique_ptr allocation1 = allocator.TryAllocateMemoryForTesting( CreateBasicRequest(kDefaultMemorySize, kDefaultMemoryAlignment)); ASSERT_NE(allocation1, nullptr); ASSERT_EQ(allocation1->GetSize(), kDefaultMemorySize); @@ -121,7 +121,7 @@ TEST_F(BuddyMemoryAllocatorTests, MultipleHeaps) { // First allocation creates first heap. ASSERT_EQ(allocator.GetStats().UsedMemoryCount, 1u); - std::unique_ptr allocation2 = allocator.TryAllocateMemory( + std::unique_ptr allocation2 = allocator.TryAllocateMemoryForTesting( CreateBasicRequest(kDefaultMemorySize, kDefaultMemoryAlignment)); ASSERT_NE(allocation2, nullptr); ASSERT_EQ(allocation2->GetSize(), kDefaultMemorySize); @@ -158,7 +158,7 @@ TEST_F(BuddyMemoryAllocatorTests, MultipleSplitHeaps) { // Allocate two 64 byte sub-allocations. std::unique_ptr allocation1 = - allocator.TryAllocateMemory(CreateBasicRequest(kDefaultMemorySize / 2, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kDefaultMemorySize / 2, 1)); ASSERT_NE(allocation1, nullptr); ASSERT_EQ(allocation1->GetSize(), kDefaultMemorySize / 2); ASSERT_EQ(allocation1->GetBlock()->Offset, 0u); @@ -168,7 +168,7 @@ TEST_F(BuddyMemoryAllocatorTests, MultipleSplitHeaps) { ASSERT_EQ(allocator.GetStats().UsedMemoryCount, 1u); std::unique_ptr allocation2 = - allocator.TryAllocateMemory(CreateBasicRequest(kDefaultMemorySize / 2, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kDefaultMemorySize / 2, 1)); ASSERT_NE(allocation2, nullptr); ASSERT_EQ(allocation2->GetSize(), kDefaultMemorySize / 2); ASSERT_EQ(allocation2->GetBlock()->Offset, kDefaultMemorySize / 2); @@ -179,7 +179,7 @@ TEST_F(BuddyMemoryAllocatorTests, MultipleSplitHeaps) { ASSERT_EQ(allocation1->GetMemory(), allocation2->GetMemory()); std::unique_ptr allocation3 = - allocator.TryAllocateMemory(CreateBasicRequest(kDefaultMemorySize / 2, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kDefaultMemorySize / 2, 1)); ASSERT_NE(allocation3, nullptr); ASSERT_EQ(allocation3->GetSize(), kDefaultMemorySize / 2); ASSERT_EQ(allocation3->GetBlock()->Offset, kDefaultMemorySize); @@ -221,7 +221,7 @@ TEST_F(BuddyMemoryAllocatorTests, MultiplSplitHeapsVariableSizes) { // Allocate two 64-byte allocations. std::unique_ptr allocation1 = - allocator.TryAllocateMemory(CreateBasicRequest(64, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(64, 1)); ASSERT_NE(allocation1, nullptr); ASSERT_EQ(allocation1->GetSize(), 64u); ASSERT_EQ(allocation1->GetBlock()->Offset, 0u); @@ -229,7 +229,7 @@ TEST_F(BuddyMemoryAllocatorTests, MultiplSplitHeapsVariableSizes) { ASSERT_EQ(allocation1->GetMethod(), AllocationMethod::kSubAllocated); std::unique_ptr allocation2 = - allocator.TryAllocateMemory(CreateBasicRequest(64, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(64, 1)); ASSERT_NE(allocation2, nullptr); ASSERT_EQ(allocation2->GetSize(), 64u); ASSERT_EQ(allocation2->GetBlock()->Offset, 64u); @@ -241,7 +241,7 @@ TEST_F(BuddyMemoryAllocatorTests, MultiplSplitHeapsVariableSizes) { ASSERT_EQ(allocation1->GetMemory(), allocation2->GetMemory()); std::unique_ptr allocation3 = - allocator.TryAllocateMemory(CreateBasicRequest(128, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(128, 1)); ASSERT_NE(allocation3, nullptr); ASSERT_EQ(allocation3->GetSize(), 128u); ASSERT_EQ(allocation3->GetBlock()->Offset, 128u); @@ -253,7 +253,7 @@ TEST_F(BuddyMemoryAllocatorTests, MultiplSplitHeapsVariableSizes) { ASSERT_NE(allocation2->GetMemory(), allocation3->GetMemory()); std::unique_ptr allocation4 = - allocator.TryAllocateMemory(CreateBasicRequest(64, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(64, 1)); ASSERT_NE(allocation4, nullptr); ASSERT_EQ(allocation4->GetSize(), 64u); ASSERT_EQ(allocation4->GetBlock()->Offset, 256u); @@ -265,7 +265,7 @@ TEST_F(BuddyMemoryAllocatorTests, MultiplSplitHeapsVariableSizes) { // R5 size forms 64 byte hole after R4. std::unique_ptr allocation5 = - allocator.TryAllocateMemory(CreateBasicRequest(128, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(128, 1)); ASSERT_NE(allocation5, nullptr); ASSERT_EQ(allocation5->GetSize(), 128u); ASSERT_EQ(allocation5->GetBlock()->Offset, 384u); @@ -311,7 +311,7 @@ TEST_F(BuddyMemoryAllocatorTests, SameSizeVariousAlignment) { std::make_unique()); std::unique_ptr allocation1 = - allocator.TryAllocateMemory(CreateBasicRequest(64, 128)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(64, 128)); ASSERT_NE(allocation1, nullptr); ASSERT_EQ(allocation1->GetSize(), 64u); ASSERT_EQ(allocation1->GetBlock()->Offset, 0u); @@ -321,7 +321,7 @@ TEST_F(BuddyMemoryAllocatorTests, SameSizeVariousAlignment) { ASSERT_EQ(allocator.GetStats().UsedMemoryCount, 1u); std::unique_ptr allocation2 = - allocator.TryAllocateMemory(CreateBasicRequest(64, 128)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(64, 128)); ASSERT_NE(allocation2, nullptr); ASSERT_EQ(allocation2->GetSize(), 64u); ASSERT_EQ(allocation2->GetBlock()->Offset, 128u); @@ -332,7 +332,7 @@ TEST_F(BuddyMemoryAllocatorTests, SameSizeVariousAlignment) { ASSERT_NE(allocation1->GetMemory(), allocation2->GetMemory()); std::unique_ptr allocation3 = - allocator.TryAllocateMemory(CreateBasicRequest(64, 128)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(64, 128)); ASSERT_NE(allocation3, nullptr); ASSERT_EQ(allocation3->GetSize(), 64u); ASSERT_EQ(allocation3->GetBlock()->Offset, 256u); @@ -343,7 +343,7 @@ TEST_F(BuddyMemoryAllocatorTests, SameSizeVariousAlignment) { ASSERT_NE(allocation2->GetMemory(), allocation3->GetMemory()); std::unique_ptr allocation4 = - allocator.TryAllocateMemory(CreateBasicRequest(64, 64)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(64, 64)); ASSERT_NE(allocation4, nullptr); ASSERT_EQ(allocation4->GetSize(), 64u); ASSERT_EQ(allocation4->GetBlock()->Offset, 320u); @@ -387,7 +387,7 @@ TEST_F(BuddyMemoryAllocatorTests, VariousSizeSameAlignment) { constexpr uint64_t alignment = 64; std::unique_ptr allocation1 = - allocator.TryAllocateMemory(CreateBasicRequest(64, alignment)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(64, alignment)); ASSERT_NE(allocation1, nullptr); ASSERT_EQ(allocation1->GetSize(), 64u); ASSERT_EQ(allocation1->GetBlock()->Offset, 0u); @@ -396,7 +396,7 @@ TEST_F(BuddyMemoryAllocatorTests, VariousSizeSameAlignment) { ASSERT_EQ(allocator.GetStats().UsedMemoryCount, 1u); std::unique_ptr allocation2 = - allocator.TryAllocateMemory(CreateBasicRequest(64, alignment)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(64, alignment)); ASSERT_NE(allocation2, nullptr); ASSERT_EQ(allocation2->GetSize(), 64u); ASSERT_EQ(allocation2->GetBlock()->Offset, 64u); @@ -407,7 +407,7 @@ TEST_F(BuddyMemoryAllocatorTests, VariousSizeSameAlignment) { ASSERT_EQ(allocation1->GetMemory(), allocation2->GetMemory()); std::unique_ptr allocation3 = - allocator.TryAllocateMemory(CreateBasicRequest(128, alignment)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(128, alignment)); ASSERT_NE(allocation3, nullptr); ASSERT_EQ(allocation3->GetSize(), 128u); ASSERT_EQ(allocation3->GetBlock()->Offset, 128u); @@ -418,7 +418,7 @@ TEST_F(BuddyMemoryAllocatorTests, VariousSizeSameAlignment) { ASSERT_NE(allocation2->GetMemory(), allocation3->GetMemory()); std::unique_ptr allocation4 = - allocator.TryAllocateMemory(CreateBasicRequest(128, alignment)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(128, alignment)); ASSERT_NE(allocation4, nullptr); ASSERT_EQ(allocation4->GetSize(), 128u); ASSERT_EQ(allocation4->GetBlock()->Offset, 256u); @@ -448,8 +448,8 @@ TEST_F(BuddyMemoryAllocatorTests, AllocationOverflow) { std::make_unique()); constexpr uint64_t largeBlock = (1ull << 63) + 1; - std::unique_ptr invalidAllocation = - allocator.TryAllocateMemory(CreateBasicRequest(largeBlock, kDefaultMemoryAlignment)); + std::unique_ptr invalidAllocation = allocator.TryAllocateMemoryForTesting( + CreateBasicRequest(largeBlock, kDefaultMemoryAlignment)); ASSERT_EQ(invalidAllocation, nullptr); } @@ -471,7 +471,7 @@ TEST_F(BuddyMemoryAllocatorTests, ReuseFreedHeaps) { // Allocate |kNumOfAllocations|. for (uint32_t i = 0; i < kNumOfAllocations; i++) { std::unique_ptr allocation = - allocator.TryAllocateMemory(CreateBasicRequest(4, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(4, 1)); ASSERT_NE(allocation, nullptr); ASSERT_EQ(allocation->GetSize(), 4u); ASSERT_EQ(allocation->GetMethod(), AllocationMethod::kSubAllocated); @@ -494,7 +494,7 @@ TEST_F(BuddyMemoryAllocatorTests, ReuseFreedHeaps) { // Allocate again reusing the same heaps. for (uint32_t i = 0; i < kNumOfAllocations; i++) { std::unique_ptr allocation = - allocator.TryAllocateMemory(CreateBasicRequest(4, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(4, 1)); ASSERT_NE(allocation, nullptr); ASSERT_EQ(allocation->GetSize(), 4u); ASSERT_EQ(allocation->GetMethod(), AllocationMethod::kSubAllocated); @@ -533,7 +533,7 @@ TEST_F(BuddyMemoryAllocatorTests, DestroyHeaps) { // Allocate |kNumOfHeaps| worth. while (heaps.size() < kNumOfHeaps) { std::unique_ptr allocation = - allocator.TryAllocateMemory(CreateBasicRequest(4, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(4, 1)); ASSERT_NE(allocation, nullptr); ASSERT_EQ(allocation->GetSize(), 4u); ASSERT_EQ(allocation->GetMethod(), AllocationMethod::kSubAllocated); diff --git a/src/tests/unittests/ConditionalMemoryAllocatorTests.cpp b/src/tests/unittests/ConditionalMemoryAllocatorTests.cpp index 7d915b802..19eb7719e 100644 --- a/src/tests/unittests/ConditionalMemoryAllocatorTests.cpp +++ b/src/tests/unittests/ConditionalMemoryAllocatorTests.cpp @@ -40,7 +40,7 @@ TEST_F(ConditionalMemoryAllocatorTests, Basic) { // Smaller allocation uses firstAllocator. { std::unique_ptr allocation = - alloc.TryAllocateMemory(CreateBasicRequest(4, 1)); + alloc.TryAllocateMemoryForTesting(CreateBasicRequest(4, 1)); ASSERT_EQ(alloc.GetFirstAllocatorForTesting()->GetStats().UsedMemoryUsage, 4u); ASSERT_NE(allocation, nullptr); alloc.DeallocateMemory(std::move(allocation)); @@ -49,7 +49,7 @@ TEST_F(ConditionalMemoryAllocatorTests, Basic) { // Equal size allocation uses firstAllocator. { std::unique_ptr allocation = - alloc.TryAllocateMemory(CreateBasicRequest(16, 1)); + alloc.TryAllocateMemoryForTesting(CreateBasicRequest(16, 1)); ASSERT_EQ(alloc.GetFirstAllocatorForTesting()->GetStats().UsedMemoryUsage, 16u); ASSERT_NE(allocation, nullptr); alloc.DeallocateMemory(std::move(allocation)); @@ -58,7 +58,7 @@ TEST_F(ConditionalMemoryAllocatorTests, Basic) { // Larger allocation uses secondAllocator. { std::unique_ptr allocation = - alloc.TryAllocateMemory(CreateBasicRequest(24, 1)); + alloc.TryAllocateMemoryForTesting(CreateBasicRequest(24, 1)); ASSERT_EQ(alloc.GetSecondAllocatorForTesting()->GetStats().UsedMemoryUsage, 24u); ASSERT_NE(allocation, nullptr); alloc.DeallocateMemory(std::move(allocation)); @@ -67,7 +67,7 @@ TEST_F(ConditionalMemoryAllocatorTests, Basic) { // Smaller allocation again uses firstAllocator. { std::unique_ptr allocation = - alloc.TryAllocateMemory(CreateBasicRequest(4, 1)); + alloc.TryAllocateMemoryForTesting(CreateBasicRequest(4, 1)); ASSERT_EQ(alloc.GetFirstAllocatorForTesting()->GetStats().UsedMemoryUsage, 4u); ASSERT_NE(allocation, nullptr); alloc.DeallocateMemory(std::move(allocation)); @@ -76,7 +76,7 @@ TEST_F(ConditionalMemoryAllocatorTests, Basic) { // Larger allocation again uses secondAllocator. { std::unique_ptr allocation = - alloc.TryAllocateMemory(CreateBasicRequest(24, 1)); + alloc.TryAllocateMemoryForTesting(CreateBasicRequest(24, 1)); ASSERT_EQ(alloc.GetSecondAllocatorForTesting()->GetStats().UsedMemoryUsage, 24u); ASSERT_NE(allocation, nullptr); alloc.DeallocateMemory(std::move(allocation)); diff --git a/src/tests/unittests/MemoryPoolTests.cpp b/src/tests/unittests/MemoryPoolTests.cpp index 11901fdc7..32ed4d6b3 100644 --- a/src/tests/unittests/MemoryPoolTests.cpp +++ b/src/tests/unittests/MemoryPoolTests.cpp @@ -41,7 +41,8 @@ TEST_F(LIFOMemoryPoolTests, SingleAllocation) { EXPECT_EQ(pool.ReleasePool(), 0u); EXPECT_EQ(pool.GetPoolSize() * pool.GetMemorySize(), 0u); - pool.ReturnToPool(*allocator.TryAllocateMemory(CreateBasicRequest(kDefaultMemorySize))); + pool.ReturnToPool( + *allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kDefaultMemorySize))); EXPECT_EQ(pool.GetPoolSize() * pool.GetMemorySize(), kDefaultMemorySize); EXPECT_EQ(pool.GetPoolSize(), 1u); @@ -66,7 +67,8 @@ TEST_F(LIFOMemoryPoolTests, MultipleAllocations) { constexpr uint64_t kPoolSize = 64; while (pool.GetPoolSize() < kPoolSize) { - pool.ReturnToPool(*allocator.TryAllocateMemory(CreateBasicRequest(kDefaultMemorySize))); + pool.ReturnToPool( + *allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kDefaultMemorySize))); } EXPECT_EQ(pool.GetPoolSize() * pool.GetMemorySize(), kDefaultMemorySize * kPoolSize); diff --git a/src/tests/unittests/PooledMemoryAllocatorTests.cpp b/src/tests/unittests/PooledMemoryAllocatorTests.cpp index 66b689743..5ceba6914 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.TryAllocateMemory( + 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.TryAllocateMemory( + std::unique_ptr firstAllocation = allocator.TryAllocateMemoryForTesting( CreateBasicRequest(kDefaultMemorySize, kDefaultMemoryAlignment)); ASSERT_NE(firstAllocation, nullptr); EXPECT_EQ(firstAllocation->GetSize(), kDefaultMemorySize); - std::unique_ptr secondAllocation = allocator.TryAllocateMemory( + 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.TryAllocateMemory( + 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.TryAllocateMemory( + 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.TryAllocateMemory( + 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 1e27f354d..3a520ed00 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.TryAllocateMemory( + 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.TryAllocateMemory( + std::unique_ptr firstAllocation = allocator.TryAllocateMemoryForTesting( CreateBasicRequest(kDefaultMemorySize, kDefaultMemoryAlignment)); ASSERT_NE(firstAllocation, nullptr); EXPECT_EQ(firstAllocation->GetSize(), kDefaultMemorySize); - std::unique_ptr secondAllocation = allocator.TryAllocateMemory( + std::unique_ptr secondAllocation = allocator.TryAllocateMemoryForTesting( CreateBasicRequest(kDefaultMemorySize, kDefaultMemoryAlignment)); ASSERT_NE(secondAllocation, nullptr); EXPECT_EQ(secondAllocation->GetSize(), kDefaultMemorySize); @@ -83,52 +83,52 @@ TEST(SegmentedMemoryAllocatorTests, MultipleHeapsVariousSizes) { // Append the 1st and 3rd segment, in sequence. uint64_t firstMemorySize = kDefaultMemorySize / 2; - std::unique_ptr firstAllocation = - allocator.TryAllocateMemory(CreateBasicRequest(firstMemorySize, kDefaultMemoryAlignment)); + 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.TryAllocateMemory(CreateBasicRequest(secondMemorySize, kDefaultMemoryAlignment)); + std::unique_ptr secondAllocation = allocator.TryAllocateMemoryForTesting( + CreateBasicRequest(secondMemorySize, kDefaultMemoryAlignment)); ASSERT_NE(secondAllocation, nullptr); EXPECT_EQ(secondAllocation->GetMethod(), AllocationMethod::kStandalone); EXPECT_EQ(secondAllocation->GetSize(), secondMemorySize); // Insert a 3rd segment in the middle or between the 1st and 2nd segment. uint64_t thirdMemorySize = kDefaultMemorySize / 4; - std::unique_ptr thirdAllocation = - allocator.TryAllocateMemory(CreateBasicRequest(thirdMemorySize, kDefaultMemoryAlignment)); + std::unique_ptr thirdAllocation = allocator.TryAllocateMemoryForTesting( + CreateBasicRequest(thirdMemorySize, kDefaultMemoryAlignment)); ASSERT_NE(thirdAllocation, nullptr); EXPECT_EQ(thirdAllocation->GetMethod(), AllocationMethod::kStandalone); EXPECT_EQ(thirdAllocation->GetSize(), thirdMemorySize); // Insert a 4th segment at the end. uint64_t fourthMemorySize = kDefaultMemorySize; - std::unique_ptr fourthAllocation = - allocator.TryAllocateMemory(CreateBasicRequest(fourthMemorySize, kDefaultMemoryAlignment)); + 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.TryAllocateMemory(CreateBasicRequest(fifthMemorySize, kDefaultMemoryAlignment)); + 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.TryAllocateMemory(CreateBasicRequest(thirdMemorySize, kDefaultMemoryAlignment)); + 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.TryAllocateMemory(CreateBasicRequest(firstMemorySize, kDefaultMemoryAlignment)); + std::unique_ptr seventhAllocation = allocator.TryAllocateMemoryForTesting( + CreateBasicRequest(firstMemorySize, kDefaultMemoryAlignment)); ASSERT_NE(seventhAllocation, nullptr); EXPECT_EQ(seventhAllocation->GetMethod(), AllocationMethod::kStandalone); EXPECT_EQ(seventhAllocation->GetSize(), firstMemorySize); @@ -164,7 +164,7 @@ TEST(SegmentedMemoryAllocatorTests, ReuseFreedHeaps) { SegmentedMemoryAllocator allocator(std::make_unique(), kDefaultMemoryAlignment); { - std::unique_ptr allocation = allocator.TryAllocateMemory( + 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.TryAllocateMemory( + 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.TryAllocateMemory( + 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 1c84a50e6..84c7300f6 100644 --- a/src/tests/unittests/SlabMemoryAllocatorTests.cpp +++ b/src/tests/unittests/SlabMemoryAllocatorTests.cpp @@ -63,7 +63,8 @@ TEST_F(SlabMemoryAllocatorTests, SingleSlab) { kNoSlabPrefetchAllowed, kDisableSlabGrowth, dummyMemoryAllocator.get()); - ASSERT_EQ(allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize * 2, 1)), nullptr); + ASSERT_EQ(allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize * 2, 1)), + nullptr); } // Verify allocation greater then slab size fails. @@ -75,8 +76,10 @@ TEST_F(SlabMemoryAllocatorTests, SingleSlab) { kNoSlabPrefetchAllowed, kDisableSlabGrowth, dummyMemoryAllocator.get()); - ASSERT_EQ(allocator.TryAllocateMemory(CreateBasicRequest(kMaxSlabSize, 1)), nullptr); - ASSERT_EQ(allocator.TryAllocateMemory(CreateBasicRequest(kMaxSlabSize - 1, 1)), nullptr); + ASSERT_EQ(allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kMaxSlabSize, 1)), + nullptr); + ASSERT_EQ(allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kMaxSlabSize - 1, 1)), + nullptr); } // Verify allocation equal to the slab size always succeeds. @@ -89,7 +92,7 @@ TEST_F(SlabMemoryAllocatorTests, SingleSlab) { kDisableSlabGrowth, dummyMemoryAllocator.get()); std::unique_ptr allocation = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocation, nullptr); EXPECT_EQ(allocation->GetOffset(), 0u); EXPECT_EQ(allocation->GetMethod(), AllocationMethod::kSubAllocated); @@ -110,11 +113,11 @@ 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 = - allocator.TryAllocateMemory(CreateBasicRequest(10, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(10, 1)); ASSERT_EQ(allocation, nullptr); // Re-attempt with an allocation that is under the fragmentation limit. - allocation = allocator.TryAllocateMemory(CreateBasicRequest(14, 1)); + allocation = allocator.TryAllocateMemoryForTesting(CreateBasicRequest(14, 1)); ASSERT_NE(allocation, nullptr); EXPECT_EQ(allocation->GetOffset(), 0u); EXPECT_EQ(allocation->GetMethod(), AllocationMethod::kSubAllocated); @@ -133,7 +136,7 @@ TEST_F(SlabMemoryAllocatorTests, SingleSlab) { kDisableSlabGrowth, dummyMemoryAllocator.get()); std::unique_ptr allocation = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocation, nullptr); EXPECT_GE(allocation->GetSize(), kBlockSize); EXPECT_GE(allocation->GetMemory()->GetSize(), kSlabSize); @@ -151,7 +154,7 @@ TEST_F(SlabMemoryAllocatorTests, SingleSlab) { kDisableSlabGrowth, dummyMemoryAllocator.get()); std::unique_ptr allocation = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocation, nullptr); EXPECT_GE(allocation->GetSize(), kBlockSize); EXPECT_GE(allocation->GetMemory()->GetSize(), kSlabSize); @@ -168,11 +171,14 @@ TEST_F(SlabMemoryAllocatorTests, SingleSlab) { kNoSlabPrefetchAllowed, kDisableSlabGrowth, dummyMemoryAllocator.get()); - EXPECT_EQ(allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1, true)), nullptr); - EXPECT_EQ(allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize / 2, 1, true)), - nullptr); - EXPECT_EQ(allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize / 4, 1, true)), + EXPECT_EQ(allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1, true)), nullptr); + EXPECT_EQ( + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize / 2, 1, true)), + nullptr); + EXPECT_EQ( + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize / 4, 1, true)), + nullptr); } } @@ -194,7 +200,7 @@ TEST_F(SlabMemoryAllocatorTests, MultipleSlabs) { std::vector> allocations = {}; for (uint32_t slabi = 0; slabi < kNumOfSlabs; slabi++) { std::unique_ptr allocation = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocation, nullptr); allocations.push_back(std::move(allocation)); } @@ -221,7 +227,7 @@ TEST_F(SlabMemoryAllocatorTests, MultipleSlabs) { std::vector> allocations = {}; for (uint32_t blocki = 0; blocki < (kDefaultSlabSize * 2 / kBlockSize); blocki++) { std::unique_ptr allocation = - allocator.TryAllocateMemory(CreateBasicRequest(22, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(22, 1)); ASSERT_NE(allocation, nullptr); allocations.push_back(std::move(allocation)); } @@ -247,37 +253,37 @@ TEST_F(SlabMemoryAllocatorTests, MultipleSlabs) { // Both allocation A and B go in Slab A, which will become full. std::unique_ptr allocationAinSlabA = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocationAinSlabA, nullptr); std::unique_ptr allocationBInSlabA = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + 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 = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocationCInSlabB, nullptr); EXPECT_NE(allocationBInSlabA->GetMemory(), allocationCInSlabB->GetMemory()); std::unique_ptr allocationDInSlabB = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + 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 = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocationEInSlabC, nullptr); EXPECT_NE(allocationDInSlabB->GetMemory(), allocationEInSlabC->GetMemory()); std::unique_ptr allocationFInSlabC = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocationFInSlabC, nullptr); EXPECT_EQ(allocationEInSlabC->GetMemory(), allocationFInSlabC->GetMemory()); @@ -292,7 +298,7 @@ TEST_F(SlabMemoryAllocatorTests, MultipleSlabs) { // Full list: C. std::unique_ptr allocationGInSlabB = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocationGInSlabB, nullptr); EXPECT_EQ(allocationDInSlabB->GetMemory(), allocationGInSlabB->GetMemory()); @@ -300,7 +306,7 @@ TEST_F(SlabMemoryAllocatorTests, MultipleSlabs) { // Full list: B -> C. std::unique_ptr allocationHInSlabA = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocationGInSlabB, nullptr); EXPECT_EQ(allocationBInSlabA->GetMemory(), allocationHInSlabA->GetMemory()); @@ -333,8 +339,8 @@ TEST_F(SlabMemoryAllocatorTests, AllocationOverflow) { kDisableSlabGrowth, dummyMemoryAllocator.get()); constexpr uint64_t largeBlock = (1ull << 63) + 1; - std::unique_ptr invalidAllocation = - allocator.TryAllocateMemory(CreateBasicRequest(largeBlock, kDefaultSlabAlignment, true)); + std::unique_ptr invalidAllocation = allocator.TryAllocateMemoryForTesting( + CreateBasicRequest(largeBlock, kDefaultSlabAlignment, true)); ASSERT_EQ(invalidAllocation, nullptr); } @@ -359,7 +365,7 @@ TEST_F(SlabMemoryAllocatorTests, ReuseSlabs) { // Allocate |kNumOfSlabs| worth. while (slabMemory.size() < kNumOfSlabs) { std::unique_ptr allocation = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocation, nullptr); EXPECT_EQ(allocation->GetSize(), kBlockSize); EXPECT_EQ(allocation->GetMethod(), AllocationMethod::kSubAllocated); @@ -394,7 +400,7 @@ TEST_F(SlabMemoryAllocatorTests, GetInfo) { dummyMemoryAllocator.get()); std::unique_ptr allocation = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_NE(allocation, nullptr); // Single sub-allocation within a slab should be allocated. @@ -428,7 +434,7 @@ TEST_F(SlabMemoryAllocatorTests, GetInfo) { poolAllocator.get()); std::unique_ptr allocation = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_NE(allocation, nullptr); EXPECT_EQ(allocator.GetStats().UsedBlockCount, 1u); @@ -462,35 +468,35 @@ TEST_F(SlabMemoryAllocatorTests, SlabGrowth) { // Slab A holds 1 allocation. std::unique_ptr allocationAInSlabA = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + 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 = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + 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 = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_EQ(allocationAInSlabC->GetSize(), kBlockSize); EXPECT_EQ(allocationAInSlabC->GetMemory()->GetSize(), kBlockSize * 2); std::unique_ptr allocationBInSlabC = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + 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 = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_EQ(allocationAInSlabD->GetSize(), kBlockSize); EXPECT_EQ(allocationAInSlabD->GetMemory()->GetSize(), kBlockSize * 2); std::unique_ptr allocationBInSlabD = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_EQ(allocationBInSlabD->GetSize(), kBlockSize); EXPECT_EQ(allocationBInSlabD->GetMemory()->GetSize(), kBlockSize * 2); @@ -517,12 +523,12 @@ TEST_F(SlabMemoryAllocatorTests, SlabGrowth) { // Slab A holds 2 allocations per slab. std::unique_ptr allocationAInSlabA = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_EQ(allocationAInSlabA->GetSize(), kBlockSize); EXPECT_EQ(allocationAInSlabA->GetMemory()->GetSize(), kMinSlabSize); std::unique_ptr allocationBInSlabA = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_EQ(allocationBInSlabA->GetSize(), kBlockSize); EXPECT_EQ(allocationBInSlabA->GetMemory()->GetSize(), kMinSlabSize); @@ -530,12 +536,12 @@ TEST_F(SlabMemoryAllocatorTests, SlabGrowth) { // Slab B holds 2 allocations per slab. std::unique_ptr allocationAInSlabB = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_EQ(allocationAInSlabB->GetSize(), kBlockSize); EXPECT_EQ(allocationAInSlabB->GetMemory()->GetSize(), kMinSlabSize); std::unique_ptr allocationBInSlabB = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_EQ(allocationBInSlabB->GetSize(), kBlockSize); EXPECT_EQ(allocationBInSlabB->GetMemory()->GetSize(), kMinSlabSize); @@ -543,7 +549,7 @@ TEST_F(SlabMemoryAllocatorTests, SlabGrowth) { // Slab C grows 2x and holds 4 allocations per slab. std::unique_ptr allocationAInSlabC = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_EQ(allocationAInSlabC->GetSize(), kBlockSize); EXPECT_EQ(allocationAInSlabC->GetMemory()->GetSize(), kMinSlabSize * 2); @@ -574,12 +580,12 @@ TEST_F(SlabMemoryAllocatorTests, SlabGrowthLimit) { // Slab A holds 2 allocations per slab. std::unique_ptr allocationAInSlabA = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_EQ(allocationAInSlabA->GetSize(), kBlockSize); EXPECT_EQ(allocationAInSlabA->GetMemory()->GetSize(), kMinSlabSize); std::unique_ptr allocationBInSlabA = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_EQ(allocationBInSlabA->GetSize(), kBlockSize); EXPECT_EQ(allocationBInSlabA->GetMemory()->GetSize(), kMinSlabSize); @@ -587,12 +593,12 @@ TEST_F(SlabMemoryAllocatorTests, SlabGrowthLimit) { // Slab B holds 2 allocations per slab. std::unique_ptr allocationAInSlabB = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_EQ(allocationAInSlabB->GetSize(), kBlockSize); EXPECT_EQ(allocationAInSlabB->GetMemory()->GetSize(), kMinSlabSize); std::unique_ptr allocationBInSlabB = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_EQ(allocationBInSlabB->GetSize(), kBlockSize); EXPECT_EQ(allocationBInSlabB->GetMemory()->GetSize(), kMinSlabSize); @@ -600,7 +606,7 @@ TEST_F(SlabMemoryAllocatorTests, SlabGrowthLimit) { // Slab C grows 2x and holds 4 allocations per slab. std::unique_ptr allocationAInSlabC = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_EQ(allocationAInSlabC->GetSize(), kBlockSize); EXPECT_EQ(allocationAInSlabC->GetMemory()->GetSize(), kMinSlabSize * 2); @@ -626,13 +632,13 @@ TEST_F(SlabMemoryAllocatorTests, SlabGrowthLimit) { // Slab A holds 1 allocation per slab. std::unique_ptr allocationAInSlabA = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + 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 = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocationAInSlabB, nullptr); EXPECT_EQ(allocationAInSlabB->GetSize(), kBlockSize); EXPECT_EQ(allocationAInSlabB->GetMemory()->GetSize(), kBlockSize); @@ -641,7 +647,7 @@ TEST_F(SlabMemoryAllocatorTests, SlabGrowthLimit) { // Slab C grows 2x and holds 2 allocation per slab. std::unique_ptr allocationAInSlabC = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocationAInSlabC, nullptr); EXPECT_EQ(allocationAInSlabC->GetSize(), kBlockSize); EXPECT_EQ(allocationAInSlabC->GetMemory()->GetSize(), kBlockSize * 2); @@ -649,14 +655,14 @@ TEST_F(SlabMemoryAllocatorTests, SlabGrowthLimit) { EXPECT_NE(allocationAInSlabB->GetMemory(), allocationAInSlabC->GetMemory()); std::unique_ptr allocationBInSlabC = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + 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 = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocationAInSlabD, nullptr); EXPECT_EQ(allocationAInSlabD->GetSize(), kBlockSize); EXPECT_EQ(allocationAInSlabD->GetMemory()->GetSize(), kBlockSize * 2); @@ -664,7 +670,7 @@ TEST_F(SlabMemoryAllocatorTests, SlabGrowthLimit) { EXPECT_NE(allocationBInSlabC->GetMemory(), allocationAInSlabD->GetMemory()); std::unique_ptr allocationBInSlabD = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocationBInSlabD, nullptr); EXPECT_EQ(allocationBInSlabD->GetSize(), kBlockSize); EXPECT_EQ(allocationBInSlabD->GetMemory()->GetSize(), kBlockSize * 2); @@ -692,7 +698,8 @@ TEST_F(SlabCacheAllocatorTests, SlabOversized) { kDefaultSlabFragmentationLimit, kNoSlabPrefetchAllowed, kDisableSlabGrowth, std::make_unique()); - EXPECT_EQ(allocator.TryAllocateMemory(CreateBasicRequest(kMaxSlabSize + 1, 1)), nullptr); + EXPECT_EQ(allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kMaxSlabSize + 1, 1)), + nullptr); } TEST_F(SlabCacheAllocatorTests, SingleSlabMultipleSize) { @@ -705,9 +712,11 @@ TEST_F(SlabCacheAllocatorTests, SingleSlabMultipleSize) { // Verify requesting an allocation without memory will not return a valid allocation. { constexpr uint64_t kBlockSize = 4; - EXPECT_EQ(allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1, true)), nullptr); - EXPECT_EQ(allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize * 2, 1, true)), + EXPECT_EQ(allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1, true)), nullptr); + EXPECT_EQ( + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize * 2, 1, true)), + nullptr); } } @@ -722,12 +731,12 @@ TEST_F(SlabCacheAllocatorTests, SingleSlabMultipleAlignments) { { constexpr uint64_t kBlockSize = 4; std::unique_ptr allocationWithAlignmentA = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocationWithAlignmentA, nullptr); EXPECT_EQ(allocationWithAlignmentA->GetSize(), AlignTo(kBlockSize, 1)); std::unique_ptr allocationWithAlignmentB = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 16)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 16)); ASSERT_NE(allocationWithAlignmentB, nullptr); EXPECT_EQ(allocationWithAlignmentB->GetSize(), AlignTo(kBlockSize, 16)); @@ -744,22 +753,22 @@ TEST_F(SlabCacheAllocatorTests, MultipleSlabsSameSize) { kDisableSlabGrowth, std::make_unique()); std::unique_ptr firstAllocation = - allocator.TryAllocateMemory(CreateBasicRequest(22, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(22, 1)); ASSERT_NE(firstAllocation, nullptr); std::unique_ptr secondAllocation = - allocator.TryAllocateMemory(CreateBasicRequest(22, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(22, 1)); ASSERT_NE(secondAllocation, nullptr); allocator.DeallocateMemory(std::move(firstAllocation)); allocator.DeallocateMemory(std::move(secondAllocation)); std::unique_ptr thirdAllocation = - allocator.TryAllocateMemory(CreateBasicRequest(44, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(44, 1)); ASSERT_NE(thirdAllocation, nullptr); std::unique_ptr fourthAllocation = - allocator.TryAllocateMemory(CreateBasicRequest(44, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(44, 1)); ASSERT_NE(fourthAllocation, nullptr); allocator.DeallocateMemory(std::move(thirdAllocation)); @@ -775,7 +784,7 @@ TEST_F(SlabCacheAllocatorTests, MultipleSlabsVariableSizes) { { constexpr uint64_t allocationSize = 22; std::unique_ptr allocation = - allocator.TryAllocateMemory(CreateBasicRequest(allocationSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(allocationSize, 1)); ASSERT_NE(allocation, nullptr); EXPECT_EQ(allocation->GetOffset(), 0u); EXPECT_EQ(allocation->GetMethod(), AllocationMethod::kSubAllocated); @@ -786,7 +795,7 @@ TEST_F(SlabCacheAllocatorTests, MultipleSlabsVariableSizes) { { constexpr uint64_t allocationSize = 44; std::unique_ptr allocation = - allocator.TryAllocateMemory(CreateBasicRequest(allocationSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(allocationSize, 1)); ASSERT_NE(allocation, nullptr); EXPECT_EQ(allocation->GetOffset(), 0u); EXPECT_EQ(allocation->GetMethod(), AllocationMethod::kSubAllocated); @@ -797,7 +806,7 @@ TEST_F(SlabCacheAllocatorTests, MultipleSlabsVariableSizes) { { constexpr uint64_t allocationSize = 88; std::unique_ptr allocation = - allocator.TryAllocateMemory(CreateBasicRequest(allocationSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(allocationSize, 1)); ASSERT_NE(allocation, nullptr); EXPECT_EQ(allocation->GetOffset(), 0u); EXPECT_EQ(allocation->GetMethod(), AllocationMethod::kSubAllocated); @@ -824,7 +833,7 @@ TEST_F(SlabCacheAllocatorTests, SingleSlabInBuddy) { constexpr uint64_t kBlockSize = 4; std::unique_ptr allocation = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); ASSERT_NE(allocation, nullptr); EXPECT_EQ(allocation->GetOffset(), 0u); EXPECT_EQ(allocation->GetMethod(), AllocationMethod::kSubAllocated); @@ -850,7 +859,7 @@ TEST_F(SlabCacheAllocatorTests, MultipleSlabsInBuddy) { { constexpr uint64_t allocationSize = 8; std::unique_ptr firstAllocation = - allocator.TryAllocateMemory(CreateBasicRequest(allocationSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(allocationSize, 1)); ASSERT_NE(firstAllocation, nullptr); EXPECT_EQ(firstAllocation->GetOffset(), 0u); EXPECT_EQ(firstAllocation->GetMethod(), AllocationMethod::kSubAllocated); @@ -859,7 +868,7 @@ TEST_F(SlabCacheAllocatorTests, MultipleSlabsInBuddy) { EXPECT_EQ(firstAllocation->GetMemory()->GetSize(), kDefaultSlabSize); std::unique_ptr secondAllocation = - allocator.TryAllocateMemory(CreateBasicRequest(allocationSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(allocationSize, 1)); ASSERT_NE(secondAllocation, nullptr); EXPECT_EQ(secondAllocation->GetOffset(), allocationSize); EXPECT_EQ(secondAllocation->GetMethod(), AllocationMethod::kSubAllocated); @@ -877,7 +886,7 @@ TEST_F(SlabCacheAllocatorTests, MultipleSlabsInBuddy) { std::vector> allocations = {}; for (uint32_t i = 0; i < kDefaultSlabSize / kSlabSize; i++) { std::unique_ptr allocation = - allocator.TryAllocateMemory(CreateBasicRequest(kSlabSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kSlabSize, 1)); ASSERT_NE(allocation, nullptr); EXPECT_EQ(allocation->GetOffset(), i * kSlabSize); EXPECT_EQ(allocation->GetMethod(), AllocationMethod::kSubAllocated); @@ -887,14 +896,14 @@ TEST_F(SlabCacheAllocatorTests, MultipleSlabsInBuddy) { // Next slab-buddy sub-allocation must be in the second buddy. std::unique_ptr firstSlabInSecondBuddy = - allocator.TryAllocateMemory(CreateBasicRequest(kSlabSize, 1)); + 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 = - allocator.TryAllocateMemory(CreateBasicRequest(kSlabSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kSlabSize, 1)); ASSERT_NE(secondSlabInSecondBuddy, nullptr); EXPECT_EQ(secondSlabInSecondBuddy->GetOffset(), kSlabSize); EXPECT_EQ(secondSlabInSecondBuddy->GetMethod(), AllocationMethod::kSubAllocated); @@ -921,7 +930,7 @@ TEST_F(SlabCacheAllocatorTests, GetInfo) { kDisableSlabGrowth, std::make_unique()); std::unique_ptr allocation = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_NE(allocation, nullptr); // Single sub-allocation within a slab should be allocated. @@ -952,7 +961,7 @@ TEST_F(SlabCacheAllocatorTests, GetInfo) { std::make_unique())); std::unique_ptr allocation = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_NE(allocation, nullptr); // Single sub-allocation within a slab should be used. @@ -986,7 +995,7 @@ TEST_F(SlabCacheAllocatorTests, GetInfo) { constexpr uint64_t kBlockSize = 4; std::unique_ptr allocation = - allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1)); + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1)); EXPECT_NE(allocation, nullptr); // Single slab block within buddy memory should be used. @@ -1019,7 +1028,8 @@ TEST_F(SlabCacheAllocatorTests, SlabPrefetch) { constexpr uint64_t kNumOfSlabs = 10u; std::vector> allocations = {}; for (size_t i = 0; i < kNumOfSlabs * (kDefaultSlabSize / kBlockSize); i++) { - allocations.push_back(allocator.TryAllocateMemory(CreateBasicRequest(kBlockSize, 1))); + allocations.push_back( + allocator.TryAllocateMemoryForTesting(CreateBasicRequest(kBlockSize, 1))); } // All but the first slab should be successfully prefetched. @@ -1044,7 +1054,7 @@ TEST_F(SlabCacheAllocatorTests, SlabPrefetchDisabled) { constexpr uint64_t kNumOfSlabs = 10u; std::vector> allocations = {}; for (size_t i = 0; i < kNumOfSlabs * (kDefaultSlabSize / kBlockSize); i++) { - allocations.push_back(allocator.TryAllocateMemory(alwaysPrefetchRequest)); + allocations.push_back(allocator.TryAllocateMemoryForTesting(alwaysPrefetchRequest)); } EXPECT_EQ(allocator.GetStats().PrefetchedMemoryMissesEliminated, 0u); @@ -1065,12 +1075,12 @@ TEST_F(SlabCacheAllocatorTests, AlwaysCache) { MemoryAllocationRequest request = CreateBasicRequest(32, 1); request.AlwaysCacheSize = true; - std::unique_ptr allocation = allocator.TryAllocateMemory(request); + std::unique_ptr allocation = allocator.TryAllocateMemoryForTesting(request); ASSERT_NE(allocation, nullptr); allocator.DeallocateMemory(std::move(allocation)); request.AvailableForAllocation = 0; - EXPECT_EQ(allocator.TryAllocateMemory(request), nullptr); + EXPECT_EQ(allocator.TryAllocateMemoryForTesting(request), nullptr); } // Verify creating more slabs than memory available fails. @@ -1086,7 +1096,8 @@ TEST_F(SlabCacheAllocatorTests, OutOfMemory) { std::vector> allocations = {}; while (true) { - std::unique_ptr allocation = allocator.TryAllocateMemory(request); + std::unique_ptr allocation = + allocator.TryAllocateMemoryForTesting(request); if (allocation == nullptr) { break; }