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, |
willchan no longer on Chromium
2013/12/24 01:21:06
My inclination is to say we should guard these pla
reveman
2013/12/26 11:46:08
My plan here is to turn TYPE_ANDROID into TYPE_ASH
willchan no longer on Chromium
2013/12/26 18:25:54
This isn't a big deal and is mostly stylistic pref
reveman
2013/12/26 21:53:35
I'll keep it in mind and re-evaluate as I land mor
| |
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(); | 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. Preferred type at the | |
73 // front of vector. | |
74 static void GetSupportedTypes(std::vector<DiscardableMemoryType>* types); | |
75 | |
76 // Sets the current discardable memory type. | |
77 static void SetType(DiscardableMemoryType type); | |
78 | |
79 // Gets the current discardable memory type. | |
80 static DiscardableMemoryType GetType(); | |
59 | 81 |
60 static scoped_ptr<DiscardableMemory> CreateLockedMemory(size_t size); | 82 static scoped_ptr<DiscardableMemory> CreateLockedMemory(size_t size); |
61 | 83 |
62 // Locks the memory so that it will not be purged by the system. Returns | 84 // 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 | 85 // DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS on success. If the return value is |
64 // DISCARDABLE_MEMORY_FAILED then this object should be discarded and | 86 // DISCARDABLE_MEMORY_LOCK_STATUS_FAILED then this object should be |
65 // a new one should be created. If the return value is | 87 // 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 | 88 // DISCARDABLE_MEMORY_LOCK_STATUS_PURGED then the memory is present but any |
67 // was in it is gone. | 89 // data that was in it is gone. |
68 virtual LockDiscardableMemoryStatus Lock() WARN_UNUSED_RESULT = 0; | 90 virtual DiscardableMemoryLockStatus Lock() WARN_UNUSED_RESULT = 0; |
69 | 91 |
70 // Unlocks the memory so that it can be purged by the system. Must be called | 92 // Unlocks the memory so that it can be purged by the system. Must be called |
71 // after every successful lock call. | 93 // after every successful lock call. |
72 virtual void Unlock() = 0; | 94 virtual void Unlock() = 0; |
73 | 95 |
74 // Returns the memory address held by this object. The object must be locked | 96 // Returns the memory address held by this object. The object must be locked |
75 // before calling this. Otherwise, this will cause a DCHECK error. | 97 // before calling this. Otherwise, this will cause a DCHECK error. |
76 virtual void* Memory() const = 0; | 98 virtual void* Memory() const = 0; |
77 | 99 |
78 // Testing utility calls. | 100 // Testing utility calls. |
79 | 101 |
80 // Check whether a purge of all discardable memory in the system is supported. | 102 // Check whether a purge of all discardable memory in the system is supported. |
81 // Use only for testing! | 103 // Use only for testing! |
82 static bool PurgeForTestingSupported(); | 104 static bool PurgeForTestingSupported(); |
83 | 105 |
84 // Purge all discardable memory in the system. This call has global effects | 106 // 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! | 107 // across all running processes, so it should only be used for testing! |
86 static void PurgeForTesting(); | 108 static void PurgeForTesting(); |
87 }; | 109 }; |
88 | 110 |
89 } // namespace base | 111 } // namespace base |
90 | 112 |
91 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_H_ | 113 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_H_ |
OLD | NEW |