This source file includes following definitions.
- RegisterMemoryPressureListeners
- UnregisterMemoryPressureListeners
- GetSupportedTypes
- CreateLockedMemoryWithType
- PurgeForTestingSupported
- PurgeForTesting
#include "base/memory/discardable_memory.h"
#include "base/android/sys_utils.h"
#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/lazy_instance.h"
#include "base/logging.h"
#include "base/memory/discardable_memory_allocator_android.h"
#include "base/memory/discardable_memory_emulated.h"
#include "base/memory/discardable_memory_malloc.h"
namespace base {
namespace {
const char kAshmemAllocatorName[] = "DiscardableMemoryAllocator";
struct DiscardableMemoryAllocatorWrapper {
DiscardableMemoryAllocatorWrapper()
: allocator(kAshmemAllocatorName,
GetOptimalAshmemRegionSizeForAllocator()) {
}
internal::DiscardableMemoryAllocator allocator;
private:
static size_t GetOptimalAshmemRegionSizeForAllocator() {
return base::android::SysUtils::AmountOfPhysicalMemoryKB() * 1024 / 8;
}
};
LazyInstance<DiscardableMemoryAllocatorWrapper>::Leaky g_context =
LAZY_INSTANCE_INITIALIZER;
}
void DiscardableMemory::RegisterMemoryPressureListeners() {
internal::DiscardableMemoryEmulated::RegisterMemoryPressureListeners();
}
void DiscardableMemory::UnregisterMemoryPressureListeners() {
internal::DiscardableMemoryEmulated::UnregisterMemoryPressureListeners();
}
void DiscardableMemory::GetSupportedTypes(
std::vector<DiscardableMemoryType>* types) {
const DiscardableMemoryType supported_types[] = {
DISCARDABLE_MEMORY_TYPE_ANDROID,
DISCARDABLE_MEMORY_TYPE_EMULATED,
DISCARDABLE_MEMORY_TYPE_MALLOC
};
types->assign(supported_types, supported_types + arraysize(supported_types));
}
scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemoryWithType(
DiscardableMemoryType type, size_t size) {
switch (type) {
case DISCARDABLE_MEMORY_TYPE_NONE:
case DISCARDABLE_MEMORY_TYPE_MAC:
return scoped_ptr<DiscardableMemory>();
case DISCARDABLE_MEMORY_TYPE_ANDROID: {
return g_context.Pointer()->allocator.Allocate(size);
}
case DISCARDABLE_MEMORY_TYPE_EMULATED: {
scoped_ptr<internal::DiscardableMemoryEmulated> memory(
new internal::DiscardableMemoryEmulated(size));
if (!memory->Initialize())
return scoped_ptr<DiscardableMemory>();
return memory.PassAs<DiscardableMemory>();
}
case DISCARDABLE_MEMORY_TYPE_MALLOC: {
scoped_ptr<internal::DiscardableMemoryMalloc> memory(
new internal::DiscardableMemoryMalloc(size));
if (!memory->Initialize())
return scoped_ptr<DiscardableMemory>();
return memory.PassAs<DiscardableMemory>();
}
}
NOTREACHED();
return scoped_ptr<DiscardableMemory>();
}
bool DiscardableMemory::PurgeForTestingSupported() {
return false;
}
void DiscardableMemory::PurgeForTesting() {
NOTIMPLEMENTED();
}
}