[Android WebView] Fix webview perf bot switchover to use org.chromium.webview_shell...
[chromium-blink-merge.git] / content / common / discardable_shared_memory_heap.h
blobba2b5911b9d8b3a453ee7ecc0febac4771975362
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_DISCARDABLE_SHARED_MEMORY_HEAP_H_
6 #define CONTENT_COMMON_DISCARDABLE_SHARED_MEMORY_HEAP_H_
8 #include "base/callback.h"
9 #include "base/containers/hash_tables.h"
10 #include "base/containers/linked_list.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/trace_event/process_memory_dump.h"
14 #include "content/common/content_export.h"
16 namespace base {
17 class DiscardableSharedMemory;
20 namespace content {
22 // Implements a heap of discardable shared memory. An array of free lists
23 // is used to keep track of free blocks.
24 class CONTENT_EXPORT DiscardableSharedMemoryHeap {
25 public:
26 class CONTENT_EXPORT Span : public base::LinkNode<Span> {
27 public:
28 ~Span();
30 base::DiscardableSharedMemory* shared_memory() { return shared_memory_; }
31 size_t start() const { return start_; }
32 size_t length() const { return length_; }
34 private:
35 friend class DiscardableSharedMemoryHeap;
37 Span(base::DiscardableSharedMemory* shared_memory,
38 size_t start,
39 size_t length);
41 base::DiscardableSharedMemory* shared_memory_;
42 size_t start_;
43 size_t length_;
45 DISALLOW_COPY_AND_ASSIGN(Span);
48 explicit DiscardableSharedMemoryHeap(size_t block_size);
49 ~DiscardableSharedMemoryHeap();
51 // Grow heap using |shared_memory| and return a span for this new memory.
52 // |shared_memory| must be aligned to the block size and |size| must be a
53 // multiple of the block size. |deleted_callback| is called when
54 // |shared_memory| has been deleted.
55 scoped_ptr<Span> Grow(scoped_ptr<base::DiscardableSharedMemory> shared_memory,
56 size_t size,
57 int32_t id,
58 const base::Closure& deleted_callback);
60 // Merge |span| into the free lists. This will coalesce |span| with
61 // neighboring free spans when possible.
62 void MergeIntoFreeLists(scoped_ptr<Span> span);
64 // Split an allocated span into two spans, one of length |blocks| followed
65 // by another span of length "span->length - blocks" blocks. Modifies |span|
66 // to point to the first span of length |blocks|. Return second span.
67 scoped_ptr<Span> Split(Span* span, size_t blocks);
69 // Search free lists for span that satisfies the request for |blocks| of
70 // memory. If found, the span is removed from the free list and returned.
71 // |slack| determines the fitness requirement. Only spans that are less
72 // or equal to |blocks| + |slack| are considered, worse fitting spans are
73 // ignored.
74 scoped_ptr<Span> SearchFreeLists(size_t blocks, size_t slack);
76 // Release free shared memory segments.
77 void ReleaseFreeMemory();
79 // Release shared memory segments that have been purged.
80 void ReleasePurgedMemory();
82 // Returns total bytes of memory in heap.
83 size_t GetSize() const;
85 // Returns bytes of memory currently in the free lists.
86 size_t GetSizeOfFreeLists() const;
88 // Dumps memory statistics for chrome://tracing.
89 bool OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd);
91 private:
92 class ScopedMemorySegment {
93 public:
94 ScopedMemorySegment(DiscardableSharedMemoryHeap* heap,
95 scoped_ptr<base::DiscardableSharedMemory> shared_memory,
96 size_t size,
97 int32_t id,
98 const base::Closure& deleted_callback);
99 ~ScopedMemorySegment();
101 bool IsUsed() const;
102 bool IsResident() const;
104 // Used for dumping memory statistics from the segment to chrome://tracing.
105 void OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd) const;
107 private:
108 DiscardableSharedMemoryHeap* const heap_;
109 scoped_ptr<base::DiscardableSharedMemory> shared_memory_;
110 const size_t size_;
111 const int32_t id_;
112 const base::Closure deleted_callback_;
114 DISALLOW_COPY_AND_ASSIGN(ScopedMemorySegment);
117 void InsertIntoFreeList(scoped_ptr<Span> span);
118 scoped_ptr<Span> RemoveFromFreeList(Span* span);
119 scoped_ptr<Span> Carve(Span* span, size_t blocks);
120 void RegisterSpan(Span* span);
121 void UnregisterSpan(Span* span);
122 bool IsMemoryUsed(const base::DiscardableSharedMemory* shared_memory,
123 size_t size);
124 bool IsMemoryResident(const base::DiscardableSharedMemory* shared_memory);
125 void ReleaseMemory(const base::DiscardableSharedMemory* shared_memory,
126 size_t size);
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);
134 size_t block_size_;
135 size_t num_blocks_;
136 size_t num_free_blocks_;
138 // Vector of memory segments.
139 ScopedVector<ScopedMemorySegment> memory_segments_;
141 // Mapping from first/last block of span to Span instance.
142 typedef base::hash_map<size_t, Span*> SpanMap;
143 SpanMap spans_;
145 // Array of linked-lists with free discardable memory regions. For i < 256,
146 // where the 1st entry is located at index 0 of the array, the kth entry
147 // is a free list of runs that consist of k blocks. The 256th entry is a
148 // free list of runs that have length >= 256 blocks.
149 base::LinkedList<Span> free_spans_[256];
151 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemoryHeap);
154 } // namespace content
156 #endif // CONTENT_COMMON_DISCARDABLE_SHARED_MEMORY_HEAP_H_