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"
17 class DiscardableSharedMemory
;
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
{
26 class CONTENT_EXPORT Span
: public base::LinkNode
<Span
> {
30 base::DiscardableSharedMemory
* shared_memory() { return shared_memory_
; }
31 size_t start() const { return start_
; }
32 size_t length() const { return length_
; }
35 friend class DiscardableSharedMemoryHeap
;
37 Span(base::DiscardableSharedMemory
* shared_memory
,
41 base::DiscardableSharedMemory
* shared_memory_
;
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
,
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
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
,
97 // Returns a MemoryAllocatorDump for a given span on |pmd| with the size of
99 base::trace_event::MemoryAllocatorDump
* CreateMemoryAllocatorDump(
102 base::trace_event::ProcessMemoryDump
* pmd
) const;
105 class ScopedMemorySegment
{
107 ScopedMemorySegment(DiscardableSharedMemoryHeap
* heap
,
108 scoped_ptr
<base::DiscardableSharedMemory
> shared_memory
,
111 const base::Closure
& deleted_callback
);
112 ~ScopedMemorySegment();
115 bool IsResident() const;
117 bool ContainsSpan(Span
* span
) const;
119 base::trace_event::MemoryAllocatorDump
* CreateMemoryAllocatorDump(
123 base::trace_event::ProcessMemoryDump
* pmd
) const;
125 // Used for dumping memory statistics from the segment to chrome://tracing.
126 void OnMemoryDump(base::trace_event::ProcessMemoryDump
* pmd
) const;
129 DiscardableSharedMemoryHeap
* const heap_
;
130 scoped_ptr
<base::DiscardableSharedMemory
> shared_memory_
;
133 const base::Closure deleted_callback_
;
135 DISALLOW_COPY_AND_ASSIGN(ScopedMemorySegment
);
138 void InsertIntoFreeList(scoped_ptr
<Span
> span
);
139 scoped_ptr
<Span
> RemoveFromFreeList(Span
* span
);
140 scoped_ptr
<Span
> Carve(Span
* span
, size_t blocks
);
141 void RegisterSpan(Span
* span
);
142 void UnregisterSpan(Span
* span
);
143 bool IsMemoryUsed(const base::DiscardableSharedMemory
* shared_memory
,
145 bool IsMemoryResident(const base::DiscardableSharedMemory
* shared_memory
);
146 void ReleaseMemory(const base::DiscardableSharedMemory
* shared_memory
,
149 // Dumps memory statistics about a memory segment for chrome://tracing.
150 void OnMemoryDump(const base::DiscardableSharedMemory
* shared_memory
,
153 base::trace_event::ProcessMemoryDump
* pmd
);
157 size_t num_free_blocks_
;
159 // Vector of memory segments.
160 ScopedVector
<ScopedMemorySegment
> memory_segments_
;
162 // Mapping from first/last block of span to Span instance.
163 typedef base::hash_map
<size_t, Span
*> SpanMap
;
166 // Array of linked-lists with free discardable memory regions. For i < 256,
167 // where the 1st entry is located at index 0 of the array, the kth entry
168 // is a free list of runs that consist of k blocks. The 256th entry is a
169 // free list of runs that have length >= 256 blocks.
170 base::LinkedList
<Span
> free_spans_
[256];
172 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemoryHeap
);
175 } // namespace content
177 #endif // CONTENT_COMMON_DISCARDABLE_SHARED_MEMORY_HEAP_H_