summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authormark@chromium.org <mark@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-05-09 13:54:36 +0000
committermark@chromium.org <mark@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2014-05-09 13:54:36 +0000
commit3fecd58c4a74b530afe0f4d68ec72f7f14976ea4 (patch)
tree7b16c57d3e2496903430164baf1981f66c5fb36a
parent420e12f432cadffaccc947c21afd9829afca0070 (diff)
downloadchromium_src-3fecd58c4a74b530afe0f4d68ec72f7f14976ea4.zip
chromium_src-3fecd58c4a74b530afe0f4d68ec72f7f14976ea4.tar.gz
chromium_src-3fecd58c4a74b530afe0f4d68ec72f7f14976ea4.tar.bz2
Add ScopedMachVM to Chrome.
R=rsesek@chromium.org Review URL: https://codereview.chromium.org/275483003 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@269273 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r--base/base.gypi2
-rw-r--r--base/mac/scoped_mach_vm.cc33
-rw-r--r--base/mac/scoped_mach_vm.h93
3 files changed, 128 insertions, 0 deletions
diff --git a/base/base.gypi b/base/base.gypi
index 3c244ad..520f567 100644
--- a/base/base.gypi
+++ b/base/base.gypi
@@ -289,6 +289,8 @@
'mac/scoped_launch_data.h',
'mac/scoped_mach_port.cc',
'mac/scoped_mach_port.h',
+ 'mac/scoped_mach_vm.cc',
+ 'mac/scoped_mach_vm.h',
'mac/scoped_nsautorelease_pool.h',
'mac/scoped_nsautorelease_pool.mm',
'mac/scoped_nsexception_enabler.h',
diff --git a/base/mac/scoped_mach_vm.cc b/base/mac/scoped_mach_vm.cc
new file mode 100644
index 0000000..a9055aa
--- /dev/null
+++ b/base/mac/scoped_mach_vm.cc
@@ -0,0 +1,33 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/mac/scoped_mach_vm.h"
+
+namespace base {
+namespace mac {
+
+void ScopedMachVM::reset(mach_vm_address_t address, mach_vm_size_t size) {
+ DCHECK(address % PAGE_SIZE == 0);
+ DCHECK(size % PAGE_SIZE == 0);
+
+ if (size_) {
+ if (address_ < address) {
+ mach_vm_deallocate(mach_task_self(),
+ address_,
+ std::min(size_, address - address_));
+ }
+ if (address_ + size_ > address + size) {
+ mach_vm_address_t deallocate_start = std::max(address_, address + size);
+ mach_vm_deallocate(mach_task_self(),
+ deallocate_start,
+ address_ + size_ - deallocate_start);
+ }
+ }
+
+ address_ = address;
+ size_ = size;
+}
+
+} // namespace mac
+} // namespace base
diff --git a/base/mac/scoped_mach_vm.h b/base/mac/scoped_mach_vm.h
new file mode 100644
index 0000000..065b968
--- /dev/null
+++ b/base/mac/scoped_mach_vm.h
@@ -0,0 +1,93 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef BASE_MAC_SCOPED_MACH_VM_H_
+#define BASE_MAC_SCOPED_MACH_VM_H_
+
+#include <mach/mach.h>
+#include <mach/mach_vm.h>
+
+#include <algorithm>
+
+#include "base/basictypes.h"
+#include "base/logging.h"
+
+// Use ScopedMachVM to supervise ownership of pages in the current process
+// through the Mach VM subsystem. Pages allocated with mach_vm_allocate can be
+// released when exiting a scope with ScopedMachVM.
+//
+// The Mach VM subsystem operates on a page-by-page basis, and a single VM
+// allocation managed by a ScopedMachVM object may span multiple pages. As far
+// as Mach is concerned, allocated pages may be deallocated individually. This
+// is in contrast to higher-level allocators such as malloc, where the base
+// address of an allocation implies the size of an allocated block.
+// Consequently, it is not sufficient to just pass the base address of an
+// allocation to ScopedMachVM, it also needs to know the size of the
+// allocation. To avoid any confusion, both the base address and size must
+// be page-aligned.
+//
+// When dealing with Mach VM, base addresses will naturally be page-aligned,
+// but user-specified sizes may not be. If there's a concern that a size is
+// not page-aligned, use the mach_vm_round_page macro to correct it.
+//
+// Example:
+//
+// mach_vm_address_t address = 0;
+// mach_vm_size_t size = 12345; // This requested size is not page-aligned.
+// kern_return_t kr =
+// mach_vm_allocate(mach_task_self(), &address, size, VM_FLAGS_ANYWHERE);
+// if (kr != KERN_SUCCESS) {
+// return false;
+// }
+// ScopedMachVM vm_owner(address, mach_vm_round_page(size));
+
+namespace base {
+namespace mac {
+
+class ScopedMachVM {
+ public:
+ explicit ScopedMachVM(mach_vm_address_t address = 0, mach_vm_size_t size = 0)
+ : address_(address),
+ size_(size) {
+ DCHECK(address % PAGE_SIZE == 0);
+ DCHECK(size % PAGE_SIZE == 0);
+ }
+
+ ~ScopedMachVM() {
+ if (size_) {
+ mach_vm_deallocate(mach_task_self(), address_, size_);
+ }
+ }
+
+ void reset(mach_vm_address_t address = 0, mach_vm_size_t size = 0);
+
+ mach_vm_address_t address() const {
+ return address_;
+ }
+
+ mach_vm_size_t size() const {
+ return size_;
+ }
+
+ void swap(ScopedMachVM& that) {
+ std::swap(address_, that.address_);
+ std::swap(size_, that.size_);
+ }
+
+ void release() {
+ address_ = 0;
+ size_ = 0;
+ }
+
+ private:
+ mach_vm_address_t address_;
+ mach_vm_size_t size_;
+
+ DISALLOW_COPY_AND_ASSIGN(ScopedMachVM);
+};
+
+} // namespace mac
+} // namespace base
+
+#endif // BASE_MAC_SCOPED_MACH_VM_H_