Index: base/memory/discardable_memory_android.cc |
diff --git a/base/memory/discardable_memory_android.cc b/base/memory/discardable_memory_android.cc |
index 519aa8d8d24a2d12198dd577f917e639c56f101c..a141872fde6c050609c505a219fda8ce91565caa 100644 |
--- a/base/memory/discardable_memory_android.cc |
+++ b/base/memory/discardable_memory_android.cc |
@@ -17,8 +17,8 @@ |
#include "base/file_util.h" |
#include "base/lazy_instance.h" |
#include "base/logging.h" |
-#include "base/memory/discardable_memory.h" |
#include "base/memory/discardable_memory_allocator_android.h" |
+#include "base/memory/discardable_memory_emulated.h" |
#include "base/synchronization/lock.h" |
#include "third_party/ashmem/ashmem.h" |
@@ -97,7 +97,7 @@ class DiscardableMemoryAndroidSimple : public DiscardableMemory { |
} |
// DiscardableMemory: |
- virtual LockDiscardableMemoryStatus Lock() OVERRIDE { |
+ virtual DiscardableMemoryLockStatus Lock() OVERRIDE { |
return internal::LockAshmemRegion(fd_, 0, size_, memory_); |
} |
@@ -187,14 +187,14 @@ bool CloseAshmemRegion(int fd, size_t size, void* address) { |
return close(fd) == 0; |
} |
-LockDiscardableMemoryStatus LockAshmemRegion(int fd, |
+DiscardableMemoryLockStatus LockAshmemRegion(int fd, |
size_t off, |
size_t size, |
const void* address) { |
const int result = ashmem_pin_region(fd, off, size); |
DCHECK_EQ(0, mprotect(address, size, PROT_READ | PROT_WRITE)); |
- return result == ASHMEM_WAS_PURGED ? |
- DISCARDABLE_MEMORY_PURGED : DISCARDABLE_MEMORY_SUCCESS; |
+ return result == ASHMEM_WAS_PURGED ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED |
+ : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS; |
} |
bool UnlockAshmemRegion(int fd, size_t off, size_t size, const void* address) { |
@@ -209,8 +209,13 @@ bool UnlockAshmemRegion(int fd, size_t off, size_t size, const void* address) { |
} // namespace internal |
// static |
-bool DiscardableMemory::SupportedNatively() { |
- return true; |
+void DiscardableMemory::GetSupportedTypes( |
+ std::vector<DiscardableMemoryType>* types) { |
+ const DiscardableMemoryType supported_types[] = { |
+ DISCARDABLE_MEMORY_TYPE_ANDROID, |
+ DISCARDABLE_MEMORY_TYPE_EMULATED |
+ }; |
+ types->assign(supported_types, supported_types + arraysize(supported_types)); |
} |
// Allocation can happen in two ways: |
@@ -230,27 +235,46 @@ bool DiscardableMemory::SupportedNatively() { |
// |
// static |
scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemory( |
- size_t size) { |
- if (!CheckSizeCanBeAlignedToNextPage(size)) |
- return scoped_ptr<DiscardableMemory>(); |
- // Pinning & unpinning works with page granularity therefore align the size |
- // upfront. |
- const size_t aligned_size = internal::AlignToNextPage(size); |
- // Note that the following code is slightly racy. The worst that can happen in |
- // practice though is taking the wrong decision (e.g. using the allocator |
- // rather than DiscardableMemoryAndroidSimple). Moreover keeping the lock |
- // acquired for the whole allocation would cause a deadlock when the allocator |
- // tries to create an ashmem region. |
- GlobalContext* const global_context = g_context.Pointer(); |
- if (GetCurrentNumberOfAshmemFDs() < 0.9 * global_context->ashmem_fd_limit) { |
- int fd; |
- void* address; |
- if (internal::CreateAshmemRegion("", aligned_size, &fd, &address)) { |
- return scoped_ptr<DiscardableMemory>( |
- new DiscardableMemoryAndroidSimple(fd, address, aligned_size)); |
+ 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: { |
+ if (!CheckSizeCanBeAlignedToNextPage(size)) |
+ return scoped_ptr<DiscardableMemory>(); |
+ // Pinning & unpinning works with page granularity therefore align the |
+ // size upfront. |
+ const size_t aligned_size = internal::AlignToNextPage(size); |
+ // Note that the following code is slightly racy. The worst that can |
+ // happen in practice though is taking the wrong decision (e.g. using |
+ // the allocator rather than DiscardableMemoryAndroidSimple). Moreover |
+ // keeping the lock acquired for the whole allocation would cause a |
+ // deadlock when the allocator tries to create an ashmem region. |
+ GlobalContext* const global_context = g_context.Pointer(); |
+ if (GetCurrentNumberOfAshmemFDs() < |
+ 0.9 * global_context->ashmem_fd_limit) { |
+ int fd; |
+ void* address; |
+ if (internal::CreateAshmemRegion("", aligned_size, &fd, &address)) { |
+ return scoped_ptr<DiscardableMemory>( |
+ new DiscardableMemoryAndroidSimple(fd, address, aligned_size)); |
+ } |
+ } |
+ return global_context->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>(); |
} |
} |
- return global_context->allocator.Allocate(size); |
+ |
+ NOTREACHED(); |
+ return scoped_ptr<DiscardableMemory>(); |
} |
// static |