2013-09-20 13:11:26 +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/. */
|
|
|
|
|
2013-10-15 22:08:26 +04:00
|
|
|
#include "CacheLog.h"
|
2013-09-20 13:11:26 +04:00
|
|
|
#include "CacheFileChunk.h"
|
|
|
|
|
|
|
|
#include "CacheFile.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
2016-12-16 06:16:31 +03:00
|
|
|
#include "mozilla/IntegerPrintfMacros.h"
|
|
|
|
|
2013-09-20 13:11:26 +04:00
|
|
|
namespace mozilla {
|
|
|
|
namespace net {
|
|
|
|
|
|
|
|
#define kMinBufSize 512
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
CacheFileChunkBuffer::CacheFileChunkBuffer(CacheFileChunk* aChunk)
|
2016-07-12 18:58:38 +03:00
|
|
|
: mChunk(aChunk),
|
|
|
|
mBuf(nullptr),
|
|
|
|
mBufSize(0),
|
|
|
|
mDataSize(0),
|
|
|
|
mReadHandlesCount(0),
|
|
|
|
mWriteHandleExists(false) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
CacheFileChunkBuffer::~CacheFileChunkBuffer() {
|
|
|
|
if (mBuf) {
|
|
|
|
CacheFileUtils::FreeBuffer(mBuf);
|
|
|
|
mBuf = nullptr;
|
|
|
|
mChunk->BuffersAllocationChanged(mBufSize, 0);
|
|
|
|
mBufSize = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void CacheFileChunkBuffer::CopyFrom(CacheFileChunkBuffer* aOther) {
|
2016-07-12 18:58:38 +03:00
|
|
|
MOZ_RELEASE_ASSERT(mBufSize >= aOther->mDataSize);
|
|
|
|
mDataSize = aOther->mDataSize;
|
|
|
|
memcpy(mBuf, aOther->mBuf, mDataSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult CacheFileChunkBuffer::FillInvalidRanges(
|
2019-05-01 11:47:10 +03:00
|
|
|
CacheFileChunkBuffer* aOther, CacheFileUtils::ValidityMap* aMap) {
|
2016-07-12 18:58:38 +03:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
rv = EnsureBufSize(aOther->mDataSize);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t invalidOffset = 0;
|
|
|
|
uint32_t invalidLength;
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < aMap->Length(); ++i) {
|
|
|
|
uint32_t validOffset = (*aMap)[i].Offset();
|
|
|
|
uint32_t validLength = (*aMap)[i].Len();
|
|
|
|
|
|
|
|
MOZ_RELEASE_ASSERT(invalidOffset <= validOffset);
|
|
|
|
invalidLength = validOffset - invalidOffset;
|
|
|
|
if (invalidLength > 0) {
|
2017-07-03 21:41:15 +03:00
|
|
|
MOZ_RELEASE_ASSERT(invalidOffset + invalidLength <= aOther->mDataSize);
|
2016-07-12 18:58:38 +03:00
|
|
|
memcpy(mBuf + invalidOffset, aOther->mBuf + invalidOffset, invalidLength);
|
|
|
|
}
|
|
|
|
invalidOffset = validOffset + validLength;
|
|
|
|
}
|
|
|
|
|
2017-07-03 21:41:15 +03:00
|
|
|
if (invalidOffset < aOther->mDataSize) {
|
|
|
|
invalidLength = aOther->mDataSize - invalidOffset;
|
2016-07-12 18:58:38 +03:00
|
|
|
memcpy(mBuf + invalidOffset, aOther->mBuf + invalidOffset, invalidLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_MUST_USE nsresult CacheFileChunkBuffer::EnsureBufSize(uint32_t aBufSize) {
|
|
|
|
AssertOwnsLock();
|
|
|
|
|
|
|
|
if (mBufSize >= aBufSize) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// find smallest power of 2 greater than or equal to aBufSize
|
|
|
|
aBufSize--;
|
|
|
|
aBufSize |= aBufSize >> 1;
|
|
|
|
aBufSize |= aBufSize >> 2;
|
|
|
|
aBufSize |= aBufSize >> 4;
|
|
|
|
aBufSize |= aBufSize >> 8;
|
|
|
|
aBufSize |= aBufSize >> 16;
|
|
|
|
aBufSize++;
|
|
|
|
|
|
|
|
const uint32_t minBufSize = kMinBufSize;
|
|
|
|
const uint32_t maxBufSize = kChunkSize;
|
|
|
|
aBufSize = clamped(aBufSize, minBufSize, maxBufSize);
|
|
|
|
|
|
|
|
if (!mChunk->CanAllocate(aBufSize - mBufSize)) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
char* newBuf = static_cast<char*>(realloc(mBuf, aBufSize));
|
2016-07-12 18:58:38 +03:00
|
|
|
if (!newBuf) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
mChunk->BuffersAllocationChanged(mBufSize, aBufSize);
|
|
|
|
mBuf = newBuf;
|
|
|
|
mBufSize = aBufSize;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheFileChunkBuffer::SetDataSize(uint32_t aDataSize) {
|
|
|
|
MOZ_RELEASE_ASSERT(
|
2016-10-24 21:47:56 +03:00
|
|
|
// EnsureBufSize must be called before SetDataSize, so the new data size
|
|
|
|
// is guaranteed to be smaller than or equal to mBufSize.
|
|
|
|
aDataSize <= mBufSize ||
|
|
|
|
// The only exception is an optimization when we read the data from the
|
|
|
|
// disk. The data is read to a separate buffer and CacheFileChunk::mBuf is
|
|
|
|
// empty (see CacheFileChunk::Read). We need to set mBuf::mDataSize
|
|
|
|
// accordingly so that DataSize() methods return correct value, but we
|
|
|
|
// don't want to allocate the buffer since it wouldn't be used in most
|
|
|
|
// cases.
|
2017-03-09 19:28:56 +03:00
|
|
|
(mBufSize == 0 && mChunk->mState == CacheFileChunk::READING));
|
2016-10-24 21:47:56 +03:00
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
mDataSize = aDataSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheFileChunkBuffer::AssertOwnsLock() const { mChunk->AssertOwnsLock(); }
|
|
|
|
|
|
|
|
void CacheFileChunkBuffer::RemoveReadHandle() {
|
|
|
|
AssertOwnsLock();
|
|
|
|
MOZ_RELEASE_ASSERT(mReadHandlesCount);
|
|
|
|
MOZ_RELEASE_ASSERT(!mWriteHandleExists);
|
|
|
|
mReadHandlesCount--;
|
|
|
|
|
|
|
|
if (mReadHandlesCount == 0 && mChunk->mBuf != this) {
|
|
|
|
DebugOnly<bool> removed = mChunk->mOldBufs.RemoveElement(this);
|
|
|
|
MOZ_ASSERT(removed);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheFileChunkBuffer::RemoveWriteHandle() {
|
|
|
|
AssertOwnsLock();
|
|
|
|
MOZ_RELEASE_ASSERT(mReadHandlesCount == 0);
|
|
|
|
MOZ_RELEASE_ASSERT(mWriteHandleExists);
|
|
|
|
mWriteHandleExists = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t CacheFileChunkBuffer::SizeOfIncludingThis(
|
|
|
|
mozilla::MallocSizeOf mallocSizeOf) const {
|
|
|
|
size_t n = mallocSizeOf(this);
|
|
|
|
|
|
|
|
if (mBuf) {
|
|
|
|
n += mallocSizeOf(mBuf);
|
|
|
|
}
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t CacheFileChunkHandle::DataSize() {
|
|
|
|
MOZ_ASSERT(mBuf, "Unexpected call on dummy handle");
|
|
|
|
mBuf->AssertOwnsLock();
|
|
|
|
return mBuf->mDataSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t CacheFileChunkHandle::Offset() {
|
|
|
|
MOZ_ASSERT(mBuf, "Unexpected call on dummy handle");
|
|
|
|
mBuf->AssertOwnsLock();
|
|
|
|
return mBuf->mChunk->Index() * kChunkSize;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
CacheFileChunkReadHandle::CacheFileChunkReadHandle(CacheFileChunkBuffer* aBuf) {
|
2016-07-12 18:58:38 +03:00
|
|
|
mBuf = aBuf;
|
|
|
|
mBuf->mReadHandlesCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
CacheFileChunkReadHandle::~CacheFileChunkReadHandle() {
|
|
|
|
mBuf->RemoveReadHandle();
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
const char* CacheFileChunkReadHandle::Buf() { return mBuf->mBuf; }
|
2016-07-12 18:58:38 +03:00
|
|
|
|
|
|
|
CacheFileChunkWriteHandle::CacheFileChunkWriteHandle(
|
2019-05-01 11:47:10 +03:00
|
|
|
CacheFileChunkBuffer* aBuf) {
|
2016-07-12 18:58:38 +03:00
|
|
|
mBuf = aBuf;
|
|
|
|
if (mBuf) {
|
|
|
|
MOZ_ASSERT(!mBuf->mWriteHandleExists);
|
|
|
|
mBuf->mWriteHandleExists = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CacheFileChunkWriteHandle::~CacheFileChunkWriteHandle() {
|
|
|
|
if (mBuf) {
|
|
|
|
mBuf->RemoveWriteHandle();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
char* CacheFileChunkWriteHandle::Buf() { return mBuf ? mBuf->mBuf : nullptr; }
|
2016-07-12 18:58:38 +03:00
|
|
|
|
|
|
|
void CacheFileChunkWriteHandle::UpdateDataSize(uint32_t aOffset,
|
|
|
|
uint32_t aLen) {
|
|
|
|
MOZ_ASSERT(mBuf, "Write performed on dummy handle?");
|
|
|
|
MOZ_ASSERT(aOffset <= mBuf->mDataSize);
|
|
|
|
MOZ_ASSERT(aOffset + aLen <= mBuf->mBufSize);
|
|
|
|
|
|
|
|
if (aOffset + aLen > mBuf->mDataSize) {
|
|
|
|
mBuf->mDataSize = aOffset + aLen;
|
|
|
|
}
|
|
|
|
|
|
|
|
mBuf->mChunk->UpdateDataSize(aOffset, aLen);
|
|
|
|
}
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class NotifyUpdateListenerEvent : public Runnable {
|
2013-09-20 13:11:26 +04:00
|
|
|
public:
|
2019-05-01 11:47:10 +03:00
|
|
|
NotifyUpdateListenerEvent(CacheFileChunkListener* aCallback,
|
|
|
|
CacheFileChunk* aChunk)
|
2017-06-12 22:34:10 +03:00
|
|
|
: Runnable("net::NotifyUpdateListenerEvent"),
|
|
|
|
mCallback(aCallback),
|
2013-09-20 13:11:26 +04:00
|
|
|
mChunk(aChunk) {
|
|
|
|
LOG(("NotifyUpdateListenerEvent::NotifyUpdateListenerEvent() [this=%p]",
|
|
|
|
this));
|
|
|
|
}
|
|
|
|
|
2014-07-15 19:37:45 +04:00
|
|
|
protected:
|
2013-09-20 13:11:26 +04:00
|
|
|
~NotifyUpdateListenerEvent() {
|
|
|
|
LOG(("NotifyUpdateListenerEvent::~NotifyUpdateListenerEvent() [this=%p]",
|
|
|
|
this));
|
|
|
|
}
|
|
|
|
|
2014-07-15 19:37:45 +04:00
|
|
|
public:
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2013-09-20 13:11:26 +04:00
|
|
|
LOG(("NotifyUpdateListenerEvent::Run() [this=%p]", this));
|
|
|
|
|
|
|
|
mCallback->OnChunkUpdated(mChunk);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
nsCOMPtr<CacheFileChunkListener> mCallback;
|
2015-10-22 13:11:00 +03:00
|
|
|
RefPtr<CacheFileChunk> mChunk;
|
2013-09-20 13:11:26 +04:00
|
|
|
};
|
|
|
|
|
2014-05-28 18:16:03 +04:00
|
|
|
bool CacheFileChunk::DispatchRelease() {
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-12 22:34:10 +03:00
|
|
|
NS_DispatchToMainThread(NewNonOwningRunnableMethod(
|
|
|
|
"net::CacheFileChunk::Release", this, &CacheFileChunk::Release));
|
2014-05-28 18:16:03 +04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
NS_IMPL_ADDREF(CacheFileChunk)
|
2014-03-28 00:38:33 +04:00
|
|
|
NS_IMETHODIMP_(MozExternalRefCountType)
|
2013-09-20 13:11:26 +04:00
|
|
|
CacheFileChunk::Release() {
|
2014-06-05 20:27:38 +04:00
|
|
|
nsrefcnt count = mRefCnt - 1;
|
2014-05-28 18:16:03 +04:00
|
|
|
if (DispatchRelease()) {
|
|
|
|
// Redispatched to the main thread.
|
2014-06-05 20:27:38 +04:00
|
|
|
return count;
|
2014-05-28 18:16:03 +04:00
|
|
|
}
|
|
|
|
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(0 != mRefCnt, "dup release");
|
2014-06-05 20:27:38 +04:00
|
|
|
count = --mRefCnt;
|
2013-09-20 13:11:26 +04:00
|
|
|
NS_LOG_RELEASE(this, count, "CacheFileChunk");
|
|
|
|
|
|
|
|
if (0 == count) {
|
|
|
|
mRefCnt = 1;
|
|
|
|
delete (this);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-05-28 18:16:03 +04:00
|
|
|
// We can safely access this chunk after decreasing mRefCnt since we re-post
|
|
|
|
// all calls to Release() happening off the main thread to the main thread.
|
|
|
|
// I.e. no other Release() that would delete the object could be run before
|
|
|
|
// we call CacheFile::DeactivateChunk().
|
|
|
|
//
|
|
|
|
// NOTE: we don't grab the CacheFile's lock, so the chunk might be addrefed
|
|
|
|
// on another thread before CacheFile::DeactivateChunk() grabs the lock on
|
|
|
|
// this thread. To make sure we won't deactivate chunk that was just returned
|
|
|
|
// to a new consumer we check mRefCnt once again in
|
|
|
|
// CacheFile::DeactivateChunk() after we grab the lock.
|
|
|
|
if (mActiveChunk && count == 1) {
|
|
|
|
mFile->DeactivateChunk(this);
|
2013-09-20 13:11:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(CacheFileChunk)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(mozilla::net::CacheFileIOListener)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
2018-02-12 22:36:46 +03:00
|
|
|
NS_INTERFACE_MAP_END
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
CacheFileChunk::CacheFileChunk(CacheFile* aFile, uint32_t aIndex,
|
2014-07-10 09:59:29 +04:00
|
|
|
bool aInitByWriter)
|
2014-04-06 22:45:18 +04:00
|
|
|
: CacheMemoryConsumer(aFile->mOpenAsMemoryOnly ? MEMORY_ONLY : DONT_REPORT),
|
|
|
|
mIndex(aIndex),
|
2013-09-20 13:11:26 +04:00
|
|
|
mState(INITIAL),
|
2014-04-22 15:23:17 +04:00
|
|
|
mStatus(NS_OK),
|
2014-05-28 18:16:03 +04:00
|
|
|
mActiveChunk(false),
|
2016-08-17 13:58:44 +03:00
|
|
|
mIsDirty(false),
|
|
|
|
mDiscardedChunk(false),
|
2016-07-12 18:58:38 +03:00
|
|
|
mBuffersSize(0),
|
2014-07-10 09:59:29 +04:00
|
|
|
mLimitAllocation(!aFile->mOpenAsMemoryOnly && aInitByWriter),
|
|
|
|
mIsPriority(aFile->mPriority),
|
2016-07-12 18:58:38 +03:00
|
|
|
mExpectedHash(0),
|
2013-09-20 13:11:26 +04:00
|
|
|
mFile(aFile) {
|
2014-07-10 09:59:29 +04:00
|
|
|
LOG(("CacheFileChunk::CacheFileChunk() [this=%p, index=%u, initByWriter=%d]",
|
|
|
|
this, aIndex, aInitByWriter));
|
2016-07-12 18:58:38 +03:00
|
|
|
mBuf = new CacheFileChunkBuffer(this);
|
2013-09-20 13:11:26 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
CacheFileChunk::~CacheFileChunk() {
|
|
|
|
LOG(("CacheFileChunk::~CacheFileChunk() [this=%p]", this));
|
2016-07-12 18:58:38 +03:00
|
|
|
}
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
void CacheFileChunk::AssertOwnsLock() const { mFile->AssertOwnsLock(); }
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2014-07-10 09:59:29 +04:00
|
|
|
void CacheFileChunk::InitNew() {
|
2016-07-12 18:58:38 +03:00
|
|
|
AssertOwnsLock();
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2014-07-10 09:59:29 +04:00
|
|
|
LOG(("CacheFileChunk::InitNew() [this=%p]", this));
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(mState == INITIAL);
|
2014-07-10 09:59:29 +04:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(mStatus));
|
2016-07-12 18:58:38 +03:00
|
|
|
MOZ_ASSERT(!mBuf->Buf());
|
|
|
|
MOZ_ASSERT(!mWritingStateHandle);
|
|
|
|
MOZ_ASSERT(!mReadingStateBuf);
|
2014-07-10 09:59:29 +04:00
|
|
|
MOZ_ASSERT(!mIsDirty);
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
mBuf = new CacheFileChunkBuffer(this);
|
2013-09-20 13:11:26 +04:00
|
|
|
mState = READY;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsresult CacheFileChunk::Read(CacheFileHandle* aHandle, uint32_t aLen,
|
2014-02-18 19:05:07 +04:00
|
|
|
CacheHash::Hash16_t aHash,
|
2019-05-01 11:47:10 +03:00
|
|
|
CacheFileChunkListener* aCallback) {
|
2016-07-12 18:58:38 +03:00
|
|
|
AssertOwnsLock();
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
LOG(("CacheFileChunk::Read() [this=%p, handle=%p, len=%d, listener=%p]", this,
|
|
|
|
aHandle, aLen, aCallback));
|
|
|
|
|
|
|
|
MOZ_ASSERT(mState == INITIAL);
|
2014-07-10 09:59:29 +04:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(mStatus));
|
2016-07-12 18:58:38 +03:00
|
|
|
MOZ_ASSERT(!mBuf->Buf());
|
|
|
|
MOZ_ASSERT(!mWritingStateHandle);
|
|
|
|
MOZ_ASSERT(!mReadingStateBuf);
|
2013-09-20 13:11:26 +04:00
|
|
|
MOZ_ASSERT(aLen);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2014-07-10 09:59:29 +04:00
|
|
|
mState = READING;
|
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
RefPtr<CacheFileChunkBuffer> tmpBuf = new CacheFileChunkBuffer(this);
|
|
|
|
rv = tmpBuf->EnsureBufSize(aLen);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
SetError(rv);
|
2014-07-10 09:59:29 +04:00
|
|
|
return mStatus;
|
|
|
|
}
|
2016-07-12 18:58:38 +03:00
|
|
|
tmpBuf->SetDataSize(aLen);
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
rv = CacheFileIOManager::Read(aHandle, mIndex * kChunkSize, tmpBuf->Buf(),
|
2014-08-18 17:36:27 +04:00
|
|
|
aLen, this);
|
2014-04-22 15:23:17 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
rv = mIndex ? NS_ERROR_FILE_CORRUPTED : NS_ERROR_FILE_NOT_FOUND;
|
|
|
|
SetError(rv);
|
|
|
|
} else {
|
2016-07-12 18:58:38 +03:00
|
|
|
mReadingStateBuf.swap(tmpBuf);
|
2014-04-22 15:23:17 +04:00
|
|
|
mListener = aCallback;
|
2016-07-12 18:58:38 +03:00
|
|
|
// mBuf contains no data but we set datasize to size of the data that will
|
|
|
|
// be read from the disk. No handle is allowed to access the non-existent
|
|
|
|
// data until reading finishes, but data can be appended or overwritten.
|
|
|
|
// These pieces are tracked in mValidityMap and will be merged with the data
|
|
|
|
// read from disk in OnDataRead().
|
|
|
|
mBuf->SetDataSize(aLen);
|
|
|
|
mExpectedHash = aHash;
|
2013-09-20 13:11:26 +04:00
|
|
|
}
|
|
|
|
|
2014-04-22 15:23:17 +04:00
|
|
|
return rv;
|
2013-09-20 13:11:26 +04:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsresult CacheFileChunk::Write(CacheFileHandle* aHandle,
|
|
|
|
CacheFileChunkListener* aCallback) {
|
2016-07-12 18:58:38 +03:00
|
|
|
AssertOwnsLock();
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
LOG(("CacheFileChunk::Write() [this=%p, handle=%p, listener=%p]", this,
|
|
|
|
aHandle, aCallback));
|
|
|
|
|
|
|
|
MOZ_ASSERT(mState == READY);
|
2014-07-10 09:59:29 +04:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(mStatus));
|
2016-07-12 18:58:38 +03:00
|
|
|
MOZ_ASSERT(!mWritingStateHandle);
|
|
|
|
MOZ_ASSERT(mBuf->DataSize()); // Don't write chunk when it is empty
|
|
|
|
MOZ_ASSERT(mBuf->ReadHandlesCount() == 0);
|
|
|
|
MOZ_ASSERT(!mBuf->WriteHandleExists());
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
2014-07-10 09:59:29 +04:00
|
|
|
mState = WRITING;
|
2016-07-12 18:58:38 +03:00
|
|
|
mWritingStateHandle = new CacheFileChunkReadHandle(mBuf);
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
rv = CacheFileIOManager::Write(
|
|
|
|
aHandle, mIndex * kChunkSize, mWritingStateHandle->Buf(),
|
|
|
|
mWritingStateHandle->DataSize(), false, false, this);
|
2014-04-22 15:23:17 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
2016-07-12 18:58:38 +03:00
|
|
|
mWritingStateHandle = nullptr;
|
2014-04-22 15:23:17 +04:00
|
|
|
SetError(rv);
|
|
|
|
} else {
|
|
|
|
mListener = aCallback;
|
|
|
|
mIsDirty = false;
|
2013-09-20 13:11:26 +04:00
|
|
|
}
|
|
|
|
|
2014-04-22 15:23:17 +04:00
|
|
|
return rv;
|
2013-09-20 13:11:26 +04:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
void CacheFileChunk::WaitForUpdate(CacheFileChunkListener* aCallback) {
|
2016-07-12 18:58:38 +03:00
|
|
|
AssertOwnsLock();
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
LOG(("CacheFileChunk::WaitForUpdate() [this=%p, listener=%p]", this,
|
|
|
|
aCallback));
|
|
|
|
|
|
|
|
MOZ_ASSERT(mFile->mOutput);
|
|
|
|
MOZ_ASSERT(IsReady());
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
for (uint32_t i = 0; i < mUpdateListeners.Length(); i++) {
|
|
|
|
MOZ_ASSERT(mUpdateListeners[i]->mCallback != aCallback);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
ChunkListenerItem* item = new ChunkListenerItem();
|
2014-08-18 17:36:27 +04:00
|
|
|
item->mTarget = CacheFileIOManager::IOTarget();
|
|
|
|
if (!item->mTarget) {
|
|
|
|
LOG(
|
|
|
|
("CacheFileChunk::WaitForUpdate() - Cannot get Cache I/O thread! Using "
|
|
|
|
"main thread for callback."));
|
2017-06-01 23:43:44 +03:00
|
|
|
item->mTarget = GetMainThreadEventTarget();
|
2014-08-18 17:36:27 +04:00
|
|
|
}
|
|
|
|
item->mCallback = aCallback;
|
|
|
|
MOZ_ASSERT(item->mTarget);
|
2013-09-20 13:11:26 +04:00
|
|
|
item->mCallback = aCallback;
|
|
|
|
|
|
|
|
mUpdateListeners.AppendElement(item);
|
|
|
|
}
|
|
|
|
|
2019-12-02 16:21:06 +03:00
|
|
|
void CacheFileChunk::CancelWait(CacheFileChunkListener* aCallback) {
|
2016-07-12 18:58:38 +03:00
|
|
|
AssertOwnsLock();
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
LOG(("CacheFileChunk::CancelWait() [this=%p, listener=%p]", this, aCallback));
|
|
|
|
|
|
|
|
MOZ_ASSERT(IsReady());
|
|
|
|
|
|
|
|
uint32_t i;
|
|
|
|
for (i = 0; i < mUpdateListeners.Length(); i++) {
|
2019-05-01 11:47:10 +03:00
|
|
|
ChunkListenerItem* item = mUpdateListeners[i];
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
if (item->mCallback == aCallback) {
|
|
|
|
mUpdateListeners.RemoveElementAt(i);
|
|
|
|
delete item;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
for (; i < mUpdateListeners.Length(); i++) {
|
|
|
|
MOZ_ASSERT(mUpdateListeners[i]->mCallback != aCallback);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult CacheFileChunk::NotifyUpdateListeners() {
|
2016-07-12 18:58:38 +03:00
|
|
|
AssertOwnsLock();
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
LOG(("CacheFileChunk::NotifyUpdateListeners() [this=%p]", this));
|
|
|
|
|
|
|
|
MOZ_ASSERT(IsReady());
|
|
|
|
|
|
|
|
nsresult rv, rv2;
|
|
|
|
|
|
|
|
rv = NS_OK;
|
|
|
|
for (uint32_t i = 0; i < mUpdateListeners.Length(); i++) {
|
2019-05-01 11:47:10 +03:00
|
|
|
ChunkListenerItem* item = mUpdateListeners[i];
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
LOG(
|
|
|
|
("CacheFileChunk::NotifyUpdateListeners() - Notifying listener %p "
|
|
|
|
"[this=%p]",
|
|
|
|
item->mCallback.get(), this));
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<NotifyUpdateListenerEvent> ev;
|
2013-09-20 13:11:26 +04:00
|
|
|
ev = new NotifyUpdateListenerEvent(item->mCallback, this);
|
|
|
|
rv2 = item->mTarget->Dispatch(ev, NS_DISPATCH_NORMAL);
|
|
|
|
if (NS_FAILED(rv2) && NS_SUCCEEDED(rv)) rv = rv2;
|
|
|
|
delete item;
|
|
|
|
}
|
|
|
|
|
|
|
|
mUpdateListeners.Clear();
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
uint32_t CacheFileChunk::Index() const { return mIndex; }
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
CacheHash::Hash16_t CacheFileChunk::Hash() const {
|
2013-09-20 13:11:26 +04:00
|
|
|
MOZ_ASSERT(IsReady());
|
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
return CacheHash::Hash16(mBuf->Buf(), mBuf->DataSize());
|
2013-09-20 13:11:26 +04:00
|
|
|
}
|
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
uint32_t CacheFileChunk::DataSize() const { return mBuf->DataSize(); }
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
void CacheFileChunk::UpdateDataSize(uint32_t aOffset, uint32_t aLen) {
|
|
|
|
AssertOwnsLock();
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2014-04-22 15:23:17 +04:00
|
|
|
// UpdateDataSize() is called only when we've written some data to the chunk
|
|
|
|
// and we never write data anymore once some error occurs.
|
2014-07-10 09:59:29 +04:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(mStatus));
|
2014-04-22 15:23:17 +04:00
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
LOG(("CacheFileChunk::UpdateDataSize() [this=%p, offset=%d, len=%d]", this,
|
|
|
|
aOffset, aLen));
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
mIsDirty = true;
|
|
|
|
|
2016-07-26 15:29:16 +03:00
|
|
|
int64_t fileSize = static_cast<int64_t>(kChunkSize) * mIndex + aOffset + aLen;
|
2013-09-20 13:11:26 +04:00
|
|
|
bool notify = false;
|
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
if (fileSize > mFile->mDataSize) {
|
2013-09-20 13:11:26 +04:00
|
|
|
mFile->mDataSize = fileSize;
|
|
|
|
notify = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mState == READY || mState == WRITING) {
|
|
|
|
MOZ_ASSERT(mValidityMap.Length() == 0);
|
|
|
|
|
2014-05-08 18:15:24 +04:00
|
|
|
if (notify) {
|
2013-09-20 13:11:26 +04:00
|
|
|
NotifyUpdateListeners();
|
2014-05-08 18:15:24 +04:00
|
|
|
}
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're still waiting for data from the disk. This chunk cannot be used by
|
|
|
|
// input stream, so there must be no update listener. We also need to keep
|
|
|
|
// track of where the data is written so that we can correctly merge the new
|
|
|
|
// data with the old one.
|
|
|
|
|
|
|
|
MOZ_ASSERT(mUpdateListeners.Length() == 0);
|
|
|
|
MOZ_ASSERT(mState == READING);
|
|
|
|
|
2014-05-08 18:15:24 +04:00
|
|
|
mValidityMap.AddPair(aOffset, aLen);
|
|
|
|
mValidityMap.Log();
|
2013-09-20 13:11:26 +04:00
|
|
|
}
|
|
|
|
|
2019-12-02 16:21:06 +03:00
|
|
|
void CacheFileChunk::Truncate(uint32_t aOffset) {
|
2017-03-09 19:28:56 +03:00
|
|
|
MOZ_RELEASE_ASSERT(mState == READY || mState == WRITING || mState == READING);
|
|
|
|
|
|
|
|
if (mState == READING) {
|
|
|
|
mIsDirty = true;
|
|
|
|
}
|
|
|
|
|
2016-08-17 13:58:44 +03:00
|
|
|
mBuf->SetDataSize(aOffset);
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsresult CacheFileChunk::OnFileOpened(CacheFileHandle* aHandle,
|
2013-09-20 13:11:26 +04:00
|
|
|
nsresult aResult) {
|
|
|
|
MOZ_CRASH("CacheFileChunk::OnFileOpened should not be called!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsresult CacheFileChunk::OnDataWritten(CacheFileHandle* aHandle,
|
|
|
|
const char* aBuf, nsresult aResult) {
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG((
|
|
|
|
"CacheFileChunk::OnDataWritten() [this=%p, handle=%p, result=0x%08" PRIx32
|
|
|
|
"]",
|
|
|
|
this, aHandle, static_cast<uint32_t>(aResult)));
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
nsCOMPtr<CacheFileChunkListener> listener;
|
|
|
|
|
|
|
|
{
|
|
|
|
CacheFileAutoLock lock(mFile);
|
|
|
|
|
|
|
|
MOZ_ASSERT(mState == WRITING);
|
|
|
|
MOZ_ASSERT(mListener);
|
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
mWritingStateHandle = nullptr;
|
|
|
|
|
2014-04-22 15:23:17 +04:00
|
|
|
if (NS_WARN_IF(NS_FAILED(aResult))) {
|
|
|
|
SetError(aResult);
|
|
|
|
}
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2014-07-10 09:59:29 +04:00
|
|
|
mState = READY;
|
2013-09-20 13:11:26 +04:00
|
|
|
mListener.swap(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
listener->OnChunkWritten(aResult, this);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsresult CacheFileChunk::OnDataRead(CacheFileHandle* aHandle, char* aBuf,
|
2013-09-20 13:11:26 +04:00
|
|
|
nsresult aResult) {
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("CacheFileChunk::OnDataRead() [this=%p, handle=%p, result=0x%08" PRIx32
|
|
|
|
"]",
|
|
|
|
this, aHandle, static_cast<uint32_t>(aResult)));
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
nsCOMPtr<CacheFileChunkListener> listener;
|
|
|
|
|
|
|
|
{
|
|
|
|
CacheFileAutoLock lock(mFile);
|
|
|
|
|
|
|
|
MOZ_ASSERT(mState == READING);
|
|
|
|
MOZ_ASSERT(mListener);
|
2016-07-12 18:58:38 +03:00
|
|
|
MOZ_ASSERT(mReadingStateBuf);
|
|
|
|
MOZ_RELEASE_ASSERT(mBuf->ReadHandlesCount() == 0);
|
|
|
|
MOZ_RELEASE_ASSERT(!mBuf->WriteHandleExists());
|
|
|
|
|
|
|
|
RefPtr<CacheFileChunkBuffer> tmpBuf;
|
|
|
|
tmpBuf.swap(mReadingStateBuf);
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(aResult)) {
|
2016-07-12 18:58:38 +03:00
|
|
|
CacheHash::Hash16_t hash =
|
|
|
|
CacheHash::Hash16(tmpBuf->Buf(), tmpBuf->DataSize());
|
|
|
|
if (hash != mExpectedHash) {
|
2013-09-20 13:11:26 +04:00
|
|
|
LOG(
|
|
|
|
("CacheFileChunk::OnDataRead() - Hash mismatch! Hash of the data is"
|
|
|
|
" %hx, hash in metadata is %hx. [this=%p, idx=%d]",
|
2016-07-12 18:58:38 +03:00
|
|
|
hash, mExpectedHash, this, mIndex));
|
2013-09-20 13:11:26 +04:00
|
|
|
aResult = NS_ERROR_FILE_CORRUPTED;
|
2016-07-12 18:58:38 +03:00
|
|
|
} else {
|
2017-03-09 19:28:56 +03:00
|
|
|
if (mBuf->DataSize() < tmpBuf->DataSize()) {
|
|
|
|
// Truncate() was called while the data was being read.
|
|
|
|
tmpBuf->SetDataSize(mBuf->DataSize());
|
|
|
|
}
|
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
if (!mBuf->Buf()) {
|
|
|
|
// Just swap the buffers if mBuf is still empty
|
|
|
|
mBuf.swap(tmpBuf);
|
2013-09-20 13:11:26 +04:00
|
|
|
} else {
|
2014-05-08 18:15:24 +04:00
|
|
|
LOG(("CacheFileChunk::OnDataRead() - Merging buffers. [this=%p]",
|
|
|
|
this));
|
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
mValidityMap.Log();
|
|
|
|
aResult = mBuf->FillInvalidRanges(tmpBuf, &mValidityMap);
|
|
|
|
mValidityMap.Clear();
|
2013-09-20 13:11:26 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(aResult)) {
|
2014-04-22 15:23:17 +04:00
|
|
|
aResult = mIndex ? NS_ERROR_FILE_CORRUPTED : NS_ERROR_FILE_NOT_FOUND;
|
|
|
|
SetError(aResult);
|
2016-07-12 18:58:38 +03:00
|
|
|
mBuf->SetDataSize(0);
|
2013-09-20 13:11:26 +04:00
|
|
|
}
|
|
|
|
|
2014-07-10 09:59:29 +04:00
|
|
|
mState = READY;
|
2013-09-20 13:11:26 +04:00
|
|
|
mListener.swap(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
listener->OnChunkRead(aResult, this);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsresult CacheFileChunk::OnFileDoomed(CacheFileHandle* aHandle,
|
2013-09-20 13:11:26 +04:00
|
|
|
nsresult aResult) {
|
|
|
|
MOZ_CRASH("CacheFileChunk::OnFileDoomed should not be called!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsresult CacheFileChunk::OnEOFSet(CacheFileHandle* aHandle, nsresult aResult) {
|
2013-09-20 13:11:26 +04:00
|
|
|
MOZ_CRASH("CacheFileChunk::OnEOFSet should not be called!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
nsresult CacheFileChunk::OnFileRenamed(CacheFileHandle* aHandle,
|
2014-02-18 19:05:07 +04:00
|
|
|
nsresult aResult) {
|
|
|
|
MOZ_CRASH("CacheFileChunk::OnFileRenamed should not be called!");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2016-02-12 16:45:00 +03:00
|
|
|
bool CacheFileChunk::IsKilled() { return mFile->IsKilled(); }
|
|
|
|
|
2014-02-27 03:11:40 +04:00
|
|
|
bool CacheFileChunk::IsReady() const {
|
2014-04-22 15:23:17 +04:00
|
|
|
return (NS_SUCCEEDED(mStatus) && (mState == READY || mState == WRITING));
|
2013-09-20 13:11:26 +04:00
|
|
|
}
|
|
|
|
|
2014-02-27 03:11:40 +04:00
|
|
|
bool CacheFileChunk::IsDirty() const {
|
2016-07-12 18:58:38 +03:00
|
|
|
AssertOwnsLock();
|
2013-09-20 13:11:26 +04:00
|
|
|
|
|
|
|
return mIsDirty;
|
|
|
|
}
|
|
|
|
|
2014-04-22 15:23:17 +04:00
|
|
|
nsresult CacheFileChunk::GetStatus() { return mStatus; }
|
|
|
|
|
|
|
|
void CacheFileChunk::SetError(nsresult aStatus) {
|
2016-12-16 06:16:31 +03:00
|
|
|
LOG(("CacheFileChunk::SetError() [this=%p, status=0x%08" PRIx32 "]", this,
|
|
|
|
static_cast<uint32_t>(aStatus)));
|
2016-07-12 18:58:38 +03:00
|
|
|
|
2014-07-10 09:59:29 +04:00
|
|
|
MOZ_ASSERT(NS_FAILED(aStatus));
|
|
|
|
|
|
|
|
if (NS_FAILED(mStatus)) {
|
|
|
|
// Remember only the first error code.
|
|
|
|
return;
|
2014-04-22 15:23:17 +04:00
|
|
|
}
|
2014-07-10 09:59:29 +04:00
|
|
|
|
|
|
|
mStatus = aStatus;
|
2014-04-22 15:23:17 +04:00
|
|
|
}
|
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
CacheFileChunkReadHandle CacheFileChunk::GetReadHandle() {
|
|
|
|
LOG(("CacheFileChunk::GetReadHandle() [this=%p]", this));
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
AssertOwnsLock();
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
MOZ_RELEASE_ASSERT(mState == READY || mState == WRITING);
|
|
|
|
// We don't release the lock when writing the data and CacheFileOutputStream
|
|
|
|
// doesn't get the read handle, so there cannot be a write handle when read
|
|
|
|
// handle is obtained.
|
|
|
|
MOZ_RELEASE_ASSERT(!mBuf->WriteHandleExists());
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
return CacheFileChunkReadHandle(mBuf);
|
2013-09-20 13:11:26 +04:00
|
|
|
}
|
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
CacheFileChunkWriteHandle CacheFileChunk::GetWriteHandle(
|
|
|
|
uint32_t aEnsuredBufSize) {
|
|
|
|
LOG(("CacheFileChunk::GetWriteHandle() [this=%p, ensuredBufSize=%u]", this,
|
|
|
|
aEnsuredBufSize));
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
AssertOwnsLock();
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
if (NS_FAILED(mStatus)) {
|
|
|
|
return CacheFileChunkWriteHandle(nullptr); // dummy handle
|
2014-07-10 09:59:29 +04:00
|
|
|
}
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
nsresult rv;
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
// We don't support multiple write handles
|
|
|
|
MOZ_RELEASE_ASSERT(!mBuf->WriteHandleExists());
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
if (mBuf->ReadHandlesCount()) {
|
|
|
|
LOG(
|
|
|
|
("CacheFileChunk::GetWriteHandle() - cloning buffer because of existing"
|
|
|
|
" read handle"));
|
2013-09-20 13:11:26 +04:00
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
MOZ_RELEASE_ASSERT(mState != READING);
|
|
|
|
RefPtr<CacheFileChunkBuffer> newBuf = new CacheFileChunkBuffer(this);
|
|
|
|
rv = newBuf->EnsureBufSize(std::max(aEnsuredBufSize, mBuf->DataSize()));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
newBuf->CopyFrom(mBuf);
|
|
|
|
mOldBufs.AppendElement(mBuf);
|
|
|
|
mBuf = newBuf;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rv = mBuf->EnsureBufSize(aEnsuredBufSize);
|
2014-07-10 09:59:29 +04:00
|
|
|
}
|
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2014-07-10 09:59:29 +04:00
|
|
|
SetError(NS_ERROR_OUT_OF_MEMORY);
|
2016-07-12 18:58:38 +03:00
|
|
|
return CacheFileChunkWriteHandle(nullptr); // dummy handle
|
2014-07-10 09:59:29 +04:00
|
|
|
}
|
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
return CacheFileChunkWriteHandle(mBuf);
|
2013-09-20 13:11:26 +04:00
|
|
|
}
|
|
|
|
|
2014-02-27 03:11:40 +04:00
|
|
|
// Memory reporting
|
|
|
|
|
|
|
|
size_t CacheFileChunk::SizeOfExcludingThis(
|
|
|
|
mozilla::MallocSizeOf mallocSizeOf) const {
|
2016-07-12 18:58:38 +03:00
|
|
|
size_t n = mBuf->SizeOfIncludingThis(mallocSizeOf);
|
|
|
|
|
|
|
|
if (mReadingStateBuf) {
|
|
|
|
n += mReadingStateBuf->SizeOfIncludingThis(mallocSizeOf);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < mOldBufs.Length(); ++i) {
|
|
|
|
n += mOldBufs[i]->SizeOfIncludingThis(mallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2014-02-27 03:11:40 +04:00
|
|
|
n += mValidityMap.SizeOfExcludingThis(mallocSizeOf);
|
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t CacheFileChunk::SizeOfIncludingThis(
|
|
|
|
mozilla::MallocSizeOf mallocSizeOf) const {
|
|
|
|
return mallocSizeOf(this) + SizeOfExcludingThis(mallocSizeOf);
|
|
|
|
}
|
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
bool CacheFileChunk::CanAllocate(uint32_t aSize) const {
|
2014-07-10 09:59:29 +04:00
|
|
|
if (!mLimitAllocation) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(("CacheFileChunk::CanAllocate() [this=%p, size=%u]", this, aSize));
|
|
|
|
|
2019-01-08 17:28:00 +03:00
|
|
|
int64_t limit = CacheObserver::MaxDiskChunksMemoryUsage(mIsPriority);
|
2014-07-10 09:59:29 +04:00
|
|
|
if (limit == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-01-08 17:28:00 +03:00
|
|
|
limit <<= 10;
|
|
|
|
if (limit > UINT32_MAX) {
|
|
|
|
limit = UINT32_MAX;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t usage = ChunksMemoryUsage();
|
2014-07-10 09:59:29 +04:00
|
|
|
if (usage + aSize > limit) {
|
|
|
|
LOG(("CacheFileChunk::CanAllocate() - Returning false. [this=%p]", this));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
void CacheFileChunk::BuffersAllocationChanged(uint32_t aFreed,
|
|
|
|
uint32_t aAllocated) {
|
|
|
|
uint32_t oldBuffersSize = mBuffersSize;
|
|
|
|
mBuffersSize += aAllocated;
|
|
|
|
mBuffersSize -= aFreed;
|
|
|
|
|
|
|
|
DoMemoryReport(sizeof(CacheFileChunk) + mBuffersSize);
|
|
|
|
|
2014-07-10 09:59:29 +04:00
|
|
|
if (!mLimitAllocation) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-07-12 18:58:38 +03:00
|
|
|
ChunksMemoryUsage() -= oldBuffersSize;
|
|
|
|
ChunksMemoryUsage() += mBuffersSize;
|
|
|
|
LOG(
|
|
|
|
("CacheFileChunk::BuffersAllocationChanged() - %s chunks usage %u "
|
2014-07-10 09:59:29 +04:00
|
|
|
"[this=%p]",
|
|
|
|
mIsPriority ? "Priority" : "Normal",
|
|
|
|
static_cast<uint32_t>(ChunksMemoryUsage()), this));
|
|
|
|
}
|
|
|
|
|
2019-05-01 11:47:10 +03:00
|
|
|
mozilla::Atomic<uint32_t, ReleaseAcquire>& CacheFileChunk::ChunksMemoryUsage()
|
2016-07-12 18:58:38 +03:00
|
|
|
const {
|
|
|
|
static mozilla::Atomic<uint32_t, ReleaseAcquire> chunksMemoryUsage(0);
|
|
|
|
static mozilla::Atomic<uint32_t, ReleaseAcquire> prioChunksMemoryUsage(0);
|
2014-07-10 09:59:29 +04:00
|
|
|
return mIsPriority ? prioChunksMemoryUsage : chunksMemoryUsage;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace net
|
|
|
|
} // namespace mozilla
|