2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-05-31 15:20:03 -07:00
|
|
|
#include "nsCache.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsDiskCache.h"
|
|
|
|
#include "nsDiskCacheBlockFile.h"
|
2010-09-16 13:21:12 -07:00
|
|
|
#include "mozilla/FileUtils.h"
|
2013-01-15 04:22:03 -08:00
|
|
|
#include <algorithm>
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-28 11:33:28 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/******************************************************************************
|
|
|
|
* nsDiskCacheBlockFile -
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* Open
|
|
|
|
*****************************************************************************/
|
|
|
|
nsresult
|
2012-06-05 19:08:30 -07:00
|
|
|
nsDiskCacheBlockFile::Open(nsIFile * blockFile,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t blockSize,
|
|
|
|
uint32_t bitMapSize,
|
2012-07-20 16:47:52 -07:00
|
|
|
nsDiskCache::CorruptCacheInfo * corruptInfo)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-20 16:47:52 -07:00
|
|
|
NS_ENSURE_ARG_POINTER(corruptInfo);
|
|
|
|
*corruptInfo = nsDiskCache::kUnexpectedError;
|
|
|
|
|
|
|
|
if (bitMapSize % 32) {
|
|
|
|
*corruptInfo = nsDiskCache::kInvalidArgPointer;
|
2011-01-18 06:12:10 -08:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2012-07-20 16:47:52 -07:00
|
|
|
}
|
2011-01-18 06:12:10 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
mBlockSize = blockSize;
|
2011-01-18 06:12:10 -08:00
|
|
|
mBitMapWords = bitMapSize / 32;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t bitMapBytes = mBitMapWords * 4;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// open the file - restricted to user, the data could be confidential
|
|
|
|
nsresult rv = blockFile->OpenNSPRFileDesc(PR_RDWR | PR_CREATE_FILE, 00600, &mFD);
|
2012-05-31 15:20:03 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
2012-07-20 16:47:52 -07:00
|
|
|
*corruptInfo = nsDiskCache::kCouldNotCreateBlockFile;
|
2012-05-31 15:20:03 -07:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: nsDiskCacheBlockFile::Open "
|
|
|
|
"[this=%p] unable to open or create file: %d",
|
|
|
|
this, rv));
|
|
|
|
return rv; // unable to open or create file
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// allocate bit map buffer
|
2012-08-22 08:56:38 -07:00
|
|
|
mBitMap = new uint32_t[mBitMapWords];
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// check if we just creating the file
|
2010-09-16 13:21:12 -07:00
|
|
|
mFileSize = PR_Available(mFD);
|
|
|
|
if (mFileSize < 0) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// XXX an error occurred. We could call PR_GetError(), but how would that help?
|
2012-07-20 16:47:52 -07:00
|
|
|
*corruptInfo = nsDiskCache::kBlockFileSizeError;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = NS_ERROR_UNEXPECTED;
|
|
|
|
goto error_exit;
|
|
|
|
}
|
2010-09-16 13:21:12 -07:00
|
|
|
if (mFileSize == 0) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// initialize bit map and write it
|
2011-01-18 06:12:10 -08:00
|
|
|
memset(mBitMap, 0, bitMapBytes);
|
2012-07-20 16:47:52 -07:00
|
|
|
if (!Write(0, mBitMap, bitMapBytes)) {
|
|
|
|
*corruptInfo = nsDiskCache::kBlockFileBitMapWriteError;
|
2007-03-22 10:30:00 -07:00
|
|
|
goto error_exit;
|
2012-07-20 16:47:52 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
} else if ((uint32_t)mFileSize < bitMapBytes) {
|
2012-07-20 16:47:52 -07:00
|
|
|
*corruptInfo = nsDiskCache::kBlockFileSizeLessThanBitMap;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = NS_ERROR_UNEXPECTED; // XXX NS_ERROR_CACHE_INVALID;
|
|
|
|
goto error_exit;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// read the bit map
|
2012-08-22 08:56:38 -07:00
|
|
|
const int32_t bytesRead = PR_Read(mFD, mBitMap, bitMapBytes);
|
|
|
|
if ((bytesRead < 0) || ((uint32_t)bytesRead < bitMapBytes)) {
|
2012-07-20 16:47:52 -07:00
|
|
|
*corruptInfo = nsDiskCache::kBlockFileBitMapReadError;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = NS_ERROR_UNEXPECTED;
|
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
#if defined(IS_LITTLE_ENDIAN)
|
|
|
|
// Swap from network format
|
2011-01-18 06:12:10 -08:00
|
|
|
for (unsigned int i = 0; i < mBitMapWords; ++i)
|
2007-03-22 10:30:00 -07:00
|
|
|
mBitMap[i] = ntohl(mBitMap[i]);
|
|
|
|
#endif
|
|
|
|
// validate block file size
|
|
|
|
// Because not whole blocks are written, the size may be a
|
|
|
|
// little bit smaller than used blocks times blocksize,
|
|
|
|
// because the last block will generally not be 'whole'.
|
2012-08-22 08:56:38 -07:00
|
|
|
const uint32_t estimatedSize = CalcBlockFileSize();
|
|
|
|
if ((uint32_t)mFileSize + blockSize < estimatedSize) {
|
2012-07-20 16:47:52 -07:00
|
|
|
*corruptInfo = nsDiskCache::kBlockFileEstimatedSizeError;
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = NS_ERROR_UNEXPECTED;
|
|
|
|
goto error_exit;
|
|
|
|
}
|
|
|
|
}
|
2012-05-31 15:20:03 -07:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: nsDiskCacheBlockFile::Open [this=%p] succeeded",
|
|
|
|
this));
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
error_exit:
|
2012-05-31 15:20:03 -07:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: nsDiskCacheBlockFile::Open [this=%p] failed with "
|
|
|
|
"error %d", this, rv));
|
2011-10-17 07:59:28 -07:00
|
|
|
Close(false);
|
2007-03-22 10:30:00 -07:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* Close
|
|
|
|
*****************************************************************************/
|
|
|
|
nsresult
|
2011-09-28 23:19:26 -07:00
|
|
|
nsDiskCacheBlockFile::Close(bool flush)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
if (mFD) {
|
|
|
|
if (flush)
|
|
|
|
rv = FlushBitMap();
|
|
|
|
PRStatus err = PR_Close(mFD);
|
|
|
|
if (NS_SUCCEEDED(rv) && (err != PR_SUCCESS))
|
|
|
|
rv = NS_ERROR_UNEXPECTED;
|
2012-07-30 07:20:58 -07:00
|
|
|
mFD = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mBitMap) {
|
|
|
|
delete [] mBitMap;
|
2012-07-30 07:20:58 -07:00
|
|
|
mBitMap = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* AllocateBlocks
|
|
|
|
*
|
|
|
|
* Allocates 1-4 blocks, using a first fit strategy,
|
|
|
|
* so that no group of blocks spans a quad block boundary.
|
|
|
|
*
|
|
|
|
* Returns block number of first block allocated or -1 on failure.
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t
|
|
|
|
nsDiskCacheBlockFile::AllocateBlocks(int32_t numBlocks)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
const int maxPos = 32 - numBlocks;
|
2012-08-22 08:56:38 -07:00
|
|
|
const uint32_t mask = (0x01 << numBlocks) - 1;
|
2011-01-18 06:12:10 -08:00
|
|
|
for (unsigned int i = 0; i < mBitMapWords; ++i) {
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mapWord = ~mBitMap[i]; // flip bits so free bits are 1
|
2007-03-22 10:30:00 -07:00
|
|
|
if (mapWord) { // At least one free bit
|
|
|
|
// Binary search for first free bit in word
|
|
|
|
int bit = 0;
|
|
|
|
if ((mapWord & 0x0FFFF) == 0) { bit |= 16; mapWord >>= 16; }
|
|
|
|
if ((mapWord & 0x000FF) == 0) { bit |= 8; mapWord >>= 8; }
|
|
|
|
if ((mapWord & 0x0000F) == 0) { bit |= 4; mapWord >>= 4; }
|
|
|
|
if ((mapWord & 0x00003) == 0) { bit |= 2; mapWord >>= 2; }
|
|
|
|
if ((mapWord & 0x00001) == 0) { bit |= 1; mapWord >>= 1; }
|
|
|
|
// Find first fit for mask
|
|
|
|
for (; bit <= maxPos; ++bit) {
|
|
|
|
// all bits selected by mask are 1, so free
|
|
|
|
if ((mask & mapWord) == mask) {
|
|
|
|
mBitMap[i] |= mask << bit;
|
2011-10-17 07:59:28 -07:00
|
|
|
mBitMapDirty = true;
|
2012-08-22 08:56:38 -07:00
|
|
|
return (int32_t)i * 32 + bit;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* DeallocateBlocks
|
|
|
|
*****************************************************************************/
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsDiskCacheBlockFile::DeallocateBlocks( int32_t startBlock, int32_t numBlocks)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
if (!mFD) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
if ((startBlock < 0) || ((uint32_t)startBlock > mBitMapWords * 32 - 1) ||
|
2007-03-22 10:30:00 -07:00
|
|
|
(numBlocks < 1) || (numBlocks > 4))
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
const int32_t startWord = startBlock >> 5; // Divide by 32
|
|
|
|
const uint32_t startBit = startBlock & 31; // Modulo by 32
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// make sure requested deallocation doesn't span a word boundary
|
|
|
|
if (startBit + numBlocks > 32) return NS_ERROR_UNEXPECTED;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mask = ((0x01 << numBlocks) - 1) << startBit;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// make sure requested deallocation is currently allocated
|
|
|
|
if ((mBitMap[startWord] & mask) != mask) return NS_ERROR_ABORT;
|
|
|
|
|
|
|
|
mBitMap[startWord] ^= mask; // flips the bits off;
|
2011-10-17 07:59:28 -07:00
|
|
|
mBitMapDirty = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
// XXX rv = FlushBitMap(); // coherency vs. performance
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* WriteBlocks
|
|
|
|
*****************************************************************************/
|
|
|
|
nsresult
|
|
|
|
nsDiskCacheBlockFile::WriteBlocks( void * buffer,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t size,
|
|
|
|
int32_t numBlocks,
|
|
|
|
int32_t * startBlock)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
// presume buffer != nullptr and startBlock != nullptr
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_TRUE(mFD, NS_ERROR_NOT_AVAILABLE);
|
2010-09-16 13:21:12 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// allocate some blocks in the cache block file
|
|
|
|
*startBlock = AllocateBlocks(numBlocks);
|
2011-01-18 06:12:10 -08:00
|
|
|
if (*startBlock < 0)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// seek to block position
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t blockPos = mBitMapWords * 4 + *startBlock * mBlockSize;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// write the blocks
|
2010-09-16 13:21:12 -07:00
|
|
|
return Write(blockPos, buffer, size) ? NS_OK : NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* ReadBlocks
|
|
|
|
*****************************************************************************/
|
|
|
|
nsresult
|
|
|
|
nsDiskCacheBlockFile::ReadBlocks( void * buffer,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t startBlock,
|
|
|
|
int32_t numBlocks,
|
|
|
|
int32_t * bytesRead)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
// presume buffer != nullptr and bytesRead != bytesRead
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
if (!mFD) return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
nsresult rv = VerifyAllocation(startBlock, numBlocks);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// seek to block position
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t blockPos = mBitMapWords * 4 + startBlock * mBlockSize;
|
|
|
|
int32_t filePos = PR_Seek(mFD, blockPos, PR_SEEK_SET);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (filePos != blockPos) return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
// read the blocks
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t bytesToRead = *bytesRead;
|
|
|
|
if ((bytesToRead <= 0) || ((uint32_t)bytesToRead > mBlockSize * numBlocks)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
bytesToRead = mBlockSize * numBlocks;
|
|
|
|
}
|
|
|
|
*bytesRead = PR_Read(mFD, buffer, bytesToRead);
|
|
|
|
|
2012-05-31 15:20:03 -07:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: nsDiskCacheBlockFile::Read [this=%p] "
|
|
|
|
"returned %d / %d bytes", this, *bytesRead, bytesToRead));
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* FlushBitMap
|
|
|
|
*****************************************************************************/
|
|
|
|
nsresult
|
|
|
|
nsDiskCacheBlockFile::FlushBitMap()
|
|
|
|
{
|
|
|
|
if (!mBitMapDirty) return NS_OK;
|
|
|
|
|
|
|
|
#if defined(IS_LITTLE_ENDIAN)
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t *bitmap = new uint32_t[mBitMapWords];
|
2007-03-22 10:30:00 -07:00
|
|
|
// Copy and swap to network format
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t *p = bitmap;
|
2011-01-18 06:12:10 -08:00
|
|
|
for (unsigned int i = 0; i < mBitMapWords; ++i, ++p)
|
2007-03-22 10:30:00 -07:00
|
|
|
*p = htonl(mBitMap[i]);
|
|
|
|
#else
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t *bitmap = mBitMap;
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// write bitmap
|
2011-01-18 06:12:10 -08:00
|
|
|
bool written = Write(0, bitmap, mBitMapWords * 4);
|
|
|
|
#if defined(IS_LITTLE_ENDIAN)
|
|
|
|
delete [] bitmap;
|
|
|
|
#endif
|
|
|
|
if (!written)
|
2010-09-16 13:21:12 -07:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
PRStatus err = PR_Sync(mFD);
|
|
|
|
if (err != PR_SUCCESS) return NS_ERROR_UNEXPECTED;
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
mBitMapDirty = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* VerifyAllocation
|
|
|
|
*
|
|
|
|
* Return values:
|
|
|
|
* NS_OK if all bits are marked allocated
|
|
|
|
* NS_ERROR_ILLEGAL_VALUE if parameters don't obey constraints
|
|
|
|
* NS_ERROR_FAILURE if some or all the bits are marked unallocated
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
nsresult
|
2012-08-22 08:56:38 -07:00
|
|
|
nsDiskCacheBlockFile::VerifyAllocation( int32_t startBlock, int32_t numBlocks)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
if ((startBlock < 0) || ((uint32_t)startBlock > mBitMapWords * 32 - 1) ||
|
2007-03-22 10:30:00 -07:00
|
|
|
(numBlocks < 1) || (numBlocks > 4))
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
const int32_t startWord = startBlock >> 5; // Divide by 32
|
|
|
|
const uint32_t startBit = startBlock & 31; // Modulo by 32
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// make sure requested deallocation doesn't span a word boundary
|
|
|
|
if (startBit + numBlocks > 32) return NS_ERROR_ILLEGAL_VALUE;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mask = ((0x01 << numBlocks) - 1) << startBit;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// check if all specified blocks are currently allocated
|
|
|
|
if ((mBitMap[startWord] & mask) != mask) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* CalcBlockFileSize
|
|
|
|
*
|
|
|
|
* Return size of the block file according to the bits set in mBitmap
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t
|
2007-03-22 10:30:00 -07:00
|
|
|
nsDiskCacheBlockFile::CalcBlockFileSize()
|
|
|
|
{
|
|
|
|
// search for last byte in mBitMap with allocated bits
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t estimatedSize = mBitMapWords * 4;
|
|
|
|
int32_t i = mBitMapWords;
|
2007-03-22 10:30:00 -07:00
|
|
|
while (--i >= 0) {
|
|
|
|
if (mBitMap[i]) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= 0) {
|
|
|
|
// binary search to find last allocated bit in byte
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mapWord = mBitMap[i];
|
|
|
|
uint32_t lastBit = 31;
|
2007-03-22 10:30:00 -07:00
|
|
|
if ((mapWord & 0xFFFF0000) == 0) { lastBit ^= 16; mapWord <<= 16; }
|
|
|
|
if ((mapWord & 0xFF000000) == 0) { lastBit ^= 8; mapWord <<= 8; }
|
|
|
|
if ((mapWord & 0xF0000000) == 0) { lastBit ^= 4; mapWord <<= 4; }
|
|
|
|
if ((mapWord & 0xC0000000) == 0) { lastBit ^= 2; mapWord <<= 2; }
|
|
|
|
if ((mapWord & 0x80000000) == 0) { lastBit ^= 1; mapWord <<= 1; }
|
|
|
|
estimatedSize += (i * 32 + lastBit + 1) * mBlockSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
return estimatedSize;
|
|
|
|
}
|
2010-09-16 13:21:12 -07:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* Write
|
|
|
|
*
|
|
|
|
* Wrapper around PR_Write that grows file in larger chunks to combat fragmentation
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
bool
|
2012-08-22 08:56:38 -07:00
|
|
|
nsDiskCacheBlockFile::Write(int32_t offset, const void *buf, int32_t amount)
|
2010-09-16 13:21:12 -07:00
|
|
|
{
|
|
|
|
/* Grow the file to 4mb right away, then double it until the file grows to 20mb.
|
|
|
|
20mb is a magic threshold because OSX stops autodefragging files bigger than that.
|
|
|
|
Beyond 20mb grow in 4mb chunks.
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
const int32_t upTo = offset + amount;
|
2010-09-16 13:21:12 -07:00
|
|
|
// Use a conservative definition of 20MB
|
2012-08-22 08:56:38 -07:00
|
|
|
const int32_t minPreallocate = 4*1024*1024;
|
|
|
|
const int32_t maxPreallocate = 20*1000*1000;
|
2010-09-16 13:21:12 -07:00
|
|
|
if (mFileSize < upTo) {
|
2010-09-24 05:20:23 -07:00
|
|
|
// maximal file size
|
2012-08-22 08:56:38 -07:00
|
|
|
const int32_t maxFileSize = mBitMapWords * 4 * (mBlockSize * 8 + 1);
|
2010-09-16 13:21:12 -07:00
|
|
|
if (upTo > maxPreallocate) {
|
|
|
|
// grow the file as a multiple of minPreallocate
|
|
|
|
mFileSize = ((upTo + minPreallocate - 1) / minPreallocate) * minPreallocate;
|
|
|
|
} else {
|
|
|
|
// Grow quickly between 1MB to 20MB
|
|
|
|
if (mFileSize)
|
|
|
|
while(mFileSize < upTo)
|
|
|
|
mFileSize *= 2;
|
2011-10-28 11:33:28 -07:00
|
|
|
mFileSize = clamped(mFileSize, minPreallocate, maxPreallocate);
|
2010-09-16 13:21:12 -07:00
|
|
|
}
|
2013-01-15 04:22:03 -08:00
|
|
|
mFileSize = std::min(mFileSize, maxFileSize);
|
2013-01-15 05:43:53 -08:00
|
|
|
#if !defined(XP_MACOSX)
|
|
|
|
mozilla::fallocate(mFD, mFileSize);
|
|
|
|
#endif
|
2010-09-16 13:21:12 -07:00
|
|
|
}
|
|
|
|
if (PR_Seek(mFD, offset, PR_SEEK_SET) != offset)
|
|
|
|
return false;
|
|
|
|
return PR_Write(mFD, buf, amount) == amount;
|
|
|
|
}
|
2012-11-05 10:22:33 -08:00
|
|
|
|
|
|
|
size_t
|
|
|
|
nsDiskCacheBlockFile::SizeOfExcludingThis(nsMallocSizeOfFun aMallocSizeOf)
|
|
|
|
{
|
|
|
|
return aMallocSizeOf(mBitMap) + aMallocSizeOf(mFD);
|
|
|
|
}
|