| 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 <mach/mach.h> | 7 #include <mach/mach.h> |
| 8 #include <sys/mman.h> | 8 #include <sys/mman.h> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.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" |
| 14 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 15 | 16 |
| 16 namespace base { | 17 namespace base { |
| 17 namespace { | 18 namespace { |
| 18 | 19 |
| 19 // The VM subsystem allows tagging of memory and 240-255 is reserved for | 20 // The VM subsystem allows tagging of memory and 240-255 is reserved for |
| 20 // application use (see mach/vm_statistics.h). Pick 252 (after chromium's atomic | 21 // application use (see mach/vm_statistics.h). Pick 252 (after chromium's atomic |
| 21 // weight of ~52). | 22 // weight of ~52). |
| 22 const int kDiscardableMemoryTag = VM_MAKE_TAG(252); | 23 const int kDiscardableMemoryTag = VM_MAKE_TAG(252); |
| 23 | 24 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 // static | 97 // static |
| 97 void DiscardableMemory::UnregisterMemoryPressureListeners() { | 98 void DiscardableMemory::UnregisterMemoryPressureListeners() { |
| 98 internal::DiscardableMemoryEmulated::UnregisterMemoryPressureListeners(); | 99 internal::DiscardableMemoryEmulated::UnregisterMemoryPressureListeners(); |
| 99 } | 100 } |
| 100 | 101 |
| 101 // static | 102 // static |
| 102 void DiscardableMemory::GetSupportedTypes( | 103 void DiscardableMemory::GetSupportedTypes( |
| 103 std::vector<DiscardableMemoryType>* types) { | 104 std::vector<DiscardableMemoryType>* types) { |
| 104 const DiscardableMemoryType supported_types[] = { | 105 const DiscardableMemoryType supported_types[] = { |
| 105 DISCARDABLE_MEMORY_TYPE_MAC, | 106 DISCARDABLE_MEMORY_TYPE_MAC, |
| 106 DISCARDABLE_MEMORY_TYPE_EMULATED | 107 DISCARDABLE_MEMORY_TYPE_EMULATED, |
| 108 DISCARDABLE_MEMORY_TYPE_MALLOC |
| 107 }; | 109 }; |
| 108 types->assign(supported_types, supported_types + arraysize(supported_types)); | 110 types->assign(supported_types, supported_types + arraysize(supported_types)); |
| 109 } | 111 } |
| 110 | 112 |
| 111 // static | 113 // static |
| 112 scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemoryWithType( | 114 scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemoryWithType( |
| 113 DiscardableMemoryType type, size_t size) { | 115 DiscardableMemoryType type, size_t size) { |
| 114 switch (type) { | 116 switch (type) { |
| 115 case DISCARDABLE_MEMORY_TYPE_NONE: | 117 case DISCARDABLE_MEMORY_TYPE_NONE: |
| 116 case DISCARDABLE_MEMORY_TYPE_ANDROID: | 118 case DISCARDABLE_MEMORY_TYPE_ANDROID: |
| 117 return scoped_ptr<DiscardableMemory>(); | 119 return scoped_ptr<DiscardableMemory>(); |
| 118 case DISCARDABLE_MEMORY_TYPE_MAC: { | 120 case DISCARDABLE_MEMORY_TYPE_MAC: { |
| 119 scoped_ptr<DiscardableMemoryMac> memory(new DiscardableMemoryMac(size)); | 121 scoped_ptr<DiscardableMemoryMac> memory(new DiscardableMemoryMac(size)); |
| 120 if (!memory->Initialize()) | 122 if (!memory->Initialize()) |
| 121 return scoped_ptr<DiscardableMemory>(); | 123 return scoped_ptr<DiscardableMemory>(); |
| 122 | 124 |
| 123 return memory.PassAs<DiscardableMemory>(); | 125 return memory.PassAs<DiscardableMemory>(); |
| 124 } | 126 } |
| 125 case DISCARDABLE_MEMORY_TYPE_EMULATED: { | 127 case DISCARDABLE_MEMORY_TYPE_EMULATED: { |
| 126 scoped_ptr<internal::DiscardableMemoryEmulated> memory( | 128 scoped_ptr<internal::DiscardableMemoryEmulated> memory( |
| 127 new internal::DiscardableMemoryEmulated(size)); | 129 new internal::DiscardableMemoryEmulated(size)); |
| 128 if (!memory->Initialize()) | 130 if (!memory->Initialize()) |
| 129 return scoped_ptr<DiscardableMemory>(); | 131 return scoped_ptr<DiscardableMemory>(); |
| 130 | 132 |
| 131 return memory.PassAs<DiscardableMemory>(); | 133 return memory.PassAs<DiscardableMemory>(); |
| 132 } | 134 } |
| 135 case DISCARDABLE_MEMORY_TYPE_MALLOC: { |
| 136 scoped_ptr<internal::DiscardableMemoryMalloc> memory( |
| 137 new internal::DiscardableMemoryMalloc(size)); |
| 138 if (!memory->Initialize()) |
| 139 return scoped_ptr<DiscardableMemory>(); |
| 140 |
| 141 return memory.PassAs<DiscardableMemory>(); |
| 142 } |
| 133 } | 143 } |
| 134 | 144 |
| 135 NOTREACHED(); | 145 NOTREACHED(); |
| 136 return scoped_ptr<DiscardableMemory>(); | 146 return scoped_ptr<DiscardableMemory>(); |
| 137 } | 147 } |
| 138 | 148 |
| 139 // static | 149 // static |
| 140 bool DiscardableMemory::PurgeForTestingSupported() { | 150 bool DiscardableMemory::PurgeForTestingSupported() { |
| 141 return true; | 151 return true; |
| 142 } | 152 } |
| 143 | 153 |
| 144 // static | 154 // static |
| 145 void DiscardableMemory::PurgeForTesting() { | 155 void DiscardableMemory::PurgeForTesting() { |
| 146 int state = 0; | 156 int state = 0; |
| 147 vm_purgable_control(mach_task_self(), 0, VM_PURGABLE_PURGE_ALL, &state); | 157 vm_purgable_control(mach_task_self(), 0, VM_PURGABLE_PURGE_ALL, &state); |
| 148 internal::DiscardableMemoryEmulated::PurgeForTesting(); | 158 internal::DiscardableMemoryEmulated::PurgeForTesting(); |
| 149 } | 159 } |
| 150 | 160 |
| 151 } // namespace base | 161 } // namespace base |
| OLD | NEW |