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

Side by Side Diff: content/common/discardable_shared_memory_heap_unittest.cc

Issue 807303002: base: Add free list implementation to browser-wide discardable memory system. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@discardable-shared-memory-ashmem
Patch Set: rebase Created 5 years, 12 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
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "content/common/discardable_shared_memory_heap.h"
6
7 #include "base/memory/discardable_shared_memory.h"
8 #include "base/process/process_metrics.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace content {
12 namespace {
13
14 class DiscardableSharedMemoryHeapTest : public testing::Test {};
15
16 TEST_F(DiscardableSharedMemoryHeapTest, Basic) {
17 size_t block_size = base::GetPageSize();
18 DiscardableSharedMemoryHeap heap(block_size);
19
20 // Free list is initially empty.
21 EXPECT_FALSE(heap.SearchFreeList(1));
22
23 const size_t kBlocks = 10;
24 size_t memory_size = block_size * kBlocks;
25
26 scoped_ptr<base::DiscardableSharedMemory> memory(
27 new base::DiscardableSharedMemory);
28 ASSERT_TRUE(memory->CreateAndMap(memory_size));
29
30 // Create new span for memory.
31 scoped_ptr<DiscardableSharedMemoryHeap::Span> new_span(
32 heap.Grow(memory.Pass(), memory_size));
33
34 // Free list should still be empty as |new_span| is currently in use.
35 EXPECT_FALSE(heap.SearchFreeList(1));
36
37 // Done using |new_span|. Merge it into the free list.
38 heap.MergeIntoFreeList(new_span.Pass());
39
40 // Free list should not contain a span that is larger than kBlocks.
41 EXPECT_FALSE(heap.SearchFreeList(kBlocks + 1));
42
43 // Free list should contain a span that satisfies the request for kBlocks.
44 scoped_ptr<DiscardableSharedMemoryHeap::Span> span =
45 heap.SearchFreeList(kBlocks);
46 ASSERT_TRUE(span);
47
48 // Delete span and shared memory backing.
49 heap.DeleteSpan(span.Pass());
50
51 // Free list should be empty again.
52 EXPECT_FALSE(heap.SearchFreeList(1));
53 }
54
55 TEST_F(DiscardableSharedMemoryHeapTest, SplitAndMerge) {
56 size_t block_size = base::GetPageSize();
57 DiscardableSharedMemoryHeap heap(block_size);
58
59 const size_t kBlocks = 6;
60 size_t memory_size = block_size * kBlocks;
61
62 scoped_ptr<base::DiscardableSharedMemory> memory(
63 new base::DiscardableSharedMemory);
64 ASSERT_TRUE(memory->CreateAndMap(memory_size));
65 scoped_ptr<DiscardableSharedMemoryHeap::Span> new_span(
66 heap.Grow(memory.Pass(), memory_size));
67
68 // Split span into two.
69 scoped_ptr<DiscardableSharedMemoryHeap::Span> leftover =
70 heap.Split(new_span.get(), 3);
71 ASSERT_TRUE(leftover);
72
73 // Merge |leftover| into free list.
74 heap.MergeIntoFreeList(leftover.Pass());
75
76 // Some of the memory is still in use.
77 EXPECT_FALSE(heap.SearchFreeList(kBlocks));
78
79 // Merge |span| into free list.
80 heap.MergeIntoFreeList(new_span.Pass());
81
82 // Remove a 2 page span from free list.
83 scoped_ptr<DiscardableSharedMemoryHeap::Span> span1 = heap.SearchFreeList(2);
84 ASSERT_TRUE(span1);
85
86 // Remove another 2 page span from free list.
87 scoped_ptr<DiscardableSharedMemoryHeap::Span> span2 = heap.SearchFreeList(2);
88 ASSERT_TRUE(span2);
89
90 // Merge |span1| back into free list.
91 heap.MergeIntoFreeList(span1.Pass());
92
93 // Some of the memory is still in use.
94 EXPECT_FALSE(heap.SearchFreeList(kBlocks));
95
96 // Merge |span2| back into free list.
97 heap.MergeIntoFreeList(span2.Pass());
98
99 // All memory has been returned to the free list.
100 scoped_ptr<DiscardableSharedMemoryHeap::Span> large_span =
101 heap.SearchFreeList(kBlocks);
102 EXPECT_TRUE(large_span);
103 }
104
105 TEST_F(DiscardableSharedMemoryHeapTest, Grow) {
106 size_t block_size = base::GetPageSize();
107 DiscardableSharedMemoryHeap heap(block_size);
108
109 scoped_ptr<base::DiscardableSharedMemory> memory1(
110 new base::DiscardableSharedMemory);
111 ASSERT_TRUE(memory1->CreateAndMap(block_size));
112 heap.MergeIntoFreeList(heap.Grow(memory1.Pass(), block_size).Pass());
113
114 // Remove a span from free list.
115 scoped_ptr<DiscardableSharedMemoryHeap::Span> span1 = heap.SearchFreeList(1);
116 EXPECT_TRUE(span1);
117
118 // No more memory available.
119 EXPECT_FALSE(heap.SearchFreeList(1));
120
121 // Grow free list using new memory.
122 scoped_ptr<base::DiscardableSharedMemory> memory2(
123 new base::DiscardableSharedMemory);
124 ASSERT_TRUE(memory2->CreateAndMap(block_size));
125 heap.MergeIntoFreeList(heap.Grow(memory2.Pass(), block_size).Pass());
126
127 // Memory should now be available.
128 scoped_ptr<DiscardableSharedMemoryHeap::Span> span2 = heap.SearchFreeList(1);
129 EXPECT_TRUE(span2);
130 }
131
132 } // namespace
133 } // namespace content
OLDNEW
« no previous file with comments | « content/common/discardable_shared_memory_heap.cc ('k') | content/common/host_discardable_shared_memory_manager.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698