Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / content / common / discardable_shared_memory_heap.h
blob23b9cfb87d237dc1a4016dc029a0086aaf05fca1
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"
15 namespace base {
16 class DiscardableSharedMemory;
19 namespace content {
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 {
24 public:
25 class CONTENT_EXPORT Span : public base::LinkNode<Span> {
26 public:
27 ~Span();
29 base::DiscardableSharedMemory* shared_memory() { return shared_memory_; }
30 size_t start() const { return start_; }
31 size_t length() const { return length_; }
33 private:
34 friend class DiscardableSharedMemoryHeap;
36 Span(base::DiscardableSharedMemory* shared_memory,
37 size_t start,
38 size_t length);
40 base::DiscardableSharedMemory* shared_memory_;
41 size_t start_;
42 size_t length_;
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,
55 size_t size,
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
71 // ignored.
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;
86 private:
87 class ScopedMemorySegment {
88 public:
89 ScopedMemorySegment(DiscardableSharedMemoryHeap* heap,
90 scoped_ptr<base::DiscardableSharedMemory> shared_memory,
91 size_t size,
92 const base::Closure& deleted_callback);
93 ~ScopedMemorySegment();
95 bool IsUsed() const;
96 bool IsResident() const;
98 private:
99 DiscardableSharedMemoryHeap* const heap_;
100 scoped_ptr<base::DiscardableSharedMemory> shared_memory_;
101 const size_t size_;
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,
113 size_t size);
114 bool IsMemoryResident(const base::DiscardableSharedMemory* shared_memory);
115 void ReleaseMemory(const base::DiscardableSharedMemory* shared_memory,
116 size_t size);
118 size_t block_size_;
119 size_t num_blocks_;
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;
127 SpanMap spans_;
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_