LCOV - code coverage report
Current view: top level - memory/volatile - VolatileBufferFallback.cpp (source / functions) Hit Total Coverage
Test: output.info Lines: 17 31 54.8 %
Date: 2017-07-14 16:53:18 Functions: 4 8 50.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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/. */
       4             : 
       5             : #include "VolatileBuffer.h"
       6             : #include "mozilla/Assertions.h"
       7             : #include "mozilla/mozalloc.h"
       8             : 
       9             : #ifdef MOZ_MEMORY
      10             : int posix_memalign(void** memptr, size_t alignment, size_t size);
      11             : #endif
      12             : 
      13             : namespace mozilla {
      14             : 
      15          50 : VolatileBuffer::VolatileBuffer()
      16             :   : mMutex("VolatileBuffer")
      17             :   , mBuf(nullptr)
      18             :   , mSize(0)
      19          50 :   , mLockCount(0)
      20             : {
      21          50 : }
      22             : 
      23          50 : bool VolatileBuffer::Init(size_t aSize, size_t aAlignment)
      24             : {
      25          50 :   MOZ_ASSERT(!mSize && !mBuf, "Init called twice");
      26          50 :   MOZ_ASSERT(!(aAlignment % sizeof(void *)),
      27             :              "Alignment must be multiple of pointer size");
      28             : 
      29          50 :   mSize = aSize;
      30             : #if defined(MOZ_MEMORY)
      31          50 :   if (posix_memalign(&mBuf, aAlignment, aSize) != 0) {
      32           0 :     return false;
      33             :   }
      34             : #elif defined(HAVE_POSIX_MEMALIGN)
      35             :   if (moz_posix_memalign(&mBuf, aAlignment, aSize) != 0) {
      36             :     return false;
      37             :   }
      38             : #else
      39             : #error "No memalign implementation found"
      40             : #endif
      41          50 :   return !!mBuf;
      42             : }
      43             : 
      44           0 : VolatileBuffer::~VolatileBuffer()
      45             : {
      46           0 :   MOZ_ASSERT(mLockCount == 0, "Being destroyed with non-zero lock count?");
      47             : 
      48           0 :   free(mBuf);
      49           0 : }
      50             : 
      51             : bool
      52          50 : VolatileBuffer::Lock(void** aBuf)
      53             : {
      54         100 :   MutexAutoLock lock(mMutex);
      55             : 
      56          50 :   MOZ_ASSERT(mBuf, "Attempting to lock an uninitialized VolatileBuffer");
      57             : 
      58          50 :   *aBuf = mBuf;
      59          50 :   mLockCount++;
      60             : 
      61         100 :   return true;
      62             : }
      63             : 
      64             : void
      65           0 : VolatileBuffer::Unlock()
      66             : {
      67           0 :   MutexAutoLock lock(mMutex);
      68             : 
      69           0 :   mLockCount--;
      70           0 :   MOZ_ASSERT(mLockCount >= 0, "VolatileBuffer unlocked too many times!");
      71           0 : }
      72             : 
      73             : bool
      74          50 : VolatileBuffer::OnHeap() const
      75             : {
      76          50 :   return true;
      77             : }
      78             : 
      79             : size_t
      80           0 : VolatileBuffer::HeapSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const
      81             : {
      82           0 :   return aMallocSizeOf(mBuf);
      83             : }
      84             : 
      85             : size_t
      86           0 : VolatileBuffer::NonHeapSizeOfExcludingThis() const
      87             : {
      88           0 :   return 0;
      89             : }
      90             : 
      91             : } // namespace mozilla

Generated by: LCOV version 1.13