OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 COMPONENTS_HTML_VIEWER_DISCARDABLE_MEMORY_ALLOCATOR_H_ | 5 #ifndef COMPONENTS_HTML_VIEWER_DISCARDABLE_MEMORY_ALLOCATOR_H_ |
6 #define COMPONENTS_HTML_VIEWER_DISCARDABLE_MEMORY_ALLOCATOR_H_ | 6 #define COMPONENTS_HTML_VIEWER_DISCARDABLE_MEMORY_ALLOCATOR_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 | 9 |
10 #include "base/memory/discardable_memory_allocator.h" | 10 #include "base/memory/discardable_memory_allocator.h" |
11 #include "base/synchronization/lock.h" | |
12 | 11 |
13 namespace html_viewer { | 12 namespace html_viewer { |
14 | 13 |
15 // A discarable memory allocator which will unallocate chunks on new | 14 // A discarable memory allocator which will unallocate chunks on new |
16 // allocations. | 15 // allocations. |
17 class DiscardableMemoryAllocator : public base::DiscardableMemoryAllocator { | 16 class DiscardableMemoryAllocator : public base::DiscardableMemoryAllocator { |
18 public: | 17 public: |
| 18 class OwnedMemoryChunk; |
| 19 |
19 explicit DiscardableMemoryAllocator(size_t desired_max_memory); | 20 explicit DiscardableMemoryAllocator(size_t desired_max_memory); |
20 ~DiscardableMemoryAllocator() override; | 21 ~DiscardableMemoryAllocator() override; |
21 | 22 |
22 // Overridden from DiscardableMemoryAllocator: | 23 // Overridden from DiscardableMemoryAllocator: |
23 scoped_ptr<base::DiscardableMemory> AllocateLockedDiscardableMemory( | 24 scoped_ptr<base::DiscardableMemory> AllocateLockedDiscardableMemory( |
24 size_t size) override; | 25 size_t size) override; |
25 | 26 |
26 private: | 27 private: |
27 class DiscardableMemoryChunkImpl; | 28 friend class OwnedMemoryChunk; |
28 friend class DiscardableMemoryChunkImpl; | |
29 | 29 |
30 // Called by DiscardableMemoryChunkImpl when they are unlocked. This puts them | 30 // Called by OwnedMemoryChunks when they are unlocked. This puts them at the |
31 // at the end of the live_unlocked_chunks_ list and passes an iterator to | 31 // end of the live_unlocked_chunks_ list and passes an iterator to their |
32 // their position in the unlocked chunk list. | 32 // position in the unlocked chunk list. |
33 std::list<DiscardableMemoryChunkImpl*>::iterator NotifyUnlocked( | 33 std::list<OwnedMemoryChunk*>::iterator NotifyUnlocked( |
34 DiscardableMemoryChunkImpl* chunk); | 34 OwnedMemoryChunk* chunk); |
35 | 35 |
36 // Called by DiscardableMemoryChunkImpl when they are locked. This removes the | 36 // Called by OwnedMemoryChunks when they are locked. This removes the passed |
37 // passed in unlocked chunk list. | 37 // in unlocked chunk list. |
38 void NotifyLocked(std::list<DiscardableMemoryChunkImpl*>::iterator it); | 38 void NotifyLocked(std::list<OwnedMemoryChunk*>::iterator it); |
39 | |
40 // Called by DiscardableMemoryChunkImpl when it's destructed. It must be | |
41 // unlocked, by definition. | |
42 void NotifyDestructed(std::list<DiscardableMemoryChunkImpl*>::iterator it); | |
43 | 39 |
44 // The amount of memory we can allocate before we try to free unlocked | 40 // The amount of memory we can allocate before we try to free unlocked |
45 // chunks. We can go over this amount if all callers keep their discardable | 41 // chunks. We can go over this amount if all callers keep their discardable |
46 // chunks locked. | 42 // chunks locked. |
47 const size_t desired_max_memory_; | 43 const size_t desired_max_memory_; |
48 | 44 |
49 // Protects all members below, since this class can be called on the main | |
50 // thread and impl side painting raster threads. | |
51 base::Lock lock_; | |
52 | |
53 // A count of the sum of memory. Used to trigger discarding the oldest | 45 // A count of the sum of memory. Used to trigger discarding the oldest |
54 // memory. | 46 // memory. |
55 size_t total_live_memory_; | 47 size_t total_live_memory_; |
56 | 48 |
57 // The number of locked chunks. | 49 // The number of locked chunks. |
58 int locked_chunks_; | 50 int locked_chunks_; |
59 | 51 |
60 // A linked list of unlocked allocated chunks so that the tail is most | 52 // A linked list of unlocked allocated chunks so that the tail is most |
61 // recently accessed chunks. | 53 // recently accessed chunks. |
62 std::list<DiscardableMemoryChunkImpl*> live_unlocked_chunks_; | 54 std::list<OwnedMemoryChunk*> live_unlocked_chunks_; |
63 | 55 |
64 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryAllocator); | 56 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryAllocator); |
65 }; | 57 }; |
66 | 58 |
67 } // namespace html_viewer | 59 } // namespace html_viewer |
68 | 60 |
69 #endif // COMPONENTS_HTML_VIEWER_DISCARDABLE_MEMORY_ALLOCATOR_H_ | 61 #endif // COMPONENTS_HTML_VIEWER_DISCARDABLE_MEMORY_ALLOCATOR_H_ |
OLD | NEW |