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"
12 namespace html_viewer
{
14 // A discarable memory allocator which will unallocate chunks on new
16 class DiscardableMemoryAllocator
: public base::DiscardableMemoryAllocator
{
18 class OwnedMemoryChunk
;
20 explicit DiscardableMemoryAllocator(size_t desired_max_memory
);
21 ~DiscardableMemoryAllocator() override
;
23 // Overridden from DiscardableMemoryAllocator:
24 scoped_ptr
<base::DiscardableMemory
> AllocateLockedDiscardableMemory(
25 size_t size
) override
;
28 friend class OwnedMemoryChunk
;
30 // Called by OwnedMemoryChunks when they are unlocked. This puts them at the
31 // end of the live_unlocked_chunks_ list and passes an iterator to their
32 // position in the unlocked chunk list.
33 std::list
<OwnedMemoryChunk
*>::iterator
NotifyUnlocked(
34 OwnedMemoryChunk
* chunk
);
36 // Called by OwnedMemoryChunks when they are locked. This removes the passed
37 // in unlocked chunk list.
38 void NotifyLocked(std::list
<OwnedMemoryChunk
*>::iterator it
);
40 // 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
43 const size_t desired_max_memory_
;
45 // A count of the sum of memory. Used to trigger discarding the oldest
47 size_t total_live_memory_
;
49 // The number of locked chunks.
52 // A linked list of unlocked allocated chunks so that the tail is most
53 // recently accessed chunks.
54 std::list
<OwnedMemoryChunk
*> live_unlocked_chunks_
;
56 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryAllocator
);
59 } // namespace html_viewer
61 #endif // COMPONENTS_HTML_VIEWER_DISCARDABLE_MEMORY_ALLOCATOR_H_