Chromium Code Reviews| Index: mojo/services/html_viewer/discardable_memory_allocator.h |
| diff --git a/mojo/services/html_viewer/discardable_memory_allocator.h b/mojo/services/html_viewer/discardable_memory_allocator.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ddc7d77d5c2e3f2bc4b3b3f708ab384ab09bc441 |
| --- /dev/null |
| +++ b/mojo/services/html_viewer/discardable_memory_allocator.h |
| @@ -0,0 +1,50 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef MOJO_SERVICES_HTML_VIEWER_DISCARDABLE_MEMORY_ALLOCATOR_H_ |
| +#define MOJO_SERVICES_HTML_VIEWER_DISCARDABLE_MEMORY_ALLOCATOR_H_ |
| + |
| +#include <list> |
| + |
| +#include "base/memory/discardable_memory_shmem_allocator.h" |
| + |
| +namespace html_viewer { |
| + |
| +// A discarable memory allocator which will unallocate chunks on new |
| +// allocations. |
| +class DiscardableMemoryAllocator |
| + : public base::DiscardableMemoryShmemAllocator { |
| + public: |
| + class OwnedMemoryChunk; |
|
jamesr
2015/03/17 23:26:19
does this forward declaration need to be public?
Elliot Glaysher
2015/03/18 20:36:22
It does if I want to hide the implementation of Di
|
| + |
| + DiscardableMemoryAllocator(size_t max_memory); |
|
jamesr
2015/03/17 23:26:19
explicit
|
| + ~DiscardableMemoryAllocator() override; |
| + |
| + // Overridden from DiscardableMemoryShmemAllocator: |
| + scoped_ptr<base::DiscardableMemoryShmemChunk> AllocateLockedDiscardableMemory( |
| + size_t size) override; |
| + |
| + private: |
| + friend class OwnedMemoryChunk; |
| + |
| + void MoveToBack(std::list<OwnedMemoryChunk*>::iterator it); |
| + |
| + // The amount of memory we can allocate before we try to free unlocked chunks. |
| + const size_t max_memory_; |
| + |
| + // A count of the sum of memory. Used to trigger discarding the oldest |
| + // memory. |
| + size_t total_live_memory_; |
| + |
| + // A linked list of allocated chunks so that the tail is the newest chunks, |
| + // and the head is the oldest chunks. On Lock()ing a chunk, it is sent to the |
| + // back of this list. |
| + std::list<OwnedMemoryChunk*> live_chunks_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryAllocator); |
| +}; |
| + |
| +} // namespace html_viewer |
| + |
| +#endif // MOJO_SERVICES_HTML_VIEWER_DISCARDABLE_MEMORY_ALLOCATOR_H_ |