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
17 changes: 10 additions & 7 deletions src/gpgmm/common/BuddyMemoryAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ namespace gpgmm {
&mBuddyBlockAllocator, allocationSize, request.Alignment, request.NeverAllocate,
[&](const auto& block) -> ResultOrError<std::unique_ptr<MemoryAllocationBase>> {
const uint64_t memoryIndex = GetMemoryIndex(block->Offset);
std::unique_ptr<MemoryAllocationBase> memoryAllocation =
mUsedPool.AcquireFromPool(memoryIndex);

std::unique_ptr<MemoryAllocationBase> memoryAllocation;
GPGMM_TRY_ASSIGN(mUsedPool.AcquireFromPool(memoryIndex), memoryAllocation);

// No existing, allocate new memory for the block.
if (memoryAllocation == nullptr) {
Expand Down Expand Up @@ -113,16 +114,18 @@ namespace gpgmm {

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

std::unique_ptr<MemoryAllocationBase> memoryAllocation =
mUsedPool.AcquireFromPool(memoryIndex);
auto result = mUsedPool.AcquireFromPool(memoryIndex);
ASSERT(result.IsSuccess());

std::unique_ptr<MemoryAllocationBase> allocation = result.AcquireResult();

MemoryBase* memory = memoryAllocation->GetMemory();
MemoryBase* memory = allocation->GetMemory();
ASSERT(memory != nullptr);

if (memory->Unref()) {
GetNextInChain()->DeallocateMemory(std::move(memoryAllocation));
GetNextInChain()->DeallocateMemory(std::move(allocation));
} else {
mUsedPool.ReturnToPool(std::move(memoryAllocation), memoryIndex);
mUsedPool.ReturnToPool(std::move(allocation), memoryIndex);
}
}

Expand Down
11 changes: 7 additions & 4 deletions src/gpgmm/common/IndexedMemoryPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@ namespace gpgmm {
IndexedMemoryPool::IndexedMemoryPool(uint64_t memorySize) : MemoryPoolBase(memorySize) {
}

std::unique_ptr<MemoryAllocationBase> IndexedMemoryPool::AcquireFromPool(uint64_t indexInPool) {
ResultOrError<std::unique_ptr<MemoryAllocationBase>> IndexedMemoryPool::AcquireFromPool(
uint64_t indexInPool) {
if (indexInPool >= mPool.size()) {
mPool.resize(indexInPool + 1);
}
return std::unique_ptr<MemoryAllocationBase>(mPool[indexInPool].release());
}

void IndexedMemoryPool::ReturnToPool(std::unique_ptr<MemoryAllocationBase> allocation,
uint64_t indexInPool) {
ASSERT(indexInPool < mPool.size());
MaybeError IndexedMemoryPool::ReturnToPool(std::unique_ptr<MemoryAllocationBase> allocation,
uint64_t indexInPool) {
GPGMM_RETURN_ERROR_IF(this, indexInPool >= mPool.size(), "Index exceeded pool size",
ErrorCode::kBadOperation);
mPool[indexInPool] = std::move(allocation);
return {};
}

uint64_t IndexedMemoryPool::ReleasePool(uint64_t bytesToRelease) {
Expand Down
7 changes: 4 additions & 3 deletions src/gpgmm/common/IndexedMemoryPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ namespace gpgmm {
~IndexedMemoryPool() override = default;

// MemoryPoolBase interface
std::unique_ptr<MemoryAllocationBase> AcquireFromPool(uint64_t indexInPool) override;
void ReturnToPool(std::unique_ptr<MemoryAllocationBase> allocation,
uint64_t indexInPool) override;
ResultOrError<std::unique_ptr<MemoryAllocationBase>> AcquireFromPool(
uint64_t indexInPool) override;
MaybeError ReturnToPool(std::unique_ptr<MemoryAllocationBase> allocation,
uint64_t indexInPool) override;
uint64_t ReleasePool(uint64_t bytesToRelease) override;

uint64_t GetPoolSize() const override;
Expand Down
15 changes: 10 additions & 5 deletions src/gpgmm/common/LIFOMemoryPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,26 @@ namespace gpgmm {
LIFOMemoryPool::LIFOMemoryPool(uint64_t memorySize) : MemoryPoolBase(memorySize) {
}

std::unique_ptr<MemoryAllocationBase> LIFOMemoryPool::AcquireFromPool(uint64_t indexInPool) {
ASSERT(indexInPool == kInvalidIndex);
ResultOrError<std::unique_ptr<MemoryAllocationBase>> LIFOMemoryPool::AcquireFromPool(
uint64_t indexInPool) {
GPGMM_RETURN_ERROR_IF(this, indexInPool != kInvalidIndex,
"Index was specified but not allowed", ErrorCode::kBadOperation);

std::unique_ptr<MemoryAllocationBase> allocation;
if (!mPool.empty()) {
allocation = std::move(mPool.front());
mPool.pop_front();
}

return allocation;
}

void LIFOMemoryPool::ReturnToPool(std::unique_ptr<MemoryAllocationBase> allocation,
uint64_t indexInPool) {
ASSERT(indexInPool == kInvalidIndex);
MaybeError LIFOMemoryPool::ReturnToPool(std::unique_ptr<MemoryAllocationBase> allocation,
uint64_t indexInPool) {
GPGMM_RETURN_ERROR_IF(this, indexInPool != kInvalidIndex,
"Index was specified but not allowed", ErrorCode::kBadOperation);
mPool.push_front(std::move(allocation));
return {};
}

uint64_t LIFOMemoryPool::ReleasePool(uint64_t bytesToRelease) {
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,10 +28,10 @@ namespace gpgmm {
~LIFOMemoryPool() override = default;

// MemoryPoolBase interface
std::unique_ptr<MemoryAllocationBase> AcquireFromPool(
ResultOrError<std::unique_ptr<MemoryAllocationBase>> AcquireFromPool(
uint64_t indexInPool = kInvalidIndex) override;
void ReturnToPool(std::unique_ptr<MemoryAllocationBase> allocation,
uint64_t indexInPool = kInvalidIndex) override;
MaybeError ReturnToPool(std::unique_ptr<MemoryAllocationBase> allocation,
uint64_t indexInPool = kInvalidIndex) override;
uint64_t ReleasePool(uint64_t bytesToFree = kInvalidSize) override;

uint64_t GetPoolSize() const override;
Expand Down
5 changes: 5 additions & 0 deletions src/gpgmm/common/MemoryPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ namespace gpgmm {
GPGMM_TRACE_EVENT_OBJECT_DESTROY(this);
}

std::unique_ptr<MemoryAllocationBase> MemoryPoolBase::AcquireFromPoolForTesting(
uint64_t indexInPool) {
return AcquireFromPool(indexInPool).AcquireResult();
}

uint64_t MemoryPoolBase::GetMemorySize() const {
return mMemorySize;
}
Expand Down
10 changes: 7 additions & 3 deletions src/gpgmm/common/MemoryPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ifndef SRC_GPGMM_COMMON_MEMORYPOOL_H_
#define SRC_GPGMM_COMMON_MEMORYPOOL_H_

#include "gpgmm/common/Error.h"
#include "gpgmm/common/MemoryAllocation.h"
#include "gpgmm/utils/Limits.h"

Expand All @@ -31,12 +32,15 @@ namespace gpgmm {

// Retrieves a memory allocation from the pool using an optional index.
// Use kInvalidIndex to specify |this| pool is not indexed.
virtual std::unique_ptr<MemoryAllocationBase> AcquireFromPool(uint64_t indexInPool) = 0;
virtual ResultOrError<std::unique_ptr<MemoryAllocationBase>> AcquireFromPool(
uint64_t indexInPool) = 0;

std::unique_ptr<MemoryAllocationBase> AcquireFromPoolForTesting(uint64_t indexInPool);

// Returns a memory allocation back to the pool using an optional index.
// Use kInvalidIndex to specify |this| pool is not indexed.
virtual void ReturnToPool(std::unique_ptr<MemoryAllocationBase> allocation,
uint64_t indexInPool) = 0;
virtual MaybeError ReturnToPool(std::unique_ptr<MemoryAllocationBase> allocation,
uint64_t indexInPool) = 0;

// Deallocate or shrink the pool.
virtual uint64_t ReleasePool(uint64_t bytesToRelease) = 0;
Expand Down
3 changes: 2 additions & 1 deletion src/gpgmm/common/PooledMemoryAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ namespace gpgmm {

GPGMM_RETURN_IF_ERROR(ValidateRequest(request));

std::unique_ptr<MemoryAllocationBase> allocation = mPool->AcquireFromPool(kInvalidIndex);
std::unique_ptr<MemoryAllocationBase> allocation;
GPGMM_TRY_ASSIGN(mPool->AcquireFromPool(kInvalidIndex), allocation);
if (allocation == nullptr) {
MemoryAllocationRequest memoryRequest = request;
memoryRequest.Alignment = mMemoryAlignment;
Expand Down
3 changes: 2 additions & 1 deletion src/gpgmm/common/SegmentedMemoryAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ namespace gpgmm {
MemorySegment* segment = GetOrCreateFreeSegment(memorySize);
ASSERT(segment != nullptr);

std::unique_ptr<MemoryAllocationBase> allocation = segment->AcquireFromPool();
std::unique_ptr<MemoryAllocationBase> allocation;
GPGMM_TRY_ASSIGN(segment->AcquireFromPool(), allocation);
if (allocation == nullptr) {
MemoryAllocationRequest memoryRequest = request;
memoryRequest.Alignment = mMemoryAlignment;
Expand Down
2 changes: 1 addition & 1 deletion src/tests/unittests/MemoryPoolTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ TEST_F(LIFOMemoryPoolTests, SingleAllocation) {
EXPECT_EQ(pool.GetPoolSize() * pool.GetMemorySize(), kDefaultMemorySize);
EXPECT_EQ(pool.GetPoolSize(), 1u);

pool.ReturnToPool(pool.AcquireFromPool());
pool.ReturnToPool(pool.AcquireFromPoolForTesting(kInvalidIndex));
EXPECT_EQ(pool.GetPoolSize() * pool.GetMemorySize(), kDefaultMemorySize);
EXPECT_EQ(pool.GetPoolSize(), 1u);

Expand Down