Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | b8da8abc | 2013-01-22 01:24:26 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_MEMORY_DISCARDABLE_MEMORY_H_ |
| 6 | #define BASE_MEMORY_DISCARDABLE_MEMORY_H_ |
| 7 | |
| 8 | #include "base/base_export.h" |
Gordon Guan | f2f7a840 | 2019-11-27 18:26:44 | [diff] [blame] | 9 | #include "build/build_config.h" |
[email protected] | b8da8abc | 2013-01-22 01:24:26 | [diff] [blame] | 10 | |
| 11 | namespace base { |
| 12 | |
ssid | 3e37155 | 2015-08-25 15:33:20 | [diff] [blame] | 13 | namespace trace_event { |
| 14 | class MemoryAllocatorDump; |
| 15 | class ProcessMemoryDump; |
Gordon Guan | ce7717d | 2019-10-30 00:43:12 | [diff] [blame] | 16 | } // namespace trace_event |
ssid | 3e37155 | 2015-08-25 15:33:20 | [diff] [blame] | 17 | |
reveman | e7b4233 | 2015-03-18 04:40:19 | [diff] [blame] | 18 | // Discardable memory is used to cache large objects without worrying about |
| 19 | // blowing out memory, both on mobile devices where there is no swap, and |
| 20 | // desktop devices where unused free memory should be used to help the user |
| 21 | // experience. This is preferable to releasing memory in response to an OOM |
| 22 | // signal because it is simpler and provides system-wide management of |
| 23 | // purgable memory, though it has less flexibility as to which objects get |
| 24 | // discarded. |
[email protected] | 345379a | 2013-01-23 20:27:51 | [diff] [blame] | 25 | // |
| 26 | // Discardable memory has two states: locked and unlocked. While the memory is |
reveman | e7b4233 | 2015-03-18 04:40:19 | [diff] [blame] | 27 | // locked, it will not be discarded. Unlocking the memory allows the |
| 28 | // discardable memory system and the OS to reclaim it if needed. Locks do not |
| 29 | // nest. |
[email protected] | 345379a | 2013-01-23 20:27:51 | [diff] [blame] | 30 | // |
| 31 | // Notes: |
| 32 | // - The paging behavior of memory while it is locked is not specified. While |
| 33 | // mobile platforms will not swap it out, it may qualify for swapping |
| 34 | // on desktop platforms. It is not expected that this will matter, as the |
| 35 | // preferred pattern of usage for DiscardableMemory is to lock down the |
| 36 | // memory, use it as quickly as possible, and then unlock it. |
| 37 | // - Because of memory alignment, the amount of memory allocated can be |
| 38 | // larger than the requested memory size. It is not very efficient for |
| 39 | // small allocations. |
[email protected] | cef6c76f | 2013-10-30 16:33:30 | [diff] [blame] | 40 | // - A discardable memory instance is not thread safe. It is the |
| 41 | // responsibility of users of discardable memory to ensure there are no |
| 42 | // races. |
[email protected] | 345379a | 2013-01-23 20:27:51 | [diff] [blame] | 43 | // |
[email protected] | b8da8abc | 2013-01-22 01:24:26 | [diff] [blame] | 44 | class BASE_EXPORT DiscardableMemory { |
| 45 | public: |
reveman | e7b4233 | 2015-03-18 04:40:19 | [diff] [blame] | 46 | DiscardableMemory(); |
| 47 | virtual ~DiscardableMemory(); |
[email protected] | b8da8abc | 2013-01-22 01:24:26 | [diff] [blame] | 48 | |
[email protected] | 9923ee1 | 2013-10-21 16:54:06 | [diff] [blame] | 49 | // Locks the memory so that it will not be purged by the system. Returns |
reveman | ef908422 | 2015-03-12 17:46:23 | [diff] [blame] | 50 | // true on success. If the return value is false then this object should be |
Gordon Guan | ce7717d | 2019-10-30 00:43:12 | [diff] [blame] | 51 | // destroyed and a new one should be created. |
Daniel Cheng | 4455c984 | 2022-01-13 23:26:37 | [diff] [blame] | 52 | [[nodiscard]] virtual bool Lock() = 0; |
[email protected] | b8da8abc | 2013-01-22 01:24:26 | [diff] [blame] | 53 | |
[email protected] | 9923ee1 | 2013-10-21 16:54:06 | [diff] [blame] | 54 | // Unlocks the memory so that it can be purged by the system. Must be called |
[email protected] | b8da8abc | 2013-01-22 01:24:26 | [diff] [blame] | 55 | // after every successful lock call. |
[email protected] | 9923ee1 | 2013-10-21 16:54:06 | [diff] [blame] | 56 | virtual void Unlock() = 0; |
[email protected] | b8da8abc | 2013-01-22 01:24:26 | [diff] [blame] | 57 | |
[email protected] | 9923ee1 | 2013-10-21 16:54:06 | [diff] [blame] | 58 | // Returns the memory address held by this object. The object must be locked |
reveman | 2cc373d | 2015-04-16 20:52:18 | [diff] [blame] | 59 | // before calling this. |
| 60 | virtual void* data() const = 0; |
| 61 | |
Gordon Guan | ce7717d | 2019-10-30 00:43:12 | [diff] [blame] | 62 | // Forces the memory to be purged, such that any following Lock() will fail. |
| 63 | // The object must be unlocked before calling this. |
| 64 | virtual void DiscardForTesting() = 0; |
| 65 | |
reveman | 2cc373d | 2015-04-16 20:52:18 | [diff] [blame] | 66 | // Handy method to simplify calling data() with a reinterpret_cast. |
reveman | 3491410 | 2015-04-22 20:03:34 | [diff] [blame] | 67 | template<typename T> T* data_as() const { |
| 68 | return reinterpret_cast<T*>(data()); |
reveman | 2cc373d | 2015-04-16 20:52:18 | [diff] [blame] | 69 | } |
ssid | 3e37155 | 2015-08-25 15:33:20 | [diff] [blame] | 70 | |
| 71 | // Used for dumping the statistics of discardable memory allocated in tracing. |
| 72 | // Returns a new MemoryAllocatorDump in the |pmd| with the size of the |
| 73 | // discardable memory. The MemoryAllocatorDump created is owned by |pmd|. See |
| 74 | // ProcessMemoryDump::CreateAllocatorDump. |
| 75 | virtual trace_event::MemoryAllocatorDump* CreateMemoryAllocatorDump( |
| 76 | const char* name, |
| 77 | trace_event::ProcessMemoryDump* pmd) const = 0; |
[email protected] | b8da8abc | 2013-01-22 01:24:26 | [diff] [blame] | 78 | }; |
| 79 | |
Gordon Guan | f2f7a840 | 2019-11-27 18:26:44 | [diff] [blame] | 80 | enum class DiscardableMemoryBacking { kSharedMemory, kMadvFree }; |
| 81 | BASE_EXPORT DiscardableMemoryBacking GetDiscardableMemoryBacking(); |
| 82 | |
[email protected] | b8da8abc | 2013-01-22 01:24:26 | [diff] [blame] | 83 | } // namespace base |
| 84 | |
| 85 | #endif // BASE_MEMORY_DISCARDABLE_MEMORY_H_ |