Chromium Code Reviews| Index: base/memory/discardable_memory_android.cc |
| diff --git a/base/memory/discardable_memory_android.cc b/base/memory/discardable_memory_android.cc |
| index fa89c189db7be5170ecf30ea3a2a882c24fc87db..2412737fed4232722e7215ba940c9a5d008db22d 100644 |
| --- a/base/memory/discardable_memory_android.cc |
| +++ b/base/memory/discardable_memory_android.cc |
| @@ -9,33 +9,46 @@ |
| #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_allocation_ashmem_factory.h" |
| +#include "base/memory/discardable_memory_ashmem.h" |
| #include "base/memory/discardable_memory_emulated.h" |
| #include "base/memory/discardable_memory_malloc.h" |
| +#include "base/memory/discardable_memory_manager.h" |
| namespace base { |
| namespace { |
| const char kAshmemAllocatorName[] = "DiscardableMemoryAllocator"; |
| -struct DiscardableMemoryAllocatorWrapper { |
| - DiscardableMemoryAllocatorWrapper() |
| - : allocator(kAshmemAllocatorName, |
| - GetOptimalAshmemRegionSizeForAllocator()) { |
| +// High limit used only to avoid running out of address space. The kernel takes |
| +// care of ensuring that we don't run out of physical memory. |
| +const size_t kDefaultDiscardableMemoryLimit = 512 * 1024 * 1024; |
| + |
| +class DiscardableMemoryManagerImpl { |
|
reveman
2014/03/20 19:33:44
This is not a DiscardableMemoryManagerImpl. This s
Philippe
2014/03/21 10:17:44
Agreed for consistency. Let's discuss this in your
|
| + public: |
| + DiscardableMemoryManagerImpl() |
| + : factory_( |
| + kAshmemAllocatorName, GetOptimalAshmemRegionSizeForAllocator()), |
| + manager_(&factory_, kDefaultDiscardableMemoryLimit) { |
| } |
| - internal::DiscardableMemoryAllocator allocator; |
| + internal::DiscardableMemoryManager* instance() { |
| + return &manager_; |
| + } |
| private: |
| // Returns 64 MBytes for a 512 MBytes device, 128 MBytes for 1024 MBytes... |
| static size_t GetOptimalAshmemRegionSizeForAllocator() { |
| // Note that this may do some I/O (without hitting the disk though) so it |
| // should not be called on the critical path. |
| - return base::android::SysUtils::AmountOfPhysicalMemoryKB() * 1024 / 8; |
| + return android::SysUtils::AmountOfPhysicalMemoryKB() * 1024 / 8; |
| } |
| + |
| + internal::DiscardableMemoryAllocationAshmemFactory factory_; |
| + internal::DiscardableMemoryManager manager_; |
| }; |
| -LazyInstance<DiscardableMemoryAllocatorWrapper>::Leaky g_context = |
| +LazyInstance<DiscardableMemoryManagerImpl>::Leaky g_manager = |
| LAZY_INSTANCE_INITIALIZER; |
| } // namespace |
| @@ -69,7 +82,13 @@ scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemoryWithType( |
| case DISCARDABLE_MEMORY_TYPE_MAC: |
| return scoped_ptr<DiscardableMemory>(); |
| case DISCARDABLE_MEMORY_TYPE_ANDROID: { |
| - return g_context.Pointer()->allocator.Allocate(size); |
| + scoped_ptr<internal::DiscardableMemoryAshmem> memory( |
| + new internal::DiscardableMemoryAshmem( |
| + g_manager.Get().instance(), size)); |
| + if (!memory->Initialize()) |
| + return scoped_ptr<DiscardableMemory>(); |
| + |
| + return memory.PassAs<DiscardableMemory>(); |
| } |
| case DISCARDABLE_MEMORY_TYPE_EMULATED: { |
| scoped_ptr<internal::DiscardableMemoryEmulated> memory( |