| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/memory/discardable_memory_allocator_android.h" | 5 #include "base/memory/discardable_memory_allocator_android.h" |
| 6 | 6 |
| 7 #include <sys/types.h> | 7 #include <sys/types.h> |
| 8 #include <unistd.h> | 8 #include <unistd.h> |
| 9 | 9 |
| 10 #include "base/memory/discardable_memory.h" | 10 #include "base/memory/discardable_memory.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
| 14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
| 15 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 17 |
| 18 namespace base { | 18 namespace base { |
| 19 namespace internal { | 19 namespace internal { |
| 20 | 20 |
| 21 const char kAllocatorName[] = "allocator-for-testing"; | 21 const char kAllocatorName[] = "allocator-for-testing"; |
| 22 | 22 |
| 23 const size_t kAshmemRegionSizeForTesting = 32 * 1024 * 1024; | 23 const size_t kAshmemRegionSizeForTesting = 32 * 1024 * 1024; |
| 24 const size_t kPageSize = 4096; | 24 const size_t kPageSize = 4096; |
| 25 | 25 |
| 26 const size_t kMaxAllowedAllocationSize = |
| 27 std::numeric_limits<size_t>::max() - kPageSize + 1; |
| 28 |
| 26 class DiscardableMemoryAllocatorTest : public testing::Test { | 29 class DiscardableMemoryAllocatorTest : public testing::Test { |
| 27 protected: | 30 protected: |
| 28 DiscardableMemoryAllocatorTest() | 31 DiscardableMemoryAllocatorTest() |
| 29 : allocator_(kAllocatorName, kAshmemRegionSizeForTesting) { | 32 : allocator_(kAllocatorName, kAshmemRegionSizeForTesting) { |
| 30 } | 33 } |
| 31 | 34 |
| 32 DiscardableMemoryAllocator allocator_; | 35 DiscardableMemoryAllocator allocator_; |
| 33 }; | 36 }; |
| 34 | 37 |
| 35 void WriteToDiscardableMemory(DiscardableMemory* memory, size_t size) { | 38 void WriteToDiscardableMemory(DiscardableMemory* memory, size_t size) { |
| 36 // Write to the first and the last pages only to avoid paging in up to 64 | 39 // Write to the first and the last pages only to avoid paging in up to 64 |
| 37 // MBytes. | 40 // MBytes. |
| 38 static_cast<char*>(memory->Memory())[0] = 'a'; | 41 static_cast<char*>(memory->Memory())[0] = 'a'; |
| 39 static_cast<char*>(memory->Memory())[size - 1] = 'a'; | 42 static_cast<char*>(memory->Memory())[size - 1] = 'a'; |
| 40 } | 43 } |
| 41 | 44 |
| 42 TEST_F(DiscardableMemoryAllocatorTest, Basic) { | 45 TEST_F(DiscardableMemoryAllocatorTest, Basic) { |
| 43 const size_t size = 128; | 46 const size_t size = 128; |
| 44 scoped_ptr<DiscardableMemory> memory(allocator_.Allocate(size)); | 47 scoped_ptr<DiscardableMemory> memory(allocator_.Allocate(size)); |
| 45 ASSERT_TRUE(memory); | 48 ASSERT_TRUE(memory); |
| 46 WriteToDiscardableMemory(memory.get(), size); | 49 WriteToDiscardableMemory(memory.get(), size); |
| 47 } | 50 } |
| 48 | 51 |
| 49 TEST_F(DiscardableMemoryAllocatorTest, ZeroAllocationIsNotSupported) { | 52 TEST_F(DiscardableMemoryAllocatorTest, ZeroAllocationIsNotSupported) { |
| 50 scoped_ptr<DiscardableMemory> memory(allocator_.Allocate(0)); | 53 scoped_ptr<DiscardableMemory> memory(allocator_.Allocate(0)); |
| 51 ASSERT_FALSE(memory); | 54 ASSERT_FALSE(memory); |
| 52 } | 55 } |
| 53 | 56 |
| 54 TEST_F(DiscardableMemoryAllocatorTest, TooLargeAllocationFails) { | 57 TEST_F(DiscardableMemoryAllocatorTest, TooLargeAllocationFails) { |
| 55 const size_t max_allowed_allocation_size = | |
| 56 std::numeric_limits<size_t>::max() - kPageSize + 1; | |
| 57 scoped_ptr<DiscardableMemory> memory( | 58 scoped_ptr<DiscardableMemory> memory( |
| 58 allocator_.Allocate(max_allowed_allocation_size + 1)); | 59 allocator_.Allocate(kMaxAllowedAllocationSize + 1)); |
| 59 // Page-alignment would have caused an overflow resulting in a small | 60 // Page-alignment would have caused an overflow resulting in a small |
| 60 // allocation if the input size wasn't checked correctly. | 61 // allocation if the input size wasn't checked correctly. |
| 61 ASSERT_FALSE(memory); | 62 ASSERT_FALSE(memory); |
| 62 } | 63 } |
| 63 | 64 |
| 64 TEST_F(DiscardableMemoryAllocatorTest, | 65 TEST_F(DiscardableMemoryAllocatorTest, |
| 65 AshmemRegionsAreNotSmallerThanRequestedSize) { | 66 AshmemRegionsAreNotSmallerThanRequestedSize) { |
| 66 const size_t size = std::numeric_limits<size_t>::max() - kPageSize + 1; | |
| 67 // The creation of the underlying ashmem region is expected to fail since | 67 // The creation of the underlying ashmem region is expected to fail since |
| 68 // there should not be enough room in the address space. When ashmem creation | 68 // there should not be enough room in the address space. When ashmem creation |
| 69 // fails, the allocator repetitively retries by dividing the size by 2. This | 69 // fails, the allocator repetitively retries by dividing the size by 2. This |
| 70 // size should not be smaller than the size the user requested so the | 70 // size should not be smaller than the size the user requested so the |
| 71 // allocation here should just fail (and not succeed with the minimum ashmem | 71 // allocation here should just fail (and not succeed with the minimum ashmem |
| 72 // region size). | 72 // region size). |
| 73 scoped_ptr<DiscardableMemory> memory(allocator_.Allocate(size)); | 73 scoped_ptr<DiscardableMemory> memory( |
| 74 allocator_.Allocate(kMaxAllowedAllocationSize)); |
| 74 ASSERT_FALSE(memory); | 75 ASSERT_FALSE(memory); |
| 75 } | 76 } |
| 76 | 77 |
| 78 TEST_F(DiscardableMemoryAllocatorTest, AshmemRegionsAreAlwaysPageAligned) { |
| 79 // Use a separate allocator here so that we can override the ashmem region |
| 80 // size. |
| 81 DiscardableMemoryAllocator allocator( |
| 82 kAllocatorName, kMaxAllowedAllocationSize); |
| 83 scoped_ptr<DiscardableMemory> memory(allocator.Allocate(kPageSize)); |
| 84 ASSERT_TRUE(memory); |
| 85 EXPECT_GT(kMaxAllowedAllocationSize, allocator.ashmem_region_size()); |
| 86 ASSERT_TRUE(allocator.ashmem_region_size() % kPageSize == 0); |
| 87 } |
| 88 |
| 77 TEST_F(DiscardableMemoryAllocatorTest, LargeAllocation) { | 89 TEST_F(DiscardableMemoryAllocatorTest, LargeAllocation) { |
| 78 // Note that large allocations should just use DiscardableMemoryAndroidSimple | 90 // Note that large allocations should just use DiscardableMemoryAndroidSimple |
| 79 // instead. | 91 // instead. |
| 80 const size_t size = 64 * 1024 * 1024; | 92 const size_t size = 64 * 1024 * 1024; |
| 81 scoped_ptr<DiscardableMemory> memory(allocator_.Allocate(size)); | 93 scoped_ptr<DiscardableMemory> memory(allocator_.Allocate(size)); |
| 82 ASSERT_TRUE(memory); | 94 ASSERT_TRUE(memory); |
| 83 WriteToDiscardableMemory(memory.get(), size); | 95 WriteToDiscardableMemory(memory.get(), size); |
| 84 } | 96 } |
| 85 | 97 |
| 86 TEST_F(DiscardableMemoryAllocatorTest, ChunksArePageAligned) { | 98 TEST_F(DiscardableMemoryAllocatorTest, ChunksArePageAligned) { |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 // The last page of the first ashmem region should be used for this | 264 // The last page of the first ashmem region should be used for this |
| 253 // allocation. | 265 // allocation. |
| 254 scoped_ptr<DiscardableMemory> memory3(allocator_.Allocate(kPageSize)); | 266 scoped_ptr<DiscardableMemory> memory3(allocator_.Allocate(kPageSize)); |
| 255 ASSERT_TRUE(memory3); | 267 ASSERT_TRUE(memory3); |
| 256 WriteToDiscardableMemory(memory3.get(), kPageSize); | 268 WriteToDiscardableMemory(memory3.get(), kPageSize); |
| 257 EXPECT_EQ(memory3->Memory(), static_cast<char*>(memory1->Memory()) + size); | 269 EXPECT_EQ(memory3->Memory(), static_cast<char*>(memory1->Memory()) + size); |
| 258 } | 270 } |
| 259 | 271 |
| 260 } // namespace internal | 272 } // namespace internal |
| 261 } // namespace base | 273 } // namespace base |
| OLD | NEW |