diff options
author | reveman <reveman@chromium.org> | 2015-03-16 12:06:21 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-03-16 19:06:57 +0000 |
commit | 2799f7fb3d3a2406b1608f0b9bc470ff76ad4ea0 (patch) | |
tree | 3341fbe69860daf6f75b67076a73642caa7d895d /base/memory | |
parent | 42f59e67e6c4d58a913d48b3a81600cdcd3ced88 (diff) | |
download | chromium_src-2799f7fb3d3a2406b1608f0b9bc470ff76ad4ea0.zip chromium_src-2799f7fb3d3a2406b1608f0b9bc470ff76ad4ea0.tar.gz chromium_src-2799f7fb3d3a2406b1608f0b9bc470ff76ad4ea0.tar.bz2 |
Re-land: base: Implement browser process support for discardable memory.
This implements support for in-process discardable
memory. Each in-process discardable memory instance is
associated with a discardable shared memory segment. This
is hopefully efficient enough with the limited use of
discardable memory in the browser process.
DiscardableSharedMemoryHeap can be used if this is not
enough.
This also moves the test implementation of the discardable
memory allocator into a TestDiscardableMemoryShmemAllocator
class and requires tests that use discardable memory to
explicitly use this test allocator. Browser tests uses the
real discardable memory allocator.
BUG=422953,463250
Review URL: https://codereview.chromium.org/1004043002
Cr-Commit-Position: refs/heads/master@{#320764}
Diffstat (limited to 'base/memory')
-rw-r--r-- | base/memory/discardable_memory_shmem_allocator.cc | 43 | ||||
-rw-r--r-- | base/memory/discardable_memory_unittest.cc | 64 |
2 files changed, 1 insertions, 106 deletions
diff --git a/base/memory/discardable_memory_shmem_allocator.cc b/base/memory/discardable_memory_shmem_allocator.cc index 761204f..a87c58d 100644 --- a/base/memory/discardable_memory_shmem_allocator.cc +++ b/base/memory/discardable_memory_shmem_allocator.cc @@ -11,45 +11,6 @@ namespace base { namespace { -class DiscardableMemoryShmemChunkImpl : public DiscardableMemoryShmemChunk { - public: - explicit DiscardableMemoryShmemChunkImpl( - scoped_ptr<DiscardableSharedMemory> shared_memory) - : shared_memory_(shared_memory.Pass()) {} - - // Overridden from DiscardableMemoryShmemChunk: - bool Lock() override { return false; } - void Unlock() override { - shared_memory_->Unlock(0, 0); - shared_memory_.reset(); - } - void* Memory() const override { return shared_memory_->memory(); } - - private: - scoped_ptr<DiscardableSharedMemory> shared_memory_; - - DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryShmemChunkImpl); -}; - -// Default allocator implementation that allocates in-process -// DiscardableSharedMemory instances. -class DiscardableMemoryShmemAllocatorImpl - : public DiscardableMemoryShmemAllocator { - public: - // Overridden from DiscardableMemoryShmemAllocator: - scoped_ptr<DiscardableMemoryShmemChunk> - AllocateLockedDiscardableMemory(size_t size) override { - scoped_ptr<DiscardableSharedMemory> memory(new DiscardableSharedMemory); - if (!memory->CreateAndMap(size)) - return nullptr; - - return make_scoped_ptr(new DiscardableMemoryShmemChunkImpl(memory.Pass())); - } -}; - -LazyInstance<DiscardableMemoryShmemAllocatorImpl>::Leaky g_default_allocator = - LAZY_INSTANCE_INITIALIZER; - DiscardableMemoryShmemAllocator* g_allocator = nullptr; } // namespace @@ -69,9 +30,7 @@ void DiscardableMemoryShmemAllocator::SetInstance( // static DiscardableMemoryShmemAllocator* DiscardableMemoryShmemAllocator::GetInstance() { - if (!g_allocator) - g_allocator = g_default_allocator.Pointer(); - + DCHECK(g_allocator); return g_allocator; } diff --git a/base/memory/discardable_memory_unittest.cc b/base/memory/discardable_memory_unittest.cc deleted file mode 100644 index 60750746..0000000 --- a/base/memory/discardable_memory_unittest.cc +++ /dev/null @@ -1,64 +0,0 @@ -// Copyright (c) 2013 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "base/memory/discardable_memory.h" - -#include "testing/gtest/include/gtest/gtest.h" - -namespace base { -namespace { - -const size_t kSize = 1024; - -// Test Lock() and Unlock() functionalities. -TEST(DiscardableMemoryTest, LockAndUnLock) { - const scoped_ptr<DiscardableMemory> memory( - DiscardableMemory::CreateLockedMemory(kSize)); - ASSERT_TRUE(memory); - void* addr = memory->Memory(); - EXPECT_NE(nullptr, addr); - memory->Unlock(); -} - -// Test delete a discardable memory while it is locked. -TEST(DiscardableMemoryTest, DeleteWhileLocked) { - const scoped_ptr<DiscardableMemory> memory( - DiscardableMemory::CreateLockedMemory(kSize)); - ASSERT_TRUE(memory); -} - -#if !defined(NDEBUG) && !defined(OS_ANDROID) -// Death tests are not supported with Android APKs. -TEST(DiscardableMemoryTest, UnlockedMemoryAccessCrashesInDebugMode) { - const scoped_ptr<DiscardableMemory> memory( - DiscardableMemory::CreateLockedMemory(kSize)); - ASSERT_TRUE(memory); - memory->Unlock(); - ASSERT_DEATH_IF_SUPPORTED( - { *static_cast<int*>(memory->Memory()) = 0xdeadbeef; }, ".*"); -} -#endif - -// Test behavior when creating enough instances that could use up a 32-bit -// address space. -// This is disabled under AddressSanitizer on Windows as it crashes (by design) -// on OOM. See http://llvm.org/PR22026 for the details. -#if !defined(ADDRESS_SANITIZER) || !defined(OS_WIN) -TEST(DiscardableMemoryTest, AddressSpace) { - const size_t kLargeSize = 4 * 1024 * 1024; // 4MiB. - const size_t kNumberOfInstances = 1024 + 1; // >4GiB total. - - scoped_ptr<DiscardableMemory> instances[kNumberOfInstances]; - for (auto& memory : instances) { - memory = DiscardableMemory::CreateLockedMemory(kLargeSize); - ASSERT_TRUE(memory); - void* addr = memory->Memory(); - EXPECT_NE(nullptr, addr); - memory->Unlock(); - } -} -#endif - -} // namespace -} // namespace base |