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
2 changes: 1 addition & 1 deletion include/gpgmm_d3d12.h
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ namespace gpgmm::d3d12 {
ALLOCATION_METHOD Method;
};

/** \brief ResourceAllocation is MemoryAllocation that contains a ID3D12Resource.
/** \brief ResourceAllocation is an allocation that contains a ID3D12Resource.

It can represent a allocation using a resource in one of three ways: 1) ID3D12Resource "placed"
in a ID3D12Heap, 2) a ID3D12Resource at a specific offset, or 3) a ID3D12Resource without a
Expand Down
4 changes: 2 additions & 2 deletions src/gpgmm/common/Backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
namespace gpgmm {

// Forward declare common types.
class MemoryAllocation;
class MemoryAllocationBase;

template <typename CommonType, typename BackendTrait>
struct CommonTrait;

// Define common types.
template <typename BackendTrait>
struct CommonTrait<MemoryAllocation, BackendTrait> {
struct CommonTrait<MemoryAllocationBase, BackendTrait> {
using CommonType = typename BackendTrait::AllocationType;
};

Expand Down
24 changes: 13 additions & 11 deletions src/gpgmm/common/BuddyMemoryAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace gpgmm {
return static_cast<uint64_t>(SafeDivide(offset, mMemorySize));
}

ResultOrError<std::unique_ptr<MemoryAllocation>> BuddyMemoryAllocator::TryAllocateMemory(
ResultOrError<std::unique_ptr<MemoryAllocationBase>> BuddyMemoryAllocator::TryAllocateMemory(
const MemoryAllocationRequest& request) {
GPGMM_TRACE_EVENT_DURATION(TraceEventCategory::kDefault,
"BuddyMemoryAllocator.TryAllocateMemory");
Expand All @@ -57,22 +57,23 @@ namespace gpgmm {
GPGMM_RETURN_INVALID_IF(allocationSize > mMemorySize);

// Attempt to sub-allocate a block of the requested size.
std::unique_ptr<MemoryAllocation> subAllocation;
std::unique_ptr<MemoryAllocationBase> subAllocation;
GPGMM_TRY_ASSIGN(
TrySubAllocateMemory(
&mBuddyBlockAllocator, allocationSize, request.Alignment, request.NeverAllocate,
[&](const auto& block) -> ResultOrError<MemoryBase*> {
const uint64_t memoryIndex = GetMemoryIndex(block->Offset);
MemoryAllocation memoryAllocation = mUsedPool.AcquireFromPool(memoryIndex);
MemoryAllocationBase memoryAllocation = mUsedPool.AcquireFromPool(memoryIndex);

// No existing, allocate new memory for the block.
if (memoryAllocation == GPGMM_INVALID_ALLOCATION) {
MemoryAllocationRequest newRequest = request;
newRequest.SizeInBytes = mMemorySize;
newRequest.Alignment = mMemoryAlignment;

ResultOrError<std::unique_ptr<MemoryAllocation>> memoryAllocationResult =
GetNextInChain()->TryAllocateMemory(newRequest);
ResultOrError<std::unique_ptr<MemoryAllocationBase>>
memoryAllocationResult =
GetNextInChain()->TryAllocateMemory(newRequest);
if (!memoryAllocationResult.IsSuccess()) {
return memoryAllocationResult.GetErrorCode();
}
Expand All @@ -94,12 +95,13 @@ namespace gpgmm {
// Memory allocation offset is always memory-relative.
const uint64_t memoryOffset = block->Offset % mMemorySize;

return std::make_unique<MemoryAllocation>(/*allocator*/ this, subAllocation->GetMemory(),
memoryOffset, AllocationMethod::kSubAllocated,
block, request.SizeInBytes);
return std::make_unique<MemoryAllocationBase>(
/*allocator*/ this, subAllocation->GetMemory(), memoryOffset,
AllocationMethod::kSubAllocated, block, request.SizeInBytes);
}

void BuddyMemoryAllocator::DeallocateMemory(std::unique_ptr<MemoryAllocation> subAllocation) {
void BuddyMemoryAllocator::DeallocateMemory(
std::unique_ptr<MemoryAllocationBase> subAllocation) {
std::lock_guard<std::mutex> lock(mMutex);

GPGMM_TRACE_EVENT_DURATION(TraceEventCategory::kDefault,
Expand All @@ -114,14 +116,14 @@ namespace gpgmm {

mBuddyBlockAllocator.DeallocateBlock(subAllocation->GetBlock());

MemoryAllocation memoryAllocation = mUsedPool.AcquireFromPool(memoryIndex);
MemoryAllocationBase memoryAllocation = mUsedPool.AcquireFromPool(memoryIndex);

MemoryBase* memory = memoryAllocation.GetMemory();
ASSERT(memory != nullptr);

if (memory->Unref()) {
GetNextInChain()->DeallocateMemory(
std::make_unique<MemoryAllocation>(memoryAllocation));
std::make_unique<MemoryAllocationBase>(memoryAllocation));
} else {
mUsedPool.ReturnToPool(memoryAllocation, memoryIndex);
}
Expand Down
4 changes: 2 additions & 2 deletions src/gpgmm/common/BuddyMemoryAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ namespace gpgmm {
std::unique_ptr<MemoryAllocatorBase> memoryAllocator);

// MemoryAllocatorBase interface
ResultOrError<std::unique_ptr<MemoryAllocation>> TryAllocateMemory(
ResultOrError<std::unique_ptr<MemoryAllocationBase>> TryAllocateMemory(
const MemoryAllocationRequest& request) override;
void DeallocateMemory(std::unique_ptr<MemoryAllocation> subAllocation) override;
void DeallocateMemory(std::unique_ptr<MemoryAllocationBase> subAllocation) override;

uint64_t GetMemorySize() const override;
uint64_t GetMemoryAlignment() const override;
Expand Down
6 changes: 3 additions & 3 deletions src/gpgmm/common/ConditionalMemoryAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ namespace gpgmm {
mConditionalSize(conditionalSize) {
}

ResultOrError<std::unique_ptr<MemoryAllocation>> ConditionalMemoryAllocator::TryAllocateMemory(
const MemoryAllocationRequest& request) {
ResultOrError<std::unique_ptr<MemoryAllocationBase>>
ConditionalMemoryAllocator::TryAllocateMemory(const MemoryAllocationRequest& request) {
GPGMM_TRACE_EVENT_DURATION(TraceEventCategory::kDefault,
"ConditionalMemoryAllocator.TryAllocateMemory");
if (request.SizeInBytes <= mConditionalSize) {
Expand All @@ -40,7 +40,7 @@ namespace gpgmm {
}

void ConditionalMemoryAllocator::DeallocateMemory(
std::unique_ptr<MemoryAllocation> allocation) {
std::unique_ptr<MemoryAllocationBase> allocation) {
// ConditionalMemoryAllocator cannot allocate memory itself, so it must not deallocate.
allocation->GetAllocator()->DeallocateMemory(std::move(allocation));
}
Expand Down
4 changes: 2 additions & 2 deletions src/gpgmm/common/ConditionalMemoryAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ namespace gpgmm {
~ConditionalMemoryAllocator() override = default;

// MemoryAllocatorBase interface
ResultOrError<std::unique_ptr<MemoryAllocation>> TryAllocateMemory(
ResultOrError<std::unique_ptr<MemoryAllocationBase>> TryAllocateMemory(
const MemoryAllocationRequest& request) override;
void DeallocateMemory(std::unique_ptr<MemoryAllocation> allocation) override;
void DeallocateMemory(std::unique_ptr<MemoryAllocationBase> allocation) override;

MemoryAllocatorStats GetStats() const override;

Expand Down
11 changes: 6 additions & 5 deletions src/gpgmm/common/DedicatedMemoryAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ namespace gpgmm {
: MemoryAllocatorBase(std::move(memoryAllocator)), mMemoryAlignment(memoryAlignment) {
}

ResultOrError<std::unique_ptr<MemoryAllocation>> DedicatedMemoryAllocator::TryAllocateMemory(
const MemoryAllocationRequest& request) {
ResultOrError<std::unique_ptr<MemoryAllocationBase>>
DedicatedMemoryAllocator::TryAllocateMemory(const MemoryAllocationRequest& request) {
GPGMM_TRACE_EVENT_DURATION(TraceEventCategory::kDefault,
"DedicatedMemoryAllocator.TryAllocateMemory");

Expand All @@ -39,7 +39,7 @@ namespace gpgmm {
memoryRequest.Alignment = mMemoryAlignment;
memoryRequest.SizeInBytes = AlignTo(request.SizeInBytes, mMemoryAlignment);

std::unique_ptr<MemoryAllocation> allocation;
std::unique_ptr<MemoryAllocationBase> allocation;
GPGMM_TRY_ASSIGN(GetNextInChain()->TryAllocateMemory(memoryRequest), allocation);

if (memoryRequest.SizeInBytes > request.SizeInBytes) {
Expand All @@ -51,12 +51,13 @@ namespace gpgmm {
mStats.UsedBlockCount++;
mStats.UsedBlockUsage += allocation->GetSize();

return std::make_unique<MemoryAllocation>(
return std::make_unique<MemoryAllocationBase>(
this, allocation->GetMemory(), /*offset*/ 0, allocation->GetMethod(),
new MemoryBlock{0, allocation->GetSize()}, request.SizeInBytes);
}

void DedicatedMemoryAllocator::DeallocateMemory(std::unique_ptr<MemoryAllocation> allocation) {
void DedicatedMemoryAllocator::DeallocateMemory(
std::unique_ptr<MemoryAllocationBase> allocation) {
GPGMM_TRACE_EVENT_DURATION(TraceEventCategory::kDefault,
"DedicatedMemoryAllocator.DeallocateMemory");

Expand Down
4 changes: 2 additions & 2 deletions src/gpgmm/common/DedicatedMemoryAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ namespace gpgmm {
uint64_t memoryAlignment);

// MemoryAllocatorBase interface
ResultOrError<std::unique_ptr<MemoryAllocation>> TryAllocateMemory(
ResultOrError<std::unique_ptr<MemoryAllocationBase>> TryAllocateMemory(
const MemoryAllocationRequest& request) override;
void DeallocateMemory(std::unique_ptr<MemoryAllocation> subAllocation) override;
void DeallocateMemory(std::unique_ptr<MemoryAllocationBase> subAllocation) override;
uint64_t GetMemoryAlignment() const override;

MemoryAllocatorStats GetStats() const override;
Expand Down
6 changes: 3 additions & 3 deletions src/gpgmm/common/IndexedMemoryPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ namespace gpgmm {
IndexedMemoryPool::IndexedMemoryPool(uint64_t memorySize) : MemoryPoolBase(memorySize) {
}

MemoryAllocation IndexedMemoryPool::AcquireFromPool(uint64_t indexInPool) {
MemoryAllocationBase IndexedMemoryPool::AcquireFromPool(uint64_t indexInPool) {
if (indexInPool >= mPool.size()) {
mPool.resize(indexInPool + 1);
}
MemoryAllocation tmp = mPool[indexInPool];
MemoryAllocationBase tmp = mPool[indexInPool];
mPool[indexInPool] = {}; // invalidate it
return tmp;
}

void IndexedMemoryPool::ReturnToPool(MemoryAllocation allocation, uint64_t indexInPool) {
void IndexedMemoryPool::ReturnToPool(MemoryAllocationBase allocation, uint64_t indexInPool) {
ASSERT(indexInPool < mPool.size());
ASSERT(allocation.GetSize() == GetMemorySize());
mPool[indexInPool] = std::move(allocation);
Expand Down
6 changes: 3 additions & 3 deletions src/gpgmm/common/IndexedMemoryPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ namespace gpgmm {
~IndexedMemoryPool() override = default;

// MemoryPoolBase interface
MemoryAllocation AcquireFromPool(uint64_t indexInPool) override;
void ReturnToPool(MemoryAllocation allocation, uint64_t indexInPool) override;
MemoryAllocationBase AcquireFromPool(uint64_t indexInPool) override;
void ReturnToPool(MemoryAllocationBase allocation, uint64_t indexInPool) override;
uint64_t ReleasePool(uint64_t bytesToRelease) override;

uint64_t GetPoolSize() const override;

private:
std::vector<MemoryAllocation> mPool;
std::vector<MemoryAllocationBase> mPool;
};

} // namespace gpgmm
Expand Down
6 changes: 3 additions & 3 deletions src/gpgmm/common/LIFOMemoryPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ namespace gpgmm {
LIFOMemoryPool::LIFOMemoryPool(uint64_t memorySize) : MemoryPoolBase(memorySize) {
}

MemoryAllocation LIFOMemoryPool::AcquireFromPool(uint64_t indexInPool) {
MemoryAllocationBase LIFOMemoryPool::AcquireFromPool(uint64_t indexInPool) {
ASSERT(indexInPool == kInvalidIndex);

MemoryAllocation allocation = {};
MemoryAllocationBase allocation = {};
if (!mPool.empty()) {
allocation = std::move(mPool.front());
mPool.pop_front();
}
return allocation;
}

void LIFOMemoryPool::ReturnToPool(MemoryAllocation allocation, uint64_t indexInPool) {
void LIFOMemoryPool::ReturnToPool(MemoryAllocationBase allocation, uint64_t indexInPool) {
ASSERT(indexInPool == kInvalidIndex);
ASSERT(allocation.GetSize() == GetMemorySize());

Expand Down
6 changes: 3 additions & 3 deletions src/gpgmm/common/LIFOMemoryPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ namespace gpgmm {
~LIFOMemoryPool() override = default;

// MemoryPoolBase interface
MemoryAllocation AcquireFromPool(uint64_t indexInPool = kInvalidIndex) override;
void ReturnToPool(MemoryAllocation allocation,
MemoryAllocationBase AcquireFromPool(uint64_t indexInPool = kInvalidIndex) override;
void ReturnToPool(MemoryAllocationBase allocation,
uint64_t indexInPool = kInvalidIndex) override;
uint64_t ReleasePool(uint64_t bytesToFree = kInvalidSize) override;

uint64_t GetPoolSize() const override;

private:
std::deque<MemoryAllocation> mPool;
std::deque<MemoryAllocationBase> mPool;
};

} // namespace gpgmm
Expand Down
40 changes: 20 additions & 20 deletions src/gpgmm/common/MemoryAllocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

namespace gpgmm {

MemoryAllocation::MemoryAllocation()
MemoryAllocationBase::MemoryAllocationBase()
: mAllocator(nullptr),
mMemory(nullptr),
mOffset(kInvalidOffset),
Expand All @@ -32,12 +32,12 @@ namespace gpgmm {
mBlock(nullptr) {
}

MemoryAllocation::MemoryAllocation(MemoryAllocatorBase* allocator,
MemoryBase* memory,
uint64_t offset,
AllocationMethod method,
MemoryBlock* block,
uint64_t requestSize)
MemoryAllocationBase::MemoryAllocationBase(MemoryAllocatorBase* allocator,
MemoryBase* memory,
uint64_t offset,
AllocationMethod method,
MemoryBlock* block,
uint64_t requestSize)
: mAllocator(allocator),
mMemory(memory),
mOffset(offset),
Expand All @@ -48,9 +48,9 @@ namespace gpgmm {
mBlock(block) {
}

MemoryAllocation::MemoryAllocation(MemoryAllocatorBase* allocator,
MemoryBase* memory,
uint64_t requestSize)
MemoryAllocationBase::MemoryAllocationBase(MemoryAllocatorBase* allocator,
MemoryBase* memory,
uint64_t requestSize)
: mAllocator(allocator),
mMemory(memory),
mOffset(0),
Expand All @@ -61,24 +61,24 @@ namespace gpgmm {
mBlock(nullptr) {
}

bool MemoryAllocation::operator==(const MemoryAllocation& other) const {
bool MemoryAllocationBase::operator==(const MemoryAllocationBase& other) const {
return (other.mAllocator == mAllocator && other.mMemory == mMemory &&
other.mOffset == mOffset && other.mMethod == mMethod && other.mBlock == mBlock);
}

bool MemoryAllocation::operator!=(const MemoryAllocation& other) const {
bool MemoryAllocationBase::operator!=(const MemoryAllocationBase& other) const {
return !operator==(other);
}

MemoryBase* MemoryAllocation::GetMemory() const {
MemoryBase* MemoryAllocationBase::GetMemory() const {
return mMemory;
}

MemoryAllocatorBase* MemoryAllocation::GetAllocator() const {
MemoryAllocatorBase* MemoryAllocationBase::GetAllocator() const {
return mAllocator;
}

uint64_t MemoryAllocation::GetSize() const {
uint64_t MemoryAllocationBase::GetSize() const {
switch (mMethod) {
case gpgmm::AllocationMethod::kStandalone:
ASSERT(mMemory != nullptr);
Expand All @@ -95,15 +95,15 @@ namespace gpgmm {
}
}

uint64_t MemoryAllocation::GetRequestSize() const {
uint64_t MemoryAllocationBase::GetRequestSize() const {
#ifdef GPGMM_ENABLE_MEMORY_ALIGN_CHECKS
return mRequestSize;
#else
return kInvalidSize;
#endif
}

uint64_t MemoryAllocation::GetAlignment() const {
uint64_t MemoryAllocationBase::GetAlignment() const {
switch (mMethod) {
case gpgmm::AllocationMethod::kStandalone:
ASSERT(mMemory != nullptr);
Expand All @@ -122,15 +122,15 @@ namespace gpgmm {
}
}

uint64_t MemoryAllocation::GetOffset() const {
uint64_t MemoryAllocationBase::GetOffset() const {
return mOffset;
}

AllocationMethod MemoryAllocation::GetMethod() const {
AllocationMethod MemoryAllocationBase::GetMethod() const {
return mMethod;
}

MemoryBlock* MemoryAllocation::GetBlock() const {
MemoryBlock* MemoryAllocationBase::GetBlock() const {
return mBlock;
}

Expand Down
Loading