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