| 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 #ifndef BASE_MEMORY_DISCARDABLE_MEMORY_H_ | 5 #ifndef BASE_MEMORY_DISCARDABLE_MEMORY_H_ |
| 6 #define BASE_MEMORY_DISCARDABLE_MEMORY_H_ | 6 #define BASE_MEMORY_DISCARDABLE_MEMORY_H_ |
| 7 | 7 |
| 8 #include <string> | 8 #include <string> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "base/base_export.h" | 11 #include "base/base_export.h" |
| 12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 15 | 15 |
| 16 namespace base { | 16 namespace base { |
| 17 | 17 |
| 18 enum DiscardableMemoryType { | 18 enum DiscardableMemoryType { |
| 19 DISCARDABLE_MEMORY_TYPE_NONE, | 19 DISCARDABLE_MEMORY_TYPE_NONE, |
| 20 DISCARDABLE_MEMORY_TYPE_ASHMEM, | |
| 21 DISCARDABLE_MEMORY_TYPE_MACH, | |
| 22 DISCARDABLE_MEMORY_TYPE_EMULATED, | |
| 23 DISCARDABLE_MEMORY_TYPE_SHMEM | 20 DISCARDABLE_MEMORY_TYPE_SHMEM |
| 24 }; | 21 }; |
| 25 | 22 |
| 26 enum DiscardableMemoryLockStatus { | |
| 27 DISCARDABLE_MEMORY_LOCK_STATUS_FAILED, | |
| 28 DISCARDABLE_MEMORY_LOCK_STATUS_PURGED, | |
| 29 DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS | |
| 30 }; | |
| 31 | |
| 32 // Platform abstraction for discardable memory. DiscardableMemory is used to | 23 // Platform abstraction for discardable memory. DiscardableMemory is used to |
| 33 // cache large objects without worrying about blowing out memory, both on mobile | 24 // cache large objects without worrying about blowing out memory, both on mobile |
| 34 // devices where there is no swap, and desktop devices where unused free memory | 25 // devices where there is no swap, and desktop devices where unused free memory |
| 35 // should be used to help the user experience. This is preferable to releasing | 26 // should be used to help the user experience. This is preferable to releasing |
| 36 // memory in response to an OOM signal because it is simpler, though it has less | 27 // memory in response to an OOM signal because it is simpler, though it has less |
| 37 // flexibility as to which objects get discarded. | 28 // flexibility as to which objects get discarded. |
| 38 // | 29 // |
| 39 // Discardable memory has two states: locked and unlocked. While the memory is | 30 // Discardable memory has two states: locked and unlocked. While the memory is |
| 40 // locked, it will not be discarded. Unlocking the memory allows the OS to | 31 // locked, it will not be discarded. Unlocking the memory allows the OS to |
| 41 // reclaim it if needed. Locks do not nest. | 32 // reclaim it if needed. Locks do not nest. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 // Gets the preferred discardable memory type. | 73 // Gets the preferred discardable memory type. |
| 83 static DiscardableMemoryType GetPreferredType(); | 74 static DiscardableMemoryType GetPreferredType(); |
| 84 | 75 |
| 85 // Create a DiscardableMemory instance with specified |type| and |size|. | 76 // Create a DiscardableMemory instance with specified |type| and |size|. |
| 86 static scoped_ptr<DiscardableMemory> CreateLockedMemoryWithType( | 77 static scoped_ptr<DiscardableMemory> CreateLockedMemoryWithType( |
| 87 DiscardableMemoryType type, size_t size); | 78 DiscardableMemoryType type, size_t size); |
| 88 | 79 |
| 89 // Create a DiscardableMemory instance with preferred type and |size|. | 80 // Create a DiscardableMemory instance with preferred type and |size|. |
| 90 static scoped_ptr<DiscardableMemory> CreateLockedMemory(size_t size); | 81 static scoped_ptr<DiscardableMemory> CreateLockedMemory(size_t size); |
| 91 | 82 |
| 92 // Discardable memory implementations might allow an elevated usage level | |
| 93 // while in frequent use. Call this to have the usage reduced to the base | |
| 94 // level. Returns true if there's no need to call this again until | |
| 95 // memory instances have been used. This indicates that all discardable | |
| 96 // memory implementations have reduced usage to the base level or below. | |
| 97 // Note: calling this too often or while discardable memory is in frequent | |
| 98 // use can hurt performance, whereas calling it too infrequently can result | |
| 99 // in memory bloat. | |
| 100 static bool ReduceMemoryUsage(); | |
| 101 | |
| 102 // Locks the memory so that it will not be purged by the system. Returns | 83 // Locks the memory so that it will not be purged by the system. Returns |
| 103 // DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS on success. If the return value is | 84 // true on success. If the return value is false then this object should be |
| 104 // DISCARDABLE_MEMORY_LOCK_STATUS_FAILED then this object should be | 85 // discarded and a new one should be created. |
| 105 // discarded and a new one should be created. If the return value is | 86 virtual bool Lock() WARN_UNUSED_RESULT = 0; |
| 106 // DISCARDABLE_MEMORY_LOCK_STATUS_PURGED then the memory is present but any | |
| 107 // data that was in it is gone. | |
| 108 virtual DiscardableMemoryLockStatus Lock() WARN_UNUSED_RESULT = 0; | |
| 109 | 87 |
| 110 // Unlocks the memory so that it can be purged by the system. Must be called | 88 // Unlocks the memory so that it can be purged by the system. Must be called |
| 111 // after every successful lock call. | 89 // after every successful lock call. |
| 112 virtual void Unlock() = 0; | 90 virtual void Unlock() = 0; |
| 113 | 91 |
| 114 // Returns the memory address held by this object. The object must be locked | 92 // Returns the memory address held by this object. The object must be locked |
| 115 // before calling this. Otherwise, this will cause a DCHECK error. | 93 // before calling this. Otherwise, this will cause a DCHECK error. |
| 116 virtual void* Memory() const = 0; | 94 virtual void* Memory() const = 0; |
| 117 }; | 95 }; |
| 118 | 96 |
| 119 } // namespace base | 97 } // namespace base |
| 120 | 98 |
| 121 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_H_ | 99 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_H_ |
| OLD | NEW |