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