OLD | NEW |
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_ashmem.h" | 12 #include "base/memory/discardable_memory_allocator_android.h" |
13 #include "base/memory/discardable_memory_ashmem_allocator.h" | |
14 #include "base/memory/discardable_memory_emulated.h" | 13 #include "base/memory/discardable_memory_emulated.h" |
15 #include "base/memory/discardable_memory_malloc.h" | 14 #include "base/memory/discardable_memory_malloc.h" |
16 | 15 |
17 namespace base { | 16 namespace base { |
18 namespace { | 17 namespace { |
19 | 18 |
20 const char kAshmemAllocatorName[] = "DiscardableMemoryAshmemAllocator"; | 19 const char kAshmemAllocatorName[] = "DiscardableMemoryAllocator"; |
21 | 20 |
22 // When ashmem is used, have the DiscardableMemoryManager trigger userspace | 21 struct DiscardableMemoryAllocatorWrapper { |
23 // eviction when address space usage gets too high (e.g. 512 MBytes). | 22 DiscardableMemoryAllocatorWrapper() |
24 const size_t kAshmemMaxAddressSpaceUsage = 512 * 1024 * 1024; | |
25 | |
26 // Holds the state used for ashmem allocations. | |
27 struct AshmemGlobalContext { | |
28 AshmemGlobalContext() | |
29 : allocator(kAshmemAllocatorName, | 23 : allocator(kAshmemAllocatorName, |
30 GetOptimalAshmemRegionSizeForAllocator()) { | 24 GetOptimalAshmemRegionSizeForAllocator()) { |
31 manager.SetMemoryLimit(kAshmemMaxAddressSpaceUsage); | |
32 } | 25 } |
33 | 26 |
34 internal::DiscardableMemoryAshmemAllocator allocator; | 27 internal::DiscardableMemoryAllocator allocator; |
35 internal::DiscardableMemoryManager manager; | |
36 | 28 |
37 private: | 29 private: |
38 // Returns 64 MBytes for a 512 MBytes device, 128 MBytes for 1024 MBytes... | 30 // Returns 64 MBytes for a 512 MBytes device, 128 MBytes for 1024 MBytes... |
39 static size_t GetOptimalAshmemRegionSizeForAllocator() { | 31 static size_t GetOptimalAshmemRegionSizeForAllocator() { |
40 // Note that this may do some I/O (without hitting the disk though) so it | 32 // Note that this may do some I/O (without hitting the disk though) so it |
41 // should not be called on the critical path. | 33 // should not be called on the critical path. |
42 return base::android::SysUtils::AmountOfPhysicalMemoryKB() * 1024 / 8; | 34 return base::android::SysUtils::AmountOfPhysicalMemoryKB() * 1024 / 8; |
43 } | 35 } |
44 }; | 36 }; |
45 | 37 |
46 LazyInstance<AshmemGlobalContext>::Leaky g_context = LAZY_INSTANCE_INITIALIZER; | 38 LazyInstance<DiscardableMemoryAllocatorWrapper>::Leaky g_context = |
| 39 LAZY_INSTANCE_INITIALIZER; |
47 | 40 |
48 } // namespace | 41 } // namespace |
49 | 42 |
50 // static | 43 // static |
51 void DiscardableMemory::RegisterMemoryPressureListeners() { | 44 void DiscardableMemory::RegisterMemoryPressureListeners() { |
52 internal::DiscardableMemoryEmulated::RegisterMemoryPressureListeners(); | 45 internal::DiscardableMemoryEmulated::RegisterMemoryPressureListeners(); |
53 } | 46 } |
54 | 47 |
55 // static | 48 // static |
56 void DiscardableMemory::UnregisterMemoryPressureListeners() { | 49 void DiscardableMemory::UnregisterMemoryPressureListeners() { |
57 internal::DiscardableMemoryEmulated::UnregisterMemoryPressureListeners(); | 50 internal::DiscardableMemoryEmulated::UnregisterMemoryPressureListeners(); |
58 } | 51 } |
59 | 52 |
60 // static | 53 // static |
61 void DiscardableMemory::GetSupportedTypes( | 54 void DiscardableMemory::GetSupportedTypes( |
62 std::vector<DiscardableMemoryType>* types) { | 55 std::vector<DiscardableMemoryType>* types) { |
63 const DiscardableMemoryType supported_types[] = { | 56 const DiscardableMemoryType supported_types[] = { |
64 DISCARDABLE_MEMORY_TYPE_ASHMEM, | 57 DISCARDABLE_MEMORY_TYPE_ANDROID, |
65 DISCARDABLE_MEMORY_TYPE_EMULATED, | 58 DISCARDABLE_MEMORY_TYPE_EMULATED, |
66 DISCARDABLE_MEMORY_TYPE_MALLOC | 59 DISCARDABLE_MEMORY_TYPE_MALLOC |
67 }; | 60 }; |
68 types->assign(supported_types, supported_types + arraysize(supported_types)); | 61 types->assign(supported_types, supported_types + arraysize(supported_types)); |
69 } | 62 } |
70 | 63 |
71 // static | 64 // static |
72 scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemoryWithType( | 65 scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemoryWithType( |
73 DiscardableMemoryType type, size_t size) { | 66 DiscardableMemoryType type, size_t size) { |
74 switch (type) { | 67 switch (type) { |
75 case DISCARDABLE_MEMORY_TYPE_NONE: | 68 case DISCARDABLE_MEMORY_TYPE_NONE: |
76 case DISCARDABLE_MEMORY_TYPE_MAC: | 69 case DISCARDABLE_MEMORY_TYPE_MAC: |
77 return scoped_ptr<DiscardableMemory>(); | 70 return scoped_ptr<DiscardableMemory>(); |
78 case DISCARDABLE_MEMORY_TYPE_ASHMEM: { | 71 case DISCARDABLE_MEMORY_TYPE_ANDROID: { |
79 AshmemGlobalContext* const global_context = g_context.Pointer(); | 72 return g_context.Pointer()->allocator.Allocate(size); |
80 scoped_ptr<internal::DiscardableMemoryAshmem> memory( | |
81 new internal::DiscardableMemoryAshmem( | |
82 size, &global_context->allocator, &global_context->manager)); | |
83 if (!memory->Initialize()) | |
84 return scoped_ptr<DiscardableMemory>(); | |
85 | |
86 return memory.PassAs<DiscardableMemory>(); | |
87 } | 73 } |
88 case DISCARDABLE_MEMORY_TYPE_EMULATED: { | 74 case DISCARDABLE_MEMORY_TYPE_EMULATED: { |
89 scoped_ptr<internal::DiscardableMemoryEmulated> memory( | 75 scoped_ptr<internal::DiscardableMemoryEmulated> memory( |
90 new internal::DiscardableMemoryEmulated(size)); | 76 new internal::DiscardableMemoryEmulated(size)); |
91 if (!memory->Initialize()) | 77 if (!memory->Initialize()) |
92 return scoped_ptr<DiscardableMemory>(); | 78 return scoped_ptr<DiscardableMemory>(); |
93 | 79 |
94 return memory.PassAs<DiscardableMemory>(); | 80 return memory.PassAs<DiscardableMemory>(); |
95 } | 81 } |
96 case DISCARDABLE_MEMORY_TYPE_MALLOC: { | 82 case DISCARDABLE_MEMORY_TYPE_MALLOC: { |
97 scoped_ptr<internal::DiscardableMemoryMalloc> memory( | 83 scoped_ptr<internal::DiscardableMemoryMalloc> memory( |
98 new internal::DiscardableMemoryMalloc(size)); | 84 new internal::DiscardableMemoryMalloc(size)); |
99 if (!memory->Initialize()) | 85 if (!memory->Initialize()) |
100 return scoped_ptr<DiscardableMemory>(); | 86 return scoped_ptr<DiscardableMemory>(); |
101 | 87 |
102 return memory.PassAs<DiscardableMemory>(); | 88 return memory.PassAs<DiscardableMemory>(); |
103 } | 89 } |
104 } | 90 } |
105 | 91 |
106 NOTREACHED(); | 92 NOTREACHED(); |
107 return scoped_ptr<DiscardableMemory>(); | 93 return scoped_ptr<DiscardableMemory>(); |
108 } | 94 } |
109 | 95 |
110 // static | 96 // static |
| 97 bool DiscardableMemory::PurgeForTestingSupported() { |
| 98 return false; |
| 99 } |
| 100 |
| 101 // static |
111 void DiscardableMemory::PurgeForTesting() { | 102 void DiscardableMemory::PurgeForTesting() { |
112 g_context.Pointer()->manager.PurgeAll(); | 103 NOTIMPLEMENTED(); |
113 internal::DiscardableMemoryEmulated::PurgeForTesting(); | |
114 } | 104 } |
115 | 105 |
116 } // namespace base | 106 } // namespace base |
OLD | NEW |