summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorreveman <reveman@chromium.org>2014-10-15 13:28:54 -0700
committerCommit bot <commit-bot@chromium.org>2014-10-15 20:29:46 +0000
commit31f0530105bfc993e7d949a4a33bc70bb023acb1 (patch)
tree301def776809f4268ab994ff4ea04316c6bce748
parente83b4651dd1c9fc4e0f6f21d74627a52cf4a8782 (diff)
downloadchromium_src-31f0530105bfc993e7d949a4a33bc70bb023acb1.zip
chromium_src-31f0530105bfc993e7d949a4a33bc70bb023acb1.tar.gz
chromium_src-31f0530105bfc993e7d949a4a33bc70bb023acb1.tar.bz2
base: Use LazyInstanceTraits instead of SharedState class for discardable memory.
Switch to using LazyInstanceTraits instead of classes named SharedState for the purpose of initializing a DiscardableMemoryManager instance with a certain set of parameters. BUG=422953 Review URL: https://codereview.chromium.org/650233002 Cr-Commit-Position: refs/heads/master@{#299756}
-rw-r--r--base/memory/discardable_memory_emulated.cc47
-rw-r--r--base/memory/discardable_memory_mach.cc34
2 files changed, 56 insertions, 25 deletions
diff --git a/base/memory/discardable_memory_emulated.cc b/base/memory/discardable_memory_emulated.cc
index f9097b1..9820e2e 100644
--- a/base/memory/discardable_memory_emulated.cc
+++ b/base/memory/discardable_memory_emulated.cc
@@ -15,16 +15,31 @@ const size_t kEmulatedMemoryLimit = 512 * 1024 * 1024;
const size_t kEmulatedSoftMemoryLimit = 32 * 1024 * 1024;
const size_t kEmulatedHardMemoryLimitExpirationTimeMs = 1000;
-struct SharedState {
- SharedState()
- : manager(kEmulatedMemoryLimit,
- kEmulatedSoftMemoryLimit,
- TimeDelta::FromMilliseconds(
- kEmulatedHardMemoryLimitExpirationTimeMs)) {}
-
- internal::DiscardableMemoryManager manager;
+// internal::DiscardableMemoryManager has an explicit constructor that takes
+// a number of memory limit parameters. The LeakyLazyInstanceTraits doesn't
+// handle the case. Thus, we need our own class here.
+struct DiscardableMemoryManagerLazyInstanceTraits {
+ // Leaky as discardable memory clients can use this after the exit handler
+ // has been called.
+ static const bool kRegisterOnExit = false;
+#ifndef NDEBUG
+ static const bool kAllowedToAccessOnNonjoinableThread = true;
+#endif
+
+ static internal::DiscardableMemoryManager* New(void* instance) {
+ return new (instance) internal::DiscardableMemoryManager(
+ kEmulatedMemoryLimit,
+ kEmulatedSoftMemoryLimit,
+ TimeDelta::FromMilliseconds(kEmulatedHardMemoryLimitExpirationTimeMs));
+ }
+ static void Delete(internal::DiscardableMemoryManager* instance) {
+ instance->~DiscardableMemoryManager();
+ }
};
-LazyInstance<SharedState>::Leaky g_shared_state = LAZY_INSTANCE_INITIALIZER;
+
+LazyInstance<internal::DiscardableMemoryManager,
+ DiscardableMemoryManagerLazyInstanceTraits>
+ g_manager = LAZY_INSTANCE_INITIALIZER;
} // namespace
@@ -33,29 +48,29 @@ namespace internal {
DiscardableMemoryEmulated::DiscardableMemoryEmulated(size_t bytes)
: bytes_(bytes),
is_locked_(false) {
- g_shared_state.Pointer()->manager.Register(this, bytes);
+ g_manager.Pointer()->Register(this, bytes);
}
DiscardableMemoryEmulated::~DiscardableMemoryEmulated() {
if (is_locked_)
Unlock();
- g_shared_state.Pointer()->manager.Unregister(this);
+ g_manager.Pointer()->Unregister(this);
}
// static
bool DiscardableMemoryEmulated::ReduceMemoryUsage() {
- return g_shared_state.Pointer()->manager.ReduceMemoryUsage();
+ return g_manager.Pointer()->ReduceMemoryUsage();
}
// static
void DiscardableMemoryEmulated::ReduceMemoryUsageUntilWithinLimit(
size_t bytes) {
- g_shared_state.Pointer()->manager.ReduceMemoryUsageUntilWithinLimit(bytes);
+ g_manager.Pointer()->ReduceMemoryUsageUntilWithinLimit(bytes);
}
// static
void DiscardableMemoryEmulated::PurgeForTesting() {
- g_shared_state.Pointer()->manager.PurgeAll();
+ g_manager.Pointer()->PurgeAll();
}
bool DiscardableMemoryEmulated::Initialize() {
@@ -66,7 +81,7 @@ DiscardableMemoryLockStatus DiscardableMemoryEmulated::Lock() {
DCHECK(!is_locked_);
bool purged = false;
- if (!g_shared_state.Pointer()->manager.AcquireLock(this, &purged))
+ if (!g_manager.Pointer()->AcquireLock(this, &purged))
return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED;
is_locked_ = true;
@@ -76,7 +91,7 @@ DiscardableMemoryLockStatus DiscardableMemoryEmulated::Lock() {
void DiscardableMemoryEmulated::Unlock() {
DCHECK(is_locked_);
- g_shared_state.Pointer()->manager.ReleaseLock(this);
+ g_manager.Pointer()->ReleaseLock(this);
is_locked_ = false;
}
diff --git a/base/memory/discardable_memory_mach.cc b/base/memory/discardable_memory_mach.cc
index 1051569..c6681b1 100644
--- a/base/memory/discardable_memory_mach.cc
+++ b/base/memory/discardable_memory_mach.cc
@@ -19,13 +19,29 @@ namespace {
// address space usage gets too high (e.g. 512 MBytes).
const size_t kMachMemoryLimit = 512 * 1024 * 1024;
-struct SharedState {
- SharedState()
- : manager(kMachMemoryLimit, kMachMemoryLimit, TimeDelta::Max()) {}
+// internal::DiscardableMemoryManager has an explicit constructor that takes
+// a number of memory limit parameters. The LeakyLazyInstanceTraits doesn't
+// handle the case. Thus, we need our own class here.
+struct DiscardableMemoryManagerLazyInstanceTraits {
+ // Leaky as discardable memory clients can use this after the exit handler
+ // has been called.
+ static const bool kRegisterOnExit = false;
+#ifndef NDEBUG
+ static const bool kAllowedToAccessOnNonjoinableThread = true;
+#endif
- internal::DiscardableMemoryManager manager;
+ static internal::DiscardableMemoryManager* New(void* instance) {
+ return new (instance) internal::DiscardableMemoryManager(
+ kMachMemoryLimit, kMachMemoryLimit, TimeDelta::Max());
+ }
+ static void Delete(internal::DiscardableMemoryManager* instance) {
+ instance->~DiscardableMemoryManager();
+ }
};
-LazyInstance<SharedState>::Leaky g_shared_state = LAZY_INSTANCE_INITIALIZER;
+
+LazyInstance<internal::DiscardableMemoryManager,
+ DiscardableMemoryManagerLazyInstanceTraits>
+ g_manager = LAZY_INSTANCE_INITIALIZER;
// The VM subsystem allows tagging of memory and 240-255 is reserved for
// application use (see mach/vm_statistics.h). Pick 252 (after chromium's atomic
@@ -38,13 +54,13 @@ namespace internal {
DiscardableMemoryMach::DiscardableMemoryMach(size_t bytes)
: memory_(0, 0), bytes_(mach_vm_round_page(bytes)), is_locked_(false) {
- g_shared_state.Pointer()->manager.Register(this, bytes);
+ g_manager.Pointer()->Register(this, bytes);
}
DiscardableMemoryMach::~DiscardableMemoryMach() {
if (is_locked_)
Unlock();
- g_shared_state.Pointer()->manager.Unregister(this);
+ g_manager.Pointer()->Unregister(this);
}
// static
@@ -61,7 +77,7 @@ DiscardableMemoryLockStatus DiscardableMemoryMach::Lock() {
DCHECK(!is_locked_);
bool purged = false;
- if (!g_shared_state.Pointer()->manager.AcquireLock(this, &purged))
+ if (!g_manager.Pointer()->AcquireLock(this, &purged))
return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED;
is_locked_ = true;
@@ -71,7 +87,7 @@ DiscardableMemoryLockStatus DiscardableMemoryMach::Lock() {
void DiscardableMemoryMach::Unlock() {
DCHECK(is_locked_);
- g_shared_state.Pointer()->manager.ReleaseLock(this);
+ g_manager.Pointer()->ReleaseLock(this);
is_locked_ = false;
}