gn vim: apply to *.gni also
[chromium-blink-merge.git] / base / memory / discardable_memory.cc
blob3d82f91d78ffe964a8332f7f05b8d957ef59d717
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_ASHMEM, "ashmem" },
18 { DISCARDABLE_MEMORY_TYPE_MACH, "mach" },
19 { DISCARDABLE_MEMORY_TYPE_EMULATED, "emulated" },
20 { DISCARDABLE_MEMORY_TYPE_SHMEM, "shmem" }
23 DiscardableMemoryType g_preferred_type = DISCARDABLE_MEMORY_TYPE_NONE;
25 struct DefaultPreferredType {
26 DefaultPreferredType() : value(DISCARDABLE_MEMORY_TYPE_NONE) {
27 std::vector<DiscardableMemoryType> supported_types;
28 DiscardableMemory::GetSupportedTypes(&supported_types);
29 DCHECK(!supported_types.empty());
30 value = supported_types[0];
32 DiscardableMemoryType value;
34 LazyInstance<DefaultPreferredType>::Leaky g_default_preferred_type =
35 LAZY_INSTANCE_INITIALIZER;
37 } // namespace
39 // static
40 DiscardableMemoryType DiscardableMemory::GetNamedType(
41 const std::string& name) {
42 for (size_t i = 0; i < arraysize(kTypeNamePairs); ++i) {
43 if (name == kTypeNamePairs[i].name)
44 return kTypeNamePairs[i].type;
47 return DISCARDABLE_MEMORY_TYPE_NONE;
50 // static
51 const char* DiscardableMemory::GetTypeName(DiscardableMemoryType type) {
52 for (size_t i = 0; i < arraysize(kTypeNamePairs); ++i) {
53 if (type == kTypeNamePairs[i].type)
54 return kTypeNamePairs[i].name;
57 return "unknown";
60 // static
61 void DiscardableMemory::SetPreferredType(DiscardableMemoryType type) {
62 // NONE is a reserved value and not a valid default type.
63 DCHECK_NE(DISCARDABLE_MEMORY_TYPE_NONE, type);
65 // Make sure this function is only called once before the first call
66 // to GetPreferredType().
67 DCHECK_EQ(DISCARDABLE_MEMORY_TYPE_NONE, g_preferred_type);
69 g_preferred_type = type;
72 // static
73 DiscardableMemoryType DiscardableMemory::GetPreferredType() {
74 if (g_preferred_type == DISCARDABLE_MEMORY_TYPE_NONE)
75 g_preferred_type = g_default_preferred_type.Get().value;
77 return g_preferred_type;
80 // static
81 scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemory(
82 size_t size) {
83 return CreateLockedMemoryWithType(GetPreferredType(), size);
86 } // namespace base