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

Unified Diff: base/memory/discardable_memory.cc

Issue 114923005: base: Discardable memory types. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add switches::kUseDiscardableMemory to kForwardSwitches Created 6 years, 11 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
« no previous file with comments | « base/memory/discardable_memory.h ('k') | base/memory/discardable_memory_allocator_android.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/memory/discardable_memory.cc
diff --git a/base/memory/discardable_memory.cc b/base/memory/discardable_memory.cc
new file mode 100644
index 0000000000000000000000000000000000000000..5caae53a637a60aa0eee802b4e94ca669c2f7293
--- /dev/null
+++ b/base/memory/discardable_memory.cc
@@ -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/lazy_instance.h"
+#include "base/logging.h"
+
+namespace base {
+namespace {
+
+const struct TypeNamePair {
+ DiscardableMemoryType type;
+ const char* name;
+} kTypeNamePairs[] = {
+ { DISCARDABLE_MEMORY_TYPE_ANDROID, "android" },
+ { DISCARDABLE_MEMORY_TYPE_MAC, "mac" },
+ { DISCARDABLE_MEMORY_TYPE_EMULATED, "emulated" }
+};
+
+DiscardableMemoryType g_preferred_type = DISCARDABLE_MEMORY_TYPE_NONE;
+
+struct DefaultPreferredType {
+ DefaultPreferredType() : value(DISCARDABLE_MEMORY_TYPE_NONE) {
+ std::vector<DiscardableMemoryType> supported_types;
+ DiscardableMemory::GetSupportedTypes(&supported_types);
+ DCHECK(!supported_types.empty());
+ value = supported_types[0];
+ }
+ DiscardableMemoryType value;
+};
+LazyInstance<DefaultPreferredType>::Leaky g_default_preferred_type =
+ LAZY_INSTANCE_INITIALIZER;
+
+} // namespace
+
+// static
+DiscardableMemoryType DiscardableMemory::GetNamedType(
+ const std::string& name) {
+ for (size_t i = 0; i < arraysize(kTypeNamePairs); ++i) {
+ if (name == kTypeNamePairs[i].name)
+ return kTypeNamePairs[i].type;
+ }
+
+ return DISCARDABLE_MEMORY_TYPE_NONE;
+}
+
+// static
+const char* DiscardableMemory::GetTypeName(DiscardableMemoryType type) {
+ for (size_t i = 0; i < arraysize(kTypeNamePairs); ++i) {
+ if (type == kTypeNamePairs[i].type)
+ return kTypeNamePairs[i].name;
+ }
+
+ return "unknown";
+}
+
+// static
+void DiscardableMemory::SetPreferredType(DiscardableMemoryType type) {
+ // NONE is a reserved value and not a valid default type.
+ DCHECK_NE(DISCARDABLE_MEMORY_TYPE_NONE, type);
+
+ // Make sure this function is only called once before the first call
+ // to GetPreferredType().
+ DCHECK_EQ(DISCARDABLE_MEMORY_TYPE_NONE, g_preferred_type);
+
+ g_preferred_type = type;
+}
+
+// static
+DiscardableMemoryType DiscardableMemory::GetPreferredType() {
+ if (g_preferred_type == DISCARDABLE_MEMORY_TYPE_NONE)
+ g_preferred_type = g_default_preferred_type.Get().value;
+
+ return g_preferred_type;
+}
+
+// static
+scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemory(
+ size_t size) {
+ return CreateLockedMemoryWithType(GetPreferredType(), size);
+}
+
+} // namespace base
« no previous file with comments | « base/memory/discardable_memory.h ('k') | base/memory/discardable_memory_allocator_android.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698