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 |
| 50 // |
| 51 // Thread-safety: DiscardableMemory instances are not thread-safe. |
47 class BASE_EXPORT DiscardableMemory { | 52 class BASE_EXPORT DiscardableMemory { |
48 public: | 53 public: |
49 virtual ~DiscardableMemory() {} | 54 virtual ~DiscardableMemory() {} |
50 | 55 |
51 // Returns whether the system supports discardable memory. | 56 // Returns whether the system supports discardable memory. |
52 static bool Supported(); | 57 static bool Supported(); |
53 | 58 |
54 static scoped_ptr<DiscardableMemory> CreateLockedMemory(size_t size); | 59 static scoped_ptr<DiscardableMemory> CreateLockedMemory(size_t size); |
55 | 60 |
| 61 // Returns the memory's actual size that is greater or equal than the |
| 62 // size requested at construction. |
| 63 virtual size_t Size() const = 0; |
| 64 |
56 // Locks the memory so that it will not be purged by the system. Returns | 65 // 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 | 66 // DISCARDABLE_MEMORY_SUCCESS on success. If the return value is |
58 // DISCARDABLE_MEMORY_FAILED then this object should be discarded and | 67 // DISCARDABLE_MEMORY_FAILED then this object should be discarded and |
59 // a new one should be created. If the return value is | 68 // a new one should be created. If the return value is |
60 // DISCARDABLE_MEMORY_PURGED then the memory is present but any data that | 69 // DISCARDABLE_MEMORY_PURGED then the memory is present but any data that |
61 // was in it is gone. | 70 // was in it is gone. |
62 virtual LockDiscardableMemoryStatus Lock() WARN_UNUSED_RESULT = 0; | 71 virtual LockDiscardableMemoryStatus Lock() WARN_UNUSED_RESULT = 0; |
63 | 72 |
64 // Unlocks the memory so that it can be purged by the system. Must be called | 73 // Unlocks the memory so that it can be purged by the system. Must be called |
65 // after every successful lock call. | 74 // after every successful lock call. |
(...skipping 10 matching lines...) Expand all Loading... |
76 static bool PurgeForTestingSupported(); | 85 static bool PurgeForTestingSupported(); |
77 | 86 |
78 // Purge all discardable memory in the system. This call has global effects | 87 // 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! | 88 // across all running processes, so it should only be used for testing! |
80 static void PurgeForTesting(); | 89 static void PurgeForTesting(); |
81 }; | 90 }; |
82 | 91 |
83 } // namespace base | 92 } // namespace base |
84 | 93 |
85 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_H_ | 94 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_H_ |
OLD | NEW |