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_allocation_ashmem_factory.h" |
13 #include "base/memory/discardable_memory_ashmem.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" |
16 #include "base/memory/discardable_memory_manager.h" | |
15 | 17 |
16 namespace base { | 18 namespace base { |
17 namespace { | 19 namespace { |
18 | 20 |
19 const char kAshmemAllocatorName[] = "DiscardableMemoryAllocator"; | 21 const char kAshmemAllocatorName[] = "DiscardableMemoryAllocator"; |
20 | 22 |
21 struct DiscardableMemoryAllocatorWrapper { | 23 // High limit used only to avoid running out of address space. The kernel takes |
22 DiscardableMemoryAllocatorWrapper() | 24 // care of ensuring that we don't run out of physical memory. |
23 : allocator(kAshmemAllocatorName, | 25 const size_t kDefaultDiscardableMemoryLimit = 512 * 1024 * 1024; |
24 GetOptimalAshmemRegionSizeForAllocator()) { | 26 |
27 class DiscardableMemoryManagerImpl { | |
reveman
2014/03/20 19:33:44
This is not a DiscardableMemoryManagerImpl. This s
Philippe
2014/03/21 10:17:44
Agreed for consistency. Let's discuss this in your
| |
28 public: | |
29 DiscardableMemoryManagerImpl() | |
30 : factory_( | |
31 kAshmemAllocatorName, GetOptimalAshmemRegionSizeForAllocator()), | |
32 manager_(&factory_, kDefaultDiscardableMemoryLimit) { | |
25 } | 33 } |
26 | 34 |
27 internal::DiscardableMemoryAllocator allocator; | 35 internal::DiscardableMemoryManager* instance() { |
36 return &manager_; | |
37 } | |
28 | 38 |
29 private: | 39 private: |
30 // Returns 64 MBytes for a 512 MBytes device, 128 MBytes for 1024 MBytes... | 40 // Returns 64 MBytes for a 512 MBytes device, 128 MBytes for 1024 MBytes... |
31 static size_t GetOptimalAshmemRegionSizeForAllocator() { | 41 static size_t GetOptimalAshmemRegionSizeForAllocator() { |
32 // Note that this may do some I/O (without hitting the disk though) so it | 42 // Note that this may do some I/O (without hitting the disk though) so it |
33 // should not be called on the critical path. | 43 // should not be called on the critical path. |
34 return base::android::SysUtils::AmountOfPhysicalMemoryKB() * 1024 / 8; | 44 return android::SysUtils::AmountOfPhysicalMemoryKB() * 1024 / 8; |
35 } | 45 } |
46 | |
47 internal::DiscardableMemoryAllocationAshmemFactory factory_; | |
48 internal::DiscardableMemoryManager manager_; | |
36 }; | 49 }; |
37 | 50 |
38 LazyInstance<DiscardableMemoryAllocatorWrapper>::Leaky g_context = | 51 LazyInstance<DiscardableMemoryManagerImpl>::Leaky g_manager = |
39 LAZY_INSTANCE_INITIALIZER; | 52 LAZY_INSTANCE_INITIALIZER; |
40 | 53 |
41 } // namespace | 54 } // namespace |
42 | 55 |
43 // static | 56 // static |
44 void DiscardableMemory::RegisterMemoryPressureListeners() { | 57 void DiscardableMemory::RegisterMemoryPressureListeners() { |
45 internal::DiscardableMemoryEmulated::RegisterMemoryPressureListeners(); | 58 internal::DiscardableMemoryEmulated::RegisterMemoryPressureListeners(); |
46 } | 59 } |
47 | 60 |
48 // static | 61 // static |
(...skipping 13 matching lines...) Expand all Loading... | |
62 } | 75 } |
63 | 76 |
64 // static | 77 // static |
65 scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemoryWithType( | 78 scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemoryWithType( |
66 DiscardableMemoryType type, size_t size) { | 79 DiscardableMemoryType type, size_t size) { |
67 switch (type) { | 80 switch (type) { |
68 case DISCARDABLE_MEMORY_TYPE_NONE: | 81 case DISCARDABLE_MEMORY_TYPE_NONE: |
69 case DISCARDABLE_MEMORY_TYPE_MAC: | 82 case DISCARDABLE_MEMORY_TYPE_MAC: |
70 return scoped_ptr<DiscardableMemory>(); | 83 return scoped_ptr<DiscardableMemory>(); |
71 case DISCARDABLE_MEMORY_TYPE_ANDROID: { | 84 case DISCARDABLE_MEMORY_TYPE_ANDROID: { |
72 return g_context.Pointer()->allocator.Allocate(size); | 85 scoped_ptr<internal::DiscardableMemoryAshmem> memory( |
86 new internal::DiscardableMemoryAshmem( | |
87 g_manager.Get().instance(), size)); | |
88 if (!memory->Initialize()) | |
89 return scoped_ptr<DiscardableMemory>(); | |
90 | |
91 return memory.PassAs<DiscardableMemory>(); | |
73 } | 92 } |
74 case DISCARDABLE_MEMORY_TYPE_EMULATED: { | 93 case DISCARDABLE_MEMORY_TYPE_EMULATED: { |
75 scoped_ptr<internal::DiscardableMemoryEmulated> memory( | 94 scoped_ptr<internal::DiscardableMemoryEmulated> memory( |
76 new internal::DiscardableMemoryEmulated(size)); | 95 new internal::DiscardableMemoryEmulated(size)); |
77 if (!memory->Initialize()) | 96 if (!memory->Initialize()) |
78 return scoped_ptr<DiscardableMemory>(); | 97 return scoped_ptr<DiscardableMemory>(); |
79 | 98 |
80 return memory.PassAs<DiscardableMemory>(); | 99 return memory.PassAs<DiscardableMemory>(); |
81 } | 100 } |
82 case DISCARDABLE_MEMORY_TYPE_MALLOC: { | 101 case DISCARDABLE_MEMORY_TYPE_MALLOC: { |
(...skipping 14 matching lines...) Expand all Loading... | |
97 bool DiscardableMemory::PurgeForTestingSupported() { | 116 bool DiscardableMemory::PurgeForTestingSupported() { |
98 return false; | 117 return false; |
99 } | 118 } |
100 | 119 |
101 // static | 120 // static |
102 void DiscardableMemory::PurgeForTesting() { | 121 void DiscardableMemory::PurgeForTesting() { |
103 NOTIMPLEMENTED(); | 122 NOTIMPLEMENTED(); |
104 } | 123 } |
105 | 124 |
106 } // namespace base | 125 } // namespace base |
OLD | NEW |