| Index: base/memory/discardable_memory_manager.h
|
| diff --git a/base/memory/discardable_memory_manager.h b/base/memory/discardable_memory_manager.h
|
| index 7c0bd5ccc9401ceefad54416360da27b1381a067..844752151e64fe0b06df2a88fe2ab43da0fe619e 100644
|
| --- a/base/memory/discardable_memory_manager.h
|
| +++ b/base/memory/discardable_memory_manager.h
|
| @@ -12,14 +12,38 @@
|
| #include "base/synchronization/lock.h"
|
|
|
| namespace base {
|
| -class DiscardableMemory;
|
| +namespace internal {
|
| +
|
| +// This interface is used by the DiscardableMemoryManager class to provide some
|
| +// level of userspace control over discardable memory allocations.
|
| +class DiscardableMemoryManagerAllocation {
|
| + public:
|
| + // Allocate and acquire a lock that prevents the allocation from being purged
|
| + // by the system. Returns true if memory was previously allocated and is still
|
| + // resident.
|
| + virtual bool AllocateAndAcquireLock(size_t bytes) = 0;
|
| +
|
| + // Release a previously acquired lock on the allocation so that it can be
|
| + // purged by the system.
|
| + virtual void ReleaseLock() = 0;
|
| +
|
| + // Explicitly purge this allocation. It is illegal to call this while a lock
|
| + // is acquired on the allocation.
|
| + virtual void Purge() = 0;
|
| +
|
| + protected:
|
| + virtual ~DiscardableMemoryManagerAllocation() {}
|
| +};
|
| +
|
| +} // namespace internal
|
| } // namespace base
|
|
|
| #if defined(COMPILER_GCC)
|
| namespace BASE_HASH_NAMESPACE {
|
| template <>
|
| -struct hash<const base::DiscardableMemory*> {
|
| - size_t operator()(const base::DiscardableMemory* ptr) const {
|
| +struct hash<base::internal::DiscardableMemoryManagerAllocation*> {
|
| + size_t operator()(
|
| + base::internal::DiscardableMemoryManagerAllocation* ptr) const {
|
| return hash<size_t>()(reinterpret_cast<size_t>(ptr));
|
| }
|
| };
|
| @@ -29,87 +53,82 @@ struct hash<const base::DiscardableMemory*> {
|
| namespace base {
|
| namespace internal {
|
|
|
| -// The DiscardableMemoryManager manages a collection of emulated
|
| -// DiscardableMemory instances. It is used on platforms that do not support
|
| -// discardable memory natively. It keeps track of all DiscardableMemory
|
| -// instances (in case they need to be purged), and the total amount of
|
| -// allocated memory (in case this forces a purge).
|
| -//
|
| -// When notified of memory pressure, the manager either purges the LRU
|
| -// memory -- if the pressure is moderate -- or all discardable memory
|
| -// if the pressure is critical.
|
| +// The DiscardableMemoryManager manages a collection of
|
| +// DiscardableMemoryManagerAllocation instances. It is used on platforms that
|
| +// need some level of userspace control over discardable memory. It keeps track
|
| +// of all allocation instances (in case they need to be purged), and the total
|
| +// amount of allocated memory (in case this forces a purge). When memory usage
|
| +// reaches the limit, the manager purges the LRU memory.
|
| //
|
| -// NB - this class is an implementation detail. It has been exposed for testing
|
| -// purposes. You should not need to use this class directly.
|
| +// When notified of memory pressure, the manager either purges the LRU memory --
|
| +// if the pressure is moderate -- or all discardable memory if the pressure is
|
| +// critical.
|
| class BASE_EXPORT_PRIVATE DiscardableMemoryManager {
|
| public:
|
| + typedef DiscardableMemoryManagerAllocation Allocation;
|
| +
|
| DiscardableMemoryManager();
|
| ~DiscardableMemoryManager();
|
|
|
| - // Call this to register memory pressure listener. Must be called on a
|
| - // thread with a MessageLoop current.
|
| + // Call this to register memory pressure listener. Must be called on a thread
|
| + // with a MessageLoop current.
|
| void RegisterMemoryPressureListener();
|
|
|
| // Call this to unregister memory pressure listener.
|
| void UnregisterMemoryPressureListener();
|
|
|
| - // 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);
|
| + // The maximum number of bytes of 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 SetMemoryLimit(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 the given allocation to the manager's collection.
|
| + void Register(Allocation* allocation, size_t bytes);
|
|
|
| - // Removes the given discardable memory from the manager's collection.
|
| - void Unregister(const DiscardableMemory* discardable);
|
| + // Removes the given allocation from the manager's collection.
|
| + void Unregister(Allocation* allocation);
|
|
|
| - // 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 false if an error occurred. Otherwise, returns true and sets
|
| + // |purged| to indicate whether or not allocation has been purged since last
|
| + // use.
|
| + bool AcquireLock(Allocation* allocation, 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);
|
| + // Release a previously acquired lock on allocation. This allows the manager
|
| + // to purge it if necessary.
|
| + void ReleaseLock(Allocation* allocation);
|
|
|
| // Purges all discardable memory.
|
| void PurgeAll();
|
|
|
| - // 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 allocation has been added to the manager's collection. This
|
| + // should only be used by tests.
|
| + bool IsRegisteredForTest(Allocation* allocation) 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 allocation can be purged. This should only be used by
|
| + // tests.
|
| + bool CanBePurgedForTest(Allocation* allocation) const;
|
|
|
| - // Returns total amount of allocated discardable memory. This should only
|
| - // be used by tests.
|
| + // 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) {
|
| - }
|
| -
|
| - size_t bytes;
|
| - uint8* memory;
|
| + struct AllocationInfo {
|
| + explicit AllocationInfo(size_t bytes) : bytes(bytes), purgable(false) {}
|
| +
|
| + const size_t bytes;
|
| + bool purgable;
|
| };
|
| - typedef HashingMRUCache<const DiscardableMemory*, Allocation> AllocationMap;
|
| + typedef HashingMRUCache<Allocation*, AllocationInfo> 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
|
| + // Purges memory until usage is within
|
| // |bytes_to_keep_under_moderate_pressure_|.
|
| void Purge();
|
|
|
| @@ -117,8 +136,8 @@ class BASE_EXPORT_PRIVATE DiscardableMemoryManager {
|
| // Caller must acquire |lock_| prior to calling this function.
|
| void PurgeLRUWithLockAcquiredUntilUsageIsWithin(size_t limit);
|
|
|
| - // Ensures that we don't allocate beyond our memory limit.
|
| - // Caller must acquire |lock_| prior to calling this function.
|
| + // Ensures that we don't allocate beyond our memory limit. Caller must acquire
|
| + // |lock_| prior to calling this function.
|
| void EnforcePolicyWithLockAcquired();
|
|
|
| // Called when a change to |bytes_allocated_| has been made.
|
| @@ -127,17 +146,17 @@ class BASE_EXPORT_PRIVATE DiscardableMemoryManager {
|
| // Needs to be held when accessing members.
|
| mutable Lock lock_;
|
|
|
| - // A MRU cache of all allocated bits of discardable memory. Used for purging.
|
| + // A MRU cache of all allocated bits of memory. Used for purging.
|
| AllocationMap allocations_;
|
|
|
| - // The total amount of allocated discardable memory.
|
| + // The total amount of allocated memory.
|
| size_t bytes_allocated_;
|
|
|
| - // The maximum number of bytes of discardable memory that may be allocated.
|
| - size_t discardable_memory_limit_;
|
| + // The maximum number of bytes of memory that may be allocated.
|
| + size_t memory_limit_;
|
|
|
| - // Under moderate memory pressure, we will purge until usage is within this
|
| - // limit.
|
| + // Under moderate memory pressure, we will purge memory 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
|
|
|