Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(570)

Unified Diff: components/html_viewer/discardable_memory_allocator.cc

Issue 1677293002: Bye bye Mandoline (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: moar Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: components/html_viewer/discardable_memory_allocator.cc
diff --git a/components/html_viewer/discardable_memory_allocator.cc b/components/html_viewer/discardable_memory_allocator.cc
deleted file mode 100644
index 8e971442f3b0e2d9370a380f60e2b1afd67d589e..0000000000000000000000000000000000000000
--- a/components/html_viewer/discardable_memory_allocator.cc
+++ /dev/null
@@ -1,154 +0,0 @@
-// 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.
-
-#include "components/html_viewer/discardable_memory_allocator.h"
-
-#include <stdint.h>
-#include <utility>
-
-#include "base/macros.h"
-#include "base/memory/discardable_memory.h"
-#include "base/stl_util.h"
-#include "base/trace_event/memory_allocator_dump.h"
-#include "base/trace_event/memory_dump_manager.h"
-#include "base/trace_event/process_memory_dump.h"
-
-namespace html_viewer {
-
-// Interface to the rest of the program. These objects are owned outside of the
-// allocator.
-class DiscardableMemoryAllocator::DiscardableMemoryChunkImpl
- : public base::DiscardableMemory {
- public:
- DiscardableMemoryChunkImpl(size_t size, DiscardableMemoryAllocator* allocator)
- : is_locked_(true),
- size_(size),
- data_(new uint8_t[size]),
- allocator_(allocator) {}
-
- ~DiscardableMemoryChunkImpl() override {
- base::AutoLock lock(allocator_->lock_);
- // Either the memory is discarded or the memory chunk is unlocked.
- DCHECK(data_ || !is_locked_);
- if (!is_locked_ && data_)
- allocator_->NotifyDestructed(unlocked_position_);
- }
-
- // Overridden from DiscardableMemoryChunk:
- bool Lock() override {
- base::AutoLock lock(allocator_->lock_);
- DCHECK(!is_locked_);
- if (!data_)
- return false;
-
- is_locked_ = true;
- allocator_->NotifyLocked(unlocked_position_);
- return true;
- }
-
- void Unlock() override {
- base::AutoLock lock(allocator_->lock_);
- DCHECK(is_locked_);
- DCHECK(data_);
- is_locked_ = false;
- unlocked_position_ = allocator_->NotifyUnlocked(this);
- }
-
- void* data() const override {
- if (data_) {
- DCHECK(is_locked_);
- return data_.get();
- }
- return nullptr;
- }
-
- base::trace_event::MemoryAllocatorDump* CreateMemoryAllocatorDump(
- const char* name,
- base::trace_event::ProcessMemoryDump* pmd) const override {
- base::trace_event::MemoryAllocatorDump* dump =
- pmd->CreateAllocatorDump(name);
- dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize,
- base::trace_event::MemoryAllocatorDump::kUnitsBytes, size_);
-
- // Memory is allocated from system allocator (malloc).
- pmd->AddSuballocation(dump->guid(),
- base::trace_event::MemoryDumpManager::GetInstance()
- ->system_allocator_pool_name());
- return dump;
- }
-
- size_t size() const { return size_; }
-
- void Discard() {
- allocator_->lock_.AssertAcquired();
- DCHECK(!is_locked_);
- data_.reset();
- }
-
- private:
- bool is_locked_;
- size_t size_;
- scoped_ptr<uint8_t[]> data_;
- DiscardableMemoryAllocator* allocator_;
-
- std::list<DiscardableMemoryChunkImpl*>::iterator unlocked_position_;
-
- DISALLOW_IMPLICIT_CONSTRUCTORS(DiscardableMemoryChunkImpl);
-};
-
-DiscardableMemoryAllocator::DiscardableMemoryAllocator(
- size_t desired_max_memory)
- : desired_max_memory_(desired_max_memory),
- total_live_memory_(0u),
- locked_chunks_(0) {
-}
-
-DiscardableMemoryAllocator::~DiscardableMemoryAllocator() {
- DCHECK_EQ(0, locked_chunks_);
- STLDeleteElements(&live_unlocked_chunks_);
-}
-
-scoped_ptr<base::DiscardableMemory>
-DiscardableMemoryAllocator::AllocateLockedDiscardableMemory(size_t size) {
- base::AutoLock lock(lock_);
- scoped_ptr<DiscardableMemoryChunkImpl> chunk(
- new DiscardableMemoryChunkImpl(size, this));
- total_live_memory_ += size;
- locked_chunks_++;
-
- // Go through the list of unlocked live chunks starting from the least
- // recently used, freeing as many as we can until we get our size under the
- // desired maximum.
- auto it = live_unlocked_chunks_.begin();
- while (total_live_memory_ > desired_max_memory_ &&
- it != live_unlocked_chunks_.end()) {
- total_live_memory_ -= (*it)->size();
- (*it)->Discard();
- it = live_unlocked_chunks_.erase(it);
- }
-
- return std::move(chunk);
-}
-
-std::list<DiscardableMemoryAllocator::DiscardableMemoryChunkImpl*>::iterator
-DiscardableMemoryAllocator::NotifyUnlocked(DiscardableMemoryChunkImpl* chunk) {
- lock_.AssertAcquired();
- locked_chunks_--;
- return live_unlocked_chunks_.insert(live_unlocked_chunks_.end(), chunk);
-}
-
-void DiscardableMemoryAllocator::NotifyLocked(
- std::list<DiscardableMemoryChunkImpl*>::iterator it) {
- lock_.AssertAcquired();
- locked_chunks_++;
- live_unlocked_chunks_.erase(it);
-}
-
-void DiscardableMemoryAllocator::NotifyDestructed(
- std::list<DiscardableMemoryChunkImpl*>::iterator it) {
- lock_.AssertAcquired();
- live_unlocked_chunks_.erase(it);
-}
-
-} // namespace html_viewer
« no previous file with comments | « components/html_viewer/discardable_memory_allocator.h ('k') | components/html_viewer/discardable_memory_allocator_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698