Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / content / common / host_discardable_shared_memory_manager.h
blob148c4fc3f80a36d35cad0ec1f0ebd165c79127b6
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_
8 #include <vector>
10 #include "base/memory/discardable_memory_shmem_allocator.h"
11 #include "base/memory/discardable_shared_memory.h"
12 #include "base/memory/linked_ptr.h"
13 #include "base/memory/memory_pressure_listener.h"
14 #include "base/memory/shared_memory.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/process/process_handle.h"
17 #include "base/synchronization/lock.h"
18 #include "content/common/content_export.h"
20 namespace content {
22 // Implementation of DiscardableMemoryShmemAllocator that allocates and
23 // manages discardable memory segments for the browser process and child
24 // processes. This class is thread-safe and instances can safely be used
25 // on any thread.
26 class CONTENT_EXPORT HostDiscardableSharedMemoryManager
27 : public base::DiscardableMemoryShmemAllocator {
28 public:
29 HostDiscardableSharedMemoryManager();
30 ~HostDiscardableSharedMemoryManager() override;
32 // Returns a singleton instance.
33 static HostDiscardableSharedMemoryManager* current();
35 // Overridden from base::DiscardableMemoryShmemAllocator:
36 scoped_ptr<base::DiscardableSharedMemory>
37 AllocateLockedDiscardableSharedMemory(size_t size) override;
39 // This allocates a discardable memory segment for |process_handle|.
40 // A valid shared memory handle is returned on success.
41 void AllocateLockedDiscardableSharedMemoryForChild(
42 base::ProcessHandle process_handle,
43 size_t size,
44 base::SharedMemoryHandle* shared_memory_handle);
46 // Call this to notify the manager that child process associated with
47 // |process_handle| has been removed. The manager will use this to release
48 // memory segments allocated for child process to the OS.
49 void ProcessRemoved(base::ProcessHandle process_handle);
51 // The maximum number of bytes of memory that may be allocated. This will
52 // cause memory usage to be reduced if currently above |limit|.
53 void SetMemoryLimit(size_t limit);
55 // Reduce memory usage if above current memory limit.
56 void EnforceMemoryPolicy();
58 private:
59 struct MemorySegment {
60 MemorySegment(linked_ptr<base::DiscardableSharedMemory> memory,
61 base::ProcessHandle process_handle);
62 ~MemorySegment();
64 linked_ptr<base::DiscardableSharedMemory> memory;
65 base::ProcessHandle process_handle;
68 static bool CompareMemoryUsageTime(const MemorySegment& a,
69 const MemorySegment& b) {
70 // In this system, LRU memory segment is evicted first.
71 return a.memory->last_known_usage() > b.memory->last_known_usage();
74 void OnMemoryPressure(
75 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level);
76 void ReduceMemoryUsageUntilWithinMemoryLimit();
77 void ReduceMemoryUsageUntilWithinLimit(size_t limit);
78 void BytesAllocatedChanged(size_t new_bytes_allocated) const;
80 // Virtual for tests.
81 virtual base::Time Now() const;
82 virtual void ScheduleEnforceMemoryPolicy();
84 base::Lock lock_;
85 // Note: The elements in |segments_| are arranged in such a way that they form
86 // a heap. The LRU memory segment always first.
87 typedef std::vector<MemorySegment> MemorySegmentVector;
88 MemorySegmentVector segments_;
89 size_t memory_limit_;
90 size_t bytes_allocated_;
91 scoped_ptr<base::MemoryPressureListener> memory_pressure_listener_;
92 bool enforce_memory_policy_pending_;
93 base::WeakPtrFactory<HostDiscardableSharedMemoryManager> weak_ptr_factory_;
95 DISALLOW_COPY_AND_ASSIGN(HostDiscardableSharedMemoryManager);
98 } // namespace content
100 #endif // CONTENT_COMMON_HOST_DISCARDABLE_SHARED_MEMORY_MANAGER_H_