OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #ifndef BASE_MEMORY_DISCARDABLE_MEMORY_PROVIDER_H_ |
| 6 #define BASE_MEMORY_DISCARDABLE_MEMORY_PROVIDER_H_ |
| 7 |
| 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" |
| 13 |
| 14 namespace base { |
| 15 class DiscardableMemory; |
| 16 } // namespace base |
| 17 |
| 18 #if defined(COMPILER_GCC) |
| 19 namespace BASE_HASH_NAMESPACE { |
| 20 template <> |
| 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)); |
| 24 } |
| 25 }; |
| 26 } // namespace BASE_HASH_NAMESPACE |
| 27 #endif // COMPILER |
| 28 |
| 29 namespace base { |
| 30 namespace internal { |
| 31 |
| 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). |
| 37 // |
| 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. |
| 41 // |
| 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 { |
| 45 public: |
| 46 DiscardableMemoryProvider(); |
| 47 ~DiscardableMemoryProvider(); |
| 48 |
| 49 static DiscardableMemoryProvider* GetInstance(); |
| 50 |
| 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); |
| 56 |
| 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); |
| 61 |
| 62 // Sets the amount of memory to reclaim when we're under moderate pressure. |
| 63 void SetBytesToReclaimUnderModeratePressure(size_t bytes); |
| 64 |
| 65 // Adds the given discardable memory to the provider's collection. |
| 66 void Register(const DiscardableMemory* discardable, size_t bytes); |
| 67 |
| 68 // Removes the given discardable memory from the provider's collection. |
| 69 void Unregister(const DiscardableMemory* discardable); |
| 70 |
| 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); |
| 76 |
| 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); |
| 81 |
| 82 // Purges all discardable memory. |
| 83 void PurgeAll(); |
| 84 |
| 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; |
| 88 |
| 89 // Returns true if discardable memory can be purged. This should only |
| 90 // be used by tests. |
| 91 bool CanBePurgedForTest(const DiscardableMemory* discardable) const; |
| 92 |
| 93 // Returns total amount of allocated discardable memory. This should only |
| 94 // be used by tests. |
| 95 size_t GetBytesAllocatedForTest() const; |
| 96 |
| 97 private: |
| 98 struct Allocation { |
| 99 explicit Allocation(size_t bytes) |
| 100 : bytes(bytes), |
| 101 memory(NULL) { |
| 102 } |
| 103 |
| 104 size_t bytes; |
| 105 uint8* memory; |
| 106 }; |
| 107 typedef HashingMRUCache<const DiscardableMemory*, Allocation> AllocationMap; |
| 108 |
| 109 // This can be called as a hint that the system is under memory pressure. |
| 110 static void NotifyMemoryPressure( |
| 111 MemoryPressureListener::MemoryPressureLevel pressure_level); |
| 112 |
| 113 // Purges least recently used memory based on the value of |
| 114 // |bytes_to_reclaim_under_moderate_pressure_|. |
| 115 void Purge(); |
| 116 |
| 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); |
| 120 |
| 121 // Ensures that we don't allocate beyond our memory limit. |
| 122 // Caller must acquire |lock_| prior to calling this function. |
| 123 void EnforcePolicyWithLockAcquired(); |
| 124 |
| 125 // Needs to be held when accessing members. |
| 126 mutable Lock lock_; |
| 127 |
| 128 // A MRU cache of all allocated bits of discardable memory. Used for purging. |
| 129 AllocationMap allocations_; |
| 130 |
| 131 // The total amount of allocated discardable memory. |
| 132 size_t bytes_allocated_; |
| 133 |
| 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_; |
| 137 |
| 138 // Under moderate memory pressure, we will purge this amount of memory. |
| 139 size_t bytes_to_reclaim_under_moderate_pressure_; |
| 140 |
| 141 // Allows us to be respond when the system reports that it is under memory |
| 142 // pressure. |
| 143 MemoryPressureListener memory_pressure_listener_; |
| 144 |
| 145 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryProvider); |
| 146 }; |
| 147 |
| 148 } // namespace internal |
| 149 } // namespace base |
| 150 |
| 151 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_PROVIDER_H_ |
OLD | NEW |