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: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: #ifdef MOZ_MEMORY michael@0: #ifdef MOZ_WIDGET_ANDROID michael@0: extern "C" int __wrap_posix_memalign(void** memptr, size_t alignment, size_t size); michael@0: #else michael@0: extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size); michael@0: #endif 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: , mFd(-1) 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: mFd = open("/" ASHMEM_NAME_DEF, O_RDWR); michael@0: if (mFd < 0) { michael@0: goto heap_alloc; michael@0: } michael@0: michael@0: if (ioctl(mFd, ASHMEM_SET_SIZE, mSize) < 0) { michael@0: close(mFd); michael@0: mFd = -1; michael@0: goto heap_alloc; michael@0: } michael@0: michael@0: mBuf = mmap(nullptr, mSize, PROT_READ | PROT_WRITE, MAP_SHARED, mFd, 0); michael@0: if (mBuf != MAP_FAILED) { michael@0: return true; michael@0: } michael@0: michael@0: heap_alloc: michael@0: #ifdef MOZ_MEMORY michael@0: #ifdef MOZ_WIDGET_ANDROID michael@0: __wrap_posix_memalign(&mBuf, aAlignment, aSize); michael@0: #else michael@0: posix_memalign(&mBuf, aAlignment, aSize); michael@0: #endif michael@0: #else michael@0: mBuf = memalign(aAlignment, aSize); michael@0: #endif 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: munmap(mBuf, mSize); michael@0: close(mFd); 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: // Zero offset and zero length means we want to pin/unpin the entire thing. michael@0: struct ashmem_pin pin = { 0, 0 }; michael@0: return ioctl(mFd, ASHMEM_PIN, &pin) == ASHMEM_NOT_PURGED; 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: struct ashmem_pin pin = { 0, 0 }; michael@0: ioctl(mFd, ASHMEM_UNPIN, &pin); michael@0: } michael@0: michael@0: bool michael@0: VolatileBuffer::OnHeap() const michael@0: { michael@0: return mFd < 0; 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: return (mSize + (PAGE_SIZE - 1)) & PAGE_MASK; michael@0: } michael@0: michael@0: } // namespace mozilla