Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(410)

Side by Side Diff: base/memory/discardable_memory_allocation_ashmem_factory_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698