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

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 missing include Created 7 years 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: 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..dcecc0f665ebbd256f49fb64d1412ea756dde01f
--- /dev/null
+++ b/base/memory/discardable_memory.cc
@@ -0,0 +1,66 @@
+// 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"
+
+namespace base {
+namespace {
+
+const struct {
+ const char* name;
+ DiscardableMemoryType type;
+} kTypeNamePairs[] = {
+ { "android", DISCARDABLE_MEMORY_TYPE_ANDROID },
+ { "mac", DISCARDABLE_MEMORY_TYPE_MAC },
+ { "emulated", DISCARDABLE_MEMORY_TYPE_EMULATED }
+};
+
+struct TypeInitializedWithPreferredValue {
+ TypeInitializedWithPreferredValue() : 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<TypeInitializedWithPreferredValue>::Leaky
+ g_discardable_memory_type = LAZY_INSTANCE_INITIALIZER;
+
+} // namespace
+
+// static
+DiscardableMemoryType DiscardableMemory::GetNamedType(
+ const std::string& name) {
+ for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTypeNamePairs); ++i) {
willchan no longer on Chromium 2013/12/24 01:21:06 I don't think you need to use the unsafe version o
reveman 2013/12/26 11:46:08 Done. After after giving the array type a name.
+ 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_UNSAFE(kTypeNamePairs); ++i) {
willchan no longer on Chromium 2013/12/24 01:21:06 ditto...i think you can switch to arraysize()
reveman 2013/12/26 11:46:08 Done.
+ if (type == kTypeNamePairs[i].type)
+ return kTypeNamePairs[i].name;
+ }
+
+ return "unknown";
+}
+
+// static
+void DiscardableMemory::SetType(DiscardableMemoryType type) {
+ g_discardable_memory_type.Get().value = type;
willchan no longer on Chromium 2013/12/24 01:21:06 This looks racy to me. If this is only allowed to
reveman 2013/12/26 11:46:08 Unit tests depend on being able to change this at
willchan no longer on Chromium 2013/12/26 18:25:54 Are there any valid reasons for non-test clients t
reveman 2013/12/26 21:53:35 Not at this point.
+}
+
+// static
+DiscardableMemoryType DiscardableMemory::GetType() {
+ return g_discardable_memory_type.Get().value;
+}
+
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698