| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "base/memory/discardable_memory_android.h" | 5 #include "base/memory/discardable_memory.h" |
| 6 | |
| 7 #include <sys/mman.h> | |
| 8 #include <unistd.h> | |
| 9 | |
| 10 #include <limits> | |
| 11 | 6 |
| 12 #include "base/android/sys_utils.h" | 7 #include "base/android/sys_utils.h" |
| 13 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 14 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 15 #include "base/file_util.h" | |
| 16 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
| 17 #include "base/logging.h" | 11 #include "base/logging.h" |
| 18 #include "base/memory/discardable_memory_allocator_android.h" | 12 #include "base/memory/discardable_memory_allocator_android.h" |
| 19 #include "base/memory/discardable_memory_emulated.h" | 13 #include "base/memory/discardable_memory_emulated.h" |
| 20 #include "base/memory/discardable_memory_malloc.h" | 14 #include "base/memory/discardable_memory_malloc.h" |
| 21 #include "third_party/ashmem/ashmem.h" | |
| 22 | 15 |
| 23 namespace base { | 16 namespace base { |
| 24 namespace { | 17 namespace { |
| 25 | 18 |
| 26 const char kAshmemAllocatorName[] = "DiscardableMemoryAllocator"; | 19 const char kAshmemAllocatorName[] = "DiscardableMemoryAllocator"; |
| 27 | 20 |
| 28 struct DiscardableMemoryAllocatorWrapper { | 21 struct DiscardableMemoryAllocatorWrapper { |
| 29 DiscardableMemoryAllocatorWrapper() | 22 DiscardableMemoryAllocatorWrapper() |
| 30 : allocator(kAshmemAllocatorName, | 23 : allocator(kAshmemAllocatorName, |
| 31 GetOptimalAshmemRegionSizeForAllocator()) { | 24 GetOptimalAshmemRegionSizeForAllocator()) { |
| 32 } | 25 } |
| 33 | 26 |
| 34 internal::DiscardableMemoryAllocator allocator; | 27 internal::DiscardableMemoryAllocator allocator; |
| 35 | 28 |
| 36 private: | 29 private: |
| 37 // Returns 64 MBytes for a 512 MBytes device, 128 MBytes for 1024 MBytes... | 30 // Returns 64 MBytes for a 512 MBytes device, 128 MBytes for 1024 MBytes... |
| 38 static size_t GetOptimalAshmemRegionSizeForAllocator() { | 31 static size_t GetOptimalAshmemRegionSizeForAllocator() { |
| 39 // Note that this may do some I/O (without hitting the disk though) so it | 32 // Note that this may do some I/O (without hitting the disk though) so it |
| 40 // should not be called on the critical path. | 33 // should not be called on the critical path. |
| 41 return base::android::SysUtils::AmountOfPhysicalMemoryKB() * 1024 / 8; | 34 return base::android::SysUtils::AmountOfPhysicalMemoryKB() * 1024 / 8; |
| 42 } | 35 } |
| 43 }; | 36 }; |
| 44 | 37 |
| 45 LazyInstance<DiscardableMemoryAllocatorWrapper>::Leaky g_context = | 38 LazyInstance<DiscardableMemoryAllocatorWrapper>::Leaky g_context = |
| 46 LAZY_INSTANCE_INITIALIZER; | 39 LAZY_INSTANCE_INITIALIZER; |
| 47 | 40 |
| 48 } // namespace | 41 } // namespace |
| 49 | 42 |
| 50 namespace internal { | |
| 51 | |
| 52 bool CreateAshmemRegion(const char* name, | |
| 53 size_t size, | |
| 54 int* out_fd, | |
| 55 void** out_address) { | |
| 56 int fd = ashmem_create_region(name, size); | |
| 57 if (fd < 0) { | |
| 58 DLOG(ERROR) << "ashmem_create_region() failed"; | |
| 59 return false; | |
| 60 } | |
| 61 file_util::ScopedFD fd_closer(&fd); | |
| 62 | |
| 63 const int err = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE); | |
| 64 if (err < 0) { | |
| 65 DLOG(ERROR) << "Error " << err << " when setting protection of ashmem"; | |
| 66 return false; | |
| 67 } | |
| 68 | |
| 69 // There is a problem using MAP_PRIVATE here. As we are constantly calling | |
| 70 // Lock() and Unlock(), data could get lost if they are not written to the | |
| 71 // underlying file when Unlock() gets called. | |
| 72 void* const address = mmap( | |
| 73 NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); | |
| 74 if (address == MAP_FAILED) { | |
| 75 DPLOG(ERROR) << "Failed to map memory."; | |
| 76 return false; | |
| 77 } | |
| 78 | |
| 79 ignore_result(fd_closer.release()); | |
| 80 *out_fd = fd; | |
| 81 *out_address = address; | |
| 82 return true; | |
| 83 } | |
| 84 | |
| 85 bool CloseAshmemRegion(int fd, size_t size, void* address) { | |
| 86 if (munmap(address, size) == -1) { | |
| 87 DPLOG(ERROR) << "Failed to unmap memory."; | |
| 88 close(fd); | |
| 89 return false; | |
| 90 } | |
| 91 return close(fd) == 0; | |
| 92 } | |
| 93 | |
| 94 DiscardableMemoryLockStatus LockAshmemRegion(int fd, | |
| 95 size_t off, | |
| 96 size_t size, | |
| 97 const void* address) { | |
| 98 const int result = ashmem_pin_region(fd, off, size); | |
| 99 DCHECK_EQ(0, mprotect(address, size, PROT_READ | PROT_WRITE)); | |
| 100 return result == ASHMEM_WAS_PURGED ? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED | |
| 101 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS; | |
| 102 } | |
| 103 | |
| 104 bool UnlockAshmemRegion(int fd, size_t off, size_t size, const void* address) { | |
| 105 const int failed = ashmem_unpin_region(fd, off, size); | |
| 106 if (failed) | |
| 107 DLOG(ERROR) << "Failed to unpin memory."; | |
| 108 // This allows us to catch accesses to unlocked memory. | |
| 109 DCHECK_EQ(0, mprotect(address, size, PROT_NONE)); | |
| 110 return !failed; | |
| 111 } | |
| 112 | |
| 113 } // namespace internal | |
| 114 | |
| 115 // static | 43 // static |
| 116 void DiscardableMemory::RegisterMemoryPressureListeners() { | 44 void DiscardableMemory::RegisterMemoryPressureListeners() { |
| 117 internal::DiscardableMemoryEmulated::RegisterMemoryPressureListeners(); | 45 internal::DiscardableMemoryEmulated::RegisterMemoryPressureListeners(); |
| 118 } | 46 } |
| 119 | 47 |
| 120 // static | 48 // static |
| 121 void DiscardableMemory::UnregisterMemoryPressureListeners() { | 49 void DiscardableMemory::UnregisterMemoryPressureListeners() { |
| 122 internal::DiscardableMemoryEmulated::UnregisterMemoryPressureListeners(); | 50 internal::DiscardableMemoryEmulated::UnregisterMemoryPressureListeners(); |
| 123 } | 51 } |
| 124 | 52 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 bool DiscardableMemory::PurgeForTestingSupported() { | 97 bool DiscardableMemory::PurgeForTestingSupported() { |
| 170 return false; | 98 return false; |
| 171 } | 99 } |
| 172 | 100 |
| 173 // static | 101 // static |
| 174 void DiscardableMemory::PurgeForTesting() { | 102 void DiscardableMemory::PurgeForTesting() { |
| 175 NOTIMPLEMENTED(); | 103 NOTIMPLEMENTED(); |
| 176 } | 104 } |
| 177 | 105 |
| 178 } // namespace base | 106 } // namespace base |
| OLD | NEW |