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

Side by Side Diff: base/memory/discardable_memory_android.cc

Issue 195863005: Use DiscardableMemoryManager on Android. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix base.gypi Created 6 years, 8 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/memory/discardable_memory.h" 5 #include "base/memory/discardable_memory.h"
6 6
7 #include "base/android/sys_utils.h" 7 #include "base/android/sys_utils.h"
8 #include "base/basictypes.h" 8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h" 9 #include "base/compiler_specific.h"
10 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/memory/discardable_memory_allocator_android.h" 12 #include "base/memory/discardable_memory_allocator_android.h"
13 #include "base/memory/discardable_memory_ashmem.h"
13 #include "base/memory/discardable_memory_emulated.h" 14 #include "base/memory/discardable_memory_emulated.h"
14 #include "base/memory/discardable_memory_malloc.h" 15 #include "base/memory/discardable_memory_malloc.h"
15 16
16 namespace base { 17 namespace base {
17 namespace { 18 namespace {
18 19
19 const char kAshmemAllocatorName[] = "DiscardableMemoryAllocator"; 20 const char kAshmemAllocatorName[] = "DiscardableMemoryAllocator";
20 21
21 struct DiscardableMemoryAllocatorWrapper { 22 // When ashmem is used, have the DiscardableMemoryManager trigger userspace
22 DiscardableMemoryAllocatorWrapper() 23 // eviction when address space usage gets too high, e.g. 512 MBytes on 32 bit
24 // systems vs 16 GBytes on 64 bits.
25 const size_t kAshmemMaxAddressSpaceUsage =
26 ((sizeof(void*) == 4) ? 512 : 16 * 1024) * 1024 * 1024;
reveman 2014/04/26 00:13:37 I would prefer if used the same constant on 64bit
Philippe 2014/04/28 12:23:14 Done.
27
28 // Holds the state used for ashmem allocations.
29 struct AshmemGlobalContext {
30 AshmemGlobalContext()
23 : allocator(kAshmemAllocatorName, 31 : allocator(kAshmemAllocatorName,
24 GetOptimalAshmemRegionSizeForAllocator()) { 32 GetOptimalAshmemRegionSizeForAllocator()) {
33 manager.SetMemoryLimit(kAshmemMaxAddressSpaceUsage);
25 } 34 }
26 35
27 internal::DiscardableMemoryAllocator allocator; 36 internal::DiscardableMemoryAllocator allocator;
37 internal::DiscardableMemoryManager manager;
28 38
29 private: 39 private:
30 // Returns 64 MBytes for a 512 MBytes device, 128 MBytes for 1024 MBytes... 40 // Returns 64 MBytes for a 512 MBytes device, 128 MBytes for 1024 MBytes...
31 static size_t GetOptimalAshmemRegionSizeForAllocator() { 41 static size_t GetOptimalAshmemRegionSizeForAllocator() {
32 // Note that this may do some I/O (without hitting the disk though) so it 42 // Note that this may do some I/O (without hitting the disk though) so it
33 // should not be called on the critical path. 43 // should not be called on the critical path.
34 return base::android::SysUtils::AmountOfPhysicalMemoryKB() * 1024 / 8; 44 return base::android::SysUtils::AmountOfPhysicalMemoryKB() * 1024 / 8;
35 } 45 }
36 }; 46 };
37 47
38 LazyInstance<DiscardableMemoryAllocatorWrapper>::Leaky g_context = 48 LazyInstance<AshmemGlobalContext>::Leaky g_context = LAZY_INSTANCE_INITIALIZER;
39 LAZY_INSTANCE_INITIALIZER;
40 49
41 } // namespace 50 } // namespace
42 51
43 // static 52 // static
44 void DiscardableMemory::RegisterMemoryPressureListeners() { 53 void DiscardableMemory::RegisterMemoryPressureListeners() {
45 internal::DiscardableMemoryEmulated::RegisterMemoryPressureListeners(); 54 internal::DiscardableMemoryEmulated::RegisterMemoryPressureListeners();
46 } 55 }
47 56
48 // static 57 // static
49 void DiscardableMemory::UnregisterMemoryPressureListeners() { 58 void DiscardableMemory::UnregisterMemoryPressureListeners() {
50 internal::DiscardableMemoryEmulated::UnregisterMemoryPressureListeners(); 59 internal::DiscardableMemoryEmulated::UnregisterMemoryPressureListeners();
51 } 60 }
52 61
53 // static 62 // static
54 void DiscardableMemory::GetSupportedTypes( 63 void DiscardableMemory::GetSupportedTypes(
55 std::vector<DiscardableMemoryType>* types) { 64 std::vector<DiscardableMemoryType>* types) {
56 const DiscardableMemoryType supported_types[] = { 65 const DiscardableMemoryType supported_types[] = {
57 DISCARDABLE_MEMORY_TYPE_ANDROID, 66 DISCARDABLE_MEMORY_TYPE_ANDROID,
reveman 2014/04/26 00:13:37 Please rename this to DISCARDABLE_MEMORY_TYPE_ASHM
Philippe 2014/04/28 12:23:14 Sure.
58 DISCARDABLE_MEMORY_TYPE_EMULATED, 67 DISCARDABLE_MEMORY_TYPE_EMULATED,
59 DISCARDABLE_MEMORY_TYPE_MALLOC 68 DISCARDABLE_MEMORY_TYPE_MALLOC
60 }; 69 };
61 types->assign(supported_types, supported_types + arraysize(supported_types)); 70 types->assign(supported_types, supported_types + arraysize(supported_types));
62 } 71 }
63 72
64 // static 73 // static
65 scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemoryWithType( 74 scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemoryWithType(
66 DiscardableMemoryType type, size_t size) { 75 DiscardableMemoryType type, size_t size) {
67 switch (type) { 76 switch (type) {
68 case DISCARDABLE_MEMORY_TYPE_NONE: 77 case DISCARDABLE_MEMORY_TYPE_NONE:
69 case DISCARDABLE_MEMORY_TYPE_MAC: 78 case DISCARDABLE_MEMORY_TYPE_MAC:
70 return scoped_ptr<DiscardableMemory>(); 79 return scoped_ptr<DiscardableMemory>();
71 case DISCARDABLE_MEMORY_TYPE_ANDROID: { 80 case DISCARDABLE_MEMORY_TYPE_ANDROID: {
72 return g_context.Pointer()->allocator.Allocate(size); 81 AshmemGlobalContext* const global_context = g_context.Pointer();
82 scoped_ptr<internal::DiscardableMemoryAshmem> memory(
83 new internal::DiscardableMemoryAshmem(
84 size, &global_context->allocator, &global_context->manager));
85 if (!memory->Initialize())
86 return scoped_ptr<DiscardableMemory>();
87
88 return memory.PassAs<DiscardableMemory>();
73 } 89 }
74 case DISCARDABLE_MEMORY_TYPE_EMULATED: { 90 case DISCARDABLE_MEMORY_TYPE_EMULATED: {
75 scoped_ptr<internal::DiscardableMemoryEmulated> memory( 91 scoped_ptr<internal::DiscardableMemoryEmulated> memory(
76 new internal::DiscardableMemoryEmulated(size)); 92 new internal::DiscardableMemoryEmulated(size));
77 if (!memory->Initialize()) 93 if (!memory->Initialize())
78 return scoped_ptr<DiscardableMemory>(); 94 return scoped_ptr<DiscardableMemory>();
79 95
80 return memory.PassAs<DiscardableMemory>(); 96 return memory.PassAs<DiscardableMemory>();
81 } 97 }
82 case DISCARDABLE_MEMORY_TYPE_MALLOC: { 98 case DISCARDABLE_MEMORY_TYPE_MALLOC: {
(...skipping 14 matching lines...) Expand all
97 bool DiscardableMemory::PurgeForTestingSupported() { 113 bool DiscardableMemory::PurgeForTestingSupported() {
98 return false; 114 return false;
99 } 115 }
100 116
101 // static 117 // static
102 void DiscardableMemory::PurgeForTesting() { 118 void DiscardableMemory::PurgeForTesting() {
103 NOTIMPLEMENTED(); 119 NOTIMPLEMENTED();
104 } 120 }
105 121
106 } // namespace base 122 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698