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