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: #if defined(XP_WIN) michael@0: # define MOZALLOC_EXPORT __declspec(dllexport) michael@0: #endif michael@0: michael@0: #include "VolatileBuffer.h" michael@0: #include "mozilla/Assertions.h" michael@0: #include "mozilla/mozalloc.h" michael@0: #include "mozilla/WindowsVersion.h" michael@0: michael@0: #include michael@0: michael@0: #ifdef MOZ_MEMORY michael@0: extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size); michael@0: #endif michael@0: michael@0: #ifndef MEM_RESET_UNDO michael@0: #define MEM_RESET_UNDO 0x1000000 michael@0: #endif 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: , mFirstLock(true) 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: if (aSize < MIN_VOLATILE_ALLOC_SIZE) { michael@0: goto heap_alloc; michael@0: } michael@0: michael@0: static bool sUndoSupported = IsWin8OrLater(); michael@0: if (!sUndoSupported) { michael@0: goto heap_alloc; michael@0: } michael@0: michael@0: mBuf = VirtualAllocEx(GetCurrentProcess(), michael@0: nullptr, michael@0: mSize, michael@0: MEM_COMMIT | MEM_RESERVE, michael@0: PAGE_READWRITE); michael@0: if (mBuf) { michael@0: return true; michael@0: } michael@0: michael@0: heap_alloc: michael@0: #ifdef MOZ_MEMORY michael@0: posix_memalign(&mBuf, aAlignment, aSize); michael@0: #else michael@0: mBuf = _aligned_malloc(aSize, aAlignment); michael@0: #endif michael@0: mHeap = true; michael@0: return !!mBuf; michael@0: } michael@0: michael@0: VolatileBuffer::~VolatileBuffer() michael@0: { michael@0: if (OnHeap()) { michael@0: #ifdef MOZ_MEMORY michael@0: free(mBuf); michael@0: #else michael@0: _aligned_free(mBuf); michael@0: #endif michael@0: } else { michael@0: VirtualFreeEx(GetCurrentProcess(), mBuf, 0, MEM_RELEASE); 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: // MEM_RESET_UNDO's behavior is undefined when called on memory that michael@0: // hasn't been MEM_RESET. michael@0: if (mFirstLock) { michael@0: mFirstLock = false; michael@0: return true; michael@0: } michael@0: michael@0: void* addr = VirtualAllocEx(GetCurrentProcess(), michael@0: mBuf, michael@0: mSize, michael@0: MEM_RESET_UNDO, michael@0: PAGE_READWRITE); michael@0: return !!addr; 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: void* addr = VirtualAllocEx(GetCurrentProcess(), michael@0: mBuf, michael@0: mSize, michael@0: MEM_RESET, michael@0: PAGE_READWRITE); michael@0: MOZ_ASSERT(addr, "Failed to MEM_RESET"); 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: if (OnHeap()) { michael@0: #ifdef MOZ_MEMORY michael@0: return aMallocSizeOf(mBuf); michael@0: #else michael@0: return mSize; michael@0: #endif michael@0: } michael@0: michael@0: return 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: return (mSize + 4095) & ~4095; michael@0: } michael@0: michael@0: } // namespace mozilla