This source file includes following definitions.
- WriteToDiscardableMemory
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include "base/memory/discardable_memory_allocator_android.h"
#include <sys/types.h>
#include <unistd.h>
#include "base/memory/discardable_memory.h"
#include "base/memory/scoped_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/stringprintf.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace base {
namespace internal {
const char kAllocatorName[] = "allocator-for-testing";
const size_t kAshmemRegionSizeForTesting = 32 * 1024 * 1024;
const size_t kPageSize = 4096;
const size_t kMaxAllowedAllocationSize =
std::numeric_limits<size_t>::max() - kPageSize + 1;
class DiscardableMemoryAllocatorTest : public testing::Test {
protected:
DiscardableMemoryAllocatorTest()
: allocator_(kAllocatorName, kAshmemRegionSizeForTesting) {
}
DiscardableMemoryAllocator allocator_;
};
void WriteToDiscardableMemory(DiscardableMemory* memory, size_t size) {
static_cast<char*>(memory->Memory())[0] = 'a';
static_cast<char*>(memory->Memory())[size - 1] = 'a';
}
TEST_F(DiscardableMemoryAllocatorTest, Basic) {
const size_t size = 128;
scoped_ptr<DiscardableMemory> memory(allocator_.Allocate(size));
ASSERT_TRUE(memory);
WriteToDiscardableMemory(memory.get(), size);
}
TEST_F(DiscardableMemoryAllocatorTest, ZeroAllocationIsNotSupported) {
scoped_ptr<DiscardableMemory> memory(allocator_.Allocate(0));
ASSERT_FALSE(memory);
}
TEST_F(DiscardableMemoryAllocatorTest, TooLargeAllocationFails) {
scoped_ptr<DiscardableMemory> memory(
allocator_.Allocate(kMaxAllowedAllocationSize + 1));
ASSERT_FALSE(memory);
}
TEST_F(DiscardableMemoryAllocatorTest,
AshmemRegionsAreNotSmallerThanRequestedSize) {
scoped_ptr<DiscardableMemory> memory(
allocator_.Allocate(kMaxAllowedAllocationSize));
ASSERT_FALSE(memory);
}
TEST_F(DiscardableMemoryAllocatorTest, AshmemRegionsAreAlwaysPageAligned) {
DiscardableMemoryAllocator allocator(
kAllocatorName, kMaxAllowedAllocationSize);
scoped_ptr<DiscardableMemory> memory(allocator.Allocate(kPageSize));
ASSERT_TRUE(memory);
EXPECT_GT(kMaxAllowedAllocationSize, allocator.last_ashmem_region_size());
ASSERT_TRUE(allocator.last_ashmem_region_size() % kPageSize == 0);
}
TEST_F(DiscardableMemoryAllocatorTest, LargeAllocation) {
const size_t size = 64 * 1024 * 1024;
scoped_ptr<DiscardableMemory> memory(allocator_.Allocate(size));
ASSERT_TRUE(memory);
WriteToDiscardableMemory(memory.get(), size);
}
TEST_F(DiscardableMemoryAllocatorTest, ChunksArePageAligned) {
scoped_ptr<DiscardableMemory> memory(allocator_.Allocate(kPageSize));
ASSERT_TRUE(memory);
EXPECT_EQ(0U, reinterpret_cast<uint64_t>(memory->Memory()) % kPageSize);
WriteToDiscardableMemory(memory.get(), kPageSize);
}
TEST_F(DiscardableMemoryAllocatorTest, AllocateFreeAllocate) {
scoped_ptr<DiscardableMemory> memory(allocator_.Allocate(kPageSize));
scoped_ptr<DiscardableMemory> memory_lock(allocator_.Allocate(kPageSize));
ASSERT_TRUE(memory);
void* const address = memory->Memory();
memory->Unlock();
memory.reset();
memory = allocator_.Allocate(kPageSize);
ASSERT_TRUE(memory);
EXPECT_EQ(address, memory->Memory());
WriteToDiscardableMemory(memory.get(), kPageSize);
}
TEST_F(DiscardableMemoryAllocatorTest, FreeingWholeAshmemRegionClosesAshmem) {
scoped_ptr<DiscardableMemory> memory(allocator_.Allocate(kPageSize));
ASSERT_TRUE(memory);
const int kMagic = 0xdeadbeef;
*static_cast<int*>(memory->Memory()) = kMagic;
memory.reset();
memory = allocator_.Allocate(kPageSize);
ASSERT_TRUE(memory);
EXPECT_NE(kMagic, *static_cast<const int*>(memory->Memory()));
}
TEST_F(DiscardableMemoryAllocatorTest, AllocateUsesBestFitAlgorithm) {
scoped_ptr<DiscardableMemory> memory1(allocator_.Allocate(3 * kPageSize));
ASSERT_TRUE(memory1);
scoped_ptr<DiscardableMemory> memory2(allocator_.Allocate(2 * kPageSize));
ASSERT_TRUE(memory2);
scoped_ptr<DiscardableMemory> memory3(allocator_.Allocate(1 * kPageSize));
ASSERT_TRUE(memory3);
void* const address_3 = memory3->Memory();
memory1.reset();
memory3.reset();
memory1 = allocator_.Allocate(1 * kPageSize);
ASSERT_TRUE(memory1);
EXPECT_EQ(address_3, memory1->Memory());
WriteToDiscardableMemory(memory1.get(), kPageSize);
}
TEST_F(DiscardableMemoryAllocatorTest, MergeFreeChunks) {
scoped_ptr<DiscardableMemory> memory1(allocator_.Allocate(kPageSize));
ASSERT_TRUE(memory1);
scoped_ptr<DiscardableMemory> memory2(allocator_.Allocate(kPageSize));
ASSERT_TRUE(memory2);
scoped_ptr<DiscardableMemory> memory3(allocator_.Allocate(kPageSize));
ASSERT_TRUE(memory3);
scoped_ptr<DiscardableMemory> memory4(allocator_.Allocate(kPageSize));
ASSERT_TRUE(memory4);
void* const memory1_address = memory1->Memory();
memory1.reset();
memory3.reset();
memory2.reset();
memory1 = allocator_.Allocate(3 * kPageSize);
EXPECT_EQ(memory1_address, memory1->Memory());
}
TEST_F(DiscardableMemoryAllocatorTest, MergeFreeChunksAdvanced) {
scoped_ptr<DiscardableMemory> memory1(allocator_.Allocate(4 * kPageSize));
ASSERT_TRUE(memory1);
scoped_ptr<DiscardableMemory> memory2(allocator_.Allocate(4 * kPageSize));
ASSERT_TRUE(memory2);
void* const memory1_address = memory1->Memory();
memory1.reset();
memory1 = allocator_.Allocate(2 * kPageSize);
memory2.reset();
memory2 = allocator_.Allocate(6 * kPageSize);
EXPECT_EQ(
static_cast<const char*>(memory2->Memory()),
static_cast<const char*>(memory1_address) + 2 * kPageSize);
}
TEST_F(DiscardableMemoryAllocatorTest, MergeFreeChunksAdvanced2) {
scoped_ptr<DiscardableMemory> memory1(allocator_.Allocate(4 * kPageSize));
ASSERT_TRUE(memory1);
scoped_ptr<DiscardableMemory> memory2(allocator_.Allocate(4 * kPageSize));
ASSERT_TRUE(memory2);
void* const memory1_address = memory1->Memory();
memory1.reset();
memory1 = allocator_.Allocate(2 * kPageSize);
scoped_ptr<DiscardableMemory> memory3(allocator_.Allocate(2 * kPageSize));
memory3.reset();
memory2.reset();
memory2 = allocator_.Allocate(6 * kPageSize);
EXPECT_EQ(
static_cast<const char*>(memory2->Memory()),
static_cast<const char*>(memory1_address) + 2 * kPageSize);
}
TEST_F(DiscardableMemoryAllocatorTest, MergeFreeChunksAndDeleteAshmemRegion) {
scoped_ptr<DiscardableMemory> memory1(allocator_.Allocate(4 * kPageSize));
ASSERT_TRUE(memory1);
scoped_ptr<DiscardableMemory> memory2(allocator_.Allocate(4 * kPageSize));
ASSERT_TRUE(memory2);
memory1.reset();
memory1 = allocator_.Allocate(2 * kPageSize);
scoped_ptr<DiscardableMemory> memory3(allocator_.Allocate(2 * kPageSize));
memory1.reset();
memory3.reset();
const int kMagic = 0xdeadbeef;
*static_cast<int*>(memory2->Memory()) = kMagic;
memory2.reset();
memory2 = allocator_.Allocate(2 * kPageSize);
EXPECT_NE(kMagic, *static_cast<int*>(memory2->Memory()));
}
TEST_F(DiscardableMemoryAllocatorTest,
TooLargeFreeChunksDontCauseTooMuchFragmentationWhenRecycled) {
scoped_ptr<DiscardableMemory> memory_1(allocator_.Allocate(64 * 1024));
ASSERT_TRUE(memory_1);
scoped_ptr<DiscardableMemory> memory_2(allocator_.Allocate(32 * 1024));
ASSERT_TRUE(memory_2);
void* const address = memory_2->Memory();
memory_2.reset();
const size_t size = 16 * 1024;
memory_2 = allocator_.Allocate(size);
ASSERT_TRUE(memory_2);
EXPECT_EQ(address, memory_2->Memory());
WriteToDiscardableMemory(memory_2.get(), size);
scoped_ptr<DiscardableMemory> memory_3(allocator_.Allocate(size));
EXPECT_EQ(static_cast<char*>(address) + size, memory_3->Memory());
WriteToDiscardableMemory(memory_3.get(), size);
}
TEST_F(DiscardableMemoryAllocatorTest, UseMultipleAshmemRegions) {
const size_t size = kAshmemRegionSizeForTesting - kPageSize;
scoped_ptr<DiscardableMemory> memory1(allocator_.Allocate(size));
ASSERT_TRUE(memory1);
WriteToDiscardableMemory(memory1.get(), size);
scoped_ptr<DiscardableMemory> memory2(
allocator_.Allocate(kAshmemRegionSizeForTesting));
ASSERT_TRUE(memory2);
WriteToDiscardableMemory(memory2.get(), kAshmemRegionSizeForTesting);
scoped_ptr<DiscardableMemory> memory3(allocator_.Allocate(kPageSize));
ASSERT_TRUE(memory3);
WriteToDiscardableMemory(memory3.get(), kPageSize);
EXPECT_EQ(memory3->Memory(), static_cast<char*>(memory1->Memory()) + size);
}
TEST_F(DiscardableMemoryAllocatorTest,
HighestAllocatedChunkPointerIsUpdatedWhenHighestChunkGetsSplit) {
scoped_ptr<DiscardableMemory> memory1(allocator_.Allocate(kPageSize));
ASSERT_TRUE(memory1);
scoped_ptr<DiscardableMemory> memory2(allocator_.Allocate(4 * kPageSize));
ASSERT_TRUE(memory2);
memory2.reset();
memory2 = allocator_.Allocate(kPageSize);
scoped_ptr<DiscardableMemory> memory3(allocator_.Allocate(4 * kPageSize));
ASSERT_TRUE(memory3);
memory3.reset();
memory3 = allocator_.Allocate((3 + 4) * kPageSize);
EXPECT_EQ(memory3->Memory(),
static_cast<const char*>(memory2->Memory()) + kPageSize);
}
}
}