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"
13 const struct TypeNamePair
{
14 DiscardableMemoryType type
;
16 } kTypeNamePairs
[] = {
17 { DISCARDABLE_MEMORY_TYPE_ASHMEM
, "ashmem" },
18 { DISCARDABLE_MEMORY_TYPE_MAC
, "mac" },
19 { DISCARDABLE_MEMORY_TYPE_EMULATED
, "emulated" },
20 { DISCARDABLE_MEMORY_TYPE_MALLOC
, "malloc" }
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
;
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
;
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
;
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
;
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
;
81 scoped_ptr
<DiscardableMemory
> DiscardableMemory::CreateLockedMemory(
83 return CreateLockedMemoryWithType(GetPreferredType(), size
);