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

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: fix typo Created 5 years, 8 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(DISCARDABLE_SHARED_MEMORY_SHRINKING)
345 void DiscardableSharedMemory::Shrink() {
346 #if defined(OS_POSIX)
347 SharedMemoryHandle handle = shared_memory_.handle();
348 if (!SharedMemory::IsHandleValid(handle))
349 return;
350
351 // Truncate shared memory to size of SharedState.
352 if (HANDLE_EINTR(
353 ftruncate(handle.fd, AlignToPageSize(sizeof(SharedState)))) != 0) {
354 DPLOG(ERROR) << "ftruncate() failed";
355 return;
356 }
357 mapped_size_ = 0;
358 #else
359 NOTIMPLEMENTED();
360 #endif
361 }
362 #endif
363
360 Time DiscardableSharedMemory::Now() const { 364 Time DiscardableSharedMemory::Now() const {
361 return Time::Now(); 365 return Time::Now();
362 } 366 }
363 367
364 } // namespace base 368 } // namespace base
OLDNEW
« no previous file with comments | « base/memory/discardable_shared_memory.h ('k') | base/memory/discardable_shared_memory_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698