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.
5 #ifndef CONTENT_COMMON_HOST_DISCARDABLE_SHARED_MEMORY_MANAGER_H_
6 #define CONTENT_COMMON_HOST_DISCARDABLE_SHARED_MEMORY_MANAGER_H_
10 #include "base/containers/hash_tables.h"
11 #include "base/memory/discardable_memory_allocator.h"
12 #include "base/memory/discardable_shared_memory.h"
13 #include "base/memory/memory_pressure_listener.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/shared_memory.h"
16 #include "base/memory/weak_ptr.h"
17 #include "base/process/process_handle.h"
18 #include "base/synchronization/lock.h"
19 #include "content/common/content_export.h"
22 typedef int32_t DiscardableSharedMemoryId
;
24 // Implementation of DiscardableMemoryAllocator that allocates and manages
25 // discardable memory segments for the browser process and child processes.
26 // This class is thread-safe and instances can safely be used on any thread.
27 class CONTENT_EXPORT HostDiscardableSharedMemoryManager
28 : public base::DiscardableMemoryAllocator
{
30 HostDiscardableSharedMemoryManager();
31 ~HostDiscardableSharedMemoryManager() override
;
33 // Returns a singleton instance.
34 static HostDiscardableSharedMemoryManager
* current();
36 // Overridden from base::DiscardableMemoryAllocator:
37 scoped_ptr
<base::DiscardableMemory
> AllocateLockedDiscardableMemory(
38 size_t size
) override
;
40 // This allocates a discardable memory segment for |process_handle|.
41 // A valid shared memory handle is returned on success.
42 void AllocateLockedDiscardableSharedMemoryForChild(
43 base::ProcessHandle process_handle
,
45 DiscardableSharedMemoryId id
,
46 base::SharedMemoryHandle
* shared_memory_handle
);
48 // Call this to notify the manager that child process associated with
49 // |process_handle| has deleted discardable memory segment with |id|.
50 void ChildDeletedDiscardableSharedMemory(DiscardableSharedMemoryId id
,
51 base::ProcessHandle process_handle
);
53 // Call this to notify the manager that child process associated with
54 // |process_handle| has been removed. The manager will use this to release
55 // memory segments allocated for child process to the OS.
56 void ProcessRemoved(base::ProcessHandle process_handle
);
58 // The maximum number of bytes of memory that may be allocated. This will
59 // cause memory usage to be reduced if currently above |limit|.
60 void SetMemoryLimit(size_t limit
);
62 // Reduce memory usage if above current memory limit.
63 void EnforceMemoryPolicy();
65 // Returns bytes of allocated discardable memory.
66 size_t GetBytesAllocated();
69 class MemorySegment
: public base::RefCountedThreadSafe
<MemorySegment
> {
71 MemorySegment(scoped_ptr
<base::DiscardableSharedMemory
> memory
);
73 base::DiscardableSharedMemory
* memory() const { return memory_
.get(); }
76 friend class base::RefCountedThreadSafe
<MemorySegment
>;
80 scoped_ptr
<base::DiscardableSharedMemory
> memory_
;
82 DISALLOW_COPY_AND_ASSIGN(MemorySegment
);
85 static bool CompareMemoryUsageTime(const scoped_refptr
<MemorySegment
>& a
,
86 const scoped_refptr
<MemorySegment
>& b
) {
87 // In this system, LRU memory segment is evicted first.
88 return a
->memory()->last_known_usage() > b
->memory()->last_known_usage();
91 void AllocateLockedDiscardableSharedMemory(
92 base::ProcessHandle process_handle
,
94 DiscardableSharedMemoryId id
,
95 base::SharedMemoryHandle
* shared_memory_handle
);
96 void DeletedDiscardableSharedMemory(DiscardableSharedMemoryId id
,
97 base::ProcessHandle process_handle
);
98 void OnMemoryPressure(
99 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level
);
100 void ReduceMemoryUsageUntilWithinMemoryLimit();
101 void ReduceMemoryUsageUntilWithinLimit(size_t limit
);
102 void ReleaseMemory(base::DiscardableSharedMemory
* memory
);
103 void BytesAllocatedChanged(size_t new_bytes_allocated
) const;
105 // Virtual for tests.
106 virtual base::Time
Now() const;
107 virtual void ScheduleEnforceMemoryPolicy();
110 typedef base::hash_map
<DiscardableSharedMemoryId
,
111 scoped_refptr
<MemorySegment
>> MemorySegmentMap
;
112 typedef base::hash_map
<base::ProcessHandle
, MemorySegmentMap
> ProcessMap
;
113 ProcessMap processes_
;
114 // Note: The elements in |segments_| are arranged in such a way that they form
115 // a heap. The LRU memory segment always first.
116 typedef std::vector
<scoped_refptr
<MemorySegment
>> MemorySegmentVector
;
117 MemorySegmentVector segments_
;
118 size_t memory_limit_
;
119 size_t bytes_allocated_
;
120 scoped_ptr
<base::MemoryPressureListener
> memory_pressure_listener_
;
121 bool enforce_memory_policy_pending_
;
122 base::WeakPtrFactory
<HostDiscardableSharedMemoryManager
> weak_ptr_factory_
;
124 DISALLOW_COPY_AND_ASSIGN(HostDiscardableSharedMemoryManager
);
127 } // namespace content
129 #endif // CONTENT_COMMON_HOST_DISCARDABLE_SHARED_MEMORY_MANAGER_H_