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/NullPtr.h" michael@0: #include "mozilla/mozalloc.h" michael@0: michael@0: #ifdef MOZ_MEMORY michael@0: int posix_memalign(void** memptr, size_t alignment, size_t size); michael@0: #endif 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: { michael@0: } michael@0: michael@0: bool 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 defined(MOZ_MEMORY) michael@0: posix_memalign(&mBuf, aAlignment, aSize); michael@0: #elif defined(HAVE_POSIX_MEMALIGN) michael@0: (void)moz_posix_memalign(&mBuf, aAlignment, aSize); michael@0: #else michael@0: #error "No memalign implementation found" michael@0: #endif michael@0: return !!mBuf; michael@0: } michael@0: michael@0: VolatileBuffer::~VolatileBuffer() michael@0: { michael@0: free(mBuf); 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: mLockCount++; michael@0: michael@0: return true; michael@0: } michael@0: michael@0: void michael@0: VolatileBuffer::Unlock() michael@0: { michael@0: mLockCount--; michael@0: MOZ_ASSERT(mLockCount >= 0, "VolatileBuffer unlocked too many times!"); michael@0: } michael@0: michael@0: bool michael@0: VolatileBuffer::OnHeap() const michael@0: { michael@0: return true; michael@0: } michael@0: michael@0: size_t michael@0: VolatileBuffer::HeapSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const michael@0: { michael@0: return aMallocSizeOf(mBuf); michael@0: } michael@0: michael@0: size_t michael@0: VolatileBuffer::NonHeapSizeOfExcludingThis() const michael@0: { michael@0: return 0; michael@0: } michael@0: michael@0: } // namespace mozilla