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
,
98 class ScopedMemorySegment
{
100 ScopedMemorySegment(DiscardableSharedMemoryHeap
* heap
,
101 scoped_ptr
<base::DiscardableSharedMemory
> shared_memory
,
104 const base::Closure
& deleted_callback
);
105 ~ScopedMemorySegment();
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;
114 DiscardableSharedMemoryHeap
* const heap_
;
115 scoped_ptr
<base::DiscardableSharedMemory
> shared_memory_
;
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
,
130 bool IsMemoryResident(const base::DiscardableSharedMemory
* shared_memory
);
131 void ReleaseMemory(const base::DiscardableSharedMemory
* shared_memory
,
134 // Dumps memory statistics about a memory segment for chrome://tracing.
135 void OnMemoryDump(const base::DiscardableSharedMemory
* shared_memory
,
138 base::trace_event::ProcessMemoryDump
* pmd
);
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
;
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_