OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_COMMON_HOST_DISCARDABLE_SHARED_MEMORY_MANAGER_H_ | 5 #ifndef CONTENT_COMMON_HOST_DISCARDABLE_SHARED_MEMORY_MANAGER_H_ |
6 #define CONTENT_COMMON_HOST_DISCARDABLE_SHARED_MEMORY_MANAGER_H_ | 6 #define CONTENT_COMMON_HOST_DISCARDABLE_SHARED_MEMORY_MANAGER_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <memory> | 11 #include <memory> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/callback.h" | 14 #include "base/callback.h" |
15 #include "base/containers/hash_tables.h" | 15 #include "base/containers/hash_tables.h" |
16 #include "base/format_macros.h" | 16 #include "base/format_macros.h" |
17 #include "base/macros.h" | 17 #include "base/macros.h" |
18 #include "base/memory/discardable_memory_allocator.h" | 18 #include "base/memory/discardable_memory_allocator.h" |
19 #include "base/memory/discardable_shared_memory.h" | 19 #include "base/memory/discardable_shared_memory.h" |
| 20 #include "base/memory/memory_coordinator_client.h" |
20 #include "base/memory/memory_pressure_listener.h" | 21 #include "base/memory/memory_pressure_listener.h" |
21 #include "base/memory/ref_counted.h" | 22 #include "base/memory/ref_counted.h" |
22 #include "base/memory/shared_memory.h" | 23 #include "base/memory/shared_memory.h" |
23 #include "base/memory/weak_ptr.h" | 24 #include "base/memory/weak_ptr.h" |
24 #include "base/process/process_handle.h" | 25 #include "base/process/process_handle.h" |
25 #include "base/synchronization/lock.h" | 26 #include "base/synchronization/lock.h" |
26 #include "base/threading/thread_task_runner_handle.h" | 27 #include "base/threading/thread_task_runner_handle.h" |
27 #include "base/trace_event/memory_dump_provider.h" | 28 #include "base/trace_event/memory_dump_provider.h" |
28 #include "content/common/content_export.h" | 29 #include "content/common/content_export.h" |
29 | 30 |
30 namespace content { | 31 namespace content { |
31 typedef int32_t DiscardableSharedMemoryId; | 32 typedef int32_t DiscardableSharedMemoryId; |
32 | 33 |
33 // Implementation of DiscardableMemoryAllocator that allocates and manages | 34 // Implementation of DiscardableMemoryAllocator that allocates and manages |
34 // discardable memory segments for the browser process and child processes. | 35 // discardable memory segments for the browser process and child processes. |
35 // This class is thread-safe and instances can safely be used on any thread. | 36 // This class is thread-safe and instances can safely be used on any thread. |
36 class CONTENT_EXPORT HostDiscardableSharedMemoryManager | 37 class CONTENT_EXPORT HostDiscardableSharedMemoryManager |
37 : public base::DiscardableMemoryAllocator, | 38 : public base::DiscardableMemoryAllocator, |
38 public base::trace_event::MemoryDumpProvider { | 39 public base::trace_event::MemoryDumpProvider, |
| 40 public base::MemoryCoordinatorClient { |
39 public: | 41 public: |
40 HostDiscardableSharedMemoryManager(); | 42 HostDiscardableSharedMemoryManager(); |
41 ~HostDiscardableSharedMemoryManager() override; | 43 ~HostDiscardableSharedMemoryManager() override; |
42 | 44 |
43 // Returns a singleton instance. | 45 // Returns a singleton instance. |
44 static HostDiscardableSharedMemoryManager* current(); | 46 static HostDiscardableSharedMemoryManager* current(); |
45 | 47 |
46 // Overridden from base::DiscardableMemoryAllocator: | 48 // Overridden from base::DiscardableMemoryAllocator: |
47 std::unique_ptr<base::DiscardableMemory> AllocateLockedDiscardableMemory( | 49 std::unique_ptr<base::DiscardableMemory> AllocateLockedDiscardableMemory( |
48 size_t size) override; | 50 size_t size) override; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 | 98 |
97 DISALLOW_COPY_AND_ASSIGN(MemorySegment); | 99 DISALLOW_COPY_AND_ASSIGN(MemorySegment); |
98 }; | 100 }; |
99 | 101 |
100 static bool CompareMemoryUsageTime(const scoped_refptr<MemorySegment>& a, | 102 static bool CompareMemoryUsageTime(const scoped_refptr<MemorySegment>& a, |
101 const scoped_refptr<MemorySegment>& b) { | 103 const scoped_refptr<MemorySegment>& b) { |
102 // In this system, LRU memory segment is evicted first. | 104 // In this system, LRU memory segment is evicted first. |
103 return a->memory()->last_known_usage() > b->memory()->last_known_usage(); | 105 return a->memory()->last_known_usage() > b->memory()->last_known_usage(); |
104 } | 106 } |
105 | 107 |
| 108 // base::MemoryCoordinatorClient implementation: |
| 109 void OnMemoryStateChange(base::MemoryState state) override; |
| 110 |
106 void AllocateLockedDiscardableSharedMemory( | 111 void AllocateLockedDiscardableSharedMemory( |
107 base::ProcessHandle process_handle, | 112 base::ProcessHandle process_handle, |
108 int client_process_id, | 113 int client_process_id, |
109 size_t size, | 114 size_t size, |
110 DiscardableSharedMemoryId id, | 115 DiscardableSharedMemoryId id, |
111 base::SharedMemoryHandle* shared_memory_handle); | 116 base::SharedMemoryHandle* shared_memory_handle); |
112 void DeletedDiscardableSharedMemory(DiscardableSharedMemoryId id, | 117 void DeletedDiscardableSharedMemory(DiscardableSharedMemoryId id, |
113 int client_process_id); | 118 int client_process_id); |
114 void OnMemoryPressure( | 119 void OnMemoryPressure( |
115 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level); | 120 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level); |
(...skipping 23 matching lines...) Expand all Loading... |
139 base::Closure enforce_memory_policy_callback_; | 144 base::Closure enforce_memory_policy_callback_; |
140 bool enforce_memory_policy_pending_; | 145 bool enforce_memory_policy_pending_; |
141 base::WeakPtrFactory<HostDiscardableSharedMemoryManager> weak_ptr_factory_; | 146 base::WeakPtrFactory<HostDiscardableSharedMemoryManager> weak_ptr_factory_; |
142 | 147 |
143 DISALLOW_COPY_AND_ASSIGN(HostDiscardableSharedMemoryManager); | 148 DISALLOW_COPY_AND_ASSIGN(HostDiscardableSharedMemoryManager); |
144 }; | 149 }; |
145 | 150 |
146 } // namespace content | 151 } // namespace content |
147 | 152 |
148 #endif // CONTENT_COMMON_HOST_DISCARDABLE_SHARED_MEMORY_MANAGER_H_ | 153 #endif // CONTENT_COMMON_HOST_DISCARDABLE_SHARED_MEMORY_MANAGER_H_ |
OLD | NEW |