1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "VolatileBuffer.h"
6 #include "mozilla/Assertions.h"
7 #include "mozilla/mozalloc.h"
10 int posix_memalign(void** memptr
, size_t alignment
, size_t size
);
15 VolatileBuffer::VolatileBuffer()
16 : mMutex("VolatileBuffer"), mBuf(nullptr), mSize(0), mLockCount(0) {}
18 bool VolatileBuffer::Init(size_t aSize
, size_t aAlignment
) {
19 MOZ_ASSERT(!mSize
&& !mBuf
, "Init called twice");
20 MOZ_ASSERT(!(aAlignment
% sizeof(void*)),
21 "Alignment must be multiple of pointer size");
24 #if defined(MOZ_MEMORY) || defined(HAVE_POSIX_MEMALIGN)
25 if (posix_memalign(&mBuf
, aAlignment
, aSize
) != 0) {
29 # error "No memalign implementation found"
34 VolatileBuffer::~VolatileBuffer() {
35 MOZ_ASSERT(mLockCount
== 0, "Being destroyed with non-zero lock count?");
40 bool VolatileBuffer::Lock(void** aBuf
) {
41 MutexAutoLock
lock(mMutex
);
43 MOZ_ASSERT(mBuf
, "Attempting to lock an uninitialized VolatileBuffer");
51 void VolatileBuffer::Unlock() {
52 MutexAutoLock
lock(mMutex
);
55 MOZ_ASSERT(mLockCount
>= 0, "VolatileBuffer unlocked too many times!");
58 bool VolatileBuffer::OnHeap() const { return true; }
60 size_t VolatileBuffer::HeapSizeOfExcludingThis(
61 MallocSizeOf aMallocSizeOf
) const {
62 return aMallocSizeOf(mBuf
);
65 size_t VolatileBuffer::NonHeapSizeOfExcludingThis() const { return 0; }
67 } // namespace mozilla