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" | |
11 | 12 |
12 namespace html_viewer { | 13 namespace html_viewer { |
13 | 14 |
14 // A discarable memory allocator which will unallocate chunks on new | 15 // A discarable memory allocator which will unallocate chunks on new |
15 // allocations. | 16 // allocations. |
16 class DiscardableMemoryAllocator : public base::DiscardableMemoryAllocator { | 17 class DiscardableMemoryAllocator : public base::DiscardableMemoryAllocator { |
17 public: | 18 public: |
18 class OwnedMemoryChunk; | |
19 | |
20 explicit DiscardableMemoryAllocator(size_t desired_max_memory); | 19 explicit DiscardableMemoryAllocator(size_t desired_max_memory); |
21 ~DiscardableMemoryAllocator() override; | 20 ~DiscardableMemoryAllocator() override; |
22 | 21 |
23 // Overridden from DiscardableMemoryAllocator: | 22 // Overridden from DiscardableMemoryAllocator: |
24 scoped_ptr<base::DiscardableMemory> AllocateLockedDiscardableMemory( | 23 scoped_ptr<base::DiscardableMemory> AllocateLockedDiscardableMemory( |
25 size_t size) override; | 24 size_t size) override; |
26 | 25 |
27 private: | 26 private: |
28 friend class OwnedMemoryChunk; | 27 class DiscardableMemoryChunkImpl; |
28 friend class DiscardableMemoryChunkImpl; | |
29 | 29 |
30 // Called by OwnedMemoryChunks when they are unlocked. This puts them at the | 30 // Called by DiscardableMemoryChunkImpl when they are unlocked. This puts them |
31 // end of the live_unlocked_chunks_ list and passes an iterator to their | 31 // at the end of the live_unlocked_chunks_ list and passes an iterator to |
32 // position in the unlocked chunk list. | 32 // their position in the unlocked chunk list. |
33 std::list<OwnedMemoryChunk*>::iterator NotifyUnlocked( | 33 std::list<DiscardableMemoryChunkImpl*>::iterator NotifyUnlocked( |
34 OwnedMemoryChunk* chunk); | 34 DiscardableMemoryChunkImpl* chunk); |
35 | 35 |
36 // Called by OwnedMemoryChunks when they are locked. This removes the passed | 36 // Called by DiscardableMemoryChunkImpl when they are locked. This removes the |
37 // in unlocked chunk list. | 37 // passed in unlocked chunk list. |
38 void NotifyLocked(std::list<OwnedMemoryChunk*>::iterator it); | 38 void NotifyLocked(std::list<DiscardableMemoryChunkImpl*>::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); | |
39 | 43 |
40 // The amount of memory we can allocate before we try to free unlocked | 44 // The amount of memory we can allocate before we try to free unlocked |
41 // chunks. We can go over this amount if all callers keep their discardable | 45 // chunks. We can go over this amount if all callers keep their discardable |
42 // chunks locked. | 46 // chunks locked. |
43 const size_t desired_max_memory_; | 47 const size_t desired_max_memory_; |
44 | 48 |
49 base::Lock lock_; | |
danakj
2015/06/15 17:47:34
Could you comment or otherwise make it clear which
sky
2015/06/15 21:47:46
Also document what threads are in use here. That i
jam
2015/06/15 22:06:01
Done.
jam
2015/06/15 22:44:55
Done.
| |
50 | |
45 // A count of the sum of memory. Used to trigger discarding the oldest | 51 // A count of the sum of memory. Used to trigger discarding the oldest |
46 // memory. | 52 // memory. |
47 size_t total_live_memory_; | 53 size_t total_live_memory_; |
48 | 54 |
49 // The number of locked chunks. | 55 // The number of locked chunks. |
50 int locked_chunks_; | 56 int locked_chunks_; |
51 | 57 |
52 // A linked list of unlocked allocated chunks so that the tail is most | 58 // A linked list of unlocked allocated chunks so that the tail is most |
53 // recently accessed chunks. | 59 // recently accessed chunks. |
54 std::list<OwnedMemoryChunk*> live_unlocked_chunks_; | 60 std::list<DiscardableMemoryChunkImpl*> live_unlocked_chunks_; |
55 | 61 |
56 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryAllocator); | 62 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryAllocator); |
57 }; | 63 }; |
58 | 64 |
59 } // namespace html_viewer | 65 } // namespace html_viewer |
60 | 66 |
61 #endif // COMPONENTS_HTML_VIEWER_DISCARDABLE_MEMORY_ALLOCATOR_H_ | 67 #endif // COMPONENTS_HTML_VIEWER_DISCARDABLE_MEMORY_ALLOCATOR_H_ |
OLD | NEW |