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/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/memory/discardable_memory_ashmem.h" | 11 #include "base/memory/discardable_memory_ashmem.h" |
12 #include "base/memory/discardable_memory_ashmem_allocator.h" | 12 #include "base/memory/discardable_memory_ashmem_allocator.h" |
13 #include "base/memory/discardable_memory_emulated.h" | 13 #include "base/memory/discardable_memory_emulated.h" |
14 #include "base/memory/discardable_memory_malloc.h" | |
15 #include "base/sys_info.h" | 14 #include "base/sys_info.h" |
16 | 15 |
17 namespace base { | 16 namespace base { |
18 namespace { | 17 namespace { |
19 | 18 |
20 const char kAshmemAllocatorName[] = "DiscardableMemoryAshmemAllocator"; | 19 const char kAshmemAllocatorName[] = "DiscardableMemoryAshmemAllocator"; |
21 | 20 |
22 // For Ashmem, have the DiscardableMemoryManager trigger userspace eviction | 21 // For Ashmem, have the DiscardableMemoryManager trigger userspace eviction |
23 // when address space usage gets too high (e.g. 512 MBytes). | 22 // when address space usage gets too high (e.g. 512 MBytes). |
24 const size_t kAshmemMemoryLimit = 512 * 1024 * 1024; | 23 const size_t kAshmemMemoryLimit = 512 * 1024 * 1024; |
(...skipping 21 matching lines...) Expand all Loading... |
46 // static | 45 // static |
47 bool DiscardableMemory::ReduceMemoryUsage() { | 46 bool DiscardableMemory::ReduceMemoryUsage() { |
48 return internal::DiscardableMemoryEmulated::ReduceMemoryUsage(); | 47 return internal::DiscardableMemoryEmulated::ReduceMemoryUsage(); |
49 } | 48 } |
50 | 49 |
51 // static | 50 // static |
52 void DiscardableMemory::GetSupportedTypes( | 51 void DiscardableMemory::GetSupportedTypes( |
53 std::vector<DiscardableMemoryType>* types) { | 52 std::vector<DiscardableMemoryType>* types) { |
54 const DiscardableMemoryType supported_types[] = { | 53 const DiscardableMemoryType supported_types[] = { |
55 DISCARDABLE_MEMORY_TYPE_ASHMEM, | 54 DISCARDABLE_MEMORY_TYPE_ASHMEM, |
56 DISCARDABLE_MEMORY_TYPE_EMULATED, | 55 DISCARDABLE_MEMORY_TYPE_EMULATED |
57 DISCARDABLE_MEMORY_TYPE_MALLOC | |
58 }; | 56 }; |
59 types->assign(supported_types, supported_types + arraysize(supported_types)); | 57 types->assign(supported_types, supported_types + arraysize(supported_types)); |
60 } | 58 } |
61 | 59 |
62 // static | 60 // static |
63 scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemoryWithType( | 61 scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemoryWithType( |
64 DiscardableMemoryType type, size_t size) { | 62 DiscardableMemoryType type, size_t size) { |
65 switch (type) { | 63 switch (type) { |
66 case DISCARDABLE_MEMORY_TYPE_ASHMEM: { | 64 case DISCARDABLE_MEMORY_TYPE_ASHMEM: { |
67 SharedState* const shared_state = g_shared_state.Pointer(); | 65 SharedState* const shared_state = g_shared_state.Pointer(); |
68 scoped_ptr<internal::DiscardableMemoryAshmem> memory( | 66 scoped_ptr<internal::DiscardableMemoryAshmem> memory( |
69 new internal::DiscardableMemoryAshmem( | 67 new internal::DiscardableMemoryAshmem( |
70 size, &shared_state->allocator, &shared_state->manager)); | 68 size, &shared_state->allocator, &shared_state->manager)); |
71 if (!memory->Initialize()) | 69 if (!memory->Initialize()) |
72 return scoped_ptr<DiscardableMemory>(); | 70 return scoped_ptr<DiscardableMemory>(); |
73 | 71 |
74 return memory.PassAs<DiscardableMemory>(); | 72 return memory.PassAs<DiscardableMemory>(); |
75 } | 73 } |
76 case DISCARDABLE_MEMORY_TYPE_EMULATED: { | 74 case DISCARDABLE_MEMORY_TYPE_EMULATED: { |
77 scoped_ptr<internal::DiscardableMemoryEmulated> memory( | 75 scoped_ptr<internal::DiscardableMemoryEmulated> memory( |
78 new internal::DiscardableMemoryEmulated(size)); | 76 new internal::DiscardableMemoryEmulated(size)); |
79 if (!memory->Initialize()) | 77 if (!memory->Initialize()) |
80 return scoped_ptr<DiscardableMemory>(); | 78 return scoped_ptr<DiscardableMemory>(); |
81 | 79 |
82 return memory.PassAs<DiscardableMemory>(); | 80 return memory.PassAs<DiscardableMemory>(); |
83 } | 81 } |
84 case DISCARDABLE_MEMORY_TYPE_MALLOC: { | |
85 scoped_ptr<internal::DiscardableMemoryMalloc> memory( | |
86 new internal::DiscardableMemoryMalloc(size)); | |
87 if (!memory->Initialize()) | |
88 return scoped_ptr<DiscardableMemory>(); | |
89 | |
90 return memory.PassAs<DiscardableMemory>(); | |
91 } | |
92 case DISCARDABLE_MEMORY_TYPE_NONE: | 82 case DISCARDABLE_MEMORY_TYPE_NONE: |
93 case DISCARDABLE_MEMORY_TYPE_MAC: | 83 case DISCARDABLE_MEMORY_TYPE_MAC: |
94 NOTREACHED(); | 84 NOTREACHED(); |
95 return scoped_ptr<DiscardableMemory>(); | 85 return scoped_ptr<DiscardableMemory>(); |
96 } | 86 } |
97 | 87 |
98 NOTREACHED(); | 88 NOTREACHED(); |
99 return scoped_ptr<DiscardableMemory>(); | 89 return scoped_ptr<DiscardableMemory>(); |
100 } | 90 } |
101 | 91 |
102 // static | 92 // static |
103 void DiscardableMemory::PurgeForTesting() { | 93 void DiscardableMemory::PurgeForTesting() { |
104 g_shared_state.Pointer()->manager.PurgeAll(); | 94 g_shared_state.Pointer()->manager.PurgeAll(); |
105 internal::DiscardableMemoryEmulated::PurgeForTesting(); | 95 internal::DiscardableMemoryEmulated::PurgeForTesting(); |
106 } | 96 } |
107 | 97 |
108 } // namespace base | 98 } // namespace base |
OLD | NEW |