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" | |
8 #include "base/compiler_specific.h" | |
9 #include "base/lazy_instance.h" | |
10 #include "base/logging.h" | 7 #include "base/logging.h" |
11 #include "base/memory/discardable_memory_ashmem.h" | |
12 #include "base/memory/discardable_memory_ashmem_allocator.h" | |
13 #include "base/memory/discardable_memory_emulated.h" | |
14 #include "base/memory/discardable_memory_shmem.h" | 8 #include "base/memory/discardable_memory_shmem.h" |
15 #include "base/sys_info.h" | |
16 | 9 |
17 namespace base { | 10 namespace base { |
18 namespace { | |
19 | |
20 const char kAshmemAllocatorName[] = "DiscardableMemoryAshmemAllocator"; | |
21 | |
22 // For Ashmem, have the DiscardableMemoryManager trigger userspace eviction | |
23 // when address space usage gets too high (e.g. 512 MBytes). | |
24 const size_t kAshmemMemoryLimit = 512 * 1024 * 1024; | |
25 | |
26 size_t GetOptimalAshmemRegionSizeForAllocator() { | |
27 // Note that this may do some I/O (without hitting the disk though) so it | |
28 // should not be called on the critical path. | |
29 return base::SysInfo::AmountOfPhysicalMemory() / 8; | |
30 } | |
31 | |
32 // Holds the shared state used for allocations. | |
33 struct SharedState { | |
34 SharedState() | |
35 : manager(kAshmemMemoryLimit, kAshmemMemoryLimit, TimeDelta::Max()), | |
36 allocator(kAshmemAllocatorName, | |
37 GetOptimalAshmemRegionSizeForAllocator()) {} | |
38 | |
39 internal::DiscardableMemoryManager manager; | |
40 internal::DiscardableMemoryAshmemAllocator allocator; | |
41 }; | |
42 LazyInstance<SharedState>::Leaky g_shared_state = LAZY_INSTANCE_INITIALIZER; | |
43 | |
44 } // namespace | |
45 | |
46 // static | |
47 bool DiscardableMemory::ReduceMemoryUsage() { | |
48 return internal::DiscardableMemoryEmulated::ReduceMemoryUsage(); | |
49 } | |
50 | 11 |
51 // static | 12 // static |
52 void DiscardableMemory::GetSupportedTypes( | 13 void DiscardableMemory::GetSupportedTypes( |
53 std::vector<DiscardableMemoryType>* types) { | 14 std::vector<DiscardableMemoryType>* types) { |
54 const DiscardableMemoryType supported_types[] = { | 15 const DiscardableMemoryType supported_types[] = { |
55 DISCARDABLE_MEMORY_TYPE_SHMEM, | 16 DISCARDABLE_MEMORY_TYPE_SHMEM |
56 DISCARDABLE_MEMORY_TYPE_ASHMEM, | |
57 DISCARDABLE_MEMORY_TYPE_EMULATED | |
58 }; | 17 }; |
59 types->assign(supported_types, supported_types + arraysize(supported_types)); | 18 types->assign(supported_types, supported_types + arraysize(supported_types)); |
60 } | 19 } |
61 | 20 |
62 // static | 21 // static |
63 scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemoryWithType( | 22 scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemoryWithType( |
64 DiscardableMemoryType type, size_t size) { | 23 DiscardableMemoryType type, size_t size) { |
65 switch (type) { | 24 switch (type) { |
66 case DISCARDABLE_MEMORY_TYPE_ASHMEM: { | 25 case DISCARDABLE_MEMORY_TYPE_SHMEM: |
67 SharedState* const shared_state = g_shared_state.Pointer(); | 26 return make_scoped_ptr(new internal::DiscardableMemoryShmem(size)); |
68 scoped_ptr<internal::DiscardableMemoryAshmem> memory( | |
69 new internal::DiscardableMemoryAshmem( | |
70 size, &shared_state->allocator, &shared_state->manager)); | |
71 if (!memory->Initialize()) | |
72 return nullptr; | |
73 | |
74 return memory.Pass(); | |
75 } | |
76 case DISCARDABLE_MEMORY_TYPE_EMULATED: { | |
77 scoped_ptr<internal::DiscardableMemoryEmulated> memory( | |
78 new internal::DiscardableMemoryEmulated(size)); | |
79 if (!memory->Initialize()) | |
80 return nullptr; | |
81 | |
82 return memory.Pass(); | |
83 } | |
84 case DISCARDABLE_MEMORY_TYPE_SHMEM: { | |
85 scoped_ptr<internal::DiscardableMemoryShmem> memory( | |
86 new internal::DiscardableMemoryShmem(size)); | |
87 if (!memory->Initialize()) | |
88 return nullptr; | |
89 | |
90 return memory.Pass(); | |
91 } | |
92 case DISCARDABLE_MEMORY_TYPE_NONE: | 27 case DISCARDABLE_MEMORY_TYPE_NONE: |
93 case DISCARDABLE_MEMORY_TYPE_MACH: | |
94 NOTREACHED(); | 28 NOTREACHED(); |
95 return nullptr; | 29 return nullptr; |
96 } | 30 } |
97 | 31 |
98 NOTREACHED(); | 32 NOTREACHED(); |
99 return nullptr; | 33 return nullptr; |
100 } | 34 } |
101 | 35 |
102 } // namespace base | 36 } // namespace base |
OLD | NEW |