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
15 changes: 14 additions & 1 deletion src/gpgmm/common/IndexedMemoryPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace gpgmm {
}

uint64_t IndexedMemoryPool::ReleasePool(uint64_t bytesToRelease) {
return TrimPoolUntil(mPool, bytesToRelease);
return TrimPoolUntil(this, bytesToRelease);
}

uint64_t IndexedMemoryPool::GetPoolSize() const {
Expand All @@ -48,4 +48,17 @@ namespace gpgmm {
}
return count;
}

IndexedMemoryPool::UnderlyingContainerType::iterator IndexedMemoryPool::begin() {
return mPool.begin();
}

IndexedMemoryPool::UnderlyingContainerType::iterator IndexedMemoryPool::end() {
return mPool.end();
}

void IndexedMemoryPool::ShrinkPool(uint64_t lastIndex) {
mPool.erase(begin(), begin() + lastIndex);
}

} // namespace gpgmm
11 changes: 9 additions & 2 deletions src/gpgmm/common/IndexedMemoryPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
namespace gpgmm {

class IndexedMemoryPool final : public MemoryPoolBase {
using UnderlyingContainerType = std::vector<std::unique_ptr<MemoryAllocationBase>>;

public:
explicit IndexedMemoryPool(uint64_t memorySize);
~IndexedMemoryPool() override = default;
Expand All @@ -31,11 +33,16 @@ namespace gpgmm {
void ReturnToPool(std::unique_ptr<MemoryAllocationBase> allocation,
uint64_t indexInPool) override;
uint64_t ReleasePool(uint64_t bytesToRelease) override;

uint64_t GetPoolSize() const override;

UnderlyingContainerType::iterator begin();
UnderlyingContainerType::iterator end();

// Resizes the pool up to but not including |lastIndex|.
void ShrinkPool(uint64_t lastIndex);

private:
std::vector<std::unique_ptr<MemoryAllocationBase>> mPool;
UnderlyingContainerType mPool;
};

} // namespace gpgmm
Expand Down
15 changes: 14 additions & 1 deletion src/gpgmm/common/LIFOMemoryPool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,23 @@ namespace gpgmm {
}

uint64_t LIFOMemoryPool::ReleasePool(uint64_t bytesToRelease) {
return TrimPoolUntil(mPool, bytesToRelease);
return TrimPoolUntil(this, bytesToRelease);
}

uint64_t LIFOMemoryPool::GetPoolSize() const {
return mPool.size();
}

LIFOMemoryPool::UnderlyingContainerType::iterator LIFOMemoryPool::begin() {
return mPool.begin();
}

LIFOMemoryPool::UnderlyingContainerType::iterator LIFOMemoryPool::end() {
return mPool.end();
}

void LIFOMemoryPool::ShrinkPool(uint64_t lastIndex) {
mPool.erase(begin(), begin() + lastIndex);
}

} // namespace gpgmm
11 changes: 9 additions & 2 deletions src/gpgmm/common/LIFOMemoryPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ namespace gpgmm {

// Pool using LIFO (newest are recycled first).
class LIFOMemoryPool : public MemoryPoolBase {
using UnderlyingContainerType = std::deque<std::unique_ptr<MemoryAllocationBase>>;

public:
explicit LIFOMemoryPool(uint64_t memorySize);
~LIFOMemoryPool() override = default;
Expand All @@ -33,11 +35,16 @@ namespace gpgmm {
void ReturnToPool(std::unique_ptr<MemoryAllocationBase> allocation,
uint64_t indexInPool = kInvalidIndex) override;
uint64_t ReleasePool(uint64_t bytesToFree = kInvalidSize) override;

uint64_t GetPoolSize() const override;

UnderlyingContainerType::iterator begin();
UnderlyingContainerType::iterator end();

// Resizes the pool up to but not including |lastIndex|.
void ShrinkPool(uint64_t lastIndex);

private:
std::deque<std::unique_ptr<MemoryAllocationBase>> mPool;
UnderlyingContainerType mPool;
};

} // namespace gpgmm
Expand Down
9 changes: 5 additions & 4 deletions src/gpgmm/common/MemoryPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define SRC_GPGMM_COMMON_MEMORYPOOL_H_

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

#include <memory>
Expand Down Expand Up @@ -49,11 +50,11 @@ namespace gpgmm {

protected:
// Shrinks the size of the pool in |mMemorySize| sizes until |bytesToRelease| is reached.
template <typename T>
uint64_t TrimPoolUntil(T& pool, uint64_t bytesToRelease) {
template <typename MemoryPoolT>
uint64_t TrimPoolUntil(MemoryPoolT* pool, uint64_t bytesToRelease) {
uint64_t totalBytesReleased = 0;
uint64_t lastIndex = 0;
for (auto& allocation : pool) {
for (auto& allocation : *pool) {
totalBytesReleased += allocation->GetSize();
allocation->GetAllocator()->DeallocateMemory(std::move(allocation));
lastIndex++;
Expand All @@ -64,7 +65,7 @@ namespace gpgmm {

// Last is non-inclusive or [first, last).
if (lastIndex > 0) {
pool.erase(pool.begin(), pool.begin() + lastIndex);
pool->ShrinkPool(lastIndex);
}

return totalBytesReleased;
Expand Down