Update Chrome code to use new WebScreenOrientation types.
[chromium-blink-merge.git] / base / memory / discardable_memory_android.cc
blobfa89c189db7be5170ecf30ea3a2a882c24fc87db
1 // Copyright (c) 2013 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 "base/memory/discardable_memory.h"
7 #include "base/android/sys_utils.h"
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "base/lazy_instance.h"
11 #include "base/logging.h"
12 #include "base/memory/discardable_memory_allocator_android.h"
13 #include "base/memory/discardable_memory_emulated.h"
14 #include "base/memory/discardable_memory_malloc.h"
16 namespace base {
17 namespace {
19 const char kAshmemAllocatorName[] = "DiscardableMemoryAllocator";
21 struct DiscardableMemoryAllocatorWrapper {
22 DiscardableMemoryAllocatorWrapper()
23 : allocator(kAshmemAllocatorName,
24 GetOptimalAshmemRegionSizeForAllocator()) {
27 internal::DiscardableMemoryAllocator allocator;
29 private:
30 // Returns 64 MBytes for a 512 MBytes device, 128 MBytes for 1024 MBytes...
31 static size_t GetOptimalAshmemRegionSizeForAllocator() {
32 // Note that this may do some I/O (without hitting the disk though) so it
33 // should not be called on the critical path.
34 return base::android::SysUtils::AmountOfPhysicalMemoryKB() * 1024 / 8;
38 LazyInstance<DiscardableMemoryAllocatorWrapper>::Leaky g_context =
39 LAZY_INSTANCE_INITIALIZER;
41 } // namespace
43 // static
44 void DiscardableMemory::RegisterMemoryPressureListeners() {
45 internal::DiscardableMemoryEmulated::RegisterMemoryPressureListeners();
48 // static
49 void DiscardableMemory::UnregisterMemoryPressureListeners() {
50 internal::DiscardableMemoryEmulated::UnregisterMemoryPressureListeners();
53 // static
54 void DiscardableMemory::GetSupportedTypes(
55 std::vector<DiscardableMemoryType>* types) {
56 const DiscardableMemoryType supported_types[] = {
57 DISCARDABLE_MEMORY_TYPE_ANDROID,
58 DISCARDABLE_MEMORY_TYPE_EMULATED,
59 DISCARDABLE_MEMORY_TYPE_MALLOC
61 types->assign(supported_types, supported_types + arraysize(supported_types));
64 // static
65 scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemoryWithType(
66 DiscardableMemoryType type, size_t size) {
67 switch (type) {
68 case DISCARDABLE_MEMORY_TYPE_NONE:
69 case DISCARDABLE_MEMORY_TYPE_MAC:
70 return scoped_ptr<DiscardableMemory>();
71 case DISCARDABLE_MEMORY_TYPE_ANDROID: {
72 return g_context.Pointer()->allocator.Allocate(size);
74 case DISCARDABLE_MEMORY_TYPE_EMULATED: {
75 scoped_ptr<internal::DiscardableMemoryEmulated> memory(
76 new internal::DiscardableMemoryEmulated(size));
77 if (!memory->Initialize())
78 return scoped_ptr<DiscardableMemory>();
80 return memory.PassAs<DiscardableMemory>();
82 case DISCARDABLE_MEMORY_TYPE_MALLOC: {
83 scoped_ptr<internal::DiscardableMemoryMalloc> memory(
84 new internal::DiscardableMemoryMalloc(size));
85 if (!memory->Initialize())
86 return scoped_ptr<DiscardableMemory>();
88 return memory.PassAs<DiscardableMemory>();
92 NOTREACHED();
93 return scoped_ptr<DiscardableMemory>();
96 // static
97 bool DiscardableMemory::PurgeForTestingSupported() {
98 return false;
101 // static
102 void DiscardableMemory::PurgeForTesting() {
103 NOTIMPLEMENTED();
106 } // namespace base