memory/mozalloc/VolatileBuffer.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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 #ifndef mozalloc_VolatileBuffer_h
michael@0 6 #define mozalloc_VolatileBuffer_h
michael@0 7
michael@0 8 #include "mozilla/mozalloc.h"
michael@0 9 #include "mozilla/RefPtr.h"
michael@0 10 #include "mozilla/MemoryReporting.h"
michael@0 11 #include "mozilla/NullPtr.h"
michael@0 12
michael@0 13 /* VolatileBuffer
michael@0 14 *
michael@0 15 * This class represents a piece of memory that can potentially be reclaimed
michael@0 16 * by the OS when not in use. As long as there are one or more
michael@0 17 * VolatileBufferPtrs holding on to a VolatileBuffer, the memory will remain
michael@0 18 * available. However, when there are no VolatileBufferPtrs holding a
michael@0 19 * VolatileBuffer, the OS can purge the pages if it wants to. The OS can make
michael@0 20 * better decisions about what pages to purge than we can.
michael@0 21 *
michael@0 22 * VolatileBuffers may not always be volatile - if the allocation is too small,
michael@0 23 * or if the OS doesn't support the feature, or if the OS doesn't want to,
michael@0 24 * the buffer will be allocated on heap.
michael@0 25 *
michael@0 26 * VolatileBuffer allocations are fallible. They are intended for uses where
michael@0 27 * one may allocate large buffers for caching data. Init() must be called
michael@0 28 * exactly once.
michael@0 29 *
michael@0 30 * After getting a reference to VolatileBuffer using VolatileBufferPtr,
michael@0 31 * WasPurged() can be used to check if the OS purged any pages in the buffer.
michael@0 32 * The OS cannot purge a buffer immediately after a VolatileBuffer is
michael@0 33 * initialized. At least one VolatileBufferPtr must be created before the
michael@0 34 * buffer can be purged, so the first use of VolatileBufferPtr does not need
michael@0 35 * to check WasPurged().
michael@0 36 *
michael@0 37 * When a buffer is purged, some or all of the buffer is zeroed out. This
michael@0 38 * API cannot tell which parts of the buffer were lost.
michael@0 39 *
michael@0 40 * VolatileBuffer is not thread safe. Do not use VolatileBufferPtrs on
michael@0 41 * different threads.
michael@0 42 */
michael@0 43
michael@0 44 namespace mozilla {
michael@0 45
michael@0 46 class MOZALLOC_EXPORT VolatileBuffer : public RefCounted<VolatileBuffer>
michael@0 47 {
michael@0 48 friend class VolatileBufferPtr_base;
michael@0 49 public:
michael@0 50 MOZ_DECLARE_REFCOUNTED_TYPENAME(VolatileBuffer)
michael@0 51 VolatileBuffer();
michael@0 52 ~VolatileBuffer();
michael@0 53
michael@0 54 /* aAlignment must be a multiple of the pointer size */
michael@0 55 bool Init(size_t aSize, size_t aAlignment = sizeof(void*));
michael@0 56
michael@0 57 size_t HeapSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const;
michael@0 58 size_t NonHeapSizeOfExcludingThis() const;
michael@0 59 bool OnHeap() const;
michael@0 60
michael@0 61 protected:
michael@0 62 bool Lock(void** aBuf);
michael@0 63 void Unlock();
michael@0 64
michael@0 65 private:
michael@0 66 void* mBuf;
michael@0 67 size_t mSize;
michael@0 68 int mLockCount;
michael@0 69 #if defined(ANDROID)
michael@0 70 int mFd;
michael@0 71 #elif defined(XP_DARWIN)
michael@0 72 bool mHeap;
michael@0 73 #elif defined(XP_WIN)
michael@0 74 bool mHeap;
michael@0 75 bool mFirstLock;
michael@0 76 #endif
michael@0 77 };
michael@0 78
michael@0 79 class VolatileBufferPtr_base {
michael@0 80 public:
michael@0 81 VolatileBufferPtr_base(VolatileBuffer* vbuf) : mVBuf(vbuf) {
michael@0 82 if (vbuf) {
michael@0 83 mPurged = !vbuf->Lock(&mMapping);
michael@0 84 } else {
michael@0 85 mMapping = nullptr;
michael@0 86 mPurged = false;
michael@0 87 }
michael@0 88 }
michael@0 89
michael@0 90 ~VolatileBufferPtr_base() {
michael@0 91 if (mVBuf) {
michael@0 92 mVBuf->Unlock();
michael@0 93 }
michael@0 94 }
michael@0 95
michael@0 96 bool WasBufferPurged() const {
michael@0 97 return mPurged;
michael@0 98 }
michael@0 99
michael@0 100 protected:
michael@0 101 void* mMapping;
michael@0 102
michael@0 103 private:
michael@0 104 RefPtr<VolatileBuffer> mVBuf;
michael@0 105 bool mPurged;
michael@0 106 };
michael@0 107
michael@0 108 template <class T>
michael@0 109 class VolatileBufferPtr : public VolatileBufferPtr_base
michael@0 110 {
michael@0 111 public:
michael@0 112 VolatileBufferPtr(VolatileBuffer* vbuf) : VolatileBufferPtr_base(vbuf) {}
michael@0 113
michael@0 114 operator T*() const {
michael@0 115 return (T*) mMapping;
michael@0 116 }
michael@0 117 };
michael@0 118
michael@0 119 }; /* namespace mozilla */
michael@0 120
michael@0 121 #endif /* mozalloc_VolatileBuffer_h */

mercurial