| 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_android.h" |
| 6 | 6 |
| 7 #include <sys/mman.h> | 7 #include <sys/mman.h> |
| 8 #include <unistd.h> | 8 #include <unistd.h> |
| 9 | 9 |
| 10 #include <limits> | 10 #include <limits> |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 GetOptimalAshmemRegionSizeForAllocator()) { | 30 GetOptimalAshmemRegionSizeForAllocator()) { |
| 31 } | 31 } |
| 32 | 32 |
| 33 internal::DiscardableMemoryAllocator allocator; | 33 internal::DiscardableMemoryAllocator allocator; |
| 34 | 34 |
| 35 private: | 35 private: |
| 36 // Returns 64 MBytes for a 512 MBytes device, 128 MBytes for 1024 MBytes... | 36 // Returns 64 MBytes for a 512 MBytes device, 128 MBytes for 1024 MBytes... |
| 37 static size_t GetOptimalAshmemRegionSizeForAllocator() { | 37 static size_t GetOptimalAshmemRegionSizeForAllocator() { |
| 38 // Note that this may do some I/O (without hitting the disk though) so it | 38 // Note that this may do some I/O (without hitting the disk though) so it |
| 39 // should not be called on the critical path. | 39 // should not be called on the critical path. |
| 40 return internal::AlignToNextPage( | 40 return base::android::SysUtils::AmountOfPhysicalMemoryKB() * 1024 / 8; |
| 41 base::android::SysUtils::AmountOfPhysicalMemoryKB() * 1024 / 8); | |
| 42 } | 41 } |
| 43 }; | 42 }; |
| 44 | 43 |
| 45 LazyInstance<DiscardableMemoryAllocatorWrapper>::Leaky g_context = | 44 LazyInstance<DiscardableMemoryAllocatorWrapper>::Leaky g_context = |
| 46 LAZY_INSTANCE_INITIALIZER; | 45 LAZY_INSTANCE_INITIALIZER; |
| 47 | 46 |
| 48 } // namespace | 47 } // namespace |
| 49 | 48 |
| 50 namespace internal { | 49 namespace internal { |
| 51 | 50 |
| 52 size_t AlignToNextPage(size_t size) { | |
| 53 const size_t kPageSize = 4096; | |
| 54 DCHECK_EQ(static_cast<int>(kPageSize), getpagesize()); | |
| 55 if (size > std::numeric_limits<size_t>::max() - kPageSize + 1) | |
| 56 return 0; | |
| 57 const size_t mask = ~(kPageSize - 1); | |
| 58 return (size + kPageSize - 1) & mask; | |
| 59 } | |
| 60 | |
| 61 bool CreateAshmemRegion(const char* name, | 51 bool CreateAshmemRegion(const char* name, |
| 62 size_t size, | 52 size_t size, |
| 63 int* out_fd, | 53 int* out_fd, |
| 64 void** out_address) { | 54 void** out_address) { |
| 65 int fd = ashmem_create_region(name, size); | 55 int fd = ashmem_create_region(name, size); |
| 66 if (fd < 0) { | 56 if (fd < 0) { |
| 67 DLOG(ERROR) << "ashmem_create_region() failed"; | 57 DLOG(ERROR) << "ashmem_create_region() failed"; |
| 68 return false; | 58 return false; |
| 69 } | 59 } |
| 70 file_util::ScopedFD fd_closer(&fd); | 60 file_util::ScopedFD fd_closer(&fd); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 bool DiscardableMemory::PurgeForTestingSupported() { | 159 bool DiscardableMemory::PurgeForTestingSupported() { |
| 170 return false; | 160 return false; |
| 171 } | 161 } |
| 172 | 162 |
| 173 // static | 163 // static |
| 174 void DiscardableMemory::PurgeForTesting() { | 164 void DiscardableMemory::PurgeForTesting() { |
| 175 NOTIMPLEMENTED(); | 165 NOTIMPLEMENTED(); |
| 176 } | 166 } |
| 177 | 167 |
| 178 } // namespace base | 168 } // namespace base |
| OLD | NEW |