Add git cl format presubmit warning for extension and apps.
[chromium-blink-merge.git] / base / memory / discardable_memory.cc
blob5c03a16471d9d434b679d21128d9bab54050196a
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/memory/discardable_memory.h"
7 #include "base/lazy_instance.h"
8 #include "base/logging.h"
10 namespace base {
11 namespace {
13 const struct TypeNamePair {
14 DiscardableMemoryType type;
15 const char* name;
16 } kTypeNamePairs[] = {
17 { DISCARDABLE_MEMORY_TYPE_ANDROID, "android" },
18 { DISCARDABLE_MEMORY_TYPE_MAC, "mac" },
19 { DISCARDABLE_MEMORY_TYPE_EMULATED, "emulated" }
22 DiscardableMemoryType g_preferred_type = DISCARDABLE_MEMORY_TYPE_NONE;
24 struct DefaultPreferredType {
25 DefaultPreferredType() : value(DISCARDABLE_MEMORY_TYPE_NONE) {
26 std::vector<DiscardableMemoryType> supported_types;
27 DiscardableMemory::GetSupportedTypes(&supported_types);
28 DCHECK(!supported_types.empty());
29 value = supported_types[0];
31 DiscardableMemoryType value;
33 LazyInstance<DefaultPreferredType>::Leaky g_default_preferred_type =
34 LAZY_INSTANCE_INITIALIZER;
36 } // namespace
38 // static
39 DiscardableMemoryType DiscardableMemory::GetNamedType(
40 const std::string& name) {
41 for (size_t i = 0; i < arraysize(kTypeNamePairs); ++i) {
42 if (name == kTypeNamePairs[i].name)
43 return kTypeNamePairs[i].type;
46 return DISCARDABLE_MEMORY_TYPE_NONE;
49 // static
50 const char* DiscardableMemory::GetTypeName(DiscardableMemoryType type) {
51 for (size_t i = 0; i < arraysize(kTypeNamePairs); ++i) {
52 if (type == kTypeNamePairs[i].type)
53 return kTypeNamePairs[i].name;
56 return "unknown";
59 // static
60 void DiscardableMemory::SetPreferredType(DiscardableMemoryType type) {
61 // NONE is a reserved value and not a valid default type.
62 DCHECK_NE(DISCARDABLE_MEMORY_TYPE_NONE, type);
64 // Make sure this function is only called once before the first call
65 // to GetPreferredType().
66 DCHECK_EQ(DISCARDABLE_MEMORY_TYPE_NONE, g_preferred_type);
68 g_preferred_type = type;
71 // static
72 DiscardableMemoryType DiscardableMemory::GetPreferredType() {
73 if (g_preferred_type == DISCARDABLE_MEMORY_TYPE_NONE)
74 g_preferred_type = g_default_preferred_type.Get().value;
76 return g_preferred_type;
79 // static
80 scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemory(
81 size_t size) {
82 return CreateLockedMemoryWithType(GetPreferredType(), size);
85 } // namespace base