Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / base / memory / discardable_memory_manager.h
1 // Copyright 2014 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_MANAGER_H_
6 #define BASE_MEMORY_DISCARDABLE_MEMORY_MANAGER_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 namespace internal {
16
17 // This interface is used by the DiscardableMemoryManager class to provide some
18 // level of userspace control over discardable memory allocations.
19 class DiscardableMemoryManagerAllocation {
20  public:
21   // Allocate and acquire a lock that prevents the allocation from being purged
22   // by the system. Returns true if memory was previously allocated and is still
23   // resident.
24   virtual bool AllocateAndAcquireLock() = 0;
25
26   // Release a previously acquired lock on the allocation so that it can be
27   // purged by the system.
28   virtual void ReleaseLock() = 0;
29
30   // Explicitly purge this allocation. It is illegal to call this while a lock
31   // is acquired on the allocation.
32   virtual void Purge() = 0;
33
34  protected:
35   virtual ~DiscardableMemoryManagerAllocation() {}
36 };
37
38 }  // namespace internal
39 }  // namespace base
40
41 #if defined(COMPILER_GCC)
42 namespace BASE_HASH_NAMESPACE {
43 template <>
44 struct hash<base::internal::DiscardableMemoryManagerAllocation*> {
45   size_t operator()(
46       base::internal::DiscardableMemoryManagerAllocation* ptr) const {
47     return hash<size_t>()(reinterpret_cast<size_t>(ptr));
48   }
49 };
50 }  // namespace BASE_HASH_NAMESPACE
51 #endif  // COMPILER
52
53 namespace base {
54 namespace internal {
55
56 // The DiscardableMemoryManager manages a collection of
57 // DiscardableMemoryManagerAllocation instances. It is used on platforms that
58 // need some level of userspace control over discardable memory. It keeps track
59 // of all allocation instances (in case they need to be purged), and the total
60 // amount of allocated memory (in case this forces a purge). When memory usage
61 // reaches the limit, the manager purges the LRU memory.
62 //
63 // When notified of memory pressure, the manager either purges the LRU memory --
64 // if the pressure is moderate -- or all discardable memory if the pressure is
65 // critical.
66 class BASE_EXPORT_PRIVATE DiscardableMemoryManager {
67  public:
68   typedef DiscardableMemoryManagerAllocation Allocation;
69
70   DiscardableMemoryManager();
71   ~DiscardableMemoryManager();
72
73   // Call this to register memory pressure listener. Must be called on a thread
74   // with a MessageLoop current.
75   void RegisterMemoryPressureListener();
76
77   // Call this to unregister memory pressure listener.
78   void UnregisterMemoryPressureListener();
79
80   // The maximum number of bytes of memory that may be allocated before we force
81   // a purge. If this amount is zero, it is interpreted as having no limit at
82   // all.
83   void SetMemoryLimit(size_t bytes);
84
85   // Sets the amount of memory to keep when we're under moderate pressure.
86   void SetBytesToKeepUnderModeratePressure(size_t bytes);
87
88   // Adds the given allocation to the manager's collection.
89   void Register(Allocation* allocation, size_t bytes);
90
91   // Removes the given allocation from the manager's collection.
92   void Unregister(Allocation* allocation);
93
94   // Returns false if an error occurred. Otherwise, returns true and sets
95   // |purged| to indicate whether or not allocation has been purged since last
96   // use.
97   bool AcquireLock(Allocation* allocation, bool* purged);
98
99   // Release a previously acquired lock on allocation. This allows the manager
100   // to purge it if necessary.
101   void ReleaseLock(Allocation* allocation);
102
103   // Purges all discardable memory.
104   void PurgeAll();
105
106   // Returns true if allocation has been added to the manager's collection. This
107   // should only be used by tests.
108   bool IsRegisteredForTest(Allocation* allocation) const;
109
110   // Returns true if allocation can be purged. This should only be used by
111   // tests.
112   bool CanBePurgedForTest(Allocation* allocation) const;
113
114   // Returns total amount of allocated discardable memory. This should only be
115   // used by tests.
116   size_t GetBytesAllocatedForTest() const;
117
118  private:
119   struct AllocationInfo {
120     explicit AllocationInfo(size_t bytes) : bytes(bytes), purgable(false) {}
121
122     const size_t bytes;
123     bool purgable;
124   };
125   typedef HashingMRUCache<Allocation*, AllocationInfo> AllocationMap;
126
127   // This can be called as a hint that the system is under memory pressure.
128   void OnMemoryPressure(
129       MemoryPressureListener::MemoryPressureLevel pressure_level);
130
131   // Purges memory until usage is within
132   // |bytes_to_keep_under_moderate_pressure_|.
133   void Purge();
134
135   // Purges least recently used memory until usage is less or equal to |limit|.
136   // Caller must acquire |lock_| prior to calling this function.
137   void PurgeLRUWithLockAcquiredUntilUsageIsWithin(size_t limit);
138
139   // Ensures that we don't allocate beyond our memory limit. Caller must acquire
140   // |lock_| prior to calling this function.
141   void EnforcePolicyWithLockAcquired();
142
143   // Called when a change to |bytes_allocated_| has been made.
144   void BytesAllocatedChanged() const;
145
146   // Needs to be held when accessing members.
147   mutable Lock lock_;
148
149   // A MRU cache of all allocated bits of memory. Used for purging.
150   AllocationMap allocations_;
151
152   // The total amount of allocated memory.
153   size_t bytes_allocated_;
154
155   // The maximum number of bytes of memory that may be allocated.
156   size_t memory_limit_;
157
158   // Under moderate memory pressure, we will purge memory until usage is within
159   // this limit.
160   size_t bytes_to_keep_under_moderate_pressure_;
161
162   // Allows us to be respond when the system reports that it is under memory
163   // pressure.
164   scoped_ptr<MemoryPressureListener> memory_pressure_listener_;
165
166   DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryManager);
167 };
168
169 }  // namespace internal
170 }  // namespace base
171
172 #endif  // BASE_MEMORY_DISCARDABLE_MEMORY_MANAGER_H_