|
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 #include <fcntl.h> |
|
11 #include <linux/ashmem.h> |
|
12 #include <sys/mman.h> |
|
13 #include <sys/stat.h> |
|
14 #include <sys/types.h> |
|
15 #include <unistd.h> |
|
16 |
|
17 #ifdef MOZ_MEMORY |
|
18 #ifdef MOZ_WIDGET_ANDROID |
|
19 extern "C" int __wrap_posix_memalign(void** memptr, size_t alignment, size_t size); |
|
20 #else |
|
21 extern "C" int posix_memalign(void** memptr, size_t alignment, size_t size); |
|
22 #endif |
|
23 #endif |
|
24 |
|
25 #define MIN_VOLATILE_ALLOC_SIZE 8192 |
|
26 |
|
27 namespace mozilla { |
|
28 |
|
29 VolatileBuffer::VolatileBuffer() |
|
30 : mBuf(nullptr) |
|
31 , mSize(0) |
|
32 , mLockCount(0) |
|
33 , mFd(-1) |
|
34 { |
|
35 } |
|
36 |
|
37 bool |
|
38 VolatileBuffer::Init(size_t aSize, size_t aAlignment) |
|
39 { |
|
40 MOZ_ASSERT(!mSize && !mBuf, "Init called twice"); |
|
41 MOZ_ASSERT(!(aAlignment % sizeof(void *)), |
|
42 "Alignment must be multiple of pointer size"); |
|
43 |
|
44 mSize = aSize; |
|
45 if (aSize < MIN_VOLATILE_ALLOC_SIZE) { |
|
46 goto heap_alloc; |
|
47 } |
|
48 |
|
49 mFd = open("/" ASHMEM_NAME_DEF, O_RDWR); |
|
50 if (mFd < 0) { |
|
51 goto heap_alloc; |
|
52 } |
|
53 |
|
54 if (ioctl(mFd, ASHMEM_SET_SIZE, mSize) < 0) { |
|
55 close(mFd); |
|
56 mFd = -1; |
|
57 goto heap_alloc; |
|
58 } |
|
59 |
|
60 mBuf = mmap(nullptr, mSize, PROT_READ | PROT_WRITE, MAP_SHARED, mFd, 0); |
|
61 if (mBuf != MAP_FAILED) { |
|
62 return true; |
|
63 } |
|
64 |
|
65 heap_alloc: |
|
66 #ifdef MOZ_MEMORY |
|
67 #ifdef MOZ_WIDGET_ANDROID |
|
68 __wrap_posix_memalign(&mBuf, aAlignment, aSize); |
|
69 #else |
|
70 posix_memalign(&mBuf, aAlignment, aSize); |
|
71 #endif |
|
72 #else |
|
73 mBuf = memalign(aAlignment, aSize); |
|
74 #endif |
|
75 return !!mBuf; |
|
76 } |
|
77 |
|
78 VolatileBuffer::~VolatileBuffer() |
|
79 { |
|
80 if (OnHeap()) { |
|
81 free(mBuf); |
|
82 } else { |
|
83 munmap(mBuf, mSize); |
|
84 close(mFd); |
|
85 } |
|
86 } |
|
87 |
|
88 bool |
|
89 VolatileBuffer::Lock(void** aBuf) |
|
90 { |
|
91 MOZ_ASSERT(mBuf, "Attempting to lock an uninitialized VolatileBuffer"); |
|
92 |
|
93 *aBuf = mBuf; |
|
94 if (++mLockCount > 1 || OnHeap()) { |
|
95 return true; |
|
96 } |
|
97 |
|
98 // Zero offset and zero length means we want to pin/unpin the entire thing. |
|
99 struct ashmem_pin pin = { 0, 0 }; |
|
100 return ioctl(mFd, ASHMEM_PIN, &pin) == ASHMEM_NOT_PURGED; |
|
101 } |
|
102 |
|
103 void |
|
104 VolatileBuffer::Unlock() |
|
105 { |
|
106 MOZ_ASSERT(mLockCount > 0, "VolatileBuffer unlocked too many times!"); |
|
107 if (--mLockCount || OnHeap()) { |
|
108 return; |
|
109 } |
|
110 |
|
111 struct ashmem_pin pin = { 0, 0 }; |
|
112 ioctl(mFd, ASHMEM_UNPIN, &pin); |
|
113 } |
|
114 |
|
115 bool |
|
116 VolatileBuffer::OnHeap() const |
|
117 { |
|
118 return mFd < 0; |
|
119 } |
|
120 |
|
121 size_t |
|
122 VolatileBuffer::HeapSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const |
|
123 { |
|
124 return OnHeap() ? aMallocSizeOf(mBuf) : 0; |
|
125 } |
|
126 |
|
127 size_t |
|
128 VolatileBuffer::NonHeapSizeOfExcludingThis() const |
|
129 { |
|
130 if (OnHeap()) { |
|
131 return 0; |
|
132 } |
|
133 |
|
134 return (mSize + (PAGE_SIZE - 1)) & PAGE_MASK; |
|
135 } |
|
136 |
|
137 } // namespace mozilla |