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_DISCARDABLE_SHARED_MEMORY_HEAP_H_ | 5 #ifndef CONTENT_COMMON_DISCARDABLE_SHARED_MEMORY_HEAP_H_ |
6 #define CONTENT_COMMON_DISCARDABLE_SHARED_MEMORY_HEAP_H_ | 6 #define CONTENT_COMMON_DISCARDABLE_SHARED_MEMORY_HEAP_H_ |
7 | 7 |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/containers/hash_tables.h" | 9 #include "base/containers/hash_tables.h" |
10 #include "base/containers/linked_list.h" | 10 #include "base/containers/linked_list.h" |
11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
13 #include "base/trace_event/process_memory_dump.h" | |
13 #include "content/common/content_export.h" | 14 #include "content/common/content_export.h" |
15 #include "content/common/host_discardable_shared_memory_manager.h" | |
reveman
2015/04/27 15:08:58
nit: this might be a circular dependency in the fu
ssid
2015/04/27 15:21:56
Done.
| |
14 | 16 |
15 namespace base { | 17 namespace base { |
16 class DiscardableSharedMemory; | 18 class DiscardableSharedMemory; |
17 } | 19 } |
18 | 20 |
19 namespace content { | 21 namespace content { |
20 | 22 |
21 // Implements a heap of discardable shared memory. An array of free lists | 23 // Implements a heap of discardable shared memory. An array of free lists |
22 // is used to keep track of free blocks. | 24 // is used to keep track of free blocks. |
23 class CONTENT_EXPORT DiscardableSharedMemoryHeap { | 25 class CONTENT_EXPORT DiscardableSharedMemoryHeap { |
(...skipping 22 matching lines...) Expand all Loading... | |
46 | 48 |
47 explicit DiscardableSharedMemoryHeap(size_t block_size); | 49 explicit DiscardableSharedMemoryHeap(size_t block_size); |
48 ~DiscardableSharedMemoryHeap(); | 50 ~DiscardableSharedMemoryHeap(); |
49 | 51 |
50 // Grow heap using |shared_memory| and return a span for this new memory. | 52 // Grow heap using |shared_memory| and return a span for this new memory. |
51 // |shared_memory| must be aligned to the block size and |size| must be a | 53 // |shared_memory| must be aligned to the block size and |size| must be a |
52 // multiple of the block size. |deleted_callback| is called when | 54 // multiple of the block size. |deleted_callback| is called when |
53 // |shared_memory| has been deleted. | 55 // |shared_memory| has been deleted. |
54 scoped_ptr<Span> Grow(scoped_ptr<base::DiscardableSharedMemory> shared_memory, | 56 scoped_ptr<Span> Grow(scoped_ptr<base::DiscardableSharedMemory> shared_memory, |
55 size_t size, | 57 size_t size, |
58 DiscardableSharedMemoryId id, | |
reveman
2015/04/27 15:08:58
int32_t to avoid circular dep
ssid
2015/04/27 15:21:56
Done.
| |
56 const base::Closure& deleted_callback); | 59 const base::Closure& deleted_callback); |
57 | 60 |
58 // Merge |span| into the free lists. This will coalesce |span| with | 61 // Merge |span| into the free lists. This will coalesce |span| with |
59 // neighboring free spans when possible. | 62 // neighboring free spans when possible. |
60 void MergeIntoFreeLists(scoped_ptr<Span> span); | 63 void MergeIntoFreeLists(scoped_ptr<Span> span); |
61 | 64 |
62 // Split an allocated span into two spans, one of length |blocks| followed | 65 // Split an allocated span into two spans, one of length |blocks| followed |
63 // by another span of length "span->length - blocks" blocks. Modifies |span| | 66 // by another span of length "span->length - blocks" blocks. Modifies |span| |
64 // to point to the first span of length |blocks|. Return second span. | 67 // to point to the first span of length |blocks|. Return second span. |
65 scoped_ptr<Span> Split(Span* span, size_t blocks); | 68 scoped_ptr<Span> Split(Span* span, size_t blocks); |
(...skipping 10 matching lines...) Expand all Loading... | |
76 | 79 |
77 // Release shared memory segments that have been purged. | 80 // Release shared memory segments that have been purged. |
78 void ReleasePurgedMemory(); | 81 void ReleasePurgedMemory(); |
79 | 82 |
80 // Returns total bytes of memory in heap. | 83 // Returns total bytes of memory in heap. |
81 size_t GetSize() const; | 84 size_t GetSize() const; |
82 | 85 |
83 // Returns bytes of memory currently in the free lists. | 86 // Returns bytes of memory currently in the free lists. |
84 size_t GetSizeOfFreeLists() const; | 87 size_t GetSizeOfFreeLists() const; |
85 | 88 |
89 // Dumps memory statistics for chrome://tracing. | |
90 bool DumpInto(base::trace_event::ProcessMemoryDump* pmd); | |
91 | |
86 private: | 92 private: |
87 class ScopedMemorySegment { | 93 class ScopedMemorySegment { |
88 public: | 94 public: |
89 ScopedMemorySegment(DiscardableSharedMemoryHeap* heap, | 95 ScopedMemorySegment(DiscardableSharedMemoryHeap* heap, |
90 scoped_ptr<base::DiscardableSharedMemory> shared_memory, | 96 scoped_ptr<base::DiscardableSharedMemory> shared_memory, |
91 size_t size, | 97 size_t size, |
98 DiscardableSharedMemoryId id, | |
reveman
2015/04/27 15:08:58
int32_t to avoid circular dep
ssid
2015/04/27 15:21:56
Done.
| |
92 const base::Closure& deleted_callback); | 99 const base::Closure& deleted_callback); |
93 ~ScopedMemorySegment(); | 100 ~ScopedMemorySegment(); |
94 | 101 |
95 bool IsUsed() const; | 102 bool IsUsed() const; |
96 bool IsResident() const; | 103 bool IsResident() const; |
97 | 104 |
105 // Used for dumping memory statistics from the segment to chrome://tracing. | |
106 void DumpInto(base::trace_event::ProcessMemoryDump* pmd) const; | |
107 | |
98 private: | 108 private: |
99 DiscardableSharedMemoryHeap* const heap_; | 109 DiscardableSharedMemoryHeap* const heap_; |
100 scoped_ptr<base::DiscardableSharedMemory> shared_memory_; | 110 scoped_ptr<base::DiscardableSharedMemory> shared_memory_; |
101 const size_t size_; | 111 const size_t size_; |
112 const DiscardableSharedMemoryId id_; | |
102 const base::Closure deleted_callback_; | 113 const base::Closure deleted_callback_; |
103 | 114 |
104 DISALLOW_COPY_AND_ASSIGN(ScopedMemorySegment); | 115 DISALLOW_COPY_AND_ASSIGN(ScopedMemorySegment); |
105 }; | 116 }; |
106 | 117 |
107 void InsertIntoFreeList(scoped_ptr<Span> span); | 118 void InsertIntoFreeList(scoped_ptr<Span> span); |
108 scoped_ptr<Span> RemoveFromFreeList(Span* span); | 119 scoped_ptr<Span> RemoveFromFreeList(Span* span); |
109 scoped_ptr<Span> Carve(Span* span, size_t blocks); | 120 scoped_ptr<Span> Carve(Span* span, size_t blocks); |
110 void RegisterSpan(Span* span); | 121 void RegisterSpan(Span* span); |
111 void UnregisterSpan(Span* span); | 122 void UnregisterSpan(Span* span); |
112 bool IsMemoryUsed(const base::DiscardableSharedMemory* shared_memory, | 123 bool IsMemoryUsed(const base::DiscardableSharedMemory* shared_memory, |
113 size_t size); | 124 size_t size); |
114 bool IsMemoryResident(const base::DiscardableSharedMemory* shared_memory); | 125 bool IsMemoryResident(const base::DiscardableSharedMemory* shared_memory); |
115 void ReleaseMemory(const base::DiscardableSharedMemory* shared_memory, | 126 void ReleaseMemory(const base::DiscardableSharedMemory* shared_memory, |
116 size_t size); | 127 size_t size); |
117 | 128 |
129 // Dumps memory statistics about a memory segment for chrome://tracing. | |
130 void DumpInto(const base::DiscardableSharedMemory* shared_memory, | |
131 size_t segment_size, | |
reveman
2015/04/27 15:08:58
nit: s/segment_size/size/
ssid
2015/04/27 15:21:56
Done.
| |
132 DiscardableSharedMemoryId id, | |
reveman
2015/04/27 15:08:58
int32_t to avoid circular dep
ssid
2015/04/27 15:21:56
Done.
| |
133 base::trace_event::ProcessMemoryDump* pmd); | |
134 | |
118 size_t block_size_; | 135 size_t block_size_; |
119 size_t num_blocks_; | 136 size_t num_blocks_; |
120 size_t num_free_blocks_; | 137 size_t num_free_blocks_; |
121 | 138 |
122 // Vector of memory segments. | 139 // Vector of memory segments. |
123 ScopedVector<ScopedMemorySegment> memory_segments_; | 140 ScopedVector<ScopedMemorySegment> memory_segments_; |
124 | 141 |
125 // Mapping from first/last block of span to Span instance. | 142 // Mapping from first/last block of span to Span instance. |
126 typedef base::hash_map<size_t, Span*> SpanMap; | 143 typedef base::hash_map<size_t, Span*> SpanMap; |
127 SpanMap spans_; | 144 SpanMap spans_; |
128 | 145 |
129 // Array of linked-lists with free discardable memory regions. For i < 256, | 146 // Array of linked-lists with free discardable memory regions. For i < 256, |
130 // where the 1st entry is located at index 0 of the array, the kth entry | 147 // where the 1st entry is located at index 0 of the array, the kth entry |
131 // is a free list of runs that consist of k blocks. The 256th entry is a | 148 // is a free list of runs that consist of k blocks. The 256th entry is a |
132 // free list of runs that have length >= 256 blocks. | 149 // free list of runs that have length >= 256 blocks. |
133 base::LinkedList<Span> free_spans_[256]; | 150 base::LinkedList<Span> free_spans_[256]; |
134 | 151 |
135 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemoryHeap); | 152 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemoryHeap); |
136 }; | 153 }; |
137 | 154 |
138 } // namespace content | 155 } // namespace content |
139 | 156 |
140 #endif // CONTENT_COMMON_DISCARDABLE_SHARED_MEMORY_HEAP_H_ | 157 #endif // CONTENT_COMMON_DISCARDABLE_SHARED_MEMORY_HEAP_H_ |
OLD | NEW |