forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add discardable memory emulation for non-android/mac platforms
Adds support for emulated discardable memory. The memory is managed by a provider which listens for memory pressure notifications from the platform. Currently, only android pushes these notifications, but in future patches, we will apply pressure on other platforms in certain situations (e.g., when a tab gets backgrounded). BUG=237681 Review URL: https://codereview.chromium.org/17106004 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@231845 0039d316-1c4b-4281-b951-d872f2087c98
- Loading branch information
vollick@chromium.org
committed
Oct 30, 2013
1 parent
1bc20da
commit cef6c76
Showing
15 changed files
with
880 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright 2013 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 "base/memory/discardable_memory.h" | ||
|
||
#include "base/memory/discardable_memory_provider.h" | ||
|
||
using base::internal::DiscardableMemoryProvider; | ||
|
||
namespace base { | ||
namespace { | ||
|
||
class DiscardableMemoryEmulated : public DiscardableMemory { | ||
public: | ||
explicit DiscardableMemoryEmulated(size_t size) : is_locked_(false) { | ||
DiscardableMemoryProvider::GetInstance()->Register(this, size); | ||
} | ||
|
||
virtual ~DiscardableMemoryEmulated() { | ||
if (is_locked_) | ||
Unlock(); | ||
DiscardableMemoryProvider::GetInstance()->Unregister(this); | ||
} | ||
|
||
// DiscardableMemory: | ||
virtual LockDiscardableMemoryStatus Lock() OVERRIDE { | ||
DCHECK(!is_locked_); | ||
|
||
bool purged = false; | ||
memory_ = DiscardableMemoryProvider::GetInstance()->Acquire(this, &purged); | ||
if (!memory_) | ||
return DISCARDABLE_MEMORY_FAILED; | ||
|
||
is_locked_ = true; | ||
return purged ? DISCARDABLE_MEMORY_PURGED : DISCARDABLE_MEMORY_SUCCESS; | ||
} | ||
|
||
virtual void Unlock() OVERRIDE { | ||
DCHECK(is_locked_); | ||
DiscardableMemoryProvider::GetInstance()->Release(this, memory_.Pass()); | ||
is_locked_ = false; | ||
} | ||
|
||
virtual void* Memory() const OVERRIDE { | ||
DCHECK(memory_); | ||
return memory_.get(); | ||
} | ||
|
||
private: | ||
scoped_ptr<uint8, FreeDeleter> memory_; | ||
bool is_locked_; | ||
|
||
DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryEmulated); | ||
}; | ||
|
||
} // namespace | ||
|
||
// static | ||
bool DiscardableMemory::SupportedNatively() { | ||
return false; | ||
} | ||
|
||
// static | ||
scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemory( | ||
size_t size) { | ||
scoped_ptr<DiscardableMemory> memory(new DiscardableMemoryEmulated(size)); | ||
if (!memory) | ||
return scoped_ptr<DiscardableMemory>(); | ||
if (memory->Lock() != DISCARDABLE_MEMORY_PURGED) | ||
return scoped_ptr<DiscardableMemory>(); | ||
return memory.Pass(); | ||
} | ||
|
||
// static | ||
bool DiscardableMemory::PurgeForTestingSupported() { | ||
return true; | ||
} | ||
|
||
// static | ||
void DiscardableMemory::PurgeForTesting() { | ||
DiscardableMemoryProvider::GetInstance()->PurgeAll(); | ||
} | ||
|
||
} // namespace base |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.