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 "content/common/content_export.h"
16 class DiscardableSharedMemory
;
21 // Implements a heap of discardable shared memory. An array of free lists
22 // is used to keep track of free blocks.
23 class CONTENT_EXPORT DiscardableSharedMemoryHeap
{
25 class CONTENT_EXPORT Span
: public base::LinkNode
<Span
> {
29 base::DiscardableSharedMemory
* shared_memory() { return shared_memory_
; }
30 size_t start() const { return start_
; }
31 size_t length() const { return length_
; }
34 friend class DiscardableSharedMemoryHeap
;
36 Span(base::DiscardableSharedMemory
* shared_memory
,
40 base::DiscardableSharedMemory
* shared_memory_
;
44 DISALLOW_COPY_AND_ASSIGN(Span
);
47 explicit DiscardableSharedMemoryHeap(size_t block_size
);
48 ~DiscardableSharedMemoryHeap();
50 // 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 // multiple of the block size. |deleted_callback| is called when
53 // |shared_memory| has been deleted.
54 scoped_ptr
<Span
> Grow(scoped_ptr
<base::DiscardableSharedMemory
> shared_memory
,
56 const base::Closure
& deleted_callback
);
58 // Merge |span| into the free lists. This will coalesce |span| with
59 // neighboring free spans when possible.
60 void MergeIntoFreeLists(scoped_ptr
<Span
> span
);
62 // Split an allocated span into two spans, one of length |blocks| followed
63 // by another span of length "span->length - blocks" blocks. Modifies |span|
64 // to point to the first span of length |blocks|. Return second span.
65 scoped_ptr
<Span
> Split(Span
* span
, size_t blocks
);
67 // Search free lists for span that satisfies the request for |blocks| of
68 // memory. If found, the span is removed from the free list and returned.
69 // |slack| determines the fitness requirement. Only spans that are less
70 // or equal to |blocks| + |slack| are considered, worse fitting spans are
72 scoped_ptr
<Span
> SearchFreeLists(size_t blocks
, size_t slack
);
74 // Release free shared memory segments.
75 void ReleaseFreeMemory();
77 // Release shared memory segments that have been purged.
78 void ReleasePurgedMemory();
80 // Returns total bytes of memory in heap.
81 size_t GetSize() const;
83 // Returns bytes of memory currently in the free lists.
84 size_t GetSizeOfFreeLists() const;
87 class ScopedMemorySegment
{
89 ScopedMemorySegment(DiscardableSharedMemoryHeap
* heap
,
90 scoped_ptr
<base::DiscardableSharedMemory
> shared_memory
,
92 const base::Closure
& deleted_callback
);
93 ~ScopedMemorySegment();
96 bool IsResident() const;
99 DiscardableSharedMemoryHeap
* const heap_
;
100 scoped_ptr
<base::DiscardableSharedMemory
> shared_memory_
;
102 const base::Closure deleted_callback_
;
104 DISALLOW_COPY_AND_ASSIGN(ScopedMemorySegment
);
107 void InsertIntoFreeList(scoped_ptr
<Span
> span
);
108 scoped_ptr
<Span
> RemoveFromFreeList(Span
* span
);
109 scoped_ptr
<Span
> Carve(Span
* span
, size_t blocks
);
110 void RegisterSpan(Span
* span
);
111 void UnregisterSpan(Span
* span
);
112 bool IsMemoryUsed(const base::DiscardableSharedMemory
* shared_memory
,
114 bool IsMemoryResident(const base::DiscardableSharedMemory
* shared_memory
);
115 void ReleaseMemory(const base::DiscardableSharedMemory
* shared_memory
,
120 size_t num_free_blocks_
;
122 // Vector of memory segments.
123 ScopedVector
<ScopedMemorySegment
> memory_segments_
;
125 // Mapping from first/last block of span to Span instance.
126 typedef base::hash_map
<size_t, Span
*> SpanMap
;
129 // 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
131 // 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.
133 base::LinkedList
<Span
> free_spans_
[256];
135 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemoryHeap
);
138 } // namespace content
140 #endif // CONTENT_COMMON_DISCARDABLE_SHARED_MEMORY_HEAP_H_