Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "VolatileBuffer.h" |
michael@0 | 6 | #include "mozilla/Assertions.h" |
michael@0 | 7 | #include "mozilla/NullPtr.h" |
michael@0 | 8 | #include "mozilla/mozalloc.h" |
michael@0 | 9 | |
michael@0 | 10 | #ifdef MOZ_MEMORY |
michael@0 | 11 | int posix_memalign(void** memptr, size_t alignment, size_t size); |
michael@0 | 12 | #endif |
michael@0 | 13 | |
michael@0 | 14 | namespace mozilla { |
michael@0 | 15 | |
michael@0 | 16 | VolatileBuffer::VolatileBuffer() |
michael@0 | 17 | : mBuf(nullptr) |
michael@0 | 18 | , mSize(0) |
michael@0 | 19 | , mLockCount(0) |
michael@0 | 20 | { |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | bool VolatileBuffer::Init(size_t aSize, size_t aAlignment) |
michael@0 | 24 | { |
michael@0 | 25 | MOZ_ASSERT(!mSize && !mBuf, "Init called twice"); |
michael@0 | 26 | MOZ_ASSERT(!(aAlignment % sizeof(void *)), |
michael@0 | 27 | "Alignment must be multiple of pointer size"); |
michael@0 | 28 | |
michael@0 | 29 | mSize = aSize; |
michael@0 | 30 | #if defined(MOZ_MEMORY) |
michael@0 | 31 | posix_memalign(&mBuf, aAlignment, aSize); |
michael@0 | 32 | #elif defined(HAVE_POSIX_MEMALIGN) |
michael@0 | 33 | (void)moz_posix_memalign(&mBuf, aAlignment, aSize); |
michael@0 | 34 | #else |
michael@0 | 35 | #error "No memalign implementation found" |
michael@0 | 36 | #endif |
michael@0 | 37 | return !!mBuf; |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | VolatileBuffer::~VolatileBuffer() |
michael@0 | 41 | { |
michael@0 | 42 | free(mBuf); |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | bool |
michael@0 | 46 | VolatileBuffer::Lock(void** aBuf) |
michael@0 | 47 | { |
michael@0 | 48 | MOZ_ASSERT(mBuf, "Attempting to lock an uninitialized VolatileBuffer"); |
michael@0 | 49 | |
michael@0 | 50 | *aBuf = mBuf; |
michael@0 | 51 | mLockCount++; |
michael@0 | 52 | |
michael@0 | 53 | return true; |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | void |
michael@0 | 57 | VolatileBuffer::Unlock() |
michael@0 | 58 | { |
michael@0 | 59 | mLockCount--; |
michael@0 | 60 | MOZ_ASSERT(mLockCount >= 0, "VolatileBuffer unlocked too many times!"); |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | bool |
michael@0 | 64 | VolatileBuffer::OnHeap() const |
michael@0 | 65 | { |
michael@0 | 66 | return true; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | size_t |
michael@0 | 70 | VolatileBuffer::HeapSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const |
michael@0 | 71 | { |
michael@0 | 72 | return aMallocSizeOf(mBuf); |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | size_t |
michael@0 | 76 | VolatileBuffer::NonHeapSizeOfExcludingThis() const |
michael@0 | 77 | { |
michael@0 | 78 | return 0; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | } // namespace mozilla |