diff --git a/src/gpgmm/common/SlabMemoryAllocator.cpp b/src/gpgmm/common/SlabMemoryAllocator.cpp index e93df8849..8a04c8933 100644 --- a/src/gpgmm/common/SlabMemoryAllocator.cpp +++ b/src/gpgmm/common/SlabMemoryAllocator.cpp @@ -161,7 +161,7 @@ namespace gpgmm { Slab* freeSlab = cache->FreeList.head()->value(); ASSERT(freeSlab != nullptr); - if (freeSlab->Allocation->GetSize() >= slabSize) { + if (freeSlab->Allocation && freeSlab->Allocation->GetSize() >= slabSize) { return freeSlab->Allocation->GetSize(); } } diff --git a/src/tests/unittests/SlabMemoryAllocatorTests.cpp b/src/tests/unittests/SlabMemoryAllocatorTests.cpp index e9771f75a..0700f340c 100644 --- a/src/tests/unittests/SlabMemoryAllocatorTests.cpp +++ b/src/tests/unittests/SlabMemoryAllocatorTests.cpp @@ -1017,6 +1017,26 @@ TEST_F(SlabCacheAllocatorTests, SlabPrefetch) { } } +TEST_F(SlabCacheAllocatorTests, CachedMemory) { + constexpr uint64_t kBlockSize = 32; + constexpr uint64_t kMaxSlabSize = 512; + + SlabCacheAllocator allocator( + kMaxSlabSize, kDefaultSlabSize, kDefaultSlabAlignment, kDefaultSlabFragmentationLimit, + /*prefetchSlab*/ true, kNoSlabGrowthFactor, std::make_unique()); + + // Re-requesting same size from cached memory should always succeed. + MemoryAllocationRequest request = CreateBasicRequest(kBlockSize, 1); + request.AlwaysCacheSize = true; + + std::unique_ptr allocation = allocator.TryAllocateMemory(request); + ASSERT_NE(allocation, nullptr); + allocator.DeallocateMemory(std::move(allocation)); + + request.AvailableForAllocation = 0; + EXPECT_EQ(allocator.TryAllocateMemory(request), nullptr); +} + // Verify creating more slabs than memory available fails. TEST_F(SlabCacheAllocatorTests, OutOfMemory) { SlabCacheAllocator allocator(kDefaultSlabSize, kDefaultSlabSize, kDefaultSlabAlignment,