1 // Copyright 2015 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.
6 #include "base/command_line.h"
7 #include "content/child/child_discardable_shared_memory_manager.h"
8 #include "content/child/child_thread_impl.h"
9 #include "content/common/host_discardable_shared_memory_manager.h"
10 #include "content/public/common/content_switches.h"
11 #include "content/public/test/content_browser_test.h"
12 #include "content/public/test/content_browser_test_utils.h"
13 #include "content/shell/browser/shell.h"
18 class ChildDiscardableSharedMemoryManagerBrowserTest
19 : public ContentBrowserTest
{
21 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
22 command_line
->AppendSwitch(switches::kSingleProcess
);
25 static void ReleaseFreeMemory() {
26 ChildThreadImpl::current()
27 ->discardable_shared_memory_manager()
28 ->ReleaseFreeMemory();
31 static void AllocateLockedMemory(
33 scoped_ptr
<base::DiscardableMemoryShmemChunk
>* memory
) {
34 *memory
= ChildThreadImpl::current()
35 ->discardable_shared_memory_manager()
36 ->AllocateLockedDiscardableMemory(size
);
39 static void LockMemory(base::DiscardableMemoryShmemChunk
* memory
,
41 *result
= memory
->Lock();
44 static void UnlockMemory(base::DiscardableMemoryShmemChunk
* memory
) {
48 static void FreeMemory(scoped_ptr
<base::DiscardableMemoryShmemChunk
> memory
) {
52 IN_PROC_BROWSER_TEST_F(ChildDiscardableSharedMemoryManagerBrowserTest
,
53 DISABLED_LockMemory
) {
54 const size_t kSize
= 1024 * 1024; // 1MiB.
56 NavigateToURL(shell(), GURL(url::kAboutBlankURL
));
58 scoped_ptr
<base::DiscardableMemoryShmemChunk
> memory
;
59 PostTaskToInProcessRendererAndWait(base::Bind(
60 &ChildDiscardableSharedMemoryManagerBrowserTest::AllocateLockedMemory
,
64 void* addr
= memory
->Memory();
65 ASSERT_NE(nullptr, addr
);
67 PostTaskToInProcessRendererAndWait(
68 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::UnlockMemory
,
71 // Purge all unlocked memory.
72 HostDiscardableSharedMemoryManager::current()->SetMemoryLimit(0);
75 PostTaskToInProcessRendererAndWait(
76 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::LockMemory
,
77 memory
.get(), &result
));
79 // Should fail as memory should have been purged.
82 PostTaskToInProcessRendererAndWait(
83 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::FreeMemory
,
84 base::Passed(&memory
)));
87 IN_PROC_BROWSER_TEST_F(ChildDiscardableSharedMemoryManagerBrowserTest
,
88 DISABLED_AddressSpace
) {
89 const size_t kLargeSize
= 4 * 1024 * 1024; // 4MiB.
90 const size_t kNumberOfInstances
= 1024 + 1; // >4GiB total.
92 NavigateToURL(shell(), GURL(url::kAboutBlankURL
));
94 scoped_ptr
<base::DiscardableMemoryShmemChunk
> instances
[kNumberOfInstances
];
95 for (auto& memory
: instances
) {
96 PostTaskToInProcessRendererAndWait(base::Bind(
97 &ChildDiscardableSharedMemoryManagerBrowserTest::AllocateLockedMemory
,
98 kLargeSize
, &memory
));
100 void* addr
= memory
->Memory();
101 ASSERT_NE(nullptr, addr
);
102 PostTaskToInProcessRendererAndWait(base::Bind(
103 &ChildDiscardableSharedMemoryManagerBrowserTest::UnlockMemory
,
107 for (auto& memory
: instances
) {
108 PostTaskToInProcessRendererAndWait(
109 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::FreeMemory
,
110 base::Passed(&memory
)));