From bf82fada06dafd6c03cd756fb1cb5856be5debb9 Mon Sep 17 00:00:00 2001 From: "Bernhart, Bryan" Date: Thu, 19 Jan 2023 09:50:22 -0800 Subject: [PATCH] Handle nullptr checks for API calls consistently. --- src/gpgmm/d3d12/ErrorD3D12.h | 9 +++++++++ src/gpgmm/d3d12/ResidencyManagerD3D12.cpp | 17 ++++++++--------- src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp | 14 ++++---------- 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/src/gpgmm/d3d12/ErrorD3D12.h b/src/gpgmm/d3d12/ErrorD3D12.h index 423210ea2..41803b304 100644 --- a/src/gpgmm/d3d12/ErrorD3D12.h +++ b/src/gpgmm/d3d12/ErrorD3D12.h @@ -22,6 +22,15 @@ namespace gpgmm::d3d12 { +#define ReturnIfNullptr(expr) \ + { \ + if (GPGMM_UNLIKELY(expr == nullptr)) { \ + return E_POINTER; \ + } \ + } \ + for (;;) \ + break + #define ReturnIfFailed(expr) \ { \ HRESULT hr = expr; \ diff --git a/src/gpgmm/d3d12/ResidencyManagerD3D12.cpp b/src/gpgmm/d3d12/ResidencyManagerD3D12.cpp index beeabfeba..ad5035431 100644 --- a/src/gpgmm/d3d12/ResidencyManagerD3D12.cpp +++ b/src/gpgmm/d3d12/ResidencyManagerD3D12.cpp @@ -162,9 +162,8 @@ namespace gpgmm::d3d12 { // static HRESULT ResidencyManager::CreateResidencyManager(const RESIDENCY_DESC& descriptor, IResidencyManager** ppResidencyManagerOut) { - if (descriptor.Adapter == nullptr || descriptor.Device == nullptr) { - return E_INVALIDARG; - } + ReturnIfNullptr(descriptor.Adapter); + ReturnIfNullptr(descriptor.Device); std::unique_ptr caps; { @@ -311,12 +310,12 @@ namespace gpgmm::d3d12 { // Increments number of locks on a heap to ensure the heap remains resident. HRESULT ResidencyManager::LockHeap(IHeap* pHeap) { + ReturnIfNullptr(pHeap); + std::lock_guard lock(mMutex); Heap* heap = static_cast(pHeap); - if (heap == nullptr) { - return E_INVALIDARG; - } + ASSERT(heap != nullptr); if (!heap->IsInList() && !heap->IsResidencyLocked()) { ComPtr pageable; @@ -351,11 +350,11 @@ namespace gpgmm::d3d12 { // Decrements number of locks on a heap. When the number of locks becomes zero, the heap is // inserted into the LRU cache and becomes eligible for eviction. HRESULT ResidencyManager::UnlockHeap(IHeap* pHeap) { + ReturnIfNullptr(pHeap); + std::lock_guard lock(mMutex); Heap* heap = static_cast(pHeap); - if (heap == nullptr) { - return E_INVALIDARG; - } + ASSERT(heap != nullptr); if (!heap->IsResidencyLocked()) { return E_FAIL; diff --git a/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp b/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp index 6e8a4b5ed..65302744f 100644 --- a/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp +++ b/src/gpgmm/d3d12/ResourceAllocatorD3D12.cpp @@ -381,9 +381,7 @@ namespace gpgmm::d3d12 { HRESULT ResourceAllocator::CreateResourceAllocator(const ALLOCATOR_DESC& allocatorDescriptor, IResourceAllocator** ppResourceAllocatorOut, IResidencyManager** ppResidencyManagerOut) { - if (allocatorDescriptor.Device == nullptr) { - return E_INVALIDARG; - } + ReturnIfNullptr(allocatorDescriptor.Device); ComPtr residencyManager; if (ppResidencyManagerOut != nullptr) { @@ -430,9 +428,7 @@ namespace gpgmm::d3d12 { const ALLOCATOR_DESC& allocatorDescriptor, IResidencyManager* pResidencyManager, IResourceAllocator** ppResourceAllocatorOut) { - if (allocatorDescriptor.Device == nullptr) { - return E_INVALIDARG; - } + ReturnIfNullptr(allocatorDescriptor.Device); std::unique_ptr caps; { @@ -1234,11 +1230,9 @@ namespace gpgmm::d3d12 { HRESULT ResourceAllocator::CreateResource(const ALLOCATION_DESC& allocationDescriptor, ID3D12Resource* pCommittedResource, IResourceAllocation** ppResourceAllocationOut) { - std::lock_guard lock(mMutex); + ReturnIfNullptr(pCommittedResource); - if (pCommittedResource == nullptr) { - return E_INVALIDARG; - } + std::lock_guard lock(mMutex); ComPtr resource(pCommittedResource);