| 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 // 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 |
| 45 // A count of the sum of memory. Used to trigger discarding the oldest | 53 // A count of the sum of memory. Used to trigger discarding the oldest |
| 46 // memory. | 54 // memory. |
| 47 size_t total_live_memory_; | 55 size_t total_live_memory_; |
| 48 | 56 |
| 49 // The number of locked chunks. | 57 // The number of locked chunks. |
| 50 int locked_chunks_; | 58 int locked_chunks_; |
| 51 | 59 |
| 52 // A linked list of unlocked allocated chunks so that the tail is most | 60 // A linked list of unlocked allocated chunks so that the tail is most |
| 53 // recently accessed chunks. | 61 // recently accessed chunks. |
| 54 std::list<OwnedMemoryChunk*> live_unlocked_chunks_; | 62 std::list<DiscardableMemoryChunkImpl*> live_unlocked_chunks_; |
| 55 | 63 |
| 56 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryAllocator); | 64 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryAllocator); |
| 57 }; | 65 }; |
| 58 | 66 |
| 59 } // namespace html_viewer | 67 } // namespace html_viewer |
| 60 | 68 |
| 61 #endif // COMPONENTS_HTML_VIEWER_DISCARDABLE_MEMORY_ALLOCATOR_H_ | 69 #endif // COMPONENTS_HTML_VIEWER_DISCARDABLE_MEMORY_ALLOCATOR_H_ |
| OLD | NEW |