michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "VolatileBuffer.h" michael@0: #include "mozilla/Assertions.h" michael@0: #include "mozilla/DebugOnly.h" michael@0: #include "mozilla/mozalloc.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #define MIN_VOLATILE_ALLOC_SIZE 8192 michael@0: michael@0: namespace mozilla { michael@0: michael@0: VolatileBuffer::VolatileBuffer() michael@0: : mBuf(nullptr) michael@0: , mSize(0) michael@0: , mLockCount(0) michael@0: , mHeap(false) michael@0: { michael@0: } michael@0: michael@0: bool michael@0: VolatileBuffer::Init(size_t aSize, size_t aAlignment) michael@0: { michael@0: MOZ_ASSERT(!mSize && !mBuf, "Init called twice"); michael@0: MOZ_ASSERT(!(aAlignment % sizeof(void *)), michael@0: "Alignment must be multiple of pointer size"); michael@0: michael@0: mSize = aSize; michael@0: michael@0: kern_return_t ret = 0; michael@0: if (aSize < MIN_VOLATILE_ALLOC_SIZE) { michael@0: goto heap_alloc; michael@0: } michael@0: michael@0: ret = vm_allocate(mach_task_self(), michael@0: (vm_address_t*)&mBuf, michael@0: mSize, michael@0: VM_FLAGS_PURGABLE | VM_FLAGS_ANYWHERE); michael@0: if (ret == KERN_SUCCESS) { michael@0: return true; michael@0: } michael@0: michael@0: heap_alloc: michael@0: (void)moz_posix_memalign(&mBuf, aAlignment, aSize); michael@0: mHeap = true; michael@0: return !!mBuf; michael@0: } michael@0: michael@0: VolatileBuffer::~VolatileBuffer() michael@0: { michael@0: if (OnHeap()) { michael@0: free(mBuf); michael@0: } else { michael@0: vm_deallocate(mach_task_self(), (vm_address_t)mBuf, mSize); michael@0: } michael@0: } michael@0: michael@0: bool michael@0: VolatileBuffer::Lock(void** aBuf) michael@0: { michael@0: MOZ_ASSERT(mBuf, "Attempting to lock an uninitialized VolatileBuffer"); michael@0: michael@0: *aBuf = mBuf; michael@0: if (++mLockCount > 1 || OnHeap()) { michael@0: return true; michael@0: } michael@0: michael@0: int state = VM_PURGABLE_NONVOLATILE; michael@0: kern_return_t ret = michael@0: vm_purgable_control(mach_task_self(), michael@0: (vm_address_t)mBuf, michael@0: VM_PURGABLE_SET_STATE, michael@0: &state); michael@0: return ret == KERN_SUCCESS && !(state & VM_PURGABLE_EMPTY); michael@0: } michael@0: michael@0: void michael@0: VolatileBuffer::Unlock() michael@0: { michael@0: MOZ_ASSERT(mLockCount > 0, "VolatileBuffer unlocked too many times!"); michael@0: if (--mLockCount || OnHeap()) { michael@0: return; michael@0: } michael@0: michael@0: int state = VM_PURGABLE_VOLATILE | VM_VOLATILE_GROUP_DEFAULT; michael@0: DebugOnly ret = michael@0: vm_purgable_control(mach_task_self(), michael@0: (vm_address_t)mBuf, michael@0: VM_PURGABLE_SET_STATE, michael@0: &state); michael@0: MOZ_ASSERT(ret == KERN_SUCCESS, "Failed to set buffer as purgable"); michael@0: } michael@0: michael@0: bool michael@0: VolatileBuffer::OnHeap() const michael@0: { michael@0: return mHeap; michael@0: } michael@0: michael@0: size_t michael@0: VolatileBuffer::HeapSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const michael@0: { michael@0: return OnHeap() ? aMallocSizeOf(mBuf) : 0; michael@0: } michael@0: michael@0: size_t michael@0: VolatileBuffer::NonHeapSizeOfExcludingThis() const michael@0: { michael@0: if (OnHeap()) { michael@0: return 0; michael@0: } michael@0: michael@0: unsigned long pagemask = getpagesize() - 1; michael@0: return (mSize + pagemask) & ~pagemask; michael@0: } michael@0: michael@0: } // namespace mozilla