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 "base/base_export.h" | 8 #include "base/base_export.h" |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 // devices where there is no swap, and desktop devices where unused free memory | 23 // 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 | 24 // 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 | 25 // memory in response to an OOM signal because it is simpler, though it has less |
26 // flexibility as to which objects get discarded. | 26 // flexibility as to which objects get discarded. |
27 // | 27 // |
28 // Discardable memory has two states: locked and unlocked. While the memory is | 28 // Discardable memory has two states: locked and unlocked. While the memory is |
29 // locked, it will not be discarded. Unlocking the memory allows the OS to | 29 // locked, it will not be discarded. Unlocking the memory allows the OS to |
30 // reclaim it if needed. Locks do not nest. | 30 // reclaim it if needed. Locks do not nest. |
31 // | 31 // |
32 // Notes: | 32 // Notes: |
| 33 // - If you need more than one instance of DiscardableMemory please use |
| 34 // DiscardableMemoryAllocator to avoid using too many file descriptors on |
| 35 // platforms where discardable memory is backed by a file. |
33 // - The paging behavior of memory while it is locked is not specified. While | 36 // - The paging behavior of memory while it is locked is not specified. While |
34 // mobile platforms will not swap it out, it may qualify for swapping | 37 // mobile platforms will not swap it out, it may qualify for swapping |
35 // on desktop platforms. It is not expected that this will matter, as the | 38 // on desktop platforms. It is not expected that this will matter, as the |
36 // preferred pattern of usage for DiscardableMemory is to lock down the | 39 // preferred pattern of usage for DiscardableMemory is to lock down the |
37 // memory, use it as quickly as possible, and then unlock it. | 40 // memory, use it as quickly as possible, and then unlock it. |
38 // - Because of memory alignment, the amount of memory allocated can be | 41 // - Because of memory alignment, the amount of memory allocated can be |
39 // larger than the requested memory size. It is not very efficient for | 42 // larger than the requested memory size. It is not very efficient for |
40 // small allocations. | 43 // small allocations. |
41 // | 44 // |
42 // References: | 45 // References: |
43 // - Linux: http://lwn.net/Articles/452035/ | 46 // - Linux: http://lwn.net/Articles/452035/ |
44 // - Mac: http://trac.webkit.org/browser/trunk/Source/WebCore/platform/mac/Pur
geableBufferMac.cpp | 47 // - Mac: http://trac.webkit.org/browser/trunk/Source/WebCore/platform/mac/Pur
geableBufferMac.cpp |
45 // the comment starting with "vm_object_purgable_control" at | 48 // the comment starting with "vm_object_purgable_control" at |
46 // http://www.opensource.apple.com/source/xnu/xnu-792.13.8/osfmk/vm/v
m_object.c | 49 // http://www.opensource.apple.com/source/xnu/xnu-792.13.8/osfmk/vm/v
m_object.c |
47 class BASE_EXPORT DiscardableMemory { | 50 class BASE_EXPORT DiscardableMemory { |
48 public: | 51 public: |
49 virtual ~DiscardableMemory() {} | 52 virtual ~DiscardableMemory() {} |
50 | 53 |
51 // Returns whether the system supports discardable memory. | 54 // Returns whether the system supports discardable memory. |
52 static bool Supported(); | 55 static bool Supported(); |
53 | 56 |
54 static scoped_ptr<DiscardableMemory> CreateLockedMemory(size_t size); | 57 static scoped_ptr<DiscardableMemory> CreateLockedMemory(size_t size); |
55 | 58 |
| 59 size_t size() const { return size_; } |
| 60 |
56 // Locks the memory so that it will not be purged by the system. Returns | 61 // Locks the memory so that it will not be purged by the system. Returns |
57 // DISCARDABLE_MEMORY_SUCCESS on success. If the return value is | 62 // DISCARDABLE_MEMORY_SUCCESS on success. If the return value is |
58 // DISCARDABLE_MEMORY_FAILED then this object should be discarded and | 63 // DISCARDABLE_MEMORY_FAILED then this object should be discarded and |
59 // a new one should be created. If the return value is | 64 // a new one should be created. If the return value is |
60 // DISCARDABLE_MEMORY_PURGED then the memory is present but any data that | 65 // DISCARDABLE_MEMORY_PURGED then the memory is present but any data that |
61 // was in it is gone. | 66 // was in it is gone. |
62 virtual LockDiscardableMemoryStatus Lock() WARN_UNUSED_RESULT = 0; | 67 virtual LockDiscardableMemoryStatus Lock() WARN_UNUSED_RESULT = 0; |
63 | 68 |
64 // Unlocks the memory so that it can be purged by the system. Must be called | 69 // Unlocks the memory so that it can be purged by the system. Must be called |
65 // after every successful lock call. | 70 // after every successful lock call. |
66 virtual void Unlock() = 0; | 71 virtual void Unlock() = 0; |
67 | 72 |
68 // Returns the memory address held by this object. The object must be locked | 73 // Returns the memory address held by this object. The object must be locked |
69 // before calling this. Otherwise, this will cause a DCHECK error. | 74 // before calling this. Otherwise, this will cause a DCHECK error. |
70 virtual void* Memory() const = 0; | 75 virtual void* Memory() const = 0; |
71 | 76 |
72 // Testing utility calls. | 77 // Testing utility calls. |
73 | 78 |
74 // Check whether a purge of all discardable memory in the system is supported. | 79 // Check whether a purge of all discardable memory in the system is supported. |
75 // Use only for testing! | 80 // Use only for testing! |
76 static bool PurgeForTestingSupported(); | 81 static bool PurgeForTestingSupported(); |
77 | 82 |
78 // Purge all discardable memory in the system. This call has global effects | 83 // Purge all discardable memory in the system. This call has global effects |
79 // across all running processes, so it should only be used for testing! | 84 // across all running processes, so it should only be used for testing! |
80 static void PurgeForTesting(); | 85 static void PurgeForTesting(); |
| 86 |
| 87 protected: |
| 88 DiscardableMemory(size_t size); |
| 89 |
| 90 const size_t size_; |
81 }; | 91 }; |
82 | 92 |
83 } // namespace base | 93 } // namespace base |
84 | 94 |
85 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_H_ | 95 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_H_ |
OLD | NEW |