This source file includes following definitions.
- GetNamedType
- GetTypeName
- SetPreferredType
- GetPreferredType
- CreateLockedMemory
#include "base/memory/discardable_memory.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
namespace base {
namespace {
const struct TypeNamePair {
DiscardableMemoryType type;
const char* name;
} kTypeNamePairs[] = {
{ DISCARDABLE_MEMORY_TYPE_ANDROID, "android" },
{ DISCARDABLE_MEMORY_TYPE_MAC, "mac" },
{ DISCARDABLE_MEMORY_TYPE_EMULATED, "emulated" },
{ DISCARDABLE_MEMORY_TYPE_MALLOC, "malloc" }
};
DiscardableMemoryType g_preferred_type = DISCARDABLE_MEMORY_TYPE_NONE;
struct DefaultPreferredType {
DefaultPreferredType() : 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<DefaultPreferredType>::Leaky g_default_preferred_type =
LAZY_INSTANCE_INITIALIZER;
}
DiscardableMemoryType DiscardableMemory::GetNamedType(
const std::string& name) {
for (size_t i = 0; i < arraysize(kTypeNamePairs); ++i) {
if (name == kTypeNamePairs[i].name)
return kTypeNamePairs[i].type;
}
return DISCARDABLE_MEMORY_TYPE_NONE;
}
const char* DiscardableMemory::GetTypeName(DiscardableMemoryType type) {
for (size_t i = 0; i < arraysize(kTypeNamePairs); ++i) {
if (type == kTypeNamePairs[i].type)
return kTypeNamePairs[i].name;
}
return "unknown";
}
void DiscardableMemory::SetPreferredType(DiscardableMemoryType type) {
DCHECK_NE(DISCARDABLE_MEMORY_TYPE_NONE, type);
DCHECK_EQ(DISCARDABLE_MEMORY_TYPE_NONE, g_preferred_type);
g_preferred_type = type;
}
DiscardableMemoryType DiscardableMemory::GetPreferredType() {
if (g_preferred_type == DISCARDABLE_MEMORY_TYPE_NONE)
g_preferred_type = g_default_preferred_type.Get().value;
return g_preferred_type;
}
scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemory(
size_t size) {
return CreateLockedMemoryWithType(GetPreferredType(), size);
}
}