Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / common / discardable_shared_memory_heap.h
blob7ded677c3cc75630b61181c16a4b20258943550e
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 // Returns a unique identifier for a given tuple of (process id, segment id)
92 // that can be used to match memory dumps across different processes.
93 static base::trace_event::MemoryAllocatorDumpGuid GetSegmentGUIDForTracing(
94 uint64 tracing_process_id,
95 int32 segment_id);
97 private:
98 class ScopedMemorySegment {
99 public:
100 ScopedMemorySegment(DiscardableSharedMemoryHeap* heap,
101 scoped_ptr<base::DiscardableSharedMemory> shared_memory,
102 size_t size,
103 int32_t id,
104 const base::Closure& deleted_callback);
105 ~ScopedMemorySegment();
107 bool IsUsed() const;
108 bool IsResident() const;
110 // Used for dumping memory statistics from the segment to chrome://tracing.
111 void OnMemoryDump(base::trace_event::ProcessMemoryDump* pmd) const;
113 private:
114 DiscardableSharedMemoryHeap* const heap_;
115 scoped_ptr<base::DiscardableSharedMemory> shared_memory_;
116 const size_t size_;
117 const int32_t id_;
118 const base::Closure deleted_callback_;
120 DISALLOW_COPY_AND_ASSIGN(ScopedMemorySegment);
123 void InsertIntoFreeList(scoped_ptr<Span> span);
124 scoped_ptr<Span> RemoveFromFreeList(Span* span);
125 scoped_ptr<Span> Carve(Span* span, size_t blocks);
126 void RegisterSpan(Span* span);
127 void UnregisterSpan(Span* span);
128 bool IsMemoryUsed(const base::DiscardableSharedMemory* shared_memory,
129 size_t size);
130 bool IsMemoryResident(const base::DiscardableSharedMemory* shared_memory);
131 void ReleaseMemory(const base::DiscardableSharedMemory* shared_memory,
132 size_t size);
134 // Dumps memory statistics about a memory segment for chrome://tracing.
135 void OnMemoryDump(const base::DiscardableSharedMemory* shared_memory,
136 size_t size,
137 int32_t segment_id,
138 base::trace_event::ProcessMemoryDump* pmd);
140 size_t block_size_;
141 size_t num_blocks_;
142 size_t num_free_blocks_;
144 // Vector of memory segments.
145 ScopedVector<ScopedMemorySegment> memory_segments_;
147 // Mapping from first/last block of span to Span instance.
148 typedef base::hash_map<size_t, Span*> SpanMap;
149 SpanMap spans_;
151 // Array of linked-lists with free discardable memory regions. For i < 256,
152 // where the 1st entry is located at index 0 of the array, the kth entry
153 // is a free list of runs that consist of k blocks. The 256th entry is a
154 // free list of runs that have length >= 256 blocks.
155 base::LinkedList<Span> free_spans_[256];
157 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemoryHeap);
160 } // namespace content
162 #endif // CONTENT_COMMON_DISCARDABLE_SHARED_MEMORY_HEAP_H_