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.
5 #ifndef COMPONENTS_HTML_VIEWER_DISCARDABLE_MEMORY_ALLOCATOR_H_
6 #define COMPONENTS_HTML_VIEWER_DISCARDABLE_MEMORY_ALLOCATOR_H_
10 #include "base/memory/discardable_memory_allocator.h"
11 #include "base/synchronization/lock.h"
13 namespace html_viewer
{
15 // A discarable memory allocator which will unallocate chunks on new
17 class DiscardableMemoryAllocator
: public base::DiscardableMemoryAllocator
{
19 explicit DiscardableMemoryAllocator(size_t desired_max_memory
);
20 ~DiscardableMemoryAllocator() override
;
22 // Overridden from DiscardableMemoryAllocator:
23 scoped_ptr
<base::DiscardableMemory
> AllocateLockedDiscardableMemory(
24 size_t size
) override
;
27 class DiscardableMemoryChunkImpl
;
28 friend class DiscardableMemoryChunkImpl
;
30 // Called by DiscardableMemoryChunkImpl when they are unlocked. This puts them
31 // at the end of the live_unlocked_chunks_ list and passes an iterator to
32 // their position in the unlocked chunk list.
33 std::list
<DiscardableMemoryChunkImpl
*>::iterator
NotifyUnlocked(
34 DiscardableMemoryChunkImpl
* chunk
);
36 // Called by DiscardableMemoryChunkImpl when they are locked. This removes the
37 // passed in unlocked chunk list.
38 void NotifyLocked(std::list
<DiscardableMemoryChunkImpl
*>::iterator it
);
40 // Called by DiscardableMemoryChunkImpl when it's destructed. It must be
41 // unlocked, by definition.
42 void NotifyDestructed(std::list
<DiscardableMemoryChunkImpl
*>::iterator it
);
44 // 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
47 const size_t desired_max_memory_
;
49 // Protects all members below, since this class can be called on the main
50 // thread and impl side painting raster threads.
53 // A count of the sum of memory. Used to trigger discarding the oldest
55 size_t total_live_memory_
;
57 // The number of locked chunks.
60 // A linked list of unlocked allocated chunks so that the tail is most
61 // recently accessed chunks.
62 std::list
<DiscardableMemoryChunkImpl
*> live_unlocked_chunks_
;
64 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryAllocator
);
67 } // namespace html_viewer
69 #endif // COMPONENTS_HTML_VIEWER_DISCARDABLE_MEMORY_ALLOCATOR_H_