OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/common/discardable_shared_memory_heap.h" | 5 #include "content/common/discardable_shared_memory_heap.h" |
6 | 6 |
7 #include "base/memory/discardable_shared_memory.h" | 7 #include "base/memory/discardable_shared_memory.h" |
8 #include "base/process/process_metrics.h" | 8 #include "base/process/process_metrics.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 | 98 |
99 // All memory has been returned to the free list. | 99 // All memory has been returned to the free list. |
100 scoped_ptr<DiscardableSharedMemoryHeap::Span> large_span = | 100 scoped_ptr<DiscardableSharedMemoryHeap::Span> large_span = |
101 heap.SearchFreeList(kBlocks); | 101 heap.SearchFreeList(kBlocks); |
102 ASSERT_TRUE(large_span); | 102 ASSERT_TRUE(large_span); |
103 | 103 |
104 // Merge it into the free list again. | 104 // Merge it into the free list again. |
105 heap.MergeIntoFreeList(large_span.Pass()); | 105 heap.MergeIntoFreeList(large_span.Pass()); |
106 } | 106 } |
107 | 107 |
| 108 TEST_F(DiscardableSharedMemoryHeapTest, MergeSingleBlockSpan) { |
| 109 size_t block_size = base::GetPageSize(); |
| 110 DiscardableSharedMemoryHeap heap(block_size); |
| 111 |
| 112 const size_t kBlocks = 6; |
| 113 size_t memory_size = block_size * kBlocks; |
| 114 |
| 115 scoped_ptr<base::DiscardableSharedMemory> memory( |
| 116 new base::DiscardableSharedMemory); |
| 117 ASSERT_TRUE(memory->CreateAndMap(memory_size)); |
| 118 scoped_ptr<DiscardableSharedMemoryHeap::Span> new_span( |
| 119 heap.Grow(memory.Pass(), memory_size)); |
| 120 |
| 121 // Split span into two. |
| 122 scoped_ptr<DiscardableSharedMemoryHeap::Span> leftover = |
| 123 heap.Split(new_span.get(), 5); |
| 124 ASSERT_TRUE(leftover); |
| 125 |
| 126 // Merge |new_span| into free list. |
| 127 heap.MergeIntoFreeList(new_span.Pass()); |
| 128 |
| 129 // Merge |leftover| into free list. |
| 130 heap.MergeIntoFreeList(leftover.Pass()); |
| 131 } |
| 132 |
108 TEST_F(DiscardableSharedMemoryHeapTest, Grow) { | 133 TEST_F(DiscardableSharedMemoryHeapTest, Grow) { |
109 size_t block_size = base::GetPageSize(); | 134 size_t block_size = base::GetPageSize(); |
110 DiscardableSharedMemoryHeap heap(block_size); | 135 DiscardableSharedMemoryHeap heap(block_size); |
111 | 136 |
112 scoped_ptr<base::DiscardableSharedMemory> memory1( | 137 scoped_ptr<base::DiscardableSharedMemory> memory1( |
113 new base::DiscardableSharedMemory); | 138 new base::DiscardableSharedMemory); |
114 ASSERT_TRUE(memory1->CreateAndMap(block_size)); | 139 ASSERT_TRUE(memory1->CreateAndMap(block_size)); |
115 heap.MergeIntoFreeList(heap.Grow(memory1.Pass(), block_size).Pass()); | 140 heap.MergeIntoFreeList(heap.Grow(memory1.Pass(), block_size).Pass()); |
116 | 141 |
117 // Remove a span from free list. | 142 // Remove a span from free list. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 bool rv = span->shared_memory()->Purge(base::Time::Now()); | 178 bool rv = span->shared_memory()->Purge(base::Time::Now()); |
154 EXPECT_TRUE(rv); | 179 EXPECT_TRUE(rv); |
155 heap.ReleaseFreeMemory(); | 180 heap.ReleaseFreeMemory(); |
156 | 181 |
157 // Shared memory backing for |span| should be gone. | 182 // Shared memory backing for |span| should be gone. |
158 EXPECT_FALSE(span->shared_memory()); | 183 EXPECT_FALSE(span->shared_memory()); |
159 } | 184 } |
160 | 185 |
161 } // namespace | 186 } // namespace |
162 } // namespace content | 187 } // namespace content |
OLD | NEW |