OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2013 Google Inc. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. |
| 6 */ |
| 7 |
| 8 #include "SkPurgeableMemoryBlock.h" |
| 9 |
| 10 #include <mach/mach.h> |
| 11 |
| 12 bool SkPurgeableMemoryBlock::PlatformSupportsPurgeableMemory() { |
| 13 return true; |
| 14 } |
| 15 |
| 16 #ifdef SK_DEBUG |
| 17 bool SkPurgeableMemoryBlock::PlatformSupportsPurgingAllUnpinnedBlocks() { |
| 18 return true; |
| 19 } |
| 20 |
| 21 bool SkPurgeableMemoryBlock::PurgeAllUnpinnedBlocks() { |
| 22 // Unused. |
| 23 int state = 0; |
| 24 kern_return_t ret = vm_purgable_control(mach_task_self(), 0, VM_PURGABLE_PUR
GE_ALL, &state); |
| 25 return ret == KERN_SUCCESS; |
| 26 } |
| 27 |
| 28 bool SkPurgeableMemoryBlock::purge() { |
| 29 return false; |
| 30 } |
| 31 #endif |
| 32 |
| 33 static size_t round_to_page_size(size_t size) { |
| 34 const size_t mask = 4096 - 1; |
| 35 return (size + mask) & ~mask; |
| 36 } |
| 37 |
| 38 SkPurgeableMemoryBlock::SkPurgeableMemoryBlock(size_t size) { |
| 39 fSize = round_to_page_size(size); |
| 40 vm_address_t addr = 0; |
| 41 kern_return_t ret = vm_allocate(mach_task_self(), &addr, static_cast<vm_size
_t>(fSize), |
| 42 VM_FLAGS_PURGABLE | VM_FLAGS_ANYWHERE); |
| 43 if (KERN_SUCCESS == ret) { |
| 44 fAddr = reinterpret_cast<void*>(addr); |
| 45 fPinned = true; |
| 46 } else { |
| 47 fAddr = NULL; |
| 48 fPinned = false; |
| 49 } |
| 50 } |
| 51 |
| 52 SkPurgeableMemoryBlock::~SkPurgeableMemoryBlock() { |
| 53 SkDEBUGCODE(kern_return_t ret =) vm_deallocate(mach_task_self(), |
| 54 reinterpret_cast<vm_address_t
>(fAddr), |
| 55 static_cast<vm_size_t>(fSize)
); |
| 56 #ifdef SK_DEBUG |
| 57 if (ret != KERN_SUCCESS) { |
| 58 SkDebugf("SkPurgeableMemoryBlock destructor failed to deallocate.\n"); |
| 59 } |
| 60 #endif |
| 61 } |
| 62 |
| 63 SkPurgeableMemoryBlock::PinResult SkPurgeableMemoryBlock::pin() { |
| 64 SkASSERT(!fPinned); |
| 65 int state = VM_PURGABLE_NONVOLATILE; |
| 66 kern_return_t ret = vm_purgable_control(mach_task_self(), reinterpret_cast<v
m_address_t>(fAddr), |
| 67 VM_PURGABLE_SET_STATE, &state); |
| 68 if (ret != KERN_SUCCESS) { |
| 69 fPinned = false; |
| 70 return kFailed_PinResult; |
| 71 } |
| 72 |
| 73 fPinned = true; |
| 74 |
| 75 if (state & VM_PURGABLE_EMPTY) { |
| 76 return kUninitialized_PinResult; |
| 77 } else { |
| 78 return kRetained_PinResult; |
| 79 } |
| 80 } |
| 81 |
| 82 void SkPurgeableMemoryBlock::unpin() { |
| 83 SkASSERT(fPinned); |
| 84 int state = VM_PURGABLE_VOLATILE | VM_VOLATILE_GROUP_DEFAULT; |
| 85 SkDEBUGCODE(kern_return_t ret =) vm_purgable_control(mach_task_self(), |
| 86 reinterpret_cast<vm_add
ress_t>(fAddr), |
| 87 VM_PURGABLE_SET_STATE,
&state); |
| 88 fPinned = false; |
| 89 |
| 90 #ifdef SK_DEBUG |
| 91 if (ret != KERN_SUCCESS) { |
| 92 SkDebugf("SkPurgeableMemoryBlock::unpin() failed.\n"); |
| 93 } |
| 94 #endif |
| 95 } |
| 96 |
OLD | NEW |