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