OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_shared_memory.h" | 5 #include "base/memory/discardable_shared_memory.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 | 10 |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
195 if (result.value.u != old_state.value.u) { | 195 if (result.value.u != old_state.value.u) { |
196 // Update |last_known_usage_| in case the above CAS failed because of | 196 // Update |last_known_usage_| in case the above CAS failed because of |
197 // an incorrect timestamp. | 197 // an incorrect timestamp. |
198 last_known_usage_ = result.GetTimestamp(); | 198 last_known_usage_ = result.GetTimestamp(); |
199 return FAILED; | 199 return FAILED; |
200 } | 200 } |
201 } | 201 } |
202 | 202 |
203 // Zero for length means "everything onward". | 203 // Zero for length means "everything onward". |
204 if (!length) | 204 if (!length) |
205 length = AlignToPageSize(mapped_size_) - offset; | 205 length = AlignToPageSize(mapped_size_) - offset; |
Nico
2016/02/29 20:10:13
this writes to length_ if it's 0 and nothing after
reveman
2016/02/29 20:42:45
correct.
| |
206 | 206 |
207 size_t start = offset / base::GetPageSize(); | 207 size_t start = offset / base::GetPageSize(); |
208 size_t end = start + length / base::GetPageSize(); | 208 size_t end = start + length / base::GetPageSize(); |
209 DCHECK_LE(start, end); | 209 DCHECK_LE(start, end); |
210 DCHECK_LE(end, AlignToPageSize(mapped_size_) / base::GetPageSize()); | 210 DCHECK_LE(end, AlignToPageSize(mapped_size_) / base::GetPageSize()); |
211 | 211 |
212 // Add pages to |locked_page_count_|. | 212 // Add pages to |locked_page_count_|. |
213 // Note: Locking a page that is already locked is an error. | 213 // Note: Locking a page that is already locked is an error. |
214 locked_page_count_ += end - start; | 214 locked_page_count_ += end - start; |
215 #if DCHECK_IS_ON() | 215 #if DCHECK_IS_ON() |
216 // Detect incorrect usage by keeping track of exactly what pages are locked. | 216 // Detect incorrect usage by keeping track of exactly what pages are locked. |
217 for (auto page = start; page < end; ++page) { | 217 for (auto page = start; page < end; ++page) { |
218 auto result = locked_pages_.insert(page); | 218 auto result = locked_pages_.insert(page); |
219 DCHECK(result.second); | 219 DCHECK(result.second); |
220 } | 220 } |
221 DCHECK_EQ(locked_pages_.size(), locked_page_count_); | 221 DCHECK_EQ(locked_pages_.size(), locked_page_count_); |
222 #endif | 222 #endif |
223 | 223 |
224 // Always behave as if memory was purged when trying to lock a 0 byte segment. | |
225 if (!length) | |
226 return PURGED; | |
227 | |
224 // Pin pages if supported. | 228 // Pin pages if supported. |
225 #if defined(OS_ANDROID) | 229 #if defined(OS_ANDROID) |
226 SharedMemoryHandle handle = shared_memory_.handle(); | 230 SharedMemoryHandle handle = shared_memory_.handle(); |
227 if (SharedMemory::IsHandleValid(handle)) { | 231 if (SharedMemory::IsHandleValid(handle)) { |
228 if (ashmem_pin_region( | 232 if (ashmem_pin_region( |
229 handle.fd, AlignToPageSize(sizeof(SharedState)) + offset, length)) { | 233 handle.fd, AlignToPageSize(sizeof(SharedState)) + offset, length)) { |
230 return PURGED; | 234 return PURGED; |
231 } | 235 } |
232 } | 236 } |
233 #elif defined(OS_WIN) | 237 #elif defined(OS_WIN) |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
410 | 414 |
411 void DiscardableSharedMemory::Close() { | 415 void DiscardableSharedMemory::Close() { |
412 shared_memory_.Close(); | 416 shared_memory_.Close(); |
413 } | 417 } |
414 | 418 |
415 Time DiscardableSharedMemory::Now() const { | 419 Time DiscardableSharedMemory::Now() const { |
416 return Time::Now(); | 420 return Time::Now(); |
417 } | 421 } |
418 | 422 |
419 } // namespace base | 423 } // namespace base |
OLD | NEW |