| Index: base/memory/discardable_memory_mac.cc
|
| diff --git a/base/memory/discardable_memory_mac.cc b/base/memory/discardable_memory_mac.cc
|
| index d59dcaee855d3d6b42c7a2988e3fac925cc52387..8cd5905c904deb8392f6bef67399484e0dd78e75 100644
|
| --- a/base/memory/discardable_memory_mac.cc
|
| +++ b/base/memory/discardable_memory_mac.cc
|
| @@ -5,12 +5,11 @@
|
| #include "base/memory/discardable_memory.h"
|
|
|
| #include <mach/mach.h>
|
| +#include <sys/mman.h>
|
|
|
| #include "base/basictypes.h"
|
| #include "base/compiler_specific.h"
|
| #include "base/logging.h"
|
| -#include "base/mac/mach_logging.h"
|
| -#include "base/mac/scoped_mach_vm.h"
|
| #include "base/memory/discardable_memory_emulated.h"
|
| #include "base/memory/discardable_memory_malloc.h"
|
| #include "base/memory/scoped_ptr.h"
|
| @@ -26,44 +25,39 @@ const int kDiscardableMemoryTag = VM_MAKE_TAG(252);
|
| class DiscardableMemoryMac : public DiscardableMemory {
|
| public:
|
| explicit DiscardableMemoryMac(size_t size)
|
| - : memory_(0, 0),
|
| - size_(mach_vm_round_page(size)) {
|
| + : buffer_(0),
|
| + size_(size) {
|
| }
|
|
|
| bool Initialize() {
|
| - DCHECK_EQ(memory_.size(), 0u);
|
| - vm_address_t address = 0;
|
| kern_return_t ret = vm_allocate(mach_task_self(),
|
| - &address,
|
| + &buffer_,
|
| size_,
|
| VM_FLAGS_PURGABLE |
|
| - VM_FLAGS_ANYWHERE |
|
| - kDiscardableMemoryTag);
|
| + VM_FLAGS_ANYWHERE |
|
| + kDiscardableMemoryTag);
|
| if (ret != KERN_SUCCESS) {
|
| - MACH_DLOG(ERROR, ret) << "vm_allocate";
|
| + DLOG(ERROR) << "vm_allocate() failed";
|
| return false;
|
| }
|
|
|
| - memory_.reset(address, size_);
|
| -
|
| return true;
|
| }
|
|
|
| virtual ~DiscardableMemoryMac() {
|
| + if (buffer_)
|
| + vm_deallocate(mach_task_self(), buffer_, size_);
|
| }
|
|
|
| virtual DiscardableMemoryLockStatus Lock() OVERRIDE {
|
| - kern_return_t ret;
|
| - MACH_DCHECK((ret = vm_protect(mach_task_self(),
|
| - memory_.address(),
|
| - memory_.size(),
|
| - FALSE,
|
| - VM_PROT_DEFAULT)) == KERN_SUCCESS, ret);
|
| + DCHECK_EQ(0, mprotect(reinterpret_cast<void*>(buffer_),
|
| + size_,
|
| + PROT_READ | PROT_WRITE));
|
| int state = VM_PURGABLE_NONVOLATILE;
|
| - ret = vm_purgable_control(mach_task_self(),
|
| - memory_.address(),
|
| - VM_PURGABLE_SET_STATE,
|
| - &state);
|
| + kern_return_t ret = vm_purgable_control(mach_task_self(),
|
| + buffer_,
|
| + VM_PURGABLE_SET_STATE,
|
| + &state);
|
| if (ret != KERN_SUCCESS)
|
| return DISCARDABLE_MEMORY_LOCK_STATUS_FAILED;
|
|
|
| @@ -74,24 +68,21 @@ class DiscardableMemoryMac : public DiscardableMemory {
|
| virtual void Unlock() OVERRIDE {
|
| int state = VM_PURGABLE_VOLATILE | VM_VOLATILE_GROUP_DEFAULT;
|
| kern_return_t ret = vm_purgable_control(mach_task_self(),
|
| - memory_.address(),
|
| + buffer_,
|
| VM_PURGABLE_SET_STATE,
|
| &state);
|
| - MACH_DLOG_IF(ERROR, ret != KERN_SUCCESS, ret) << "vm_purgable_control";
|
| - MACH_DCHECK((ret = vm_protect(mach_task_self(),
|
| - memory_.address(),
|
| - memory_.size(),
|
| - FALSE,
|
| - VM_PROT_NONE)) == KERN_SUCCESS, ret);
|
| + DCHECK_EQ(0, mprotect(reinterpret_cast<void*>(buffer_), size_, PROT_NONE));
|
| + if (ret != KERN_SUCCESS)
|
| + DLOG(ERROR) << "Failed to unlock memory.";
|
| }
|
|
|
| virtual void* Memory() const OVERRIDE {
|
| - return reinterpret_cast<void*>(memory_.address());
|
| + return reinterpret_cast<void*>(buffer_);
|
| }
|
|
|
| private:
|
| - mac::ScopedMachVM memory_;
|
| - size_t size_;
|
| + vm_address_t buffer_;
|
| + const size_t size_;
|
|
|
| DISALLOW_COPY_AND_ASSIGN(DiscardableMemoryMac);
|
| };
|
|
|