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