| 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" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 | 28 |
| 29 class DiscardableMemoryAllocatorTest : public testing::Test { | 29 class DiscardableMemoryAllocatorTest : public testing::Test { |
| 30 protected: | 30 protected: |
| 31 DiscardableMemoryAllocatorTest() | 31 DiscardableMemoryAllocatorTest() |
| 32 : allocator_(kAllocatorName, kAshmemRegionSizeForTesting) { | 32 : allocator_(kAllocatorName, kAshmemRegionSizeForTesting) { |
| 33 } | 33 } |
| 34 | 34 |
| 35 DiscardableMemoryAllocator allocator_; | 35 DiscardableMemoryAllocator allocator_; |
| 36 }; | 36 }; |
| 37 | 37 |
| 38 void WriteToDiscardableMemory(DiscardableMemory* memory, size_t size) { | 38 void WriteToDiscardableAshmemChunk(DiscardableAshmemChunk* memory, |
| 39 size_t size) { |
| 39 // Write to the first and the last pages only to avoid paging in up to 64 | 40 // Write to the first and the last pages only to avoid paging in up to 64 |
| 40 // MBytes. | 41 // MBytes. |
| 41 static_cast<char*>(memory->Memory())[0] = 'a'; | 42 static_cast<char*>(memory->Memory())[0] = 'a'; |
| 42 static_cast<char*>(memory->Memory())[size - 1] = 'a'; | 43 static_cast<char*>(memory->Memory())[size - 1] = 'a'; |
| 43 } | 44 } |
| 44 | 45 |
| 45 TEST_F(DiscardableMemoryAllocatorTest, Basic) { | 46 TEST_F(DiscardableMemoryAllocatorTest, Basic) { |
| 46 const size_t size = 128; | 47 const size_t size = 128; |
| 47 scoped_ptr<DiscardableMemory> memory(allocator_.Allocate(size)); | 48 scoped_ptr<DiscardableAshmemChunk> memory(allocator_.Allocate(size)); |
| 48 ASSERT_TRUE(memory); | 49 ASSERT_TRUE(memory); |
| 49 WriteToDiscardableMemory(memory.get(), size); | 50 WriteToDiscardableAshmemChunk(memory.get(), size); |
| 50 } | 51 } |
| 51 | 52 |
| 52 TEST_F(DiscardableMemoryAllocatorTest, ZeroAllocationIsNotSupported) { | 53 TEST_F(DiscardableMemoryAllocatorTest, ZeroAllocationIsNotSupported) { |
| 53 scoped_ptr<DiscardableMemory> memory(allocator_.Allocate(0)); | 54 scoped_ptr<DiscardableAshmemChunk> memory(allocator_.Allocate(0)); |
| 54 ASSERT_FALSE(memory); | 55 ASSERT_FALSE(memory); |
| 55 } | 56 } |
| 56 | 57 |
| 57 TEST_F(DiscardableMemoryAllocatorTest, TooLargeAllocationFails) { | 58 TEST_F(DiscardableMemoryAllocatorTest, TooLargeAllocationFails) { |
| 58 scoped_ptr<DiscardableMemory> memory( | 59 scoped_ptr<DiscardableAshmemChunk> memory( |
| 59 allocator_.Allocate(kMaxAllowedAllocationSize + 1)); | 60 allocator_.Allocate(kMaxAllowedAllocationSize + 1)); |
| 60 // Page-alignment would have caused an overflow resulting in a small | 61 // Page-alignment would have caused an overflow resulting in a small |
| 61 // allocation if the input size wasn't checked correctly. | 62 // allocation if the input size wasn't checked correctly. |
| 62 ASSERT_FALSE(memory); | 63 ASSERT_FALSE(memory); |
| 63 } | 64 } |
| 64 | 65 |
| 65 TEST_F(DiscardableMemoryAllocatorTest, | 66 TEST_F(DiscardableMemoryAllocatorTest, |
| 66 AshmemRegionsAreNotSmallerThanRequestedSize) { | 67 AshmemRegionsAreNotSmallerThanRequestedSize) { |
| 67 // The creation of the underlying ashmem region is expected to fail since | 68 // 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 | 69 // 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 | 70 // 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 | 71 // 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 | 72 // allocation here should just fail (and not succeed with the minimum ashmem |
| 72 // region size). | 73 // region size). |
| 73 scoped_ptr<DiscardableMemory> memory( | 74 scoped_ptr<DiscardableAshmemChunk> memory( |
| 74 allocator_.Allocate(kMaxAllowedAllocationSize)); | 75 allocator_.Allocate(kMaxAllowedAllocationSize)); |
| 75 ASSERT_FALSE(memory); | 76 ASSERT_FALSE(memory); |
| 76 } | 77 } |
| 77 | 78 |
| 78 TEST_F(DiscardableMemoryAllocatorTest, AshmemRegionsAreAlwaysPageAligned) { | 79 TEST_F(DiscardableMemoryAllocatorTest, AshmemRegionsAreAlwaysPageAligned) { |
| 79 // Use a separate allocator here so that we can override the ashmem region | 80 // Use a separate allocator here so that we can override the ashmem region |
| 80 // size. | 81 // size. |
| 81 DiscardableMemoryAllocator allocator( | 82 DiscardableMemoryAllocator allocator( |
| 82 kAllocatorName, kMaxAllowedAllocationSize); | 83 kAllocatorName, kMaxAllowedAllocationSize); |
| 83 scoped_ptr<DiscardableMemory> memory(allocator.Allocate(kPageSize)); | 84 scoped_ptr<DiscardableAshmemChunk> memory(allocator.Allocate(kPageSize)); |
| 84 ASSERT_TRUE(memory); | 85 ASSERT_TRUE(memory); |
| 85 EXPECT_GT(kMaxAllowedAllocationSize, allocator.last_ashmem_region_size()); | 86 EXPECT_GT(kMaxAllowedAllocationSize, allocator.last_ashmem_region_size()); |
| 86 ASSERT_TRUE(allocator.last_ashmem_region_size() % kPageSize == 0); | 87 ASSERT_TRUE(allocator.last_ashmem_region_size() % kPageSize == 0); |
| 87 } | 88 } |
| 88 | 89 |
| 89 TEST_F(DiscardableMemoryAllocatorTest, LargeAllocation) { | 90 TEST_F(DiscardableMemoryAllocatorTest, LargeAllocation) { |
| 90 // Note that large allocations should just use DiscardableMemoryAndroidSimple | |
| 91 // instead. | |
| 92 const size_t size = 64 * 1024 * 1024; | 91 const size_t size = 64 * 1024 * 1024; |
| 93 scoped_ptr<DiscardableMemory> memory(allocator_.Allocate(size)); | 92 scoped_ptr<DiscardableAshmemChunk> memory(allocator_.Allocate(size)); |
| 94 ASSERT_TRUE(memory); | 93 ASSERT_TRUE(memory); |
| 95 WriteToDiscardableMemory(memory.get(), size); | 94 WriteToDiscardableAshmemChunk(memory.get(), size); |
| 96 } | 95 } |
| 97 | 96 |
| 98 TEST_F(DiscardableMemoryAllocatorTest, ChunksArePageAligned) { | 97 TEST_F(DiscardableMemoryAllocatorTest, ChunksArePageAligned) { |
| 99 scoped_ptr<DiscardableMemory> memory(allocator_.Allocate(kPageSize)); | 98 scoped_ptr<DiscardableAshmemChunk> memory(allocator_.Allocate(kPageSize)); |
| 100 ASSERT_TRUE(memory); | 99 ASSERT_TRUE(memory); |
| 101 EXPECT_EQ(0U, reinterpret_cast<uint64_t>(memory->Memory()) % kPageSize); | 100 EXPECT_EQ(0U, reinterpret_cast<uint64_t>(memory->Memory()) % kPageSize); |
| 102 WriteToDiscardableMemory(memory.get(), kPageSize); | 101 WriteToDiscardableAshmemChunk(memory.get(), kPageSize); |
| 103 } | 102 } |
| 104 | 103 |
| 105 TEST_F(DiscardableMemoryAllocatorTest, AllocateFreeAllocate) { | 104 TEST_F(DiscardableMemoryAllocatorTest, AllocateFreeAllocate) { |
| 106 scoped_ptr<DiscardableMemory> memory(allocator_.Allocate(kPageSize)); | 105 scoped_ptr<DiscardableAshmemChunk> memory(allocator_.Allocate(kPageSize)); |
| 107 // Extra allocation that prevents the region from being deleted when |memory| | 106 // Extra allocation that prevents the region from being deleted when |memory| |
| 108 // gets deleted. | 107 // gets deleted. |
| 109 scoped_ptr<DiscardableMemory> memory_lock(allocator_.Allocate(kPageSize)); | 108 scoped_ptr<DiscardableAshmemChunk> memory_lock( |
| 109 allocator_.Allocate(kPageSize)); |
| 110 ASSERT_TRUE(memory); | 110 ASSERT_TRUE(memory); |
| 111 void* const address = memory->Memory(); | 111 void* const address = memory->Memory(); |
| 112 memory->Unlock(); // Tests that the reused chunk is being locked correctly. | 112 memory->Unlock(); // Tests that the reused chunk is being locked correctly. |
| 113 memory.reset(); | 113 memory.reset(); |
| 114 memory = allocator_.Allocate(kPageSize); | 114 memory = allocator_.Allocate(kPageSize); |
| 115 ASSERT_TRUE(memory); | 115 ASSERT_TRUE(memory); |
| 116 // The previously freed chunk should be reused. | 116 // The previously freed chunk should be reused. |
| 117 EXPECT_EQ(address, memory->Memory()); | 117 EXPECT_EQ(address, memory->Memory()); |
| 118 WriteToDiscardableMemory(memory.get(), kPageSize); | 118 WriteToDiscardableAshmemChunk(memory.get(), kPageSize); |
| 119 } | 119 } |
| 120 | 120 |
| 121 TEST_F(DiscardableMemoryAllocatorTest, FreeingWholeAshmemRegionClosesAshmem) { | 121 TEST_F(DiscardableMemoryAllocatorTest, FreeingWholeAshmemRegionClosesAshmem) { |
| 122 scoped_ptr<DiscardableMemory> memory(allocator_.Allocate(kPageSize)); | 122 scoped_ptr<DiscardableAshmemChunk> memory(allocator_.Allocate(kPageSize)); |
| 123 ASSERT_TRUE(memory); | 123 ASSERT_TRUE(memory); |
| 124 const int kMagic = 0xdeadbeef; | 124 const int kMagic = 0xdeadbeef; |
| 125 *static_cast<int*>(memory->Memory()) = kMagic; | 125 *static_cast<int*>(memory->Memory()) = kMagic; |
| 126 memory.reset(); | 126 memory.reset(); |
| 127 // The previous ashmem region should have been closed thus it should not be | 127 // The previous ashmem region should have been closed thus it should not be |
| 128 // reused. | 128 // reused. |
| 129 memory = allocator_.Allocate(kPageSize); | 129 memory = allocator_.Allocate(kPageSize); |
| 130 ASSERT_TRUE(memory); | 130 ASSERT_TRUE(memory); |
| 131 EXPECT_NE(kMagic, *static_cast<const int*>(memory->Memory())); | 131 EXPECT_NE(kMagic, *static_cast<const int*>(memory->Memory())); |
| 132 } | 132 } |
| 133 | 133 |
| 134 TEST_F(DiscardableMemoryAllocatorTest, AllocateUsesBestFitAlgorithm) { | 134 TEST_F(DiscardableMemoryAllocatorTest, AllocateUsesBestFitAlgorithm) { |
| 135 scoped_ptr<DiscardableMemory> memory1(allocator_.Allocate(3 * kPageSize)); | 135 scoped_ptr<DiscardableAshmemChunk> memory1( |
| 136 allocator_.Allocate(3 * kPageSize)); |
| 136 ASSERT_TRUE(memory1); | 137 ASSERT_TRUE(memory1); |
| 137 scoped_ptr<DiscardableMemory> memory2(allocator_.Allocate(2 * kPageSize)); | 138 scoped_ptr<DiscardableAshmemChunk> memory2( |
| 139 allocator_.Allocate(2 * kPageSize)); |
| 138 ASSERT_TRUE(memory2); | 140 ASSERT_TRUE(memory2); |
| 139 scoped_ptr<DiscardableMemory> memory3(allocator_.Allocate(1 * kPageSize)); | 141 scoped_ptr<DiscardableAshmemChunk> memory3( |
| 142 allocator_.Allocate(1 * kPageSize)); |
| 140 ASSERT_TRUE(memory3); | 143 ASSERT_TRUE(memory3); |
| 141 void* const address_3 = memory3->Memory(); | 144 void* const address_3 = memory3->Memory(); |
| 142 memory1.reset(); | 145 memory1.reset(); |
| 143 // Don't free |memory2| to avoid merging the 3 blocks together. | 146 // Don't free |memory2| to avoid merging the 3 blocks together. |
| 144 memory3.reset(); | 147 memory3.reset(); |
| 145 memory1 = allocator_.Allocate(1 * kPageSize); | 148 memory1 = allocator_.Allocate(1 * kPageSize); |
| 146 ASSERT_TRUE(memory1); | 149 ASSERT_TRUE(memory1); |
| 147 // The chunk whose size is closest to the requested size should be reused. | 150 // The chunk whose size is closest to the requested size should be reused. |
| 148 EXPECT_EQ(address_3, memory1->Memory()); | 151 EXPECT_EQ(address_3, memory1->Memory()); |
| 149 WriteToDiscardableMemory(memory1.get(), kPageSize); | 152 WriteToDiscardableAshmemChunk(memory1.get(), kPageSize); |
| 150 } | 153 } |
| 151 | 154 |
| 152 TEST_F(DiscardableMemoryAllocatorTest, MergeFreeChunks) { | 155 TEST_F(DiscardableMemoryAllocatorTest, MergeFreeChunks) { |
| 153 scoped_ptr<DiscardableMemory> memory1(allocator_.Allocate(kPageSize)); | 156 scoped_ptr<DiscardableAshmemChunk> memory1(allocator_.Allocate(kPageSize)); |
| 154 ASSERT_TRUE(memory1); | 157 ASSERT_TRUE(memory1); |
| 155 scoped_ptr<DiscardableMemory> memory2(allocator_.Allocate(kPageSize)); | 158 scoped_ptr<DiscardableAshmemChunk> memory2(allocator_.Allocate(kPageSize)); |
| 156 ASSERT_TRUE(memory2); | 159 ASSERT_TRUE(memory2); |
| 157 scoped_ptr<DiscardableMemory> memory3(allocator_.Allocate(kPageSize)); | 160 scoped_ptr<DiscardableAshmemChunk> memory3(allocator_.Allocate(kPageSize)); |
| 158 ASSERT_TRUE(memory3); | 161 ASSERT_TRUE(memory3); |
| 159 scoped_ptr<DiscardableMemory> memory4(allocator_.Allocate(kPageSize)); | 162 scoped_ptr<DiscardableAshmemChunk> memory4(allocator_.Allocate(kPageSize)); |
| 160 ASSERT_TRUE(memory4); | 163 ASSERT_TRUE(memory4); |
| 161 void* const memory1_address = memory1->Memory(); | 164 void* const memory1_address = memory1->Memory(); |
| 162 memory1.reset(); | 165 memory1.reset(); |
| 163 memory3.reset(); | 166 memory3.reset(); |
| 164 // Freeing |memory2| (located between memory1 and memory3) should merge the | 167 // Freeing |memory2| (located between memory1 and memory3) should merge the |
| 165 // three free blocks together. | 168 // three free blocks together. |
| 166 memory2.reset(); | 169 memory2.reset(); |
| 167 memory1 = allocator_.Allocate(3 * kPageSize); | 170 memory1 = allocator_.Allocate(3 * kPageSize); |
| 168 EXPECT_EQ(memory1_address, memory1->Memory()); | 171 EXPECT_EQ(memory1_address, memory1->Memory()); |
| 169 } | 172 } |
| 170 | 173 |
| 171 TEST_F(DiscardableMemoryAllocatorTest, MergeFreeChunksAdvanced) { | 174 TEST_F(DiscardableMemoryAllocatorTest, MergeFreeChunksAdvanced) { |
| 172 scoped_ptr<DiscardableMemory> memory1(allocator_.Allocate(4 * kPageSize)); | 175 scoped_ptr<DiscardableAshmemChunk> memory1( |
| 176 allocator_.Allocate(4 * kPageSize)); |
| 173 ASSERT_TRUE(memory1); | 177 ASSERT_TRUE(memory1); |
| 174 scoped_ptr<DiscardableMemory> memory2(allocator_.Allocate(4 * kPageSize)); | 178 scoped_ptr<DiscardableAshmemChunk> memory2( |
| 179 allocator_.Allocate(4 * kPageSize)); |
| 175 ASSERT_TRUE(memory2); | 180 ASSERT_TRUE(memory2); |
| 176 void* const memory1_address = memory1->Memory(); | 181 void* const memory1_address = memory1->Memory(); |
| 177 memory1.reset(); | 182 memory1.reset(); |
| 178 memory1 = allocator_.Allocate(2 * kPageSize); | 183 memory1 = allocator_.Allocate(2 * kPageSize); |
| 179 memory2.reset(); | 184 memory2.reset(); |
| 180 // At this point, the region should be in this state: | 185 // At this point, the region should be in this state: |
| 181 // 8 KBytes (used), 24 KBytes (free). | 186 // 8 KBytes (used), 24 KBytes (free). |
| 182 memory2 = allocator_.Allocate(6 * kPageSize); | 187 memory2 = allocator_.Allocate(6 * kPageSize); |
| 183 EXPECT_EQ( | 188 EXPECT_EQ( |
| 184 static_cast<const char*>(memory2->Memory()), | 189 static_cast<const char*>(memory2->Memory()), |
| 185 static_cast<const char*>(memory1_address) + 2 * kPageSize); | 190 static_cast<const char*>(memory1_address) + 2 * kPageSize); |
| 186 } | 191 } |
| 187 | 192 |
| 188 TEST_F(DiscardableMemoryAllocatorTest, MergeFreeChunksAdvanced2) { | 193 TEST_F(DiscardableMemoryAllocatorTest, MergeFreeChunksAdvanced2) { |
| 189 scoped_ptr<DiscardableMemory> memory1(allocator_.Allocate(4 * kPageSize)); | 194 scoped_ptr<DiscardableAshmemChunk> memory1( |
| 195 allocator_.Allocate(4 * kPageSize)); |
| 190 ASSERT_TRUE(memory1); | 196 ASSERT_TRUE(memory1); |
| 191 scoped_ptr<DiscardableMemory> memory2(allocator_.Allocate(4 * kPageSize)); | 197 scoped_ptr<DiscardableAshmemChunk> memory2( |
| 198 allocator_.Allocate(4 * kPageSize)); |
| 192 ASSERT_TRUE(memory2); | 199 ASSERT_TRUE(memory2); |
| 193 void* const memory1_address = memory1->Memory(); | 200 void* const memory1_address = memory1->Memory(); |
| 194 memory1.reset(); | 201 memory1.reset(); |
| 195 memory1 = allocator_.Allocate(2 * kPageSize); | 202 memory1 = allocator_.Allocate(2 * kPageSize); |
| 196 scoped_ptr<DiscardableMemory> memory3(allocator_.Allocate(2 * kPageSize)); | 203 scoped_ptr<DiscardableAshmemChunk> memory3( |
| 204 allocator_.Allocate(2 * kPageSize)); |
| 197 // At this point, the region should be in this state: | 205 // At this point, the region should be in this state: |
| 198 // 8 KBytes (used), 8 KBytes (used), 16 KBytes (used). | 206 // 8 KBytes (used), 8 KBytes (used), 16 KBytes (used). |
| 199 memory3.reset(); | 207 memory3.reset(); |
| 200 memory2.reset(); | 208 memory2.reset(); |
| 201 // At this point, the region should be in this state: | 209 // At this point, the region should be in this state: |
| 202 // 8 KBytes (used), 24 KBytes (free). | 210 // 8 KBytes (used), 24 KBytes (free). |
| 203 memory2 = allocator_.Allocate(6 * kPageSize); | 211 memory2 = allocator_.Allocate(6 * kPageSize); |
| 204 EXPECT_EQ( | 212 EXPECT_EQ( |
| 205 static_cast<const char*>(memory2->Memory()), | 213 static_cast<const char*>(memory2->Memory()), |
| 206 static_cast<const char*>(memory1_address) + 2 * kPageSize); | 214 static_cast<const char*>(memory1_address) + 2 * kPageSize); |
| 207 } | 215 } |
| 208 | 216 |
| 209 TEST_F(DiscardableMemoryAllocatorTest, MergeFreeChunksAndDeleteAshmemRegion) { | 217 TEST_F(DiscardableMemoryAllocatorTest, MergeFreeChunksAndDeleteAshmemRegion) { |
| 210 scoped_ptr<DiscardableMemory> memory1(allocator_.Allocate(4 * kPageSize)); | 218 scoped_ptr<DiscardableAshmemChunk> memory1( |
| 219 allocator_.Allocate(4 * kPageSize)); |
| 211 ASSERT_TRUE(memory1); | 220 ASSERT_TRUE(memory1); |
| 212 scoped_ptr<DiscardableMemory> memory2(allocator_.Allocate(4 * kPageSize)); | 221 scoped_ptr<DiscardableAshmemChunk> memory2( |
| 222 allocator_.Allocate(4 * kPageSize)); |
| 213 ASSERT_TRUE(memory2); | 223 ASSERT_TRUE(memory2); |
| 214 memory1.reset(); | 224 memory1.reset(); |
| 215 memory1 = allocator_.Allocate(2 * kPageSize); | 225 memory1 = allocator_.Allocate(2 * kPageSize); |
| 216 scoped_ptr<DiscardableMemory> memory3(allocator_.Allocate(2 * kPageSize)); | 226 scoped_ptr<DiscardableAshmemChunk> memory3( |
| 227 allocator_.Allocate(2 * kPageSize)); |
| 217 // At this point, the region should be in this state: | 228 // At this point, the region should be in this state: |
| 218 // 8 KBytes (used), 8 KBytes (used), 16 KBytes (used). | 229 // 8 KBytes (used), 8 KBytes (used), 16 KBytes (used). |
| 219 memory1.reset(); | 230 memory1.reset(); |
| 220 memory3.reset(); | 231 memory3.reset(); |
| 221 // At this point, the region should be in this state: | 232 // At this point, the region should be in this state: |
| 222 // 8 KBytes (free), 8 KBytes (used), 8 KBytes (free). | 233 // 8 KBytes (free), 8 KBytes (used), 8 KBytes (free). |
| 223 const int kMagic = 0xdeadbeef; | 234 const int kMagic = 0xdeadbeef; |
| 224 *static_cast<int*>(memory2->Memory()) = kMagic; | 235 *static_cast<int*>(memory2->Memory()) = kMagic; |
| 225 memory2.reset(); | 236 memory2.reset(); |
| 226 // The whole region should have been deleted. | 237 // The whole region should have been deleted. |
| 227 memory2 = allocator_.Allocate(2 * kPageSize); | 238 memory2 = allocator_.Allocate(2 * kPageSize); |
| 228 EXPECT_NE(kMagic, *static_cast<int*>(memory2->Memory())); | 239 EXPECT_NE(kMagic, *static_cast<int*>(memory2->Memory())); |
| 229 } | 240 } |
| 230 | 241 |
| 231 TEST_F(DiscardableMemoryAllocatorTest, | 242 TEST_F(DiscardableMemoryAllocatorTest, |
| 232 TooLargeFreeChunksDontCauseTooMuchFragmentationWhenRecycled) { | 243 TooLargeFreeChunksDontCauseTooMuchFragmentationWhenRecycled) { |
| 233 // Keep |memory_1| below allocated so that the ashmem region doesn't get | 244 // Keep |memory_1| below allocated so that the ashmem region doesn't get |
| 234 // closed when |memory_2| is deleted. | 245 // closed when |memory_2| is deleted. |
| 235 scoped_ptr<DiscardableMemory> memory_1(allocator_.Allocate(64 * 1024)); | 246 scoped_ptr<DiscardableAshmemChunk> memory_1(allocator_.Allocate(64 * 1024)); |
| 236 ASSERT_TRUE(memory_1); | 247 ASSERT_TRUE(memory_1); |
| 237 scoped_ptr<DiscardableMemory> memory_2(allocator_.Allocate(32 * 1024)); | 248 scoped_ptr<DiscardableAshmemChunk> memory_2(allocator_.Allocate(32 * 1024)); |
| 238 ASSERT_TRUE(memory_2); | 249 ASSERT_TRUE(memory_2); |
| 239 void* const address = memory_2->Memory(); | 250 void* const address = memory_2->Memory(); |
| 240 memory_2.reset(); | 251 memory_2.reset(); |
| 241 const size_t size = 16 * 1024; | 252 const size_t size = 16 * 1024; |
| 242 memory_2 = allocator_.Allocate(size); | 253 memory_2 = allocator_.Allocate(size); |
| 243 ASSERT_TRUE(memory_2); | 254 ASSERT_TRUE(memory_2); |
| 244 EXPECT_EQ(address, memory_2->Memory()); | 255 EXPECT_EQ(address, memory_2->Memory()); |
| 245 WriteToDiscardableMemory(memory_2.get(), size); | 256 WriteToDiscardableAshmemChunk(memory_2.get(), size); |
| 246 scoped_ptr<DiscardableMemory> memory_3(allocator_.Allocate(size)); | 257 scoped_ptr<DiscardableAshmemChunk> memory_3(allocator_.Allocate(size)); |
| 247 // The unused tail (16 KBytes large) of the previously freed chunk should be | 258 // The unused tail (16 KBytes large) of the previously freed chunk should be |
| 248 // reused. | 259 // reused. |
| 249 EXPECT_EQ(static_cast<char*>(address) + size, memory_3->Memory()); | 260 EXPECT_EQ(static_cast<char*>(address) + size, memory_3->Memory()); |
| 250 WriteToDiscardableMemory(memory_3.get(), size); | 261 WriteToDiscardableAshmemChunk(memory_3.get(), size); |
| 251 } | 262 } |
| 252 | 263 |
| 253 TEST_F(DiscardableMemoryAllocatorTest, UseMultipleAshmemRegions) { | 264 TEST_F(DiscardableMemoryAllocatorTest, UseMultipleAshmemRegions) { |
| 254 // Leave one page untouched at the end of the ashmem region. | 265 // Leave one page untouched at the end of the ashmem region. |
| 255 const size_t size = kAshmemRegionSizeForTesting - kPageSize; | 266 const size_t size = kAshmemRegionSizeForTesting - kPageSize; |
| 256 scoped_ptr<DiscardableMemory> memory1(allocator_.Allocate(size)); | 267 scoped_ptr<DiscardableAshmemChunk> memory1(allocator_.Allocate(size)); |
| 257 ASSERT_TRUE(memory1); | 268 ASSERT_TRUE(memory1); |
| 258 WriteToDiscardableMemory(memory1.get(), size); | 269 WriteToDiscardableAshmemChunk(memory1.get(), size); |
| 259 | 270 |
| 260 scoped_ptr<DiscardableMemory> memory2( | 271 scoped_ptr<DiscardableAshmemChunk> memory2( |
| 261 allocator_.Allocate(kAshmemRegionSizeForTesting)); | 272 allocator_.Allocate(kAshmemRegionSizeForTesting)); |
| 262 ASSERT_TRUE(memory2); | 273 ASSERT_TRUE(memory2); |
| 263 WriteToDiscardableMemory(memory2.get(), kAshmemRegionSizeForTesting); | 274 WriteToDiscardableAshmemChunk(memory2.get(), kAshmemRegionSizeForTesting); |
| 264 // The last page of the first ashmem region should be used for this | 275 // The last page of the first ashmem region should be used for this |
| 265 // allocation. | 276 // allocation. |
| 266 scoped_ptr<DiscardableMemory> memory3(allocator_.Allocate(kPageSize)); | 277 scoped_ptr<DiscardableAshmemChunk> memory3(allocator_.Allocate(kPageSize)); |
| 267 ASSERT_TRUE(memory3); | 278 ASSERT_TRUE(memory3); |
| 268 WriteToDiscardableMemory(memory3.get(), kPageSize); | 279 WriteToDiscardableAshmemChunk(memory3.get(), kPageSize); |
| 269 EXPECT_EQ(memory3->Memory(), static_cast<char*>(memory1->Memory()) + size); | 280 EXPECT_EQ(memory3->Memory(), static_cast<char*>(memory1->Memory()) + size); |
| 270 } | 281 } |
| 271 | 282 |
| 272 TEST_F(DiscardableMemoryAllocatorTest, | 283 TEST_F(DiscardableMemoryAllocatorTest, |
| 273 HighestAllocatedChunkPointerIsUpdatedWhenHighestChunkGetsSplit) { | 284 HighestAllocatedChunkPointerIsUpdatedWhenHighestChunkGetsSplit) { |
| 274 // Prevents the ashmem region from getting closed when |memory2| gets freed. | 285 // Prevents the ashmem region from getting closed when |memory2| gets freed. |
| 275 scoped_ptr<DiscardableMemory> memory1(allocator_.Allocate(kPageSize)); | 286 scoped_ptr<DiscardableAshmemChunk> memory1(allocator_.Allocate(kPageSize)); |
| 276 ASSERT_TRUE(memory1); | 287 ASSERT_TRUE(memory1); |
| 277 | 288 |
| 278 scoped_ptr<DiscardableMemory> memory2(allocator_.Allocate(4 * kPageSize)); | 289 scoped_ptr<DiscardableAshmemChunk> memory2( |
| 290 allocator_.Allocate(4 * kPageSize)); |
| 279 ASSERT_TRUE(memory2); | 291 ASSERT_TRUE(memory2); |
| 280 | 292 |
| 281 memory2.reset(); | 293 memory2.reset(); |
| 282 memory2 = allocator_.Allocate(kPageSize); | 294 memory2 = allocator_.Allocate(kPageSize); |
| 283 // There should now be a free chunk of size 3 * |kPageSize| starting at offset | 295 // There should now be a free chunk of size 3 * |kPageSize| starting at offset |
| 284 // 2 * |kPageSize| and the pointer to the highest allocated chunk should have | 296 // 2 * |kPageSize| and the pointer to the highest allocated chunk should have |
| 285 // also been updated to |base_| + 2 * |kPageSize|. This pointer is used to | 297 // also been updated to |base_| + 2 * |kPageSize|. This pointer is used to |
| 286 // maintain the container mapping a chunk address to its previous chunk and | 298 // maintain the container mapping a chunk address to its previous chunk and |
| 287 // this map is in turn used while merging previous contiguous chunks. | 299 // this map is in turn used while merging previous contiguous chunks. |
| 288 | 300 |
| 289 // Allocate more than 3 * |kPageSize| so that the free chunk of size 3 * | 301 // Allocate more than 3 * |kPageSize| so that the free chunk of size 3 * |
| 290 // |kPageSize| is not reused and |highest_allocated_chunk_| gets used instead. | 302 // |kPageSize| is not reused and |highest_allocated_chunk_| gets used instead. |
| 291 scoped_ptr<DiscardableMemory> memory3(allocator_.Allocate(4 * kPageSize)); | 303 scoped_ptr<DiscardableAshmemChunk> memory3( |
| 304 allocator_.Allocate(4 * kPageSize)); |
| 292 ASSERT_TRUE(memory3); | 305 ASSERT_TRUE(memory3); |
| 293 | 306 |
| 294 // Deleting |memory3| (whose size is 4 * |kPageSize|) should result in a merge | 307 // Deleting |memory3| (whose size is 4 * |kPageSize|) should result in a merge |
| 295 // with its previous chunk which is the free chunk of size |3 * kPageSize|. | 308 // with its previous chunk which is the free chunk of size |3 * kPageSize|. |
| 296 memory3.reset(); | 309 memory3.reset(); |
| 297 memory3 = allocator_.Allocate((3 + 4) * kPageSize); | 310 memory3 = allocator_.Allocate((3 + 4) * kPageSize); |
| 298 EXPECT_EQ(memory3->Memory(), | 311 EXPECT_EQ(memory3->Memory(), |
| 299 static_cast<const char*>(memory2->Memory()) + kPageSize); | 312 static_cast<const char*>(memory2->Memory()) + kPageSize); |
| 300 } | 313 } |
| 301 | 314 |
| 302 } // namespace internal | 315 } // namespace internal |
| 303 } // namespace base | 316 } // namespace base |
| OLD | NEW |