scoped_mach_vm.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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
  3. // found in the LICENSE file.
  4. #ifndef BASE_MAC_SCOPED_MACH_VM_H_
  5. #define BASE_MAC_SCOPED_MACH_VM_H_
  6. #include <mach/mach.h>
  7. #include <stddef.h>
  8. #include <algorithm>
  9. #include <utility>
  10. #include "base/base_export.h"
  11. #include "base/check_op.h"
  12. // Use ScopedMachVM to supervise ownership of pages in the current process
  13. // through the Mach VM subsystem. Pages allocated with vm_allocate can be
  14. // released when exiting a scope with ScopedMachVM.
  15. //
  16. // The Mach VM subsystem operates on a page-by-page basis, and a single VM
  17. // allocation managed by a ScopedMachVM object may span multiple pages. As far
  18. // as Mach is concerned, allocated pages may be deallocated individually. This
  19. // is in contrast to higher-level allocators such as malloc, where the base
  20. // address of an allocation implies the size of an allocated block.
  21. // Consequently, it is not sufficient to just pass the base address of an
  22. // allocation to ScopedMachVM, it also needs to know the size of the
  23. // allocation. To avoid any confusion, both the base address and size must
  24. // be page-aligned.
  25. //
  26. // When dealing with Mach VM, base addresses will naturally be page-aligned,
  27. // but user-specified sizes may not be. If there's a concern that a size is
  28. // not page-aligned, use the mach_vm_round_page macro to correct it.
  29. //
  30. // Example:
  31. //
  32. // vm_address_t address = 0;
  33. // vm_size_t size = 12345; // This requested size is not page-aligned.
  34. // kern_return_t kr =
  35. // vm_allocate(mach_task_self(), &address, size, VM_FLAGS_ANYWHERE);
  36. // if (kr != KERN_SUCCESS) {
  37. // return false;
  38. // }
  39. // ScopedMachVM vm_owner(address, mach_vm_round_page(size));
  40. namespace base::mac {
  41. class BASE_EXPORT ScopedMachVM {
  42. public:
  43. explicit ScopedMachVM(vm_address_t address = 0, vm_size_t size = 0)
  44. : address_(address), size_(size) {
  45. DCHECK_EQ(address % PAGE_SIZE, 0u);
  46. DCHECK_EQ(size % PAGE_SIZE, 0u);
  47. }
  48. ScopedMachVM(const ScopedMachVM&) = delete;
  49. ScopedMachVM& operator=(const ScopedMachVM&) = delete;
  50. ~ScopedMachVM() {
  51. if (size_) {
  52. vm_deallocate(mach_task_self(), address_, size_);
  53. }
  54. }
  55. // Resets the scoper to manage a new memory region. Both |address| and |size|
  56. // must be page-aligned. If the new region is a smaller subset of the
  57. // existing region (i.e. the new and old regions overlap), the non-
  58. // overlapping part of the old region is deallocated.
  59. void reset(vm_address_t address = 0, vm_size_t size = 0);
  60. // Like reset() but does not DCHECK that |address| and |size| are page-
  61. // aligned.
  62. void reset_unaligned(vm_address_t address, vm_size_t size);
  63. vm_address_t address() const {
  64. return address_;
  65. }
  66. vm_size_t size() const {
  67. return size_;
  68. }
  69. void swap(ScopedMachVM& that) {
  70. std::swap(address_, that.address_);
  71. std::swap(size_, that.size_);
  72. }
  73. void release() {
  74. address_ = 0;
  75. size_ = 0;
  76. }
  77. private:
  78. vm_address_t address_;
  79. vm_size_t size_;
  80. };
  81. } // namespace base::mac
  82. #endif // BASE_MAC_SCOPED_MACH_VM_H_