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_
10 #include "base/containers/hash_tables.h"
11 #include "base/containers/linked_list.h"
12 #include "base/memory/linked_ptr.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/memory/scoped_vector.h"
15 #include "content/common/content_export.h"
18 class DiscardableSharedMemory
;
23 // Implements a heap of discardable shared memory. A free list is used to keep
24 // track of free blocks.
25 class CONTENT_EXPORT DiscardableSharedMemoryHeap
{
27 class CONTENT_EXPORT Span
: public base::LinkNode
<Span
> {
31 base::DiscardableSharedMemory
* shared_memory() { return shared_memory_
; }
32 size_t start() const { return start_
; }
33 size_t length() const { return length_
; }
36 friend class DiscardableSharedMemoryHeap
;
38 Span(base::DiscardableSharedMemory
* shared_memory
,
42 base::DiscardableSharedMemory
* shared_memory_
;
46 DISALLOW_COPY_AND_ASSIGN(Span
);
49 explicit DiscardableSharedMemoryHeap(size_t block_size
);
50 ~DiscardableSharedMemoryHeap();
52 // Grow heap using |shared_memory| and return a span for this new memory.
53 // |shared_memory| must be aligned to the block size and |size| must be a
54 // multiple of the block size.
55 scoped_ptr
<Span
> Grow(scoped_ptr
<base::DiscardableSharedMemory
> shared_memory
,
58 // Merge |span| into the free list. This will coalesce |span| with
59 // neighboring spans in free list when possible.
60 void MergeIntoFreeList(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 list for span that satisfies the request for |blocks| of
68 // memory. If found, the span is removed from the free list and returned.
69 scoped_ptr
<Span
> SearchFreeList(size_t blocks
);
71 // Release shared memory segments that have been purged. Returns bytes of
72 // memory that were released.
73 size_t ReleaseFreeMemory();
76 scoped_ptr
<Span
> RemoveFromFreeList(Span
* span
);
77 scoped_ptr
<Span
> Carve(Span
* span
, size_t blocks
);
78 void RegisterSpan(Span
* span
);
79 void UnregisterSpan(Span
* span
);
80 void ReleaseMemory(base::DiscardableSharedMemory
* shared_memory
);
84 // Discardable shared memory instances.
85 ScopedVector
<base::DiscardableSharedMemory
> shared_memory_segments_
;
87 // Mapping from first/last block of span to Span instance.
88 typedef base::hash_map
<size_t, Span
*> SpanMap
;
91 // Linked-list of free discardable memory regions.
92 base::LinkedList
<Span
> free_spans_
;
94 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemoryHeap
);
97 } // namespace content
99 #endif // CONTENT_COMMON_DISCARDABLE_SHARED_MEMORY_HEAP_H_