|
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/DebugOnly.h" |
|
8 #include "mozilla/mozalloc.h" |
|
9 |
|
10 #include <mach/mach.h> |
|
11 #include <sys/mman.h> |
|
12 #include <unistd.h> |
|
13 |
|
14 #define MIN_VOLATILE_ALLOC_SIZE 8192 |
|
15 |
|
16 namespace mozilla { |
|
17 |
|
18 VolatileBuffer::VolatileBuffer() |
|
19 : mBuf(nullptr) |
|
20 , mSize(0) |
|
21 , mLockCount(0) |
|
22 , mHeap(false) |
|
23 { |
|
24 } |
|
25 |
|
26 bool |
|
27 VolatileBuffer::Init(size_t aSize, size_t aAlignment) |
|
28 { |
|
29 MOZ_ASSERT(!mSize && !mBuf, "Init called twice"); |
|
30 MOZ_ASSERT(!(aAlignment % sizeof(void *)), |
|
31 "Alignment must be multiple of pointer size"); |
|
32 |
|
33 mSize = aSize; |
|
34 |
|
35 kern_return_t ret = 0; |
|
36 if (aSize < MIN_VOLATILE_ALLOC_SIZE) { |
|
37 goto heap_alloc; |
|
38 } |
|
39 |
|
40 ret = vm_allocate(mach_task_self(), |
|
41 (vm_address_t*)&mBuf, |
|
42 mSize, |
|
43 VM_FLAGS_PURGABLE | VM_FLAGS_ANYWHERE); |
|
44 if (ret == KERN_SUCCESS) { |
|
45 return true; |
|
46 } |
|
47 |
|
48 heap_alloc: |
|
49 (void)moz_posix_memalign(&mBuf, aAlignment, aSize); |
|
50 mHeap = true; |
|
51 return !!mBuf; |
|
52 } |
|
53 |
|
54 VolatileBuffer::~VolatileBuffer() |
|
55 { |
|
56 if (OnHeap()) { |
|
57 free(mBuf); |
|
58 } else { |
|
59 vm_deallocate(mach_task_self(), (vm_address_t)mBuf, mSize); |
|
60 } |
|
61 } |
|
62 |
|
63 bool |
|
64 VolatileBuffer::Lock(void** aBuf) |
|
65 { |
|
66 MOZ_ASSERT(mBuf, "Attempting to lock an uninitialized VolatileBuffer"); |
|
67 |
|
68 *aBuf = mBuf; |
|
69 if (++mLockCount > 1 || OnHeap()) { |
|
70 return true; |
|
71 } |
|
72 |
|
73 int state = VM_PURGABLE_NONVOLATILE; |
|
74 kern_return_t ret = |
|
75 vm_purgable_control(mach_task_self(), |
|
76 (vm_address_t)mBuf, |
|
77 VM_PURGABLE_SET_STATE, |
|
78 &state); |
|
79 return ret == KERN_SUCCESS && !(state & VM_PURGABLE_EMPTY); |
|
80 } |
|
81 |
|
82 void |
|
83 VolatileBuffer::Unlock() |
|
84 { |
|
85 MOZ_ASSERT(mLockCount > 0, "VolatileBuffer unlocked too many times!"); |
|
86 if (--mLockCount || OnHeap()) { |
|
87 return; |
|
88 } |
|
89 |
|
90 int state = VM_PURGABLE_VOLATILE | VM_VOLATILE_GROUP_DEFAULT; |
|
91 DebugOnly<kern_return_t> ret = |
|
92 vm_purgable_control(mach_task_self(), |
|
93 (vm_address_t)mBuf, |
|
94 VM_PURGABLE_SET_STATE, |
|
95 &state); |
|
96 MOZ_ASSERT(ret == KERN_SUCCESS, "Failed to set buffer as purgable"); |
|
97 } |
|
98 |
|
99 bool |
|
100 VolatileBuffer::OnHeap() const |
|
101 { |
|
102 return mHeap; |
|
103 } |
|
104 |
|
105 size_t |
|
106 VolatileBuffer::HeapSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const |
|
107 { |
|
108 return OnHeap() ? aMallocSizeOf(mBuf) : 0; |
|
109 } |
|
110 |
|
111 size_t |
|
112 VolatileBuffer::NonHeapSizeOfExcludingThis() const |
|
113 { |
|
114 if (OnHeap()) { |
|
115 return 0; |
|
116 } |
|
117 |
|
118 unsigned long pagemask = getpagesize() - 1; |
|
119 return (mSize + pagemask) & ~pagemask; |
|
120 } |
|
121 |
|
122 } // namespace mozilla |