Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "VolatileBuffer.h" |
michael@0 | 6 | #include "mozilla/Assertions.h" |
michael@0 | 7 | #include "mozilla/DebugOnly.h" |
michael@0 | 8 | #include "mozilla/mozalloc.h" |
michael@0 | 9 | |
michael@0 | 10 | #include <mach/mach.h> |
michael@0 | 11 | #include <sys/mman.h> |
michael@0 | 12 | #include <unistd.h> |
michael@0 | 13 | |
michael@0 | 14 | #define MIN_VOLATILE_ALLOC_SIZE 8192 |
michael@0 | 15 | |
michael@0 | 16 | namespace mozilla { |
michael@0 | 17 | |
michael@0 | 18 | VolatileBuffer::VolatileBuffer() |
michael@0 | 19 | : mBuf(nullptr) |
michael@0 | 20 | , mSize(0) |
michael@0 | 21 | , mLockCount(0) |
michael@0 | 22 | , mHeap(false) |
michael@0 | 23 | { |
michael@0 | 24 | } |
michael@0 | 25 | |
michael@0 | 26 | bool |
michael@0 | 27 | VolatileBuffer::Init(size_t aSize, size_t aAlignment) |
michael@0 | 28 | { |
michael@0 | 29 | MOZ_ASSERT(!mSize && !mBuf, "Init called twice"); |
michael@0 | 30 | MOZ_ASSERT(!(aAlignment % sizeof(void *)), |
michael@0 | 31 | "Alignment must be multiple of pointer size"); |
michael@0 | 32 | |
michael@0 | 33 | mSize = aSize; |
michael@0 | 34 | |
michael@0 | 35 | kern_return_t ret = 0; |
michael@0 | 36 | if (aSize < MIN_VOLATILE_ALLOC_SIZE) { |
michael@0 | 37 | goto heap_alloc; |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | ret = vm_allocate(mach_task_self(), |
michael@0 | 41 | (vm_address_t*)&mBuf, |
michael@0 | 42 | mSize, |
michael@0 | 43 | VM_FLAGS_PURGABLE | VM_FLAGS_ANYWHERE); |
michael@0 | 44 | if (ret == KERN_SUCCESS) { |
michael@0 | 45 | return true; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | heap_alloc: |
michael@0 | 49 | (void)moz_posix_memalign(&mBuf, aAlignment, aSize); |
michael@0 | 50 | mHeap = true; |
michael@0 | 51 | return !!mBuf; |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | VolatileBuffer::~VolatileBuffer() |
michael@0 | 55 | { |
michael@0 | 56 | if (OnHeap()) { |
michael@0 | 57 | free(mBuf); |
michael@0 | 58 | } else { |
michael@0 | 59 | vm_deallocate(mach_task_self(), (vm_address_t)mBuf, mSize); |
michael@0 | 60 | } |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | bool |
michael@0 | 64 | VolatileBuffer::Lock(void** aBuf) |
michael@0 | 65 | { |
michael@0 | 66 | MOZ_ASSERT(mBuf, "Attempting to lock an uninitialized VolatileBuffer"); |
michael@0 | 67 | |
michael@0 | 68 | *aBuf = mBuf; |
michael@0 | 69 | if (++mLockCount > 1 || OnHeap()) { |
michael@0 | 70 | return true; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | int state = VM_PURGABLE_NONVOLATILE; |
michael@0 | 74 | kern_return_t ret = |
michael@0 | 75 | vm_purgable_control(mach_task_self(), |
michael@0 | 76 | (vm_address_t)mBuf, |
michael@0 | 77 | VM_PURGABLE_SET_STATE, |
michael@0 | 78 | &state); |
michael@0 | 79 | return ret == KERN_SUCCESS && !(state & VM_PURGABLE_EMPTY); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | void |
michael@0 | 83 | VolatileBuffer::Unlock() |
michael@0 | 84 | { |
michael@0 | 85 | MOZ_ASSERT(mLockCount > 0, "VolatileBuffer unlocked too many times!"); |
michael@0 | 86 | if (--mLockCount || OnHeap()) { |
michael@0 | 87 | return; |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | int state = VM_PURGABLE_VOLATILE | VM_VOLATILE_GROUP_DEFAULT; |
michael@0 | 91 | DebugOnly<kern_return_t> ret = |
michael@0 | 92 | vm_purgable_control(mach_task_self(), |
michael@0 | 93 | (vm_address_t)mBuf, |
michael@0 | 94 | VM_PURGABLE_SET_STATE, |
michael@0 | 95 | &state); |
michael@0 | 96 | MOZ_ASSERT(ret == KERN_SUCCESS, "Failed to set buffer as purgable"); |
michael@0 | 97 | } |
michael@0 | 98 | |
michael@0 | 99 | bool |
michael@0 | 100 | VolatileBuffer::OnHeap() const |
michael@0 | 101 | { |
michael@0 | 102 | return mHeap; |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | size_t |
michael@0 | 106 | VolatileBuffer::HeapSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const |
michael@0 | 107 | { |
michael@0 | 108 | return OnHeap() ? aMallocSizeOf(mBuf) : 0; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | size_t |
michael@0 | 112 | VolatileBuffer::NonHeapSizeOfExcludingThis() const |
michael@0 | 113 | { |
michael@0 | 114 | if (OnHeap()) { |
michael@0 | 115 | return 0; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | unsigned long pagemask = getpagesize() - 1; |
michael@0 | 119 | return (mSize + pagemask) & ~pagemask; |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | } // namespace mozilla |