OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #ifndef CONTENT_COMMON_DISCARDABLE_SHARED_MEMORY_HEAP_H_ |
| 6 #define CONTENT_COMMON_DISCARDABLE_SHARED_MEMORY_HEAP_H_ |
| 7 |
| 8 #include <bitset> |
| 9 |
| 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" |
| 16 |
| 17 namespace base { |
| 18 class DiscardableSharedMemory; |
| 19 } |
| 20 |
| 21 namespace content { |
| 22 |
| 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 { |
| 26 public: |
| 27 class CONTENT_EXPORT Span : public base::LinkNode<Span> { |
| 28 public: |
| 29 ~Span(); |
| 30 |
| 31 base::DiscardableSharedMemory* shared_memory() { |
| 32 return shared_memory_.get(); |
| 33 } |
| 34 size_t start() const { return start_; } |
| 35 size_t length() const { return length_; } |
| 36 |
| 37 private: |
| 38 friend class DiscardableSharedMemoryHeap; |
| 39 |
| 40 Span(linked_ptr<base::DiscardableSharedMemory> shared_memory, |
| 41 size_t start, |
| 42 size_t length); |
| 43 |
| 44 linked_ptr<base::DiscardableSharedMemory> shared_memory_; |
| 45 size_t start_; |
| 46 size_t length_; |
| 47 |
| 48 DISALLOW_COPY_AND_ASSIGN(Span); |
| 49 }; |
| 50 |
| 51 explicit DiscardableSharedMemoryHeap(size_t block_size); |
| 52 ~DiscardableSharedMemoryHeap(); |
| 53 |
| 54 // Grow heap using |memory| and return a span for this new memory. |
| 55 // |memory| must be aligned to the block size and |size| must be a |
| 56 // multiple of the block size. |
| 57 scoped_ptr<Span> Grow(scoped_ptr<base::DiscardableSharedMemory> memory, |
| 58 size_t size); |
| 59 |
| 60 // Merge |span| into the free list. This will coalesce |span| with |
| 61 // neighboring spans in free list when possible. |
| 62 void MergeIntoFreeList(scoped_ptr<Span> span); |
| 63 |
| 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); |
| 68 |
| 69 // Search free list for span that satisfies the request for |blocks| of |
| 70 // memory. If found, the span is removed from the free list and returned. |
| 71 scoped_ptr<Span> SearchFreeList(size_t blocks); |
| 72 |
| 73 // Delete span and release memory if possible. |
| 74 void DeleteSpan(scoped_ptr<Span> span); |
| 75 |
| 76 private: |
| 77 scoped_ptr<Span> RemoveFromFreeList(Span* span); |
| 78 scoped_ptr<Span> Carve(Span* span, size_t blocks); |
| 79 void RegisterSpan(Span* span); |
| 80 |
| 81 size_t block_size_; |
| 82 |
| 83 // Mapping from first/last block of span to Span instance. |
| 84 typedef base::hash_map<size_t, Span*> SpanMap; |
| 85 SpanMap spans_; |
| 86 |
| 87 // Linked-list of free discardable memory regions. |
| 88 base::LinkedList<Span> free_spans_; |
| 89 |
| 90 DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemoryHeap); |
| 91 }; |
| 92 |
| 93 } // namespace content |
| 94 |
| 95 #endif // CONTENT_COMMON_DISCARDABLE_SHARED_MEMORY_HEAP_H_ |
OLD | NEW |