blob: bc1398c6abdbb094dafe6f02742be02fb93de322 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2013 The Chromium Authors
[email protected]b8da8abc2013-01-22 01:24:262// 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 Guanf2f7a8402019-11-27 18:26:449#include "build/build_config.h"
[email protected]b8da8abc2013-01-22 01:24:2610
11namespace base {
12
ssid3e371552015-08-25 15:33:2013namespace trace_event {
14class MemoryAllocatorDump;
15class ProcessMemoryDump;
Gordon Guance7717d2019-10-30 00:43:1216} // namespace trace_event
ssid3e371552015-08-25 15:33:2017
revemane7b42332015-03-18 04:40:1918// 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]345379a2013-01-23 20:27:5125//
26// Discardable memory has two states: locked and unlocked. While the memory is
revemane7b42332015-03-18 04:40:1927// 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]345379a2013-01-23 20:27:5130//
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]cef6c76f2013-10-30 16:33:3040// - 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]345379a2013-01-23 20:27:5143//
[email protected]b8da8abc2013-01-22 01:24:2644class BASE_EXPORT DiscardableMemory {
45 public:
revemane7b42332015-03-18 04:40:1946 DiscardableMemory();
47 virtual ~DiscardableMemory();
[email protected]b8da8abc2013-01-22 01:24:2648
[email protected]9923ee12013-10-21 16:54:0649 // Locks the memory so that it will not be purged by the system. Returns
revemanef9084222015-03-12 17:46:2350 // true on success. If the return value is false then this object should be
Gordon Guance7717d2019-10-30 00:43:1251 // destroyed and a new one should be created.
Daniel Cheng4455c9842022-01-13 23:26:3752 [[nodiscard]] virtual bool Lock() = 0;
[email protected]b8da8abc2013-01-22 01:24:2653
[email protected]9923ee12013-10-21 16:54:0654 // Unlocks the memory so that it can be purged by the system. Must be called
[email protected]b8da8abc2013-01-22 01:24:2655 // after every successful lock call.
[email protected]9923ee12013-10-21 16:54:0656 virtual void Unlock() = 0;
[email protected]b8da8abc2013-01-22 01:24:2657
[email protected]9923ee12013-10-21 16:54:0658 // Returns the memory address held by this object. The object must be locked
reveman2cc373d2015-04-16 20:52:1859 // before calling this.
60 virtual void* data() const = 0;
61
Gordon Guance7717d2019-10-30 00:43:1262 // 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
reveman2cc373d2015-04-16 20:52:1866 // Handy method to simplify calling data() with a reinterpret_cast.
reveman34914102015-04-22 20:03:3467 template<typename T> T* data_as() const {
68 return reinterpret_cast<T*>(data());
reveman2cc373d2015-04-16 20:52:1869 }
ssid3e371552015-08-25 15:33:2070
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]b8da8abc2013-01-22 01:24:2678};
79
Gordon Guanf2f7a8402019-11-27 18:26:4480enum class DiscardableMemoryBacking { kSharedMemory, kMadvFree };
81BASE_EXPORT DiscardableMemoryBacking GetDiscardableMemoryBacking();
82
[email protected]b8da8abc2013-01-22 01:24:2683} // namespace base
84
85#endif // BASE_MEMORY_DISCARDABLE_MEMORY_H_