scoped_mach_vm.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 "base/base_export.h"
  10. #include "base/check_op.h"
  11. #include "base/macros.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 {
  41. namespace mac {
  42. class BASE_EXPORT ScopedMachVM {
  43. public:
  44. explicit ScopedMachVM(vm_address_t address = 0, vm_size_t size = 0)
  45. : address_(address), size_(size) {
  46. DCHECK_EQ(address % PAGE_SIZE, 0u);
  47. DCHECK_EQ(size % PAGE_SIZE, 0u);
  48. }
  49. ~ScopedMachVM() {
  50. if (size_) {
  51. vm_deallocate(mach_task_self(), address_, size_);
  52. }
  53. }
  54. // Resets the scoper to manage a new memory region. Both |address| and |size|
  55. // must be page-aligned. If the new region is a smaller subset of the
  56. // existing region (i.e. the new and old regions overlap), the non-
  57. // overlapping part of the old region is deallocated.
  58. void reset(vm_address_t address = 0, vm_size_t size = 0);
  59. // Like reset() but does not DCHECK that |address| and |size| are page-
  60. // aligned.
  61. void reset_unaligned(vm_address_t address, vm_size_t size);
  62. vm_address_t address() const {
  63. return address_;
  64. }
  65. vm_size_t size() const {
  66. return size_;
  67. }
  68. void swap(ScopedMachVM& that) {
  69. std::swap(address_, that.address_);
  70. std::swap(size_, that.size_);
  71. }
  72. void release() {
  73. address_ = 0;
  74. size_ = 0;
  75. }
  76. private:
  77. vm_address_t address_;
  78. vm_size_t size_;
  79. DISALLOW_COPY_AND_ASSIGN(ScopedMachVM);
  80. };
  81. } // namespace mac
  82. } // namespace base
  83. #endif // BASE_MAC_SCOPED_MACH_VM_H_