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/DebugOnly.h"
8 #include "mozilla/mozalloc.h"
10 #include <mach/mach.h>
14 #define MIN_VOLATILE_ALLOC_SIZE 8192
18 VolatileBuffer::VolatileBuffer()
19 : mMutex("VolatileBuffer"),
25 bool VolatileBuffer::Init(size_t aSize
, size_t aAlignment
) {
26 MOZ_ASSERT(!mSize
&& !mBuf
, "Init called twice");
27 MOZ_ASSERT(!(aAlignment
% sizeof(void*)),
28 "Alignment must be multiple of pointer size");
32 kern_return_t ret
= 0;
33 if (aSize
< MIN_VOLATILE_ALLOC_SIZE
) {
37 ret
= vm_allocate(mach_task_self(), (vm_address_t
*)&mBuf
, mSize
,
38 VM_FLAGS_PURGABLE
| VM_FLAGS_ANYWHERE
);
39 if (ret
== KERN_SUCCESS
) {
44 (void)posix_memalign(&mBuf
, aAlignment
, aSize
);
49 VolatileBuffer::~VolatileBuffer() {
50 MOZ_ASSERT(mLockCount
== 0, "Being destroyed with non-zero lock count?");
55 vm_deallocate(mach_task_self(), (vm_address_t
)mBuf
, mSize
);
59 bool VolatileBuffer::Lock(void** aBuf
) {
60 MutexAutoLock
lock(mMutex
);
62 MOZ_ASSERT(mBuf
, "Attempting to lock an uninitialized VolatileBuffer");
65 if (++mLockCount
> 1 || OnHeap()) {
69 int state
= VM_PURGABLE_NONVOLATILE
;
70 kern_return_t ret
= vm_purgable_control(mach_task_self(), (vm_address_t
)mBuf
,
71 VM_PURGABLE_SET_STATE
, &state
);
72 return ret
== KERN_SUCCESS
&& !(state
& VM_PURGABLE_EMPTY
);
75 void VolatileBuffer::Unlock() {
76 MutexAutoLock
lock(mMutex
);
78 MOZ_ASSERT(mLockCount
> 0, "VolatileBuffer unlocked too many times!");
79 if (--mLockCount
|| OnHeap()) {
83 int state
= VM_PURGABLE_VOLATILE
| VM_VOLATILE_GROUP_DEFAULT
;
84 DebugOnly
<kern_return_t
> ret
= vm_purgable_control(
85 mach_task_self(), (vm_address_t
)mBuf
, VM_PURGABLE_SET_STATE
, &state
);
86 MOZ_ASSERT(ret
== KERN_SUCCESS
, "Failed to set buffer as purgable");
89 bool VolatileBuffer::OnHeap() const { return mHeap
; }
91 size_t VolatileBuffer::HeapSizeOfExcludingThis(
92 MallocSizeOf aMallocSizeOf
) const {
93 return OnHeap() ? aMallocSizeOf(mBuf
) : 0;
96 size_t VolatileBuffer::NonHeapSizeOfExcludingThis() const {
101 unsigned long pagemask
= getpagesize() - 1;
102 return (mSize
+ pagemask
) & ~pagemask
;
105 } // namespace mozilla