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

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

Issue 2535213002: [WIP] Add SharedMemoryTracker to dump base::SharedMemory usage
Patch Set: Implement buckets Created 3 years, 11 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"
24 #include "build/build_config.h" 25 #include "build/build_config.h"
25 26
26 #if defined(OS_ANDROID) 27 #if defined(OS_ANDROID)
27 #include "base/os_compat_android.h" 28 #include "base/os_compat_android.h"
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
277 } 278 }
278 #endif 279 #endif
279 280
280 memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE), 281 memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE),
281 MAP_SHARED, mapped_file_, offset); 282 MAP_SHARED, mapped_file_, offset);
282 283
283 bool mmap_succeeded = memory_ != (void*)-1 && memory_ != NULL; 284 bool mmap_succeeded = memory_ != (void*)-1 && memory_ != NULL;
284 if (mmap_succeeded) { 285 if (mmap_succeeded) {
285 mapped_size_ = bytes; 286 mapped_size_ = bytes;
286 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) & 287 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) &
287 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1)); 288 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1));
289 if (!SharedMemoryTracker::GetInstance()->IncrementMemoryUsage(*this))
290 return false;
288 } else { 291 } else {
289 memory_ = NULL; 292 memory_ = NULL;
290 } 293 }
291 294
292 return mmap_succeeded; 295 return mmap_succeeded;
293 } 296 }
294 297
295 bool SharedMemory::Unmap() { 298 bool SharedMemory::Unmap() {
296 if (memory_ == NULL) 299 if (memory_ == NULL)
297 return false; 300 return false;
298 301
299 munmap(memory_, mapped_size_); 302 munmap(memory_, mapped_size_);
303 SharedMemoryTracker::GetInstance()->DecrementMemoryUsage(*this);
300 memory_ = NULL; 304 memory_ = NULL;
301 mapped_size_ = 0; 305 mapped_size_ = 0;
302 return true; 306 return true;
303 } 307 }
304 308
305 SharedMemoryHandle SharedMemory::handle() const { 309 SharedMemoryHandle SharedMemory::handle() const {
306 return FileDescriptor(mapped_file_, false); 310 return FileDescriptor(mapped_file_, false);
307 } 311 }
308 312
309 SharedMemoryHandle SharedMemory::TakeHandle() { 313 SharedMemoryHandle SharedMemory::TakeHandle() {
310 FileDescriptor handle(mapped_file_, true); 314 FileDescriptor handle(mapped_file_, true);
311 mapped_file_ = -1; 315 mapped_file_ = -1;
312 memory_ = nullptr; 316 memory_ = nullptr;
313 mapped_size_ = 0; 317 mapped_size_ = 0;
314 return handle; 318 return handle;
315 } 319 }
316 320
317 void SharedMemory::Close() { 321 void SharedMemory::Close() {
318 if (mapped_file_ > 0) { 322 if (mapped_file_ > 0) {
319 if (IGNORE_EINTR(close(mapped_file_)) < 0) 323 if (IGNORE_EINTR(close(mapped_file_)) < 0)
320 PLOG(ERROR) << "close"; 324 PLOG(ERROR) << "close";
325 SharedMemoryTracker::GetInstance()->DecrementMemoryUsage(*this);
321 mapped_file_ = -1; 326 mapped_file_ = -1;
322 } 327 }
323 if (readonly_mapped_file_ > 0) { 328 if (readonly_mapped_file_ > 0) {
324 if (IGNORE_EINTR(close(readonly_mapped_file_)) < 0) 329 if (IGNORE_EINTR(close(readonly_mapped_file_)) < 0)
325 PLOG(ERROR) << "close"; 330 PLOG(ERROR) << "close";
326 readonly_mapped_file_ = -1; 331 readonly_mapped_file_ = -1;
327 } 332 }
328 } 333 }
329 334
330 #if !defined(OS_ANDROID) 335 #if !defined(OS_ANDROID)
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
393 } // namespace base 398 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698