Skip to content

Commit

Permalink
Remove LinkedList from SlabCacheAllocator. (#533)
Browse files Browse the repository at this point in the history
Support pooled dynamic memory allocation for CPU allocations. #321
  • Loading branch information
bbernhar authored Jul 20, 2022
1 parent a60e3b4 commit 85a4ce3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 12 deletions.
15 changes: 5 additions & 10 deletions src/gpgmm/common/SlabMemoryAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,6 @@ namespace gpgmm {

SlabCacheAllocator::~SlabCacheAllocator() {
mSizeCache.clear();
mSlabAllocators.clear();
}

std::unique_ptr<MemoryAllocation> SlabCacheAllocator::TryAllocateMemory(
Expand All @@ -466,13 +465,12 @@ namespace gpgmm {
// Create a slab allocator for the new entry.
auto entry =
mSizeCache.GetOrCreate(SlabAllocatorCacheEntry(blockSize), request.AlwaysCacheSize);
SlabMemoryAllocator* slabAllocator = entry->GetValue().pSlabAllocator;
SlabMemoryAllocator* slabAllocator = entry->GetValue().SlabAllocator.get();
if (slabAllocator == nullptr) {
slabAllocator = new SlabMemoryAllocator(
entry->GetValue().SlabAllocator = std::make_unique<SlabMemoryAllocator>(
blockSize, mMaxSlabSize, mMinSlabSize, mSlabAlignment, mSlabFragmentationLimit,
mAllowSlabPrefetch, mSlabGrowthFactor, GetNextInChain());
entry->GetValue().pSlabAllocator = slabAllocator;
slabAllocator->InsertAfter(mSlabAllocators.tail());
slabAllocator = entry->GetValue().SlabAllocator.get();
}

ASSERT(slabAllocator != nullptr);
Expand All @@ -495,25 +493,22 @@ namespace gpgmm {

auto entry =
mSizeCache.GetOrCreate(SlabAllocatorCacheEntry(subAllocation->GetSize()), false);
SlabMemoryAllocator* slabAllocator = entry->GetValue().pSlabAllocator;
SlabMemoryAllocator* slabAllocator = entry->GetValue().SlabAllocator.get();
ASSERT(slabAllocator != nullptr);

slabAllocator->DeallocateMemory(std::move(subAllocation));

// If this is the last sub-allocation, remove the cached allocator.
// Once |entry| goes out of scope, it will unlink itself from the cache.
entry->Unref();
if (entry->HasOneRef()) {
SafeDelete(slabAllocator);
}
}

MemoryAllocatorInfo SlabCacheAllocator::GetInfo() const {
std::lock_guard<std::mutex> lock(mMutex);

MemoryAllocatorInfo result = {};
for (const auto& entry : mSizeCache) {
const MemoryAllocatorInfo& info = entry->GetValue().pSlabAllocator->GetInfo();
const MemoryAllocatorInfo& info = entry->GetValue().SlabAllocator->GetInfo();
result.UsedBlockCount += info.UsedBlockCount;
result.UsedBlockUsage += info.UsedBlockUsage;
result.PrefetchedMemoryMisses += info.PrefetchedMemoryMisses;
Expand Down
3 changes: 1 addition & 2 deletions src/gpgmm/common/SlabMemoryAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ namespace gpgmm {
return mBlockSize;
}

SlabMemoryAllocator* pSlabAllocator = nullptr;
std::unique_ptr<SlabMemoryAllocator> SlabAllocator;

private:
const uint64_t mBlockSize;
Expand All @@ -148,7 +148,6 @@ namespace gpgmm {
const bool mAllowSlabPrefetch;
const double mSlabGrowthFactor;

LinkedList<MemoryAllocator> mSlabAllocators;
MemoryCache<SlabAllocatorCacheEntry> mSizeCache;
};

Expand Down

0 comments on commit 85a4ce3

Please sign in to comment.