2014-01-22 17:23:27 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef mozalloc_VolatileBuffer_h
|
|
|
|
#define mozalloc_VolatileBuffer_h
|
|
|
|
|
|
|
|
#include "mozilla/mozalloc.h"
|
2015-01-17 02:47:35 +03:00
|
|
|
#include "mozilla/Mutex.h"
|
2015-10-18 08:24:48 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2014-01-22 17:23:27 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2015-09-23 04:27:34 +03:00
|
|
|
#include "mozilla/RefCounted.h"
|
2014-01-22 17:23:27 +04:00
|
|
|
|
|
|
|
/* VolatileBuffer
|
|
|
|
*
|
|
|
|
* This class represents a piece of memory that can potentially be reclaimed
|
|
|
|
* by the OS when not in use. As long as there are one or more
|
|
|
|
* VolatileBufferPtrs holding on to a VolatileBuffer, the memory will remain
|
|
|
|
* available. However, when there are no VolatileBufferPtrs holding a
|
|
|
|
* VolatileBuffer, the OS can purge the pages if it wants to. The OS can make
|
|
|
|
* better decisions about what pages to purge than we can.
|
|
|
|
*
|
|
|
|
* VolatileBuffers may not always be volatile - if the allocation is too small,
|
|
|
|
* or if the OS doesn't support the feature, or if the OS doesn't want to,
|
|
|
|
* the buffer will be allocated on heap.
|
|
|
|
*
|
|
|
|
* VolatileBuffer allocations are fallible. They are intended for uses where
|
|
|
|
* one may allocate large buffers for caching data. Init() must be called
|
|
|
|
* exactly once.
|
|
|
|
*
|
|
|
|
* After getting a reference to VolatileBuffer using VolatileBufferPtr,
|
|
|
|
* WasPurged() can be used to check if the OS purged any pages in the buffer.
|
|
|
|
* The OS cannot purge a buffer immediately after a VolatileBuffer is
|
|
|
|
* initialized. At least one VolatileBufferPtr must be created before the
|
|
|
|
* buffer can be purged, so the first use of VolatileBufferPtr does not need
|
|
|
|
* to check WasPurged().
|
|
|
|
*
|
|
|
|
* When a buffer is purged, some or all of the buffer is zeroed out. This
|
|
|
|
* API cannot tell which parts of the buffer were lost.
|
|
|
|
*
|
2015-01-17 02:47:35 +03:00
|
|
|
* VolatileBuffer and VolatileBufferPtr are threadsafe.
|
2014-01-22 17:23:27 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
2015-01-17 02:47:35 +03:00
|
|
|
class VolatileBuffer {
|
2014-01-22 17:23:27 +04:00
|
|
|
friend class VolatileBufferPtr_base;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-01-22 17:23:27 +04:00
|
|
|
public:
|
2014-02-21 18:11:33 +04:00
|
|
|
MOZ_DECLARE_REFCOUNTED_TYPENAME(VolatileBuffer)
|
2015-01-17 02:47:35 +03:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(VolatileBuffer)
|
|
|
|
|
2014-01-22 17:23:27 +04:00
|
|
|
VolatileBuffer();
|
|
|
|
|
|
|
|
/* aAlignment must be a multiple of the pointer size */
|
|
|
|
bool Init(size_t aSize, size_t aAlignment = sizeof(void*));
|
|
|
|
|
|
|
|
size_t HeapSizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const;
|
|
|
|
size_t NonHeapSizeOfExcludingThis() const;
|
2014-08-19 23:30:55 +04:00
|
|
|
bool OnHeap() const;
|
2014-01-22 17:23:27 +04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
bool Lock(void** aBuf);
|
|
|
|
void Unlock();
|
|
|
|
|
|
|
|
private:
|
2015-01-17 02:47:35 +03:00
|
|
|
~VolatileBuffer();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Protects mLockCount, mFirstLock, and changes to the volatility of our
|
|
|
|
* buffer. Other member variables are read-only except in Init() and the
|
|
|
|
* destructor.
|
|
|
|
*/
|
|
|
|
Mutex mMutex;
|
|
|
|
|
2014-01-22 17:23:27 +04:00
|
|
|
void* mBuf;
|
|
|
|
size_t mSize;
|
|
|
|
int mLockCount;
|
|
|
|
#if defined(ANDROID)
|
|
|
|
int mFd;
|
|
|
|
#elif defined(XP_DARWIN)
|
|
|
|
bool mHeap;
|
|
|
|
#elif defined(XP_WIN)
|
|
|
|
bool mHeap;
|
|
|
|
bool mFirstLock;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
class VolatileBufferPtr_base {
|
|
|
|
public:
|
2016-01-12 21:16:59 +03:00
|
|
|
explicit VolatileBufferPtr_base(VolatileBuffer* vbuf)
|
|
|
|
: mVBuf(vbuf), mMapping(nullptr), mPurged(false) {
|
2014-04-18 20:26:49 +04:00
|
|
|
Lock();
|
2014-01-22 17:23:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
~VolatileBufferPtr_base() { Unlock(); }
|
|
|
|
|
|
|
|
bool WasBufferPurged() const { return mPurged; }
|
|
|
|
|
|
|
|
protected:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<VolatileBuffer> mVBuf;
|
2014-01-22 17:23:27 +04:00
|
|
|
void* mMapping;
|
|
|
|
|
2014-04-18 20:26:49 +04:00
|
|
|
void Set(VolatileBuffer* vbuf) {
|
|
|
|
Unlock();
|
|
|
|
mVBuf = vbuf;
|
|
|
|
Lock();
|
|
|
|
}
|
|
|
|
|
2014-01-22 17:23:27 +04:00
|
|
|
private:
|
|
|
|
bool mPurged;
|
2014-04-18 20:26:49 +04:00
|
|
|
|
|
|
|
void Lock() {
|
|
|
|
if (mVBuf) {
|
|
|
|
mPurged = !mVBuf->Lock(&mMapping);
|
|
|
|
} else {
|
|
|
|
mMapping = nullptr;
|
|
|
|
mPurged = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Unlock() {
|
|
|
|
if (mVBuf) {
|
|
|
|
mVBuf->Unlock();
|
|
|
|
}
|
|
|
|
}
|
2014-01-22 17:23:27 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
class VolatileBufferPtr : public VolatileBufferPtr_base {
|
|
|
|
public:
|
2014-05-26 05:25:33 +04:00
|
|
|
explicit VolatileBufferPtr(VolatileBuffer* vbuf)
|
|
|
|
: VolatileBufferPtr_base(vbuf) {}
|
2014-04-18 20:26:49 +04:00
|
|
|
VolatileBufferPtr() : VolatileBufferPtr_base(nullptr) {}
|
2014-01-22 17:23:27 +04:00
|
|
|
|
2014-09-11 04:06:37 +04:00
|
|
|
VolatileBufferPtr(VolatileBufferPtr&& aOther)
|
|
|
|
: VolatileBufferPtr_base(aOther.mVBuf) {
|
|
|
|
aOther.Set(nullptr);
|
|
|
|
}
|
|
|
|
|
2014-01-22 17:23:27 +04:00
|
|
|
operator T*() const { return (T*)mMapping; }
|
2014-04-18 20:26:49 +04:00
|
|
|
|
2014-09-11 04:06:37 +04:00
|
|
|
VolatileBufferPtr& operator=(VolatileBuffer* aVBuf) {
|
|
|
|
Set(aVBuf);
|
|
|
|
return *this;
|
2014-04-18 20:26:49 +04:00
|
|
|
}
|
2014-09-11 04:06:37 +04:00
|
|
|
|
|
|
|
VolatileBufferPtr& operator=(VolatileBufferPtr&& aOther) {
|
|
|
|
MOZ_ASSERT(this != &aOther, "Self-moves are prohibited");
|
|
|
|
Set(aOther.mVBuf);
|
|
|
|
aOther.Set(nullptr);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2014-04-18 20:26:49 +04:00
|
|
|
private:
|
2015-01-07 02:35:02 +03:00
|
|
|
VolatileBufferPtr(VolatileBufferPtr const& vbufptr) = delete;
|
2014-01-22 17:23:27 +04:00
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace mozilla
|
2014-01-22 17:23:27 +04:00
|
|
|
|
|
|
|
#endif /* mozalloc_VolatileBuffer_h */
|