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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « base/memory/discardable_memory.h ('k') | base/memory/discardable_memory_allocator_android.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2013 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.
4
5 #include "base/memory/discardable_memory.h"
6
7 #include "base/lazy_instance.h"
8 #include "base/logging.h"
9
10 namespace base {
11 namespace {
12
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" }
20 };
21
22 DiscardableMemoryType g_preferred_type = DISCARDABLE_MEMORY_TYPE_NONE;
23
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];
30 }
31 DiscardableMemoryType value;
32 };
33 LazyInstance<DefaultPreferredType>::Leaky g_default_preferred_type =
34 LAZY_INSTANCE_INITIALIZER;
35
36 } // namespace
37
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;
44 }
45
46 return DISCARDABLE_MEMORY_TYPE_NONE;
47 }
48
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;
54 }
55
56 return "unknown";
57 }
58
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);
63
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);
67
68 g_preferred_type = type;
69 }
70
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;
75
76 return g_preferred_type;
77 }
78
79 // static
80 scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemory(
81 size_t size) {
82 return CreateLockedMemoryWithType(GetPreferredType(), size);
83 }
84
85 } // namespace base
OLDNEW
« 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