| 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" | |
| 8 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 9 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 10 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 11 #include "base/logging.h" | 10 #include "base/logging.h" |
| 12 #include "base/memory/discardable_memory_ashmem.h" | 11 #include "base/memory/discardable_memory_ashmem.h" |
| 13 #include "base/memory/discardable_memory_ashmem_allocator.h" | 12 #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" |
| 15 #include "base/sys_utils.h" |
| 16 | 16 |
| 17 namespace base { | 17 namespace base { |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 const char kAshmemAllocatorName[] = "DiscardableMemoryAshmemAllocator"; | 20 const char kAshmemAllocatorName[] = "DiscardableMemoryAshmemAllocator"; |
| 21 | 21 |
| 22 // When ashmem is used, have the DiscardableMemoryManager trigger userspace | 22 // When ashmem is used, have the DiscardableMemoryManager trigger userspace |
| 23 // eviction when address space usage gets too high (e.g. 512 MBytes). | 23 // eviction when address space usage gets too high (e.g. 512 MBytes). |
| 24 const size_t kAshmemMaxAddressSpaceUsage = 512 * 1024 * 1024; | 24 const size_t kAshmemMaxAddressSpaceUsage = 512 * 1024 * 1024; |
| 25 | 25 |
| 26 // Holds the state used for ashmem allocations. | 26 // Holds the state used for ashmem allocations. |
| 27 struct AshmemGlobalContext { | 27 struct AshmemGlobalContext { |
| 28 AshmemGlobalContext() | 28 AshmemGlobalContext() |
| 29 : allocator(kAshmemAllocatorName, | 29 : allocator(kAshmemAllocatorName, |
| 30 GetOptimalAshmemRegionSizeForAllocator()) { | 30 GetOptimalAshmemRegionSizeForAllocator()) { |
| 31 manager.SetMemoryLimit(kAshmemMaxAddressSpaceUsage); | 31 manager.SetMemoryLimit(kAshmemMaxAddressSpaceUsage); |
| 32 } | 32 } |
| 33 | 33 |
| 34 internal::DiscardableMemoryAshmemAllocator allocator; | 34 internal::DiscardableMemoryAshmemAllocator allocator; |
| 35 internal::DiscardableMemoryManager manager; | 35 internal::DiscardableMemoryManager manager; |
| 36 | 36 |
| 37 private: | 37 private: |
| 38 // 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... |
| 39 static size_t GetOptimalAshmemRegionSizeForAllocator() { | 39 static size_t GetOptimalAshmemRegionSizeForAllocator() { |
| 40 // 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 |
| 41 // should not be called on the critical path. | 41 // should not be called on the critical path. |
| 42 return base::android::SysUtils::AmountOfPhysicalMemoryKB() * 1024 / 8; | 42 return base::SysUtils::AmountOfPhysicalMemoryKB() * 1024 / 8; |
| 43 } | 43 } |
| 44 }; | 44 }; |
| 45 | 45 |
| 46 LazyInstance<AshmemGlobalContext>::Leaky g_context = LAZY_INSTANCE_INITIALIZER; | 46 LazyInstance<AshmemGlobalContext>::Leaky g_context = LAZY_INSTANCE_INITIALIZER; |
| 47 | 47 |
| 48 } // namespace | 48 } // namespace |
| 49 | 49 |
| 50 // static | 50 // static |
| 51 void DiscardableMemory::RegisterMemoryPressureListeners() { | 51 void DiscardableMemory::RegisterMemoryPressureListeners() { |
| 52 internal::DiscardableMemoryEmulated::RegisterMemoryPressureListeners(); | 52 internal::DiscardableMemoryEmulated::RegisterMemoryPressureListeners(); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 return scoped_ptr<DiscardableMemory>(); | 107 return scoped_ptr<DiscardableMemory>(); |
| 108 } | 108 } |
| 109 | 109 |
| 110 // static | 110 // static |
| 111 void DiscardableMemory::PurgeForTesting() { | 111 void DiscardableMemory::PurgeForTesting() { |
| 112 g_context.Pointer()->manager.PurgeAll(); | 112 g_context.Pointer()->manager.PurgeAll(); |
| 113 internal::DiscardableMemoryEmulated::PurgeForTesting(); | 113 internal::DiscardableMemoryEmulated::PurgeForTesting(); |
| 114 } | 114 } |
| 115 | 115 |
| 116 } // namespace base | 116 } // namespace base |
| OLD | NEW |