base: Change DCHECK_IS_ON to a macro DCHECK_IS_ON().
[chromium-blink-merge.git] / content / common / discardable_shared_memory_heap_unittest.cc
blob2ab736cc7046cf67c04d1159522449d390447356
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.
5 #include "content/common/discardable_shared_memory_heap.h"
7 #include "base/memory/discardable_shared_memory.h"
8 #include "base/process/process_metrics.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace content {
12 namespace {
14 class DiscardableSharedMemoryHeapTest : public testing::Test {};
16 TEST_F(DiscardableSharedMemoryHeapTest, Basic) {
17 size_t block_size = base::GetPageSize();
18 DiscardableSharedMemoryHeap heap(block_size);
20 // Free list is initially empty.
21 EXPECT_FALSE(heap.SearchFreeList(1));
23 const size_t kBlocks = 10;
24 size_t memory_size = block_size * kBlocks;
26 scoped_ptr<base::DiscardableSharedMemory> memory(
27 new base::DiscardableSharedMemory);
28 ASSERT_TRUE(memory->CreateAndMap(memory_size));
30 // Create new span for memory.
31 scoped_ptr<DiscardableSharedMemoryHeap::Span> new_span(
32 heap.Grow(memory.Pass(), memory_size));
34 // Free list should still be empty as |new_span| is currently in use.
35 EXPECT_FALSE(heap.SearchFreeList(1));
37 // Done using |new_span|. Merge it into the free list.
38 heap.MergeIntoFreeList(new_span.Pass());
40 // Free list should not contain a span that is larger than kBlocks.
41 EXPECT_FALSE(heap.SearchFreeList(kBlocks + 1));
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);
48 // Delete span and shared memory backing.
49 heap.DeleteSpan(span.Pass());
51 // Free list should be empty again.
52 EXPECT_FALSE(heap.SearchFreeList(1));
55 TEST_F(DiscardableSharedMemoryHeapTest, SplitAndMerge) {
56 size_t block_size = base::GetPageSize();
57 DiscardableSharedMemoryHeap heap(block_size);
59 const size_t kBlocks = 6;
60 size_t memory_size = block_size * kBlocks;
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));
68 // Split span into two.
69 scoped_ptr<DiscardableSharedMemoryHeap::Span> leftover =
70 heap.Split(new_span.get(), 3);
71 ASSERT_TRUE(leftover);
73 // Merge |leftover| into free list.
74 heap.MergeIntoFreeList(leftover.Pass());
76 // Some of the memory is still in use.
77 EXPECT_FALSE(heap.SearchFreeList(kBlocks));
79 // Merge |span| into free list.
80 heap.MergeIntoFreeList(new_span.Pass());
82 // Remove a 2 page span from free list.
83 scoped_ptr<DiscardableSharedMemoryHeap::Span> span1 = heap.SearchFreeList(2);
84 ASSERT_TRUE(span1);
86 // Remove another 2 page span from free list.
87 scoped_ptr<DiscardableSharedMemoryHeap::Span> span2 = heap.SearchFreeList(2);
88 ASSERT_TRUE(span2);
90 // Merge |span1| back into free list.
91 heap.MergeIntoFreeList(span1.Pass());
93 // Some of the memory is still in use.
94 EXPECT_FALSE(heap.SearchFreeList(kBlocks));
96 // Merge |span2| back into free list.
97 heap.MergeIntoFreeList(span2.Pass());
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);
105 TEST_F(DiscardableSharedMemoryHeapTest, Grow) {
106 size_t block_size = base::GetPageSize();
107 DiscardableSharedMemoryHeap heap(block_size);
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());
114 // Remove a span from free list.
115 scoped_ptr<DiscardableSharedMemoryHeap::Span> span1 = heap.SearchFreeList(1);
116 EXPECT_TRUE(span1);
118 // No more memory available.
119 EXPECT_FALSE(heap.SearchFreeList(1));
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());
127 // Memory should now be available.
128 scoped_ptr<DiscardableSharedMemoryHeap::Span> span2 = heap.SearchFreeList(1);
129 EXPECT_TRUE(span2);
132 } // namespace
133 } // namespace content