Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: base/memory/discardable_shared_memory.cc

Issue 1018743003: Re-land: base: Replace PurgeAndTruncate with Shrink function. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: remove IsShrinkingSupported Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #if defined(OS_POSIX) 7 #if defined(OS_POSIX)
8 #include <unistd.h> 8 #include <unistd.h>
9 #endif 9 #endif
10 10
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 last_known_usage_ = result.GetLockState() == SharedState::LOCKED 318 last_known_usage_ = result.GetLockState() == SharedState::LOCKED
319 ? current_time 319 ? current_time
320 : result.GetTimestamp(); 320 : result.GetTimestamp();
321 return false; 321 return false;
322 } 322 }
323 323
324 last_known_usage_ = Time(); 324 last_known_usage_ = Time();
325 return true; 325 return true;
326 } 326 }
327 327
328 bool DiscardableSharedMemory::PurgeAndTruncate(Time current_time) {
329 if (!Purge(current_time))
330 return false;
331
332 #if defined(OS_POSIX)
333 // Truncate shared memory to size of SharedState.
334 SharedMemoryHandle handle = shared_memory_.handle();
335 if (SharedMemory::IsHandleValid(handle)) {
336 if (HANDLE_EINTR(ftruncate(handle.fd, sizeof(SharedState))) != 0)
337 DPLOG(ERROR) << "ftruncate() failed";
338 }
339 #endif
340
341 return true;
342 }
343
344 bool DiscardableSharedMemory::IsMemoryResident() const { 328 bool DiscardableSharedMemory::IsMemoryResident() const {
345 DCHECK(shared_memory_.memory()); 329 DCHECK(shared_memory_.memory());
346 330
347 SharedState result(subtle::NoBarrier_Load( 331 SharedState result(subtle::NoBarrier_Load(
348 &SharedStateFromSharedMemory(shared_memory_)->value.i)); 332 &SharedStateFromSharedMemory(shared_memory_)->value.i));
349 333
350 return result.GetLockState() == SharedState::LOCKED || 334 return result.GetLockState() == SharedState::LOCKED ||
351 !result.GetTimestamp().is_null(); 335 !result.GetTimestamp().is_null();
352 } 336 }
353 337
354 void DiscardableSharedMemory::Close() { 338 void DiscardableSharedMemory::Close() {
355 shared_memory_.Unmap(); 339 shared_memory_.Unmap();
356 shared_memory_.Close(); 340 shared_memory_.Close();
357 mapped_size_ = 0; 341 mapped_size_ = 0;
358 } 342 }
359 343
344 #if defined(OS_POSIX) && !defined(OS_ANDROID)
345 void DiscardableSharedMemory::Shrink() {
346 SharedMemoryHandle handle = shared_memory_.handle();
347 if (!SharedMemory::IsHandleValid(handle))
348 return;
349
350 // Truncate shared memory to size of SharedState.
351 if (HANDLE_EINTR(
352 ftruncate(handle.fd, AlignToPageSize(sizeof(SharedState)))) != 0) {
353 DPLOG(ERROR) << "ftruncate() failed";
354 return;
355 }
356
357 mapped_size_ = 0;
358 }
359 #endif
360
360 Time DiscardableSharedMemory::Now() const { 361 Time DiscardableSharedMemory::Now() const {
361 return Time::Now(); 362 return Time::Now();
362 } 363 }
363 364
364 } // namespace base 365 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698