| 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> |
| 9 #include <vector> |
| 10 |
| 8 #include "base/base_export.h" | 11 #include "base/base_export.h" |
| 9 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
| 10 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
| 11 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
| 12 | 15 |
| 13 namespace base { | 16 namespace base { |
| 14 | 17 |
| 15 enum LockDiscardableMemoryStatus { | 18 enum DiscardableMemoryType { |
| 16 DISCARDABLE_MEMORY_FAILED = -1, | 19 DISCARDABLE_MEMORY_TYPE_NONE, |
| 17 DISCARDABLE_MEMORY_PURGED = 0, | 20 DISCARDABLE_MEMORY_TYPE_ANDROID, |
| 18 DISCARDABLE_MEMORY_SUCCESS = 1 | 21 DISCARDABLE_MEMORY_TYPE_MAC, |
| 22 DISCARDABLE_MEMORY_TYPE_EMULATED |
| 23 }; |
| 24 |
| 25 enum DiscardableMemoryLockStatus { |
| 26 DISCARDABLE_MEMORY_LOCK_STATUS_FAILED, |
| 27 DISCARDABLE_MEMORY_LOCK_STATUS_PURGED, |
| 28 DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS |
| 19 }; | 29 }; |
| 20 | 30 |
| 21 // Platform abstraction for discardable memory. DiscardableMemory is used to | 31 // Platform abstraction for discardable memory. DiscardableMemory is used to |
| 22 // cache large objects without worrying about blowing out memory, both on mobile | 32 // cache large objects without worrying about blowing out memory, both on mobile |
| 23 // devices where there is no swap, and desktop devices where unused free memory | 33 // devices where there is no swap, and desktop devices where unused free memory |
| 24 // should be used to help the user experience. This is preferable to releasing | 34 // should be used to help the user experience. This is preferable to releasing |
| 25 // memory in response to an OOM signal because it is simpler, though it has less | 35 // memory in response to an OOM signal because it is simpler, though it has less |
| 26 // flexibility as to which objects get discarded. | 36 // flexibility as to which objects get discarded. |
| 27 // | 37 // |
| 28 // Discardable memory has two states: locked and unlocked. While the memory is | 38 // Discardable memory has two states: locked and unlocked. While the memory is |
| (...skipping 17 matching lines...) Expand all Loading... |
| 46 // - Linux: http://lwn.net/Articles/452035/ | 56 // - Linux: http://lwn.net/Articles/452035/ |
| 47 // - Mac: http://trac.webkit.org/browser/trunk/Source/WebCore/platform/mac/Pur
geableBufferMac.cpp | 57 // - Mac: http://trac.webkit.org/browser/trunk/Source/WebCore/platform/mac/Pur
geableBufferMac.cpp |
| 48 // the comment starting with "vm_object_purgable_control" at | 58 // the comment starting with "vm_object_purgable_control" at |
| 49 // http://www.opensource.apple.com/source/xnu/xnu-792.13.8/osfmk/vm/v
m_object.c | 59 // http://www.opensource.apple.com/source/xnu/xnu-792.13.8/osfmk/vm/v
m_object.c |
| 50 // | 60 // |
| 51 // Thread-safety: DiscardableMemory instances are not thread-safe. | 61 // Thread-safety: DiscardableMemory instances are not thread-safe. |
| 52 class BASE_EXPORT DiscardableMemory { | 62 class BASE_EXPORT DiscardableMemory { |
| 53 public: | 63 public: |
| 54 virtual ~DiscardableMemory() {} | 64 virtual ~DiscardableMemory() {} |
| 55 | 65 |
| 56 // Check whether the system supports discardable memory natively. Returns | 66 // Gets the discardable memory type with a given name. |
| 57 // false if the support is emulated. | 67 static DiscardableMemoryType GetNamedType(const std::string& name); |
| 58 static bool SupportedNatively(); | |
| 59 | 68 |
| 69 // Gets the name of a discardable memory type. |
| 70 static const char* GetTypeName(DiscardableMemoryType type); |
| 71 |
| 72 // Gets system supported discardable memory types. Default preferred type |
| 73 // at the front of vector. |
| 74 static void GetSupportedTypes(std::vector<DiscardableMemoryType>* types); |
| 75 |
| 76 // Sets the preferred discardable memory type. This overrides the default |
| 77 // preferred type. Can only be called once prior to GetPreferredType() |
| 78 // or CreateLockedMemory(). Caller is responsible for correct ordering. |
| 79 static void SetPreferredType(DiscardableMemoryType type); |
| 80 |
| 81 // Gets the preferred discardable memory type. |
| 82 static DiscardableMemoryType GetPreferredType(); |
| 83 |
| 84 // Create a DiscardableMemory instance with specified |type| and |size|. |
| 85 static scoped_ptr<DiscardableMemory> CreateLockedMemoryWithType( |
| 86 DiscardableMemoryType type, size_t size); |
| 87 |
| 88 // Create a DiscardableMemory instance with preferred type and |size|. |
| 60 static scoped_ptr<DiscardableMemory> CreateLockedMemory(size_t size); | 89 static scoped_ptr<DiscardableMemory> CreateLockedMemory(size_t size); |
| 61 | 90 |
| 62 // Locks the memory so that it will not be purged by the system. Returns | 91 // Locks the memory so that it will not be purged by the system. Returns |
| 63 // DISCARDABLE_MEMORY_SUCCESS on success. If the return value is | 92 // DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS on success. If the return value is |
| 64 // DISCARDABLE_MEMORY_FAILED then this object should be discarded and | 93 // DISCARDABLE_MEMORY_LOCK_STATUS_FAILED then this object should be |
| 65 // a new one should be created. If the return value is | 94 // discarded and a new one should be created. If the return value is |
| 66 // DISCARDABLE_MEMORY_PURGED then the memory is present but any data that | 95 // DISCARDABLE_MEMORY_LOCK_STATUS_PURGED then the memory is present but any |
| 67 // was in it is gone. | 96 // data that was in it is gone. |
| 68 virtual LockDiscardableMemoryStatus Lock() WARN_UNUSED_RESULT = 0; | 97 virtual DiscardableMemoryLockStatus Lock() WARN_UNUSED_RESULT = 0; |
| 69 | 98 |
| 70 // Unlocks the memory so that it can be purged by the system. Must be called | 99 // Unlocks the memory so that it can be purged by the system. Must be called |
| 71 // after every successful lock call. | 100 // after every successful lock call. |
| 72 virtual void Unlock() = 0; | 101 virtual void Unlock() = 0; |
| 73 | 102 |
| 74 // Returns the memory address held by this object. The object must be locked | 103 // Returns the memory address held by this object. The object must be locked |
| 75 // before calling this. Otherwise, this will cause a DCHECK error. | 104 // before calling this. Otherwise, this will cause a DCHECK error. |
| 76 virtual void* Memory() const = 0; | 105 virtual void* Memory() const = 0; |
| 77 | 106 |
| 78 // Testing utility calls. | 107 // Testing utility calls. |
| 79 | 108 |
| 80 // Check whether a purge of all discardable memory in the system is supported. | 109 // Check whether a purge of all discardable memory in the system is supported. |
| 81 // Use only for testing! | 110 // Use only for testing! |
| 82 static bool PurgeForTestingSupported(); | 111 static bool PurgeForTestingSupported(); |
| 83 | 112 |
| 84 // Purge all discardable memory in the system. This call has global effects | 113 // Purge all discardable memory in the system. This call has global effects |
| 85 // across all running processes, so it should only be used for testing! | 114 // across all running processes, so it should only be used for testing! |
| 86 static void PurgeForTesting(); | 115 static void PurgeForTesting(); |
| 87 }; | 116 }; |
| 88 | 117 |
| 89 } // namespace base | 118 } // namespace base |
| 90 | 119 |
| 91 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_H_ | 120 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_H_ |
| OLD | NEW |