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

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

Issue 2654073002: base: Introduce SharedMemoryTracker for POSIX (but not macOS) (Closed)
Patch Set: Fix PRESUBMIT.py Created 3 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/shared_memory.h" 5 #include "base/memory/shared_memory.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <fcntl.h> 8 #include <fcntl.h>
9 #include <stddef.h> 9 #include <stddef.h>
10 #include <sys/mman.h> 10 #include <sys/mman.h>
11 #include <sys/stat.h> 11 #include <sys/stat.h>
12 #include <unistd.h> 12 #include <unistd.h>
13 13
14 #include "base/files/file_util.h" 14 #include "base/files/file_util.h"
15 #include "base/files/scoped_file.h" 15 #include "base/files/scoped_file.h"
16 #include "base/logging.h" 16 #include "base/logging.h"
17 #include "base/memory/shared_memory_helper.h" 17 #include "base/memory/shared_memory_helper.h"
18 #include "base/memory/shared_memory_tracker.h"
18 #include "base/posix/eintr_wrapper.h" 19 #include "base/posix/eintr_wrapper.h"
19 #include "base/posix/safe_strerror.h" 20 #include "base/posix/safe_strerror.h"
20 #include "base/process/process_metrics.h" 21 #include "base/process/process_metrics.h"
21 #include "base/scoped_generic.h" 22 #include "base/scoped_generic.h"
22 #include "base/strings/utf_string_conversions.h" 23 #include "base/strings/utf_string_conversions.h"
23 #include "base/threading/thread_restrictions.h" 24 #include "base/threading/thread_restrictions.h"
25 #include "base/trace_event/trace_event.h"
24 #include "build/build_config.h" 26 #include "build/build_config.h"
25 27
26 #if defined(OS_ANDROID) 28 #if defined(OS_ANDROID)
27 #include "base/os_compat_android.h" 29 #include "base/os_compat_android.h"
28 #include "third_party/ashmem/ashmem.h" 30 #include "third_party/ashmem/ashmem.h"
29 #endif 31 #endif
30 32
31 namespace base { 33 namespace base {
32 34
33 SharedMemory::SharedMemory() 35 SharedMemory::SharedMemory()
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 bytes = ashmem_bytes; 278 bytes = ashmem_bytes;
277 } 279 }
278 #endif 280 #endif
279 281
280 memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE), 282 memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE),
281 MAP_SHARED, mapped_file_, offset); 283 MAP_SHARED, mapped_file_, offset);
282 284
283 bool mmap_succeeded = memory_ != (void*)-1 && memory_ != NULL; 285 bool mmap_succeeded = memory_ != (void*)-1 && memory_ != NULL;
284 if (mmap_succeeded) { 286 if (mmap_succeeded) {
285 mapped_size_ = bytes; 287 mapped_size_ = bytes;
286 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & 288 DCHECK_EQ(0U,
287 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); 289 reinterpret_cast<uintptr_t>(memory_) &
290 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1));
291 SharedMemoryTracker::GetInstance()->IncrementMemoryUsage(*this);
288 } else { 292 } else {
289 memory_ = NULL; 293 memory_ = NULL;
290 } 294 }
291 295
292 return mmap_succeeded; 296 return mmap_succeeded;
293 } 297 }
294 298
295 bool SharedMemory::Unmap() { 299 bool SharedMemory::Unmap() {
296 if (memory_ == NULL) 300 if (memory_ == NULL)
297 return false; 301 return false;
298 302
299 munmap(memory_, mapped_size_); 303 munmap(memory_, mapped_size_);
304 SharedMemoryTracker::GetInstance()->DecrementMemoryUsage(*this);
300 memory_ = NULL; 305 memory_ = NULL;
301 mapped_size_ = 0; 306 mapped_size_ = 0;
302 return true; 307 return true;
303 } 308 }
304 309
305 SharedMemoryHandle SharedMemory::handle() const { 310 SharedMemoryHandle SharedMemory::handle() const {
306 return FileDescriptor(mapped_file_, false); 311 return FileDescriptor(mapped_file_, false);
307 } 312 }
308 313
309 SharedMemoryHandle SharedMemory::TakeHandle() { 314 SharedMemoryHandle SharedMemory::TakeHandle() {
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
383 new_handle->auto_close = true; 388 new_handle->auto_close = true;
384 389
385 if (close_self) { 390 if (close_self) {
386 Unmap(); 391 Unmap();
387 Close(); 392 Close();
388 } 393 }
389 394
390 return true; 395 return true;
391 } 396 }
392 397
398 bool SharedMemory::GetUniqueId(SharedMemory::UniqueId* id) const {
399 // This function is called just after mmap. fstat is a system call that might
400 // cause I/O. It's safe to call fstat here because mmap for shared memory is
401 // called in two cases:
402 // 1) To handle file-mapped memory
403 // 2) To handle annonymous shared memory
404 // In 1), I/O is already permitted. In 2), the backend is on page cache and
405 // fstat doesn't cause I/O access to the disk. See the discussion at
406 // crbug.com/604726#c41.
407 base::ThreadRestrictions::ScopedAllowIO allow_io;
408 struct stat file_stat;
409 if (HANDLE_EINTR(::fstat(static_cast<int>(handle().fd), &file_stat)) != 0)
410 return false;
411 id->first = file_stat.st_dev;
412 id->second = file_stat.st_ino;
413 return true;
414 }
415
393 } // namespace base 416 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698