Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_COMMON_DISCARDABLE_SHARED_MEMORY_HEAP_H_ | 5 #ifndef CONTENT_COMMON_DISCARDABLE_SHARED_MEMORY_HEAP_H_ |
| 6 #define CONTENT_COMMON_DISCARDABLE_SHARED_MEMORY_HEAP_H_ | 6 #define CONTENT_COMMON_DISCARDABLE_SHARED_MEMORY_HEAP_H_ |
| 7 | 7 |
| 8 #include <bitset> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/containers/hash_tables.h" | 10 #include "base/containers/hash_tables.h" |
| 11 #include "base/containers/linked_list.h" | 11 #include "base/containers/linked_list.h" |
| 12 #include "base/memory/linked_ptr.h" | 12 #include "base/memory/linked_ptr.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/scoped_vector.h" | |
| 15 #include "content/common/content_export.h" | 14 #include "content/common/content_export.h" |
| 16 | 15 |
| 17 namespace base { | 16 namespace base { |
| 18 class DiscardableSharedMemory; | 17 class DiscardableSharedMemory; |
| 19 } | 18 } |
| 20 | 19 |
| 21 namespace content { | 20 namespace content { |
| 22 | 21 |
| 23 // Implements a heap of discardable shared memory. A free list is used to keep | 22 // Implements a heap of discardable shared memory. A free list is used to keep |
| 24 // track of free blocks. | 23 // track of free blocks. |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 43 size_t start_; | 42 size_t start_; |
| 44 size_t length_; | 43 size_t length_; |
| 45 | 44 |
| 46 DISALLOW_COPY_AND_ASSIGN(Span); | 45 DISALLOW_COPY_AND_ASSIGN(Span); |
| 47 }; | 46 }; |
| 48 | 47 |
| 49 explicit DiscardableSharedMemoryHeap(size_t block_size); | 48 explicit DiscardableSharedMemoryHeap(size_t block_size); |
| 50 ~DiscardableSharedMemoryHeap(); | 49 ~DiscardableSharedMemoryHeap(); |
| 51 | 50 |
| 52 // Grow heap using |shared_memory| and return a span for this new memory. | 51 // 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 | 52 // |shared_memory| must be aligned to the block size and size must be a |
|
Avi (use Gerrit)
2015/03/09 16:03:37
What size?
reveman
2015/03/09 18:08:26
DiscardableSharedMemory::mapped_size(), but I rein
| |
| 54 // multiple of the block size. | 53 // multiple of the block size. |
| 55 scoped_ptr<Span> Grow(scoped_ptr<base::DiscardableSharedMemory> shared_memory, | 54 scoped_ptr<Span> Grow( |
| 56 size_t size); | 55 scoped_ptr<base::DiscardableSharedMemory> shared_memory); |
| 57 | 56 |
| 58 // Merge |span| into the free list. This will coalesce |span| with | 57 // Merge |span| into the free list. This will coalesce |span| with |
| 59 // neighboring spans in free list when possible. | 58 // neighboring spans in free list when possible. |
| 60 void MergeIntoFreeList(scoped_ptr<Span> span); | 59 void MergeIntoFreeList(scoped_ptr<Span> span); |
| 61 | 60 |
| 62 // Split an allocated span into two spans, one of length |blocks| followed | 61 // Split an allocated span into two spans, one of length |blocks| followed |
| 63 // by another span of length "span->length - blocks" blocks. Modifies |span| | 62 // by another span of length "span->length - blocks" blocks. Modifies |span| |
| 64 // to point to the first span of length |blocks|. Return second span. | 63 // to point to the first span of length |blocks|. Return second span. |
| 65 scoped_ptr<Span> Split(Span* span, size_t blocks); | 64 scoped_ptr<Span> Split(Span* span, size_t blocks); |
| 66 | 65 |
| 67 // Search free list for span that satisfies the request for |blocks| of | 66 // 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. | 67 // memory. If found, the span is removed from the free list and returned. |
| 69 scoped_ptr<Span> SearchFreeList(size_t blocks); | 68 scoped_ptr<Span> SearchFreeList(size_t blocks); |
| 70 | 69 |
| 71 // Release shared memory segments that have been purged. Returns bytes of | 70 // Release free shared memory segments. |
| 72 // memory that were released. | 71 void ReleaseFreeMemory(); |
| 73 size_t ReleaseFreeMemory(); | 72 |
| 73 // Release shared memory segments that have been purged. | |
| 74 void ReleasePurgedMemory(); | |
| 75 | |
| 76 // Returns total bytes of memory in heap. | |
| 77 size_t GetSize() const; | |
| 78 | |
| 79 // Returns bytes of memory currently in the free list. | |
| 80 size_t GetFreeListSize() const; | |
| 74 | 81 |
| 75 private: | 82 private: |
| 83 class ScopedMemorySegment { | |
| 84 public: | |
| 85 ScopedMemorySegment(scoped_ptr<base::DiscardableSharedMemory> shared_memory, | |
| 86 DiscardableSharedMemoryHeap* heap); | |
| 87 ~ScopedMemorySegment(); | |
| 88 | |
| 89 bool IsUsed() const; | |
| 90 bool IsResident() const; | |
| 91 | |
| 92 private: | |
| 93 scoped_ptr<base::DiscardableSharedMemory> shared_memory_; | |
| 94 DiscardableSharedMemoryHeap* heap_; | |
| 95 | |
| 96 DISALLOW_COPY_AND_ASSIGN(ScopedMemorySegment); | |
| 97 }; | |
| 98 | |
| 76 scoped_ptr<Span> RemoveFromFreeList(Span* span); | 99 scoped_ptr<Span> RemoveFromFreeList(Span* span); |
| 77 scoped_ptr<Span> Carve(Span* span, size_t blocks); | 100 scoped_ptr<Span> Carve(Span* span, size_t blocks); |
| 78 void RegisterSpan(Span* span); | 101 void RegisterSpan(Span* span); |
| 79 void UnregisterSpan(Span* span); | 102 void UnregisterSpan(Span* span); |
| 80 void ReleaseMemory(base::DiscardableSharedMemory* shared_memory); | 103 bool IsMemoryUsed(const base::DiscardableSharedMemory* shared_memory); |
| 104 bool IsMemoryResident(const base::DiscardableSharedMemory* shared_memory); | |
| 105 void ReleaseMemory(const base::DiscardableSharedMemory* shared_memory); | |
| 81 | 106 |
| 82 size_t block_size_; | 107 size_t block_size_; |
| 108 size_t num_blocks_; | |
| 109 size_t num_free_blocks_; | |
| 83 | 110 |
| 84 // Discardable shared memory instances. | 111 // Vector of memory segments. |
| 85 ScopedVector<base::DiscardableSharedMemory> shared_memory_segments_; | 112 std::vector<linked_ptr<ScopedMemorySegment>> memory_segments_; |
|
Avi (use Gerrit)
2015/03/09 16:03:37
Why are you switching to linked_ptr? ScopedVector
Avi (use Gerrit)
2015/03/09 17:21:25
http://crbug.com/137767
reveman
2015/03/09 18:08:26
I used linked_ptr as it's not spec compliant to us
| |
| 86 | 113 |
| 87 // Mapping from first/last block of span to Span instance. | 114 // Mapping from first/last block of span to Span instance. |
| 88 typedef base::hash_map<size_t, Span*> SpanMap; | 115 typedef base::hash_map<size_t, Span*> SpanMap; |
| 89 SpanMap spans_; | 116 SpanMap spans_; |
| 90 | 117 |
| 91 // Linked-list of free discardable memory regions. | 118 // Linked-list of free discardable memory regions. |
| 92 base::LinkedList<Span> free_spans_; | 119 base::LinkedList<Span> free_spans_; |
| 93 | 120 |
| 94 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemoryHeap); | 121 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemoryHeap); |
| 95 }; | 122 }; |
| 96 | 123 |
| 97 } // namespace content | 124 } // namespace content |
| 98 | 125 |
| 99 #endif // CONTENT_COMMON_DISCARDABLE_SHARED_MEMORY_HEAP_H_ | 126 #endif // CONTENT_COMMON_DISCARDABLE_SHARED_MEMORY_HEAP_H_ |
| OLD | NEW |