1 // Copyright 2013 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_PROVIDER_H_
6 #define BASE_MEMORY_DISCARDABLE_MEMORY_PROVIDER_H_
8 #include "base/base_export.h"
9 #include "base/containers/hash_tables.h"
10 #include "base/containers/mru_cache.h"
11 #include "base/memory/memory_pressure_listener.h"
12 #include "base/synchronization/lock.h"
15 class DiscardableMemory;
18 #if defined(COMPILER_GCC)
19 namespace BASE_HASH_NAMESPACE {
21 struct hash<const base::DiscardableMemory*> {
22 size_t operator()(const base::DiscardableMemory* ptr) const {
23 return hash<size_t>()(reinterpret_cast<size_t>(ptr));
26 } // namespace BASE_HASH_NAMESPACE
32 // The DiscardableMemoryProvider manages a collection of emulated
33 // DiscardableMemory instances. It is used on platforms that do not support
34 // discardable memory natively. It keeps track of all DiscardableMemory
35 // instances (in case they need to be purged), and the total amount of
36 // allocated memory (in case this forces a purge).
38 // When notified of memory pressure, the provider either purges the LRU
39 // memory -- if the pressure is moderate -- or all discardable memory
40 // if the pressure is critical.
42 // NB - this class is an implementation detail. It has been exposed for testing
43 // purposes. You should not need to use this class directly.
44 class BASE_EXPORT_PRIVATE DiscardableMemoryProvider {
46 DiscardableMemoryProvider();
47 ~DiscardableMemoryProvider();
49 static DiscardableMemoryProvider* GetInstance();
51 // Sets the instance of DiscardableMemoryProvider to be returned by
52 // GetInstance. This should only be used by tests and must be called
53 // prior to GetInstance(). The ownership of the given provider is
54 // retained by the caller.
55 static void SetInstanceForTest(DiscardableMemoryProvider* provider);
57 // The maximum number of bytes of discardable memory that may be allocated
58 // before we assume moderate memory pressure. If this amount is zero, it is
59 // interpreted as having no limit at all.
60 void SetDiscardableMemoryLimit(size_t bytes);
62 // Sets the amount of memory to reclaim when we're under moderate pressure.
63 void SetBytesToReclaimUnderModeratePressure(size_t bytes);
65 // Adds the given discardable memory to the provider's collection.
66 void Register(const DiscardableMemory* discardable, size_t bytes);
68 // Removes the given discardable memory from the provider's collection.
69 void Unregister(const DiscardableMemory* discardable);
71 // Returns NULL if an error occurred. Otherwise, returns the backing buffer
72 // and sets |purged| to indicate whether or not the backing buffer has been
73 // purged since last use.
74 scoped_ptr<uint8, FreeDeleter> Acquire(
75 const DiscardableMemory* discardable, bool* purged);
77 // Release a previously acquired backing buffer. This gives the buffer back
78 // to the provider where it can be purged if necessary.
79 void Release(const DiscardableMemory* discardable,
80 scoped_ptr<uint8, FreeDeleter> memory);
82 // Purges all discardable memory.
85 // Returns true if discardable memory has been added to the provider's
86 // collection. This should only be used by tests.
87 bool IsRegisteredForTest(const DiscardableMemory* discardable) const;
89 // Returns true if discardable memory can be purged. This should only
91 bool CanBePurgedForTest(const DiscardableMemory* discardable) const;
93 // Returns total amount of allocated discardable memory. This should only
95 size_t GetBytesAllocatedForTest() const;
99 explicit Allocation(size_t bytes)
107 typedef HashingMRUCache<const DiscardableMemory*, Allocation> AllocationMap;
109 // This can be called as a hint that the system is under memory pressure.
110 static void NotifyMemoryPressure(
111 MemoryPressureListener::MemoryPressureLevel pressure_level);
113 // Purges least recently used memory based on the value of
114 // |bytes_to_reclaim_under_moderate_pressure_|.
117 // Purges least recently used memory until usage is less or equal to |limit|.
118 // Caller must acquire |lock_| prior to calling this function.
119 void PurgeLRUWithLockAcquiredUntilUsageIsWithin(size_t limit);
121 // Ensures that we don't allocate beyond our memory limit.
122 // Caller must acquire |lock_| prior to calling this function.
123 void EnforcePolicyWithLockAcquired();
125 // Needs to be held when accessing members.
128 // A MRU cache of all allocated bits of discardable memory. Used for purging.
129 AllocationMap allocations_;
131 // The total amount of allocated discardable memory.
132 size_t bytes_allocated_;
134 // The maximum number of bytes of discardable memory that may be allocated
135 // before we assume moderate memory pressure.
136 size_t discardable_memory_limit_;
138 // Under moderate memory pressure, we will purge this amount of memory.
139 size_t bytes_to_reclaim_under_moderate_pressure_;
141 // Allows us to be respond when the system reports that it is under memory
143 MemoryPressureListener memory_pressure_listener_;
145 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryProvider);
148 } // namespace internal
151 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_PROVIDER_H_