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.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 "base/basictypes.h" | |
11 #include "base/compiler_specific.h" | |
12 #include "base/file_util.h" | |
10 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
11 #include "base/logging.h" | 14 #include "base/logging.h" |
12 #include "base/posix/eintr_wrapper.h" | 15 #include "base/posix/eintr_wrapper.h" |
13 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
14 #include "third_party/ashmem/ashmem.h" | 17 #include "third_party/ashmem/ashmem.h" |
15 | 18 |
19 namespace base { | |
16 namespace { | 20 namespace { |
17 | 21 |
22 // Protects |g_num_discardable_memory| below. | |
18 base::LazyInstance<base::Lock>::Leaky g_discardable_memory_lock = | 23 base::LazyInstance<base::Lock>::Leaky g_discardable_memory_lock = |
19 LAZY_INSTANCE_INITIALIZER; | 24 LAZY_INSTANCE_INITIALIZER; |
20 | 25 |
21 // Total number of discardable memory in the process. | 26 // Total number of discardable memory in the process. |
22 int g_num_discardable_memory = 0; | 27 int g_num_discardable_memory = 0; |
23 | 28 |
24 // Upper limit on the number of discardable memory to avoid hitting file | 29 // Upper limit on the number of discardable memory to avoid hitting file |
25 // descriptor limit. | 30 // descriptor limit. |
26 const int kDiscardableMemoryNumLimit = 128; | 31 const int kDiscardableMemoryNumLimit = 128; |
27 | 32 |
28 } | 33 class DiscardableMemoryAndroid : public DiscardableMemory { |
34 public: | |
35 DiscardableMemoryAndroid(int fd, void* address, size_t size) | |
36 : fd_(fd), | |
37 memory_(address), | |
38 size_(size) { | |
39 DCHECK_GE(fd_, 0); | |
40 DCHECK(memory_); | |
41 } | |
29 | 42 |
30 namespace base { | 43 virtual ~DiscardableMemoryAndroid() { |
44 internal::DeleteAshmemRegion(fd_, size_, memory_); | |
45 } | |
46 | |
47 // DiscardableMemory: | |
48 virtual LockDiscardableMemoryStatus Lock() OVERRIDE { | |
49 return internal::LockAshmemRegion(fd_, 0, 0, memory_); | |
50 } | |
51 | |
52 virtual void Unlock() OVERRIDE { | |
53 internal::UnlockAshmemRegion(fd_, 0, 0, memory_); | |
54 } | |
55 | |
56 virtual void* Memory() const OVERRIDE { | |
57 return memory_; | |
58 } | |
59 | |
60 private: | |
61 const int fd_; | |
62 void* const memory_; | |
63 const size_t size_; | |
64 | |
65 DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryAndroid); | |
66 }; | |
67 | |
68 } // namespace | |
31 | 69 |
32 // static | 70 // static |
33 bool DiscardableMemory::Supported() { | 71 bool DiscardableMemory::Supported() { |
34 return true; | 72 return true; |
35 } | 73 } |
36 | 74 |
37 DiscardableMemory::~DiscardableMemory() { | 75 // static |
38 if (is_locked_) | 76 scoped_ptr<DiscardableMemory> DiscardableMemory::CreateLockedMemory( |
39 Unlock(); | 77 size_t size) { |
40 // If fd_ is smaller than 0, initialization must have failed and | 78 int fd; |
41 // g_num_discardable_memory is not incremented by the caller. | 79 void* address; |
42 if (fd_ < 0) | 80 if (!internal::CreateAshmemRegion("", size, &fd, &address)) |
43 return; | 81 return scoped_ptr<DiscardableMemory>(); |
44 HANDLE_EINTR(close(fd_)); | 82 return scoped_ptr<DiscardableMemory>( |
45 fd_ = -1; | 83 new DiscardableMemoryAndroid(fd, address, size)); |
46 ReleaseFileDescriptor(); | |
47 } | |
48 | |
49 bool DiscardableMemory::ReserveFileDescriptor() { | |
50 base::AutoLock lock(g_discardable_memory_lock.Get()); | |
51 if (g_num_discardable_memory < kDiscardableMemoryNumLimit) { | |
52 ++g_num_discardable_memory; | |
53 return true; | |
54 } | |
55 return false; | |
56 } | |
57 | |
58 void DiscardableMemory::ReleaseFileDescriptor() { | |
59 base::AutoLock lock(g_discardable_memory_lock.Get()); | |
60 --g_num_discardable_memory; | |
61 DCHECK_LE(0, g_num_discardable_memory); | |
62 } | |
63 | |
64 bool DiscardableMemory::InitializeAndLock(size_t size) { | |
65 // When this function returns true, fd_ should be larger or equal than 0 | |
66 // and g_num_discardable_memory is incremented by 1. Otherwise, fd_ | |
67 // is less than 0 and g_num_discardable_memory is not incremented by | |
68 // the caller. | |
69 DCHECK_EQ(fd_, -1); | |
70 DCHECK(!memory_); | |
71 if (!ReserveFileDescriptor()) | |
72 return false; | |
73 | |
74 size_ = size; | |
75 fd_ = ashmem_create_region("", size); | |
76 | |
77 if (fd_ < 0) { | |
78 DLOG(ERROR) << "ashmem_create_region() failed"; | |
79 ReleaseFileDescriptor(); | |
80 return false; | |
81 } | |
82 | |
83 int err = ashmem_set_prot_region(fd_, PROT_READ | PROT_WRITE); | |
84 if (err < 0) { | |
85 DLOG(ERROR) << "Error " << err << " when setting protection of ashmem"; | |
86 HANDLE_EINTR(close(fd_)); | |
87 fd_ = -1; | |
88 ReleaseFileDescriptor(); | |
89 return false; | |
90 } | |
91 | |
92 if (!Map()) { | |
93 // Close the file descriptor in case of any initialization errors. | |
94 HANDLE_EINTR(close(fd_)); | |
95 fd_ = -1; | |
96 ReleaseFileDescriptor(); | |
97 return false; | |
98 } | |
99 | |
100 is_locked_ = true; | |
101 return true; | |
102 } | |
103 | |
104 LockDiscardableMemoryStatus DiscardableMemory::Lock() { | |
105 DCHECK_NE(fd_, -1); | |
106 DCHECK(!is_locked_); | |
107 | |
108 bool purged = false; | |
109 if (ashmem_pin_region(fd_, 0, 0) == ASHMEM_WAS_PURGED) | |
110 purged = true; | |
111 | |
112 if (!Map()) | |
113 return DISCARDABLE_MEMORY_FAILED; | |
114 | |
115 is_locked_ = true; | |
116 return purged ? DISCARDABLE_MEMORY_PURGED : DISCARDABLE_MEMORY_SUCCESS; | |
117 } | |
118 | |
119 void DiscardableMemory::Unlock() { | |
120 DCHECK_GE(fd_, 0); | |
121 DCHECK(is_locked_); | |
122 | |
123 Unmap(); | |
124 if (ashmem_unpin_region(fd_, 0, 0)) | |
125 DLOG(ERROR) << "Failed to unpin memory."; | |
126 is_locked_ = false; | |
127 } | |
128 | |
129 bool DiscardableMemory::Map() { | |
130 DCHECK(!memory_); | |
131 // There is a problem using MAP_PRIVATE here. As we are constantly calling | |
132 // Lock() and Unlock(), data could get lost if they are not written to the | |
133 // underlying file when Unlock() gets called. | |
134 memory_ = mmap(NULL, size_, PROT_READ | PROT_WRITE, MAP_SHARED, fd_, 0); | |
135 if (memory_ == (void*)-1) { | |
136 DPLOG(ERROR) << "Failed to map memory."; | |
137 memory_ = NULL; | |
138 if (ashmem_unpin_region(fd_, 0, 0)) | |
139 DLOG(ERROR) << "Failed to unpin memory."; | |
140 return false; | |
141 } | |
142 return true; | |
143 } | |
144 | |
145 void DiscardableMemory::Unmap() { | |
146 DCHECK(memory_); | |
147 | |
148 if (-1 == munmap(memory_, size_)) | |
149 DPLOG(ERROR) << "Failed to unmap memory."; | |
150 | |
151 memory_ = NULL; | |
152 } | 84 } |
153 | 85 |
154 // static | 86 // static |
155 bool DiscardableMemory::PurgeForTestingSupported() { | 87 bool DiscardableMemory::PurgeForTestingSupported() { |
156 return false; | 88 return false; |
157 } | 89 } |
158 | 90 |
159 // static | 91 // static |
160 void DiscardableMemory::PurgeForTesting() { | 92 void DiscardableMemory::PurgeForTesting() { |
161 NOTIMPLEMENTED(); | 93 NOTIMPLEMENTED(); |
162 } | 94 } |
163 | 95 |
96 namespace internal { | |
97 | |
98 bool CreateAshmemRegion(const char* name, | |
99 size_t size, | |
100 int* out_fd, | |
101 void** out_address) { | |
102 base::AutoLock lock(g_discardable_memory_lock.Get()); | |
103 if (g_num_discardable_memory + 1 > kDiscardableMemoryNumLimit) | |
104 return false; | |
105 int fd = ashmem_create_region(name, size); | |
106 if (fd < 0) { | |
107 DLOG(ERROR) << "ashmem_create_region() failed"; | |
108 return false; | |
109 } | |
110 file_util::ScopedFD fd_closer(&fd); | |
111 | |
112 const int err = ashmem_set_prot_region(fd, PROT_READ | PROT_WRITE); | |
113 if (err < 0) { | |
114 DLOG(ERROR) << "Error " << err << " when setting protection of ashmem"; | |
115 return false; | |
116 } | |
117 | |
118 // There is a problem using MAP_PRIVATE here. As we are constantly calling | |
119 // Lock() and Unlock(), data could get lost if they are not written to the | |
120 // underlying file when Unlock() gets called. | |
121 void* const address = mmap( | |
122 NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); | |
123 if (address == MAP_FAILED) { | |
124 DPLOG(ERROR) << "Failed to map memory."; | |
125 return false; | |
126 } | |
127 | |
128 ignore_result(fd_closer.release()); | |
129 ++g_num_discardable_memory; | |
130 *out_fd = fd; | |
131 *out_address = address; | |
132 return true; | |
133 } | |
134 | |
135 bool DeleteAshmemRegion(int fd, size_t size, void* address) { | |
136 base::AutoLock lock(g_discardable_memory_lock.Get()); | |
137 --g_num_discardable_memory; | |
138 if (munmap(address, size) == -1) { | |
139 DPLOG(ERROR) << "Failed to unmap memory."; | |
140 HANDLE_EINTR(close(fd)); | |
willchan no longer on Chromium
2013/10/17 01:21:37
Please drop this HANDLE_EINTR: https://code.google
Philippe
2013/10/17 08:56:24
Thanks, I didn't know about this issue.
| |
141 return false; | |
142 } | |
143 return HANDLE_EINTR(close(fd)) == 0; | |
willchan no longer on Chromium
2013/10/17 01:21:37
Ditto
Philippe
2013/10/17 08:56:24
Done.
| |
144 } | |
145 | |
146 LockDiscardableMemoryStatus LockAshmemRegion(int fd, | |
147 size_t off, | |
148 size_t size, | |
149 const void* address) { | |
150 const int result = ashmem_pin_region(fd, off, size); | |
151 #if !defined(NDEBUG) | |
willchan no longer on Chromium
2013/10/17 01:21:37
I don't get this. Why is this NDEBUG stuff needed?
Philippe
2013/10/17 08:56:24
Yeah, good point. I guess I added the DCHECK after
| |
152 DCHECK_EQ(0, mprotect(address, size, PROT_READ | PROT_WRITE)); | |
153 #endif | |
154 return result == ASHMEM_WAS_PURGED ? | |
155 DISCARDABLE_MEMORY_PURGED : DISCARDABLE_MEMORY_SUCCESS; | |
156 } | |
157 | |
158 bool UnlockAshmemRegion(int fd, size_t off, size_t size, const void* address) { | |
159 const int failed = ashmem_unpin_region(fd, off, size); | |
160 if (failed) | |
161 DLOG(ERROR) << "Failed to unpin memory."; | |
162 #if !defined(NDEBUG) | |
163 // This allows us to catch accesses to unlocked memory. | |
164 DCHECK_EQ(0, mprotect(address, size, PROT_NONE)); | |
165 #endif | |
166 return !failed; | |
167 } | |
168 | |
169 } // namespace internal | |
164 } // namespace base | 170 } // namespace base |
OLD | NEW |