2001-04-17 07:54:48 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
2012-05-21 15:12:37 +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/. */
|
2001-04-17 07:54:48 +04:00
|
|
|
|
2012-06-01 02:20:03 +04:00
|
|
|
#include "nsCache.h"
|
2002-08-07 05:13:29 +04:00
|
|
|
#include "nsDiskCache.h"
|
2001-04-17 07:54:48 +04:00
|
|
|
#include "nsDiskCacheBlockFile.h"
|
2010-09-17 00:21:12 +04:00
|
|
|
#include "mozilla/FileUtils.h"
|
2013-06-23 16:03:39 +04:00
|
|
|
#include "mozilla/MemoryReporting.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2001-04-17 07:54:48 +04:00
|
|
|
|
2011-10-28 22:33:28 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2001-04-17 07:54:48 +04:00
|
|
|
/******************************************************************************
|
|
|
|
* nsDiskCacheBlockFile -
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* Open
|
|
|
|
*****************************************************************************/
|
|
|
|
nsresult
|
2012-06-06 06:08:30 +04:00
|
|
|
nsDiskCacheBlockFile::Open(nsIFile * blockFile,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t blockSize,
|
|
|
|
uint32_t bitMapSize,
|
2012-07-21 03:47:52 +04:00
|
|
|
nsDiskCache::CorruptCacheInfo * corruptInfo)
|
2001-04-17 07:54:48 +04:00
|
|
|
{
|
2012-07-21 03:47:52 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(corruptInfo);
|
|
|
|
*corruptInfo = nsDiskCache::kUnexpectedError;
|
|
|
|
|
|
|
|
if (bitMapSize % 32) {
|
|
|
|
*corruptInfo = nsDiskCache::kInvalidArgPointer;
|
2011-01-18 17:12:10 +03:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2012-07-21 03:47:52 +04:00
|
|
|
}
|
2011-01-18 17:12:10 +03:00
|
|
|
|
2001-04-17 07:54:48 +04:00
|
|
|
mBlockSize = blockSize;
|
2011-01-18 17:12:10 +03:00
|
|
|
mBitMapWords = bitMapSize / 32;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t bitMapBytes = mBitMapWords * 4;
|
2001-04-17 07:54:48 +04:00
|
|
|
|
2005-06-10 21:32:48 +04:00
|
|
|
// open the file - restricted to user, the data could be confidential
|
|
|
|
nsresult rv = blockFile->OpenNSPRFileDesc(PR_RDWR | PR_CREATE_FILE, 00600, &mFD);
|
2012-06-01 02:20:03 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
2012-07-21 03:47:52 +04:00
|
|
|
*corruptInfo = nsDiskCache::kCouldNotCreateBlockFile;
|
2012-06-01 02:20:03 +04: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
|
|
|
|
}
|
2001-04-17 07:54:48 +04:00
|
|
|
|
|
|
|
// allocate bit map buffer
|
2012-08-22 19:56:38 +04:00
|
|
|
mBitMap = new uint32_t[mBitMapWords];
|
2001-04-17 07:54:48 +04:00
|
|
|
|
|
|
|
// check if we just creating the file
|
2010-09-17 00:21:12 +04:00
|
|
|
mFileSize = PR_Available(mFD);
|
|
|
|
if (mFileSize < 0) {
|
2001-05-19 03:24:09 +04:00
|
|
|
// XXX an error occurred. We could call PR_GetError(), but how would that help?
|
2012-07-21 03:47:52 +04:00
|
|
|
*corruptInfo = nsDiskCache::kBlockFileSizeError;
|
2001-05-19 03:24:09 +04:00
|
|
|
rv = NS_ERROR_UNEXPECTED;
|
|
|
|
goto error_exit;
|
|
|
|
}
|
2010-09-17 00:21:12 +04:00
|
|
|
if (mFileSize == 0) {
|
2001-04-17 07:54:48 +04:00
|
|
|
// initialize bit map and write it
|
2011-01-18 17:12:10 +03:00
|
|
|
memset(mBitMap, 0, bitMapBytes);
|
2012-07-21 03:47:52 +04:00
|
|
|
if (!Write(0, mBitMap, bitMapBytes)) {
|
|
|
|
*corruptInfo = nsDiskCache::kBlockFileBitMapWriteError;
|
2005-10-08 05:20:36 +04:00
|
|
|
goto error_exit;
|
2012-07-21 03:47:52 +04:00
|
|
|
}
|
2001-04-17 07:54:48 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
} else if ((uint32_t)mFileSize < bitMapBytes) {
|
2012-07-21 03:47:52 +04:00
|
|
|
*corruptInfo = nsDiskCache::kBlockFileSizeLessThanBitMap;
|
2001-04-17 07:54:48 +04:00
|
|
|
rv = NS_ERROR_UNEXPECTED; // XXX NS_ERROR_CACHE_INVALID;
|
|
|
|
goto error_exit;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
// read the bit map
|
2012-08-22 19:56:38 +04:00
|
|
|
const int32_t bytesRead = PR_Read(mFD, mBitMap, bitMapBytes);
|
|
|
|
if ((bytesRead < 0) || ((uint32_t)bytesRead < bitMapBytes)) {
|
2012-07-21 03:47:52 +04:00
|
|
|
*corruptInfo = nsDiskCache::kBlockFileBitMapReadError;
|
2001-04-17 07:54:48 +04:00
|
|
|
rv = NS_ERROR_UNEXPECTED;
|
|
|
|
goto error_exit;
|
|
|
|
}
|
2005-10-10 21:30:14 +04:00
|
|
|
#if defined(IS_LITTLE_ENDIAN)
|
|
|
|
// Swap from network format
|
2011-01-18 17:12:10 +03:00
|
|
|
for (unsigned int i = 0; i < mBitMapWords; ++i)
|
2005-10-10 21:30:14 +04:00
|
|
|
mBitMap[i] = ntohl(mBitMap[i]);
|
|
|
|
#endif
|
2005-10-08 05:20:36 +04:00
|
|
|
// validate block file size
|
2006-05-26 22:21:12 +04:00
|
|
|
// 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 19:56:38 +04:00
|
|
|
const uint32_t estimatedSize = CalcBlockFileSize();
|
|
|
|
if ((uint32_t)mFileSize + blockSize < estimatedSize) {
|
2012-07-21 03:47:52 +04:00
|
|
|
*corruptInfo = nsDiskCache::kBlockFileEstimatedSizeError;
|
2005-10-08 05:20:36 +04:00
|
|
|
rv = NS_ERROR_UNEXPECTED;
|
|
|
|
goto error_exit;
|
|
|
|
}
|
2001-04-17 07:54:48 +04:00
|
|
|
}
|
2012-06-01 02:20:03 +04:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: nsDiskCacheBlockFile::Open [this=%p] succeeded",
|
|
|
|
this));
|
2001-04-17 07:54:48 +04:00
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
error_exit:
|
2012-06-01 02:20:03 +04:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: nsDiskCacheBlockFile::Open [this=%p] failed with "
|
|
|
|
"error %d", this, rv));
|
2011-10-17 18:59:28 +04:00
|
|
|
Close(false);
|
2001-04-17 07:54:48 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* Close
|
|
|
|
*****************************************************************************/
|
|
|
|
nsresult
|
2011-09-29 10:19:26 +04:00
|
|
|
nsDiskCacheBlockFile::Close(bool flush)
|
2001-04-17 07:54:48 +04:00
|
|
|
{
|
2003-03-11 02:19:05 +03:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
2005-10-08 05:20:36 +04:00
|
|
|
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 18:20:58 +04:00
|
|
|
mFD = nullptr;
|
2005-10-08 05:20:36 +04:00
|
|
|
}
|
2003-03-11 02:19:05 +03:00
|
|
|
|
2006-05-26 22:21:12 +04:00
|
|
|
if (mBitMap) {
|
|
|
|
delete [] mBitMap;
|
2012-07-30 18:20:58 +04:00
|
|
|
mBitMap = nullptr;
|
2006-05-26 22:21:12 +04:00
|
|
|
}
|
2001-05-19 03:24:09 +04:00
|
|
|
|
|
|
|
return rv;
|
2001-04-17 07:54:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* 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 19:56:38 +04:00
|
|
|
int32_t
|
|
|
|
nsDiskCacheBlockFile::AllocateBlocks(int32_t numBlocks)
|
2001-04-17 07:54:48 +04:00
|
|
|
{
|
2005-10-08 05:20:36 +04:00
|
|
|
const int maxPos = 32 - numBlocks;
|
2012-08-22 19:56:38 +04:00
|
|
|
const uint32_t mask = (0x01 << numBlocks) - 1;
|
2011-01-18 17:12:10 +03:00
|
|
|
for (unsigned int i = 0; i < mBitMapWords; ++i) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mapWord = ~mBitMap[i]; // flip bits so free bits are 1
|
2005-10-08 05:20:36 +04: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 18:59:28 +04:00
|
|
|
mBitMapDirty = true;
|
2012-08-22 19:56:38 +04:00
|
|
|
return (int32_t)i * 32 + bit;
|
2005-10-08 05:20:36 +04:00
|
|
|
}
|
|
|
|
}
|
2001-04-17 07:54:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* DeallocateBlocks
|
|
|
|
*****************************************************************************/
|
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
nsDiskCacheBlockFile::DeallocateBlocks( int32_t startBlock, int32_t numBlocks)
|
2001-04-17 07:54:48 +04:00
|
|
|
{
|
2001-05-19 03:24:09 +04:00
|
|
|
if (!mFD) return NS_ERROR_NOT_AVAILABLE;
|
2005-10-08 05:20:36 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
if ((startBlock < 0) || ((uint32_t)startBlock > mBitMapWords * 32 - 1) ||
|
2001-04-17 07:54:48 +04:00
|
|
|
(numBlocks < 1) || (numBlocks > 4))
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
const int32_t startWord = startBlock >> 5; // Divide by 32
|
|
|
|
const uint32_t startBit = startBlock & 31; // Modulo by 32
|
2005-10-08 05:20:36 +04:00
|
|
|
|
|
|
|
// make sure requested deallocation doesn't span a word boundary
|
|
|
|
if (startBit + numBlocks > 32) return NS_ERROR_UNEXPECTED;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mask = ((0x01 << numBlocks) - 1) << startBit;
|
2001-04-17 07:54:48 +04:00
|
|
|
|
|
|
|
// make sure requested deallocation is currently allocated
|
2005-10-08 05:20:36 +04:00
|
|
|
if ((mBitMap[startWord] & mask) != mask) return NS_ERROR_ABORT;
|
|
|
|
|
|
|
|
mBitMap[startWord] ^= mask; // flips the bits off;
|
2011-10-17 18:59:28 +04:00
|
|
|
mBitMapDirty = true;
|
2001-04-17 07:54:48 +04:00
|
|
|
// XXX rv = FlushBitMap(); // coherency vs. performance
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* WriteBlocks
|
|
|
|
*****************************************************************************/
|
|
|
|
nsresult
|
2001-05-19 03:24:09 +04:00
|
|
|
nsDiskCacheBlockFile::WriteBlocks( void * buffer,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t size,
|
|
|
|
int32_t numBlocks,
|
|
|
|
int32_t * startBlock)
|
2001-04-17 07:54:48 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
// presume buffer != nullptr and startBlock != nullptr
|
2006-03-09 23:02:54 +03:00
|
|
|
NS_ENSURE_TRUE(mFD, NS_ERROR_NOT_AVAILABLE);
|
2010-09-17 00:21:12 +04:00
|
|
|
|
2006-05-26 22:21:12 +04:00
|
|
|
// allocate some blocks in the cache block file
|
|
|
|
*startBlock = AllocateBlocks(numBlocks);
|
2011-01-18 17:12:10 +03:00
|
|
|
if (*startBlock < 0)
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
|
2001-04-17 07:54:48 +04:00
|
|
|
// seek to block position
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t blockPos = mBitMapWords * 4 + *startBlock * mBlockSize;
|
2001-04-17 07:54:48 +04:00
|
|
|
|
|
|
|
// write the blocks
|
2010-09-17 00:21:12 +04:00
|
|
|
return Write(blockPos, buffer, size) ? NS_OK : NS_ERROR_FAILURE;
|
2001-04-17 07:54:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* ReadBlocks
|
|
|
|
*****************************************************************************/
|
|
|
|
nsresult
|
2006-05-26 22:21:12 +04:00
|
|
|
nsDiskCacheBlockFile::ReadBlocks( void * buffer,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t startBlock,
|
|
|
|
int32_t numBlocks,
|
|
|
|
int32_t * bytesRead)
|
2001-04-17 07:54:48 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
// presume buffer != nullptr and bytesRead != bytesRead
|
2006-05-26 22:21:12 +04:00
|
|
|
|
2001-05-19 03:24:09 +04:00
|
|
|
if (!mFD) return NS_ERROR_NOT_AVAILABLE;
|
2001-04-17 07:54:48 +04:00
|
|
|
nsresult rv = VerifyAllocation(startBlock, numBlocks);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// seek to block position
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t blockPos = mBitMapWords * 4 + startBlock * mBlockSize;
|
|
|
|
int32_t filePos = PR_Seek(mFD, blockPos, PR_SEEK_SET);
|
2001-05-19 03:24:09 +04:00
|
|
|
if (filePos != blockPos) return NS_ERROR_UNEXPECTED;
|
2001-04-17 07:54:48 +04:00
|
|
|
|
|
|
|
// read the blocks
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t bytesToRead = *bytesRead;
|
|
|
|
if ((bytesToRead <= 0) || ((uint32_t)bytesToRead > mBlockSize * numBlocks)) {
|
2006-05-26 22:21:12 +04:00
|
|
|
bytesToRead = mBlockSize * numBlocks;
|
|
|
|
}
|
|
|
|
*bytesRead = PR_Read(mFD, buffer, bytesToRead);
|
2001-04-17 07:54:48 +04:00
|
|
|
|
2012-06-01 02:20:03 +04:00
|
|
|
CACHE_LOG_DEBUG(("CACHE: nsDiskCacheBlockFile::Read [this=%p] "
|
|
|
|
"returned %d / %d bytes", this, *bytesRead, bytesToRead));
|
|
|
|
|
2006-05-26 22:21:12 +04:00
|
|
|
return NS_OK;
|
2001-04-17 07:54:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* FlushBitMap
|
|
|
|
*****************************************************************************/
|
|
|
|
nsresult
|
|
|
|
nsDiskCacheBlockFile::FlushBitMap()
|
|
|
|
{
|
|
|
|
if (!mBitMapDirty) return NS_OK;
|
|
|
|
|
2005-10-08 05:20:36 +04:00
|
|
|
#if defined(IS_LITTLE_ENDIAN)
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t *bitmap = new uint32_t[mBitMapWords];
|
2005-10-08 05:20:36 +04:00
|
|
|
// Copy and swap to network format
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t *p = bitmap;
|
2011-01-18 17:12:10 +03:00
|
|
|
for (unsigned int i = 0; i < mBitMapWords; ++i, ++p)
|
2005-10-08 05:20:36 +04:00
|
|
|
*p = htonl(mBitMap[i]);
|
|
|
|
#else
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t *bitmap = mBitMap;
|
2005-10-08 05:20:36 +04:00
|
|
|
#endif
|
|
|
|
|
2001-04-17 07:54:48 +04:00
|
|
|
// write bitmap
|
2011-01-18 17:12:10 +03:00
|
|
|
bool written = Write(0, bitmap, mBitMapWords * 4);
|
|
|
|
#if defined(IS_LITTLE_ENDIAN)
|
|
|
|
delete [] bitmap;
|
|
|
|
#endif
|
|
|
|
if (!written)
|
2010-09-17 00:21:12 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2001-05-19 03:24:09 +04:00
|
|
|
|
|
|
|
PRStatus err = PR_Sync(mFD);
|
|
|
|
if (err != PR_SUCCESS) return NS_ERROR_UNEXPECTED;
|
2001-04-17 07:54:48 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mBitMapDirty = false;
|
2001-04-17 07:54:48 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
2005-10-08 05:20:36 +04:00
|
|
|
* VerifyAllocation
|
2001-04-17 07:54:48 +04:00
|
|
|
*
|
|
|
|
* 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 19:56:38 +04:00
|
|
|
nsDiskCacheBlockFile::VerifyAllocation( int32_t startBlock, int32_t numBlocks)
|
2001-04-17 07:54:48 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
if ((startBlock < 0) || ((uint32_t)startBlock > mBitMapWords * 32 - 1) ||
|
2001-04-17 07:54:48 +04:00
|
|
|
(numBlocks < 1) || (numBlocks > 4))
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
const int32_t startWord = startBlock >> 5; // Divide by 32
|
|
|
|
const uint32_t startBit = startBlock & 31; // Modulo by 32
|
2005-10-08 05:20:36 +04:00
|
|
|
|
|
|
|
// make sure requested deallocation doesn't span a word boundary
|
|
|
|
if (startBit + numBlocks > 32) return NS_ERROR_ILLEGAL_VALUE;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mask = ((0x01 << numBlocks) - 1) << startBit;
|
2001-04-17 07:54:48 +04:00
|
|
|
|
|
|
|
// check if all specified blocks are currently allocated
|
2005-10-08 05:20:36 +04:00
|
|
|
if ((mBitMap[startWord] & mask) != mask) return NS_ERROR_FAILURE;
|
2001-04-17 07:54:48 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************************
|
2005-10-08 05:20:36 +04:00
|
|
|
* CalcBlockFileSize
|
2001-04-17 07:54:48 +04:00
|
|
|
*
|
2005-10-08 05:20:36 +04:00
|
|
|
* Return size of the block file according to the bits set in mBitmap
|
2001-04-17 07:54:48 +04:00
|
|
|
*
|
|
|
|
*****************************************************************************/
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t
|
2005-10-08 05:20:36 +04:00
|
|
|
nsDiskCacheBlockFile::CalcBlockFileSize()
|
2001-04-17 07:54:48 +04:00
|
|
|
{
|
|
|
|
// search for last byte in mBitMap with allocated bits
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t estimatedSize = mBitMapWords * 4;
|
|
|
|
int32_t i = mBitMapWords;
|
2001-04-17 07:54:48 +04:00
|
|
|
while (--i >= 0) {
|
|
|
|
if (mBitMap[i]) break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i >= 0) {
|
|
|
|
// binary search to find last allocated bit in byte
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mapWord = mBitMap[i];
|
|
|
|
uint32_t lastBit = 31;
|
2005-10-08 05:20:36 +04: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;
|
2001-04-17 07:54:48 +04:00
|
|
|
}
|
|
|
|
|
2005-10-08 05:20:36 +04:00
|
|
|
return estimatedSize;
|
2001-04-17 07:54:48 +04:00
|
|
|
}
|
2010-09-17 00:21:12 +04:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* Write
|
|
|
|
*
|
|
|
|
* Wrapper around PR_Write that grows file in larger chunks to combat fragmentation
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
bool
|
2012-08-22 19:56:38 +04:00
|
|
|
nsDiskCacheBlockFile::Write(int32_t offset, const void *buf, int32_t amount)
|
2010-09-17 00:21:12 +04: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 19:56:38 +04:00
|
|
|
const int32_t upTo = offset + amount;
|
2010-09-17 00:21:12 +04:00
|
|
|
// Use a conservative definition of 20MB
|
2012-08-22 19:56:38 +04:00
|
|
|
const int32_t minPreallocate = 4*1024*1024;
|
|
|
|
const int32_t maxPreallocate = 20*1000*1000;
|
2010-09-17 00:21:12 +04:00
|
|
|
if (mFileSize < upTo) {
|
2010-09-24 16:20:23 +04:00
|
|
|
// maximal file size
|
2012-08-22 19:56:38 +04:00
|
|
|
const int32_t maxFileSize = mBitMapWords * 4 * (mBlockSize * 8 + 1);
|
2010-09-17 00:21:12 +04: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 22:33:28 +04:00
|
|
|
mFileSize = clamped(mFileSize, minPreallocate, maxPreallocate);
|
2010-09-17 00:21:12 +04:00
|
|
|
}
|
2013-01-15 16:22:03 +04:00
|
|
|
mFileSize = std::min(mFileSize, maxFileSize);
|
2013-01-15 17:43:53 +04:00
|
|
|
#if !defined(XP_MACOSX)
|
|
|
|
mozilla::fallocate(mFD, mFileSize);
|
|
|
|
#endif
|
2010-09-17 00:21:12 +04:00
|
|
|
}
|
|
|
|
if (PR_Seek(mFD, offset, PR_SEEK_SET) != offset)
|
|
|
|
return false;
|
|
|
|
return PR_Write(mFD, buf, amount) == amount;
|
|
|
|
}
|
2012-11-05 22:22:33 +04:00
|
|
|
|
|
|
|
size_t
|
2013-06-23 16:03:39 +04:00
|
|
|
nsDiskCacheBlockFile::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf)
|
2012-11-05 22:22:33 +04:00
|
|
|
{
|
|
|
|
return aMallocSizeOf(mBitMap) + aMallocSizeOf(mFD);
|
|
|
|
}
|