1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef BASE_MEMORY_DISCARDABLE_MEMORY_MANAGER_H_
6 #define BASE_MEMORY_DISCARDABLE_MEMORY_MANAGER_H_
8 #include "base/base_export.h"
9 #include "base/containers/hash_tables.h"
10 #include "base/containers/mru_cache.h"
11 #include "base/synchronization/lock.h"
12 #include "base/time/time.h"
17 // This interface is used by the DiscardableMemoryManager class to provide some
18 // level of userspace control over discardable memory allocations.
19 class DiscardableMemoryManagerAllocation
{
21 // Allocate and acquire a lock that prevents the allocation from being purged
22 // by the system. Returns true if memory was previously allocated and is still
24 virtual bool AllocateAndAcquireLock() = 0;
26 // Release a previously acquired lock on the allocation so that it can be
27 // purged by the system.
28 virtual void ReleaseLock() = 0;
30 // Explicitly purge this allocation. It is illegal to call this while a lock
31 // is acquired on the allocation.
32 virtual void Purge() = 0;
35 virtual ~DiscardableMemoryManagerAllocation() {}
38 } // namespace internal
44 // The DiscardableMemoryManager manages a collection of
45 // DiscardableMemoryManagerAllocation instances. It is used on platforms that
46 // need some level of userspace control over discardable memory. It keeps track
47 // of all allocation instances (in case they need to be purged), and the total
48 // amount of allocated memory (in case this forces a purge). When memory usage
49 // reaches the limit, the manager purges the LRU memory.
50 class BASE_EXPORT_PRIVATE DiscardableMemoryManager
{
52 typedef DiscardableMemoryManagerAllocation Allocation
;
54 DiscardableMemoryManager(size_t memory_limit
,
55 size_t soft_memory_limit
,
56 TimeDelta hard_memory_limit_expiration_time
);
57 virtual ~DiscardableMemoryManager();
59 // The maximum number of bytes of memory that may be allocated before we force
61 void SetMemoryLimit(size_t bytes
);
63 // The number of bytes of memory that may be allocated but unused for the hard
64 // limit expiration time without getting purged.
65 void SetSoftMemoryLimit(size_t bytes
);
67 // Sets the memory usage cutoff time for hard memory limit.
68 void SetHardMemoryLimitExpirationTime(
69 TimeDelta hard_memory_limit_expiration_time
);
71 // This will attempt to reduce memory footprint until within soft memory
72 // limit. Returns true if there's no need to call this again until allocations
74 bool ReduceMemoryUsage();
76 // This can be called to attempt to reduce memory footprint until within
77 // limit for bytes to keep under moderate pressure.
78 void ReduceMemoryUsageUntilWithinLimit(size_t bytes
);
80 // Adds the given allocation to the manager's collection.
81 void Register(Allocation
* allocation
, size_t bytes
);
83 // Removes the given allocation from the manager's collection.
84 void Unregister(Allocation
* allocation
);
86 // Returns false if an error occurred. Otherwise, returns true and sets
87 // |purged| to indicate whether or not allocation has been purged since last
89 bool AcquireLock(Allocation
* allocation
, bool* purged
);
91 // Release a previously acquired lock on allocation. This allows the manager
92 // to purge it if necessary.
93 void ReleaseLock(Allocation
* allocation
);
95 // Purges all discardable memory.
98 // Returns true if allocation has been added to the manager's collection. This
99 // should only be used by tests.
100 bool IsRegisteredForTest(Allocation
* allocation
) const;
102 // Returns true if allocation can be purged. This should only be used by
104 bool CanBePurgedForTest(Allocation
* allocation
) const;
106 // Returns total amount of allocated discardable memory. This should only be
108 size_t GetBytesAllocatedForTest() const;
111 struct AllocationInfo
{
112 explicit AllocationInfo(size_t bytes
) : bytes(bytes
), purgable(false) {}
116 TimeTicks last_usage
;
118 typedef HashingMRUCache
<Allocation
*, AllocationInfo
> AllocationMap
;
120 // Purges memory not used since |hard_memory_limit_expiration_time_| before
121 // "right now" until usage is less or equal to |soft_memory_limit_|.
122 // Returns true if total amount of memory is less or equal to soft memory
124 bool PurgeIfNotUsedSinceHardLimitCutoffUntilWithinSoftMemoryLimit();
126 // Purges memory that has not been used since |timestamp| until usage is less
127 // or equal to |limit|.
128 // Caller must acquire |lock_| prior to calling this function.
129 void PurgeIfNotUsedSinceTimestampUntilUsageIsWithinLimitWithLockAcquired(
133 // Called when a change to |bytes_allocated_| has been made.
134 void BytesAllocatedChanged(size_t new_bytes_allocated
) const;
136 // Virtual for tests.
137 virtual TimeTicks
Now() const;
139 // Needs to be held when accessing members.
142 // A MRU cache of all allocated bits of memory. Used for purging.
143 AllocationMap allocations_
;
145 // The total amount of allocated memory.
146 size_t bytes_allocated_
;
148 // The maximum number of bytes of memory that may be allocated.
149 size_t memory_limit_
;
151 // The number of bytes of memory that may be allocated but not used for
152 // |hard_memory_limit_expiration_time_| amount of time when receiving an idle
154 size_t soft_memory_limit_
;
156 // Amount of time it takes for an allocation to become affected by
157 // |soft_memory_limit_|.
158 TimeDelta hard_memory_limit_expiration_time_
;
160 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryManager
);
163 } // namespace internal
166 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_MANAGER_H_