From 60a6c339891a728e77eac2456cbbc6d679a467af Mon Sep 17 00:00:00 2001 From: "Bernhart, Bryan" Date: Fri, 7 Apr 2023 13:41:54 -0700 Subject: [PATCH] Refactor GPGMM_INVALID_IF => GPGMM_RETURN_INVALID_IF. --- src/gpgmm/common/BuddyBlockAllocator.cpp | 8 +++- src/gpgmm/common/BuddyMemoryAllocator.cpp | 4 +- src/gpgmm/common/DedicatedMemoryAllocator.cpp | 2 +- src/gpgmm/common/Error.h | 40 +++++++++---------- src/gpgmm/common/MemoryAllocation.h | 4 ++ src/gpgmm/common/PooledMemoryAllocator.cpp | 2 +- src/gpgmm/common/SegmentedMemoryAllocator.cpp | 2 +- src/gpgmm/common/SlabBlockAllocator.cpp | 8 +++- src/gpgmm/common/SlabMemoryAllocator.cpp | 8 ++-- src/gpgmm/vk/DeviceMemoryAllocatorVk.cpp | 2 +- 10 files changed, 44 insertions(+), 36 deletions(-) diff --git a/src/gpgmm/common/BuddyBlockAllocator.cpp b/src/gpgmm/common/BuddyBlockAllocator.cpp index 9d05e1a67..429fe0956 100644 --- a/src/gpgmm/common/BuddyBlockAllocator.cpp +++ b/src/gpgmm/common/BuddyBlockAllocator.cpp @@ -146,7 +146,9 @@ namespace gpgmm { MemoryBlock* BuddyBlockAllocator::TryAllocateBlock(uint64_t requestSize, uint64_t alignment) { // Request cannot exceed max block size. - GPGMM_INVALID_IF(requestSize > mMaxBlockSize); + if (requestSize > mMaxBlockSize) { + return nullptr; + } // Compute the level const uint32_t sizeToLevel = ComputeLevelFromBlockSize(requestSize); @@ -156,7 +158,9 @@ namespace gpgmm { uint64_t currBlockLevel = GetNextFreeAlignedBlock(sizeToLevel, alignment); // Error when no free blocks exist (allocator is full) - GPGMM_INVALID_IF(currBlockLevel == kInvalidOffset); + if (currBlockLevel == kInvalidOffset) { + return nullptr; + } // Split free blocks level-by-level. // Terminate when the current block level is equal to the computed level of the requested diff --git a/src/gpgmm/common/BuddyMemoryAllocator.cpp b/src/gpgmm/common/BuddyMemoryAllocator.cpp index 9bc6c3442..307025fba 100644 --- a/src/gpgmm/common/BuddyMemoryAllocator.cpp +++ b/src/gpgmm/common/BuddyMemoryAllocator.cpp @@ -48,13 +48,13 @@ namespace gpgmm { std::lock_guard lock(mMutex); - GPGMM_INVALID_IF(!ValidateRequest(request)); + GPGMM_RETURN_INVALID_IF(!ValidateRequest(request)); // Round allocation size to nearest power-of-two. const uint64_t allocationSize = NextPowerOfTwo(request.SizeInBytes); // Request cannot exceed memory size. - GPGMM_INVALID_IF(allocationSize > mMemorySize); + GPGMM_RETURN_INVALID_IF(allocationSize > mMemorySize); // Attempt to sub-allocate a block of the requested size. std::unique_ptr subAllocation; diff --git a/src/gpgmm/common/DedicatedMemoryAllocator.cpp b/src/gpgmm/common/DedicatedMemoryAllocator.cpp index c16dcc0cc..0601df9b1 100644 --- a/src/gpgmm/common/DedicatedMemoryAllocator.cpp +++ b/src/gpgmm/common/DedicatedMemoryAllocator.cpp @@ -33,7 +33,7 @@ namespace gpgmm { std::lock_guard lock(mMutex); - GPGMM_INVALID_IF(!ValidateRequest(request)); + GPGMM_RETURN_INVALID_IF(!ValidateRequest(request)); MemoryAllocationRequest memoryRequest = request; memoryRequest.Alignment = mMemoryAlignment; diff --git a/src/gpgmm/common/Error.h b/src/gpgmm/common/Error.h index 715ac0823..76682c42f 100644 --- a/src/gpgmm/common/Error.h +++ b/src/gpgmm/common/Error.h @@ -19,6 +19,24 @@ #include +#define GPGMM_TRY_ASSIGN(expr, value) \ + { \ + auto result = expr; \ + if (GPGMM_UNLIKELY(!result.IsSuccess())) { \ + return result; \ + } \ + value = result.AcquireResult(); \ + } \ + for (;;) \ + break + +#define GPGMM_RETURN_INVALID_IF(expr) \ + if (GPGMM_UNLIKELY(expr)) { \ + return {}; \ + } \ + for (;;) \ + break + namespace gpgmm { enum class ErrorCodeType : uint32_t; @@ -82,28 +100,6 @@ namespace gpgmm { template using ResultOrError = Result; -#define GPGMM_INVALID_ALLOCATION \ - MemoryAllocation { \ - } - -#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) \ - if (GPGMM_UNLIKELY(expr)) { \ - return {}; \ - } \ - for (;;) \ - break - } // namespace gpgmm #endif // GPGMM_COMMON_ERROR_H_ diff --git a/src/gpgmm/common/MemoryAllocation.h b/src/gpgmm/common/MemoryAllocation.h index b6b99d937..25b13e2b2 100644 --- a/src/gpgmm/common/MemoryAllocation.h +++ b/src/gpgmm/common/MemoryAllocation.h @@ -19,6 +19,10 @@ #include "gpgmm/common/Object.h" #include "gpgmm/utils/Limits.h" +#define GPGMM_INVALID_ALLOCATION \ + MemoryAllocation { \ + } + namespace gpgmm { /** \enum AllocationMethod diff --git a/src/gpgmm/common/PooledMemoryAllocator.cpp b/src/gpgmm/common/PooledMemoryAllocator.cpp index b09b64736..dedfeaaad 100644 --- a/src/gpgmm/common/PooledMemoryAllocator.cpp +++ b/src/gpgmm/common/PooledMemoryAllocator.cpp @@ -41,7 +41,7 @@ namespace gpgmm { std::lock_guard lock(mMutex); - GPGMM_INVALID_IF(!ValidateRequest(request)); + GPGMM_RETURN_INVALID_IF(!ValidateRequest(request)); MemoryAllocation allocation = mPool->AcquireFromPool(); if (allocation == GPGMM_INVALID_ALLOCATION) { diff --git a/src/gpgmm/common/SegmentedMemoryAllocator.cpp b/src/gpgmm/common/SegmentedMemoryAllocator.cpp index 206b6a6c9..4a6160e8e 100644 --- a/src/gpgmm/common/SegmentedMemoryAllocator.cpp +++ b/src/gpgmm/common/SegmentedMemoryAllocator.cpp @@ -126,7 +126,7 @@ namespace gpgmm { std::lock_guard lock(mMutex); - GPGMM_INVALID_IF(!ValidateRequest(request)); + GPGMM_RETURN_INVALID_IF(!ValidateRequest(request)); const uint64_t memorySize = AlignTo(request.SizeInBytes, mMemoryAlignment); MemorySegment* segment = GetOrCreateFreeSegment(memorySize); diff --git a/src/gpgmm/common/SlabBlockAllocator.cpp b/src/gpgmm/common/SlabBlockAllocator.cpp index 54b0c6f84..66f5f207f 100644 --- a/src/gpgmm/common/SlabBlockAllocator.cpp +++ b/src/gpgmm/common/SlabBlockAllocator.cpp @@ -67,10 +67,14 @@ namespace gpgmm { MemoryBlock* SlabBlockAllocator::TryAllocateBlock(uint64_t requestSize, uint64_t alignment) { // Requested cannot exceed block size. - GPGMM_INVALID_IF(requestSize > mBlockSize); + if (requestSize > mBlockSize) { + return nullptr; + } // Offset must be equal to a multiple of |mBlockSize|. - GPGMM_INVALID_IF(!IsAligned(mBlockSize, alignment)); + if (!IsAligned(mBlockSize, alignment)) { + return nullptr; + } // Pop off HEAD in the free-list. SlabBlock* head = mFreeList.pHead; diff --git a/src/gpgmm/common/SlabMemoryAllocator.cpp b/src/gpgmm/common/SlabMemoryAllocator.cpp index 61d5cb751..9df3e937e 100644 --- a/src/gpgmm/common/SlabMemoryAllocator.cpp +++ b/src/gpgmm/common/SlabMemoryAllocator.cpp @@ -202,7 +202,7 @@ namespace gpgmm { std::lock_guard lock(mMutex); - GPGMM_INVALID_IF(request.SizeInBytes > mBlockSize); + GPGMM_RETURN_INVALID_IF(request.SizeInBytes > mBlockSize); uint64_t slabSize = ComputeSlabSize(request.SizeInBytes, std::max(mMinSlabSize, mLastUsedSlabSize), @@ -229,7 +229,7 @@ namespace gpgmm { uint64_t newSlabSize = ComputeSlabSize( request.SizeInBytes, static_cast(slabSize * mSlabGrowthFactor), request.AvailableForAllocation); - GPGMM_INVALID_IF(newSlabSize == kInvalidSize); + GPGMM_RETURN_INVALID_IF(newSlabSize == kInvalidSize); // If the new slab size exceeds the limit, then re-use the previous, smaller size. if (newSlabSize > mMaxSlabSize) { @@ -518,10 +518,10 @@ namespace gpgmm { std::lock_guard lock(mMutex); - GPGMM_INVALID_IF(!ValidateRequest(request)); + GPGMM_RETURN_INVALID_IF(!ValidateRequest(request)); const uint64_t blockSize = AlignTo(request.SizeInBytes, request.Alignment); - GPGMM_INVALID_IF(blockSize > mMaxSlabSize); + GPGMM_RETURN_INVALID_IF(blockSize > mMaxSlabSize); // Create a slab allocator for the new entry. auto entry = diff --git a/src/gpgmm/vk/DeviceMemoryAllocatorVk.cpp b/src/gpgmm/vk/DeviceMemoryAllocatorVk.cpp index ec12fa396..d086fe3b2 100644 --- a/src/gpgmm/vk/DeviceMemoryAllocatorVk.cpp +++ b/src/gpgmm/vk/DeviceMemoryAllocatorVk.cpp @@ -47,7 +47,7 @@ namespace gpgmm::vk { return {}; } - GPGMM_INVALID_IF(!ValidateRequest(request)); + GPGMM_RETURN_INVALID_IF(!ValidateRequest(request)); VkMemoryAllocateInfo allocateInfo = {}; allocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;