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 { | |
19 DISCARDABLE_MEMORY_TYPE_NONE, | |
20 DISCARDABLE_MEMORY_TYPE_SHMEM | |
21 }; | |
22 | |
23 // Platform abstraction for discardable memory. DiscardableMemory is used to | 18 // Platform abstraction for discardable memory. DiscardableMemory is used to |
24 // cache large objects without worrying about blowing out memory, both on mobile | 19 // cache large objects without worrying about blowing out memory, both on mobile |
25 // devices where there is no swap, and desktop devices where unused free memory | 20 // devices where there is no swap, and desktop devices where unused free memory |
26 // should be used to help the user experience. This is preferable to releasing | 21 // should be used to help the user experience. This is preferable to releasing |
27 // memory in response to an OOM signal because it is simpler, though it has less | 22 // memory in response to an OOM signal because it is simpler, though it has less |
28 // flexibility as to which objects get discarded. | 23 // flexibility as to which objects get discarded. |
29 // | 24 // |
30 // Discardable memory has two states: locked and unlocked. While the memory is | 25 // Discardable memory has two states: locked and unlocked. While the memory is |
31 // locked, it will not be discarded. Unlocking the memory allows the OS to | 26 // locked, it will not be discarded. Unlocking the memory allows the OS to |
32 // reclaim it if needed. Locks do not nest. | 27 // reclaim it if needed. Locks do not nest. |
(...skipping 15 matching lines...) Expand all Loading... |
48 // - Linux: http://lwn.net/Articles/452035/ | 43 // - Linux: http://lwn.net/Articles/452035/ |
49 // - Mac: http://trac.webkit.org/browser/trunk/Source/WebCore/platform/mac/Pur
geableBufferMac.cpp | 44 // - Mac: http://trac.webkit.org/browser/trunk/Source/WebCore/platform/mac/Pur
geableBufferMac.cpp |
50 // the comment starting with "vm_object_purgable_control" at | 45 // the comment starting with "vm_object_purgable_control" at |
51 // http://www.opensource.apple.com/source/xnu/xnu-792.13.8/osfmk/vm/v
m_object.c | 46 // http://www.opensource.apple.com/source/xnu/xnu-792.13.8/osfmk/vm/v
m_object.c |
52 // | 47 // |
53 // Thread-safety: DiscardableMemory instances are not thread-safe. | 48 // Thread-safety: DiscardableMemory instances are not thread-safe. |
54 class BASE_EXPORT DiscardableMemory { | 49 class BASE_EXPORT DiscardableMemory { |
55 public: | 50 public: |
56 virtual ~DiscardableMemory() {} | 51 virtual ~DiscardableMemory() {} |
57 | 52 |
58 // Gets the discardable memory type with a given name. | 53 // Create a DiscardableMemory instance with |size|. |
59 static DiscardableMemoryType GetNamedType(const std::string& name); | |
60 | |
61 // Gets the name of a discardable memory type. | |
62 static const char* GetTypeName(DiscardableMemoryType type); | |
63 | |
64 // Gets system supported discardable memory types. Default preferred type | |
65 // at the front of vector. | |
66 static void GetSupportedTypes(std::vector<DiscardableMemoryType>* types); | |
67 | |
68 // Sets the preferred discardable memory type. This overrides the default | |
69 // preferred type. Can only be called once prior to GetPreferredType() | |
70 // or CreateLockedMemory(). Caller is responsible for correct ordering. | |
71 static void SetPreferredType(DiscardableMemoryType type); | |
72 | |
73 // Gets the preferred discardable memory type. | |
74 static DiscardableMemoryType GetPreferredType(); | |
75 | |
76 // Create a DiscardableMemory instance with specified |type| and |size|. | |
77 static scoped_ptr<DiscardableMemory> CreateLockedMemoryWithType( | |
78 DiscardableMemoryType type, size_t size); | |
79 | |
80 // Create a DiscardableMemory instance with preferred type and |size|. | |
81 static scoped_ptr<DiscardableMemory> CreateLockedMemory(size_t size); | 54 static scoped_ptr<DiscardableMemory> CreateLockedMemory(size_t size); |
82 | 55 |
83 // Locks the memory so that it will not be purged by the system. Returns | 56 // Locks the memory so that it will not be purged by the system. Returns |
84 // true on success. If the return value is false then this object should be | 57 // true on success. If the return value is false then this object should be |
85 // discarded and a new one should be created. | 58 // discarded and a new one should be created. |
86 virtual bool Lock() WARN_UNUSED_RESULT = 0; | 59 virtual bool Lock() WARN_UNUSED_RESULT = 0; |
87 | 60 |
88 // Unlocks the memory so that it can be purged by the system. Must be called | 61 // Unlocks the memory so that it can be purged by the system. Must be called |
89 // after every successful lock call. | 62 // after every successful lock call. |
90 virtual void Unlock() = 0; | 63 virtual void Unlock() = 0; |
91 | 64 |
92 // Returns the memory address held by this object. The object must be locked | 65 // Returns the memory address held by this object. The object must be locked |
93 // before calling this. Otherwise, this will cause a DCHECK error. | 66 // before calling this. Otherwise, this will cause a DCHECK error. |
94 virtual void* Memory() const = 0; | 67 virtual void* Memory() const = 0; |
95 }; | 68 }; |
96 | 69 |
97 } // namespace base | 70 } // namespace base |
98 | 71 |
99 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_H_ | 72 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_H_ |
OLD | NEW |