Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/gpgmm/common/BuddyBlockAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/gpgmm/common/BuddyMemoryAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@ namespace gpgmm {

std::lock_guard<std::mutex> 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<MemoryAllocation> subAllocation;
Expand Down
2 changes: 1 addition & 1 deletion src/gpgmm/common/DedicatedMemoryAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace gpgmm {

std::lock_guard<std::mutex> lock(mMutex);

GPGMM_INVALID_IF(!ValidateRequest(request));
GPGMM_RETURN_INVALID_IF(!ValidateRequest(request));

MemoryAllocationRequest memoryRequest = request;
memoryRequest.Alignment = mMemoryAlignment;
Expand Down
40 changes: 18 additions & 22 deletions src/gpgmm/common/Error.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@

#include <utility>

#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;
Expand Down Expand Up @@ -82,28 +100,6 @@ namespace gpgmm {
template <typename ResultT>
using ResultOrError = Result<ErrorCodeType, ResultT>;

#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_
4 changes: 4 additions & 0 deletions src/gpgmm/common/MemoryAllocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
#include "gpgmm/common/Object.h"
#include "gpgmm/utils/Limits.h"

#define GPGMM_INVALID_ALLOCATION \
MemoryAllocation { \
}

namespace gpgmm {

/** \enum AllocationMethod
Expand Down
2 changes: 1 addition & 1 deletion src/gpgmm/common/PooledMemoryAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace gpgmm {

std::lock_guard<std::mutex> lock(mMutex);

GPGMM_INVALID_IF(!ValidateRequest(request));
GPGMM_RETURN_INVALID_IF(!ValidateRequest(request));

MemoryAllocation allocation = mPool->AcquireFromPool();
if (allocation == GPGMM_INVALID_ALLOCATION) {
Expand Down
2 changes: 1 addition & 1 deletion src/gpgmm/common/SegmentedMemoryAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ namespace gpgmm {

std::lock_guard<std::mutex> 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);
Expand Down
8 changes: 6 additions & 2 deletions src/gpgmm/common/SlabBlockAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/gpgmm/common/SlabMemoryAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ namespace gpgmm {

std::lock_guard<std::mutex> 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),
Expand All @@ -229,7 +229,7 @@ namespace gpgmm {
uint64_t newSlabSize = ComputeSlabSize(
request.SizeInBytes, static_cast<uint64_t>(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) {
Expand Down Expand Up @@ -518,10 +518,10 @@ namespace gpgmm {

std::lock_guard<std::mutex> 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 =
Expand Down
2 changes: 1 addition & 1 deletion src/gpgmm/vk/DeviceMemoryAllocatorVk.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down