memory/mozalloc/VolatileBufferAshmem.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial