Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3565)

Unified Diff: base/memory/discardable_memory_manager.h

Issue 204733003: base: Refactor DiscardableMemoryManager for use with different type of allocations. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add missing unlock call and remove unnecessary unlock call Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: base/memory/discardable_memory_manager.h
diff --git a/base/memory/discardable_memory_manager.h b/base/memory/discardable_memory_manager.h
index 9ad5be7ee2d1cad0697f3af2f02557e8230e3f89..86f1e65d2760ab09ad19eddd98e1f8edf23a62bf 100644
--- a/base/memory/discardable_memory_manager.h
+++ b/base/memory/discardable_memory_manager.h
@@ -8,25 +8,11 @@
#include "base/base_export.h"
#include "base/containers/hash_tables.h"
#include "base/containers/mru_cache.h"
-#include "base/memory/memory_pressure_listener.h"
+#include "base/memory/discardable_memory_allocation.h"
+#include "base/memory/linked_ptr.h"
#include "base/synchronization/lock.h"
namespace base {
-class DiscardableMemory;
-} // namespace base
-
-#if defined(COMPILER_GCC)
-namespace BASE_HASH_NAMESPACE {
-template <>
-struct hash<const base::DiscardableMemory*> {
- size_t operator()(const base::DiscardableMemory* ptr) const {
- return hash<size_t>()(reinterpret_cast<size_t>(ptr));
- }
-};
-} // namespace BASE_HASH_NAMESPACE
-#endif // COMPILER
-
-namespace base {
namespace internal {
// The DiscardableMemoryManager manages a collection of emulated
@@ -43,75 +29,58 @@ namespace internal {
// purposes. You should not need to use this class directly.
class BASE_EXPORT_PRIVATE DiscardableMemoryManager {
public:
- DiscardableMemoryManager();
- ~DiscardableMemoryManager();
-
- // Call this to register memory pressure listener. Must be called on a
- // thread with a MessageLoop current.
- void RegisterMemoryPressureListener();
+ typedef int AllocationId;
- // Call this to unregister memory pressure listener.
- void UnregisterMemoryPressureListener();
+ explicit DiscardableMemoryManager(
+ DiscardableMemoryAllocation::Factory* allocation_factory,
+ size_t discardable_memory_limit);
+ virtual ~DiscardableMemoryManager();
// The maximum number of bytes of discardable memory that may be allocated
// before we force a purge. If this amount is zero, it is interpreted as
// having no limit at all.
void SetDiscardableMemoryLimit(size_t bytes);
- // Sets the amount of memory to keep when we're under moderate pressure.
- void SetBytesToKeepUnderModeratePressure(size_t bytes);
-
- // Adds the given discardable memory to the manager's collection.
- void Register(const DiscardableMemory* discardable, size_t bytes);
+ // Adds a discardable memory allocation to the manager's collection.
+ AllocationId Register(size_t bytes);
// Removes the given discardable memory from the manager's collection.
- void Unregister(const DiscardableMemory* discardable);
+ void Unregister(AllocationId allocation_id);
- // Returns NULL if an error occurred. Otherwise, returns the backing buffer
- // and sets |purged| to indicate whether or not the backing buffer has been
- // purged since last use.
- scoped_ptr<uint8, FreeDeleter> Acquire(
- const DiscardableMemory* discardable, bool* purged);
+ // Returns NULL if an error occurred. Otherwise, returns the a pointer to
+ // the memory and sets |purged| to indicate whether or not the allocation
+ // has been purged since last use.
+ void* Lock(AllocationId allocation_id, bool* purged);
- // Release a previously acquired backing buffer. This gives the buffer back
- // to the manager where it can be purged if necessary.
- void Release(const DiscardableMemory* discardable,
- scoped_ptr<uint8, FreeDeleter> memory);
+ // Unlock a previously locked allocation. This allows the manager to purge
+ // it if necessary.
+ void Unlock(AllocationId allocation_id);
- // Purges all discardable memory.
- void PurgeAll();
+ // Purges memory until usage is less or equal to |limit|.
+ void PurgeUntilUsageIsWithin(size_t limit);
- // Returns true if discardable memory has been added to the manager's
- // collection. This should only be used by tests.
- bool IsRegisteredForTest(const DiscardableMemory* discardable) const;
+ // Returns true if discardable memory allocation has been added to the
+ // manager's collection. This should only be used by tests.
+ bool IsRegisteredForTest(AllocationId allocation_id) const;
- // Returns true if discardable memory can be purged. This should only
- // be used by tests.
- bool CanBePurgedForTest(const DiscardableMemory* discardable) const;
+ // Returns true if discardable memory allocation can be purged. This should
+ // only be used by tests.
+ bool CanBePurgedForTest(AllocationId allocation_id) const;
// Returns total amount of allocated discardable memory. This should only
// be used by tests.
size_t GetBytesAllocatedForTest() const;
private:
- struct Allocation {
- explicit Allocation(size_t bytes)
- : bytes(bytes),
- memory(NULL) {
- }
+ struct AllocationInfo {
+ explicit AllocationInfo(size_t bytes);
+ ~AllocationInfo();
+ linked_ptr<DiscardableMemoryAllocation> allocation;
willchan no longer on Chromium 2014/04/01 01:11:08 So sad that we don't have unique_ptr yet :( linked
size_t bytes;
- uint8* memory;
+ bool locked;
};
- typedef HashingMRUCache<const DiscardableMemory*, Allocation> AllocationMap;
-
- // This can be called as a hint that the system is under memory pressure.
- void OnMemoryPressure(
- MemoryPressureListener::MemoryPressureLevel pressure_level);
-
- // Purges until discardable memory usage is within
- // |bytes_to_keep_under_moderate_pressure_|.
- void Purge();
+ typedef HashingMRUCache<AllocationId, AllocationInfo> AllocationMap;
// Purges least recently used memory until usage is less or equal to |limit|.
// Caller must acquire |lock_| prior to calling this function.
@@ -122,7 +91,13 @@ class BASE_EXPORT_PRIVATE DiscardableMemoryManager {
void EnforcePolicyWithLockAcquired();
// Needs to be held when accessing members.
- mutable Lock lock_;
+ mutable base::Lock lock_;
willchan no longer on Chromium 2014/04/01 01:11:08 Not really necessary, but who cares I guess.
reveman 2014/04/03 17:02:12 Necessary as this class now has a function named "
+
+ // Factory instance used to create new discardable memory allocations.
+ DiscardableMemoryAllocation::Factory* allocation_factory_;
+
+ // Unique ID used for next allocation.
+ AllocationId next_allocation_id_;
// A MRU cache of all allocated bits of discardable memory. Used for purging.
AllocationMap allocations_;
@@ -133,14 +108,6 @@ class BASE_EXPORT_PRIVATE DiscardableMemoryManager {
// The maximum number of bytes of discardable memory that may be allocated.
size_t discardable_memory_limit_;
- // Under moderate memory pressure, we will purge until usage is within this
- // limit.
- size_t bytes_to_keep_under_moderate_pressure_;
-
- // Allows us to be respond when the system reports that it is under memory
- // pressure.
- scoped_ptr<MemoryPressureListener> memory_pressure_listener_;
-
DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryManager);
};

Powered by Google App Engine
This is Rietveld 408576698