Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 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; | |
|
Avi (use Gerrit)
2013/06/18 00:00:46
no indent
| |
| 16 } // namespace base | |
| 17 | |
| 18 #if defined(COMPILER_GCC) | |
| 19 namespace BASE_HASH_NAMESPACE { | |
| 20 template <> | |
| 21 struct hash<base::DiscardableMemory*> { | |
| 22 size_t operator()(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 | |
| 31 // The DiscardableMemoryProvider is used on platforms that do not support | |
| 32 // discardable memory natively. It keeps track of all DiscardableMemory | |
| 33 // instances (in case they need to be purged), and the total amount of allocated | |
| 34 // memory (in case this forces a purge). | |
| 35 class BASE_EXPORT DiscardableMemoryProvider { | |
| 36 public: | |
| 37 // The bool here is just a placeholder. The MRU cache acts like a map, but we | |
| 38 // only actually care about the keys. | |
| 39 typedef HashingMRUCache<DiscardableMemory*, bool> AllocationMap; | |
| 40 | |
| 41 DiscardableMemoryProvider(); | |
| 42 virtual ~DiscardableMemoryProvider(); | |
| 43 | |
| 44 static DiscardableMemoryProvider* GetInstance(); | |
| 45 | |
| 46 // This can be called as a hint that the system is under memory pressure. | |
| 47 static void NotifyMemoryPressure(MemoryPressureListener::MemoryPressureLevel); | |
|
Avi (use Gerrit)
2013/06/18 00:00:46
you need to name your param
| |
| 48 | |
| 49 // The maximum number of bytes of discardable memory that may be allocated | |
| 50 // before we assume moderate memory pressure. If this amount is zero, it is | |
| 51 // interpreted as having no limit at all. | |
| 52 void SetDiscardableMemoryLimit(size_t bytes); | |
| 53 | |
| 54 // Gets the maximum amount of discardable memory that may be allocated. | |
| 55 size_t discardable_memory_limit() const; | |
| 56 | |
| 57 // Sets the amount of memory to reclaim when we're under moderate pressure. | |
| 58 void SetBytesToReclaimUnderModeratePressure(size_t bytes); | |
| 59 | |
| 60 // Gets the amount of memory to reclaim when we're under moderate pressure. | |
| 61 size_t bytes_to_reclaim_under_moderate_pressure() const; | |
| 62 | |
| 63 private: | |
| 64 friend class DiscardableMemory; | |
| 65 friend class DiscardableMemoryProviderTest; | |
| 66 | |
| 67 // Sets the instance of DiscardableMemoryProvider to be returned by | |
| 68 // GetInstance. This should only be used by tests. The ownership of the given | |
| 69 // provider is retained by the caller. | |
| 70 static void SetInstanceForTest(DiscardableMemoryProvider* provider); | |
| 71 | |
| 72 // Adds the given discardable memory to the provider's collection. If the | |
| 73 // discardable has already been added, it is first unregistered and its | |
| 74 // memory deallocated. | |
| 75 void Register(DiscardableMemory* discardable); | |
| 76 | |
| 77 // Removes the given discardable memory from the provider's collection. | |
| 78 void Unregister(DiscardableMemory* discardable); | |
| 79 | |
| 80 // Called by discardable memory instances to allow the provider to keep track | |
| 81 // of the total amout of discardable memory allocated. | |
| 82 void DidAllocate(size_t bytes); | |
| 83 | |
| 84 // Called by discardable memory instances to allow the provider to keep track | |
| 85 // of the total amount of discardable memory allocated. | |
| 86 void DidDeallocate(size_t bytes); | |
| 87 | |
| 88 // Called by discardable memory instances to allow the provider to maintain | |
| 89 // its MRU cache. Returns false if the discardable has not been registered. | |
| 90 bool DidAccess(DiscardableMemory* discardable); | |
| 91 | |
| 92 // Purges all discardable memory. | |
| 93 void PurgeAll(); | |
| 94 | |
| 95 // Purges least recently used memory. | |
| 96 void PurgeLRU(); | |
| 97 | |
| 98 // Ensures that we don't allocate beyond our memory limit. | |
| 99 void EnforcePolicy(); | |
| 100 | |
| 101 // A MRU cache of all allocated bits of discardable memory. Used for purging. | |
| 102 AllocationMap allocations_; | |
| 103 | |
| 104 // The total amount of allocated discardable memory. | |
| 105 size_t bytes_allocated_; | |
| 106 | |
| 107 // The maximum number of bytes of discardable memory that may be allocated | |
| 108 // before we assume moderate memory pressure. | |
| 109 size_t discardable_memory_limit_; | |
| 110 | |
| 111 // Under moderate memory pressure, we will purge this amount of memory. | |
| 112 size_t bytes_to_reclaim_under_moderate_pressure_; | |
| 113 | |
| 114 // Needs to be held when accessing |allocations_|. | |
| 115 Lock allocations_lock_; | |
| 116 | |
| 117 // Needs to be held when accessing |bytes_allocated_| or the two limits above. | |
| 118 Lock bytes_allocated_lock_; | |
| 119 | |
| 120 // Allows us to be respond when the system reports that it is under memory | |
| 121 // pressure. | |
| 122 scoped_ptr<MemoryPressureListener> memory_pressure_listener_; | |
| 123 | |
| 124 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryProvider); | |
| 125 }; | |
| 126 | |
| 127 } // namespace base | |
| 128 | |
| 129 #endif // BASE_MEMORY_DISCARDABLE_MEMORY_PROVIDER_H_ | |
| OLD | NEW |