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" |
14 | 15 |
15 namespace base { | 16 namespace base { |
16 class DiscardableSharedMemory; | 17 class DiscardableSharedMemory; |
17 } | 18 } |
18 | 19 |
19 namespace content { | 20 namespace content { |
20 | 21 |
21 // Implements a heap of discardable shared memory. An array of free lists | 22 // Implements a heap of discardable shared memory. An array of free lists |
22 // is used to keep track of free blocks. | 23 // is used to keep track of free blocks. |
(...skipping 23 matching lines...) Expand all Loading... | |
46 | 47 |
47 explicit DiscardableSharedMemoryHeap(size_t block_size); | 48 explicit DiscardableSharedMemoryHeap(size_t block_size); |
48 ~DiscardableSharedMemoryHeap(); | 49 ~DiscardableSharedMemoryHeap(); |
49 | 50 |
50 // Grow heap using |shared_memory| and return a span for this new memory. | 51 // 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 | 52 // |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 | 53 // multiple of the block size. |deleted_callback| is called when |
53 // |shared_memory| has been deleted. | 54 // |shared_memory| has been deleted. |
54 scoped_ptr<Span> Grow(scoped_ptr<base::DiscardableSharedMemory> shared_memory, | 55 scoped_ptr<Span> Grow(scoped_ptr<base::DiscardableSharedMemory> shared_memory, |
55 size_t size, | 56 size_t size, |
57 int32_t id, | |
56 const base::Closure& deleted_callback); | 58 const base::Closure& deleted_callback); |
57 | 59 |
58 // Merge |span| into the free lists. This will coalesce |span| with | 60 // Merge |span| into the free lists. This will coalesce |span| with |
59 // neighboring free spans when possible. | 61 // neighboring free spans when possible. |
60 void MergeIntoFreeLists(scoped_ptr<Span> span); | 62 void MergeIntoFreeLists(scoped_ptr<Span> span); |
61 | 63 |
62 // Split an allocated span into two spans, one of length |blocks| followed | 64 // Split an allocated span into two spans, one of length |blocks| followed |
63 // by another span of length "span->length - blocks" blocks. Modifies |span| | 65 // by another span of length "span->length - blocks" blocks. Modifies |span| |
64 // to point to the first span of length |blocks|. Return second span. | 66 // to point to the first span of length |blocks|. Return second span. |
65 scoped_ptr<Span> Split(Span* span, size_t blocks); | 67 scoped_ptr<Span> Split(Span* span, size_t blocks); |
(...skipping 10 matching lines...) Expand all Loading... | |
76 | 78 |
77 // Release shared memory segments that have been purged. | 79 // Release shared memory segments that have been purged. |
78 void ReleasePurgedMemory(); | 80 void ReleasePurgedMemory(); |
79 | 81 |
80 // Returns total bytes of memory in heap. | 82 // Returns total bytes of memory in heap. |
81 size_t GetSize() const; | 83 size_t GetSize() const; |
82 | 84 |
83 // Returns bytes of memory currently in the free lists. | 85 // Returns bytes of memory currently in the free lists. |
84 size_t GetSizeOfFreeLists() const; | 86 size_t GetSizeOfFreeLists() const; |
85 | 87 |
88 // Dumps memory statistics for chrome://tracing. | |
89 void OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd); | |
Primiano Tucci (use gerrit)
2015/04/29 17:32:02
this should be a bool (See my next comment)
reveman
2015/04/29 18:31:00
Btw, I was under the impression that CreateAllocat
| |
90 | |
86 private: | 91 private: |
87 class ScopedMemorySegment { | 92 class ScopedMemorySegment { |
88 public: | 93 public: |
89 ScopedMemorySegment(DiscardableSharedMemoryHeap* heap, | 94 ScopedMemorySegment(DiscardableSharedMemoryHeap* heap, |
90 scoped_ptr<base::DiscardableSharedMemory> shared_memory, | 95 scoped_ptr<base::DiscardableSharedMemory> shared_memory, |
91 size_t size, | 96 size_t size, |
97 int32_t id, | |
92 const base::Closure& deleted_callback); | 98 const base::Closure& deleted_callback); |
93 ~ScopedMemorySegment(); | 99 ~ScopedMemorySegment(); |
94 | 100 |
95 bool IsUsed() const; | 101 bool IsUsed() const; |
96 bool IsResident() const; | 102 bool IsResident() const; |
97 | 103 |
104 // Used for dumping memory statistics from the segment to chrome://tracing. | |
105 void OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd) const; | |
106 | |
98 private: | 107 private: |
99 DiscardableSharedMemoryHeap* const heap_; | 108 DiscardableSharedMemoryHeap* const heap_; |
100 scoped_ptr<base::DiscardableSharedMemory> shared_memory_; | 109 scoped_ptr<base::DiscardableSharedMemory> shared_memory_; |
101 const size_t size_; | 110 const size_t size_; |
111 const int32_t id_; | |
102 const base::Closure deleted_callback_; | 112 const base::Closure deleted_callback_; |
103 | 113 |
104 DISALLOW_COPY_AND_ASSIGN(ScopedMemorySegment); | 114 DISALLOW_COPY_AND_ASSIGN(ScopedMemorySegment); |
105 }; | 115 }; |
106 | 116 |
107 void InsertIntoFreeList(scoped_ptr<Span> span); | 117 void InsertIntoFreeList(scoped_ptr<Span> span); |
108 scoped_ptr<Span> RemoveFromFreeList(Span* span); | 118 scoped_ptr<Span> RemoveFromFreeList(Span* span); |
109 scoped_ptr<Span> Carve(Span* span, size_t blocks); | 119 scoped_ptr<Span> Carve(Span* span, size_t blocks); |
110 void RegisterSpan(Span* span); | 120 void RegisterSpan(Span* span); |
111 void UnregisterSpan(Span* span); | 121 void UnregisterSpan(Span* span); |
112 bool IsMemoryUsed(const base::DiscardableSharedMemory* shared_memory, | 122 bool IsMemoryUsed(const base::DiscardableSharedMemory* shared_memory, |
113 size_t size); | 123 size_t size); |
114 bool IsMemoryResident(const base::DiscardableSharedMemory* shared_memory); | 124 bool IsMemoryResident(const base::DiscardableSharedMemory* shared_memory); |
115 void ReleaseMemory(const base::DiscardableSharedMemory* shared_memory, | 125 void ReleaseMemory(const base::DiscardableSharedMemory* shared_memory, |
116 size_t size); | 126 size_t size); |
117 | 127 |
128 // Dumps memory statistics about a memory segment for chrome://tracing. | |
129 void OnMemoryDump(const base::DiscardableSharedMemory* shared_memory, | |
130 size_t size, | |
131 int32_t id, | |
132 base::trace_event::ProcessMemoryDump* pmd); | |
133 | |
118 size_t block_size_; | 134 size_t block_size_; |
119 size_t num_blocks_; | 135 size_t num_blocks_; |
120 size_t num_free_blocks_; | 136 size_t num_free_blocks_; |
121 | 137 |
122 // Vector of memory segments. | 138 // Vector of memory segments. |
123 ScopedVector<ScopedMemorySegment> memory_segments_; | 139 ScopedVector<ScopedMemorySegment> memory_segments_; |
124 | 140 |
125 // Mapping from first/last block of span to Span instance. | 141 // Mapping from first/last block of span to Span instance. |
126 typedef base::hash_map<size_t, Span*> SpanMap; | 142 typedef base::hash_map<size_t, Span*> SpanMap; |
127 SpanMap spans_; | 143 SpanMap spans_; |
128 | 144 |
129 // Array of linked-lists with free discardable memory regions. For i < 256, | 145 // 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 | 146 // 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 | 147 // 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. | 148 // free list of runs that have length >= 256 blocks. |
133 base::LinkedList<Span> free_spans_[256]; | 149 base::LinkedList<Span> free_spans_[256]; |
134 | 150 |
135 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemoryHeap); | 151 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemoryHeap); |
136 }; | 152 }; |
137 | 153 |
138 } // namespace content | 154 } // namespace content |
139 | 155 |
140 #endif // CONTENT_COMMON_DISCARDABLE_SHARED_MEMORY_HEAP_H_ | 156 #endif // CONTENT_COMMON_DISCARDABLE_SHARED_MEMORY_HEAP_H_ |
OLD | NEW |