michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* vim: set sw=4 ts=8 et tw=80 : */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef _nsDiskCacheBlockFile_h_ michael@0: #define _nsDiskCacheBlockFile_h_ michael@0: michael@0: #include "mozilla/MemoryReporting.h" michael@0: #include "nsIFile.h" michael@0: #include "nsDiskCache.h" michael@0: michael@0: /****************************************************************************** michael@0: * nsDiskCacheBlockFile michael@0: * michael@0: * The structure of a cache block file is a 4096 bytes bit map, followed by michael@0: * some number of blocks of mBlockSize. The creator of a michael@0: * nsDiskCacheBlockFile object must provide the block size for a given file. michael@0: * michael@0: *****************************************************************************/ michael@0: class nsDiskCacheBlockFile { michael@0: public: michael@0: nsDiskCacheBlockFile() michael@0: : mFD(nullptr) michael@0: , mBitMap(nullptr) michael@0: , mBlockSize(0) michael@0: , mBitMapWords(0) michael@0: , mFileSize(0) michael@0: , mBitMapDirty(false) michael@0: {} michael@0: ~nsDiskCacheBlockFile() { (void) Close(true); } michael@0: michael@0: nsresult Open( nsIFile * blockFile, uint32_t blockSize, michael@0: uint32_t bitMapSize, nsDiskCache::CorruptCacheInfo * corruptInfo); michael@0: nsresult Close(bool flush); michael@0: michael@0: /* michael@0: * Trim michael@0: * Truncates the block file to the end of the last allocated block. michael@0: */ michael@0: nsresult Trim() { return nsDiskCache::Truncate(mFD, CalcBlockFileSize()); } michael@0: nsresult DeallocateBlocks( int32_t startBlock, int32_t numBlocks); michael@0: nsresult WriteBlocks( void * buffer, uint32_t size, int32_t numBlocks, michael@0: int32_t * startBlock); michael@0: nsresult ReadBlocks( void * buffer, int32_t startBlock, int32_t numBlocks, michael@0: int32_t * bytesRead); michael@0: michael@0: size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf); michael@0: michael@0: private: michael@0: nsresult FlushBitMap(); michael@0: int32_t AllocateBlocks( int32_t numBlocks); michael@0: nsresult VerifyAllocation( int32_t startBlock, int32_t numBLocks); michael@0: uint32_t CalcBlockFileSize(); michael@0: bool Write(int32_t offset, const void *buf, int32_t amount); michael@0: michael@0: /** michael@0: * Data members michael@0: */ michael@0: PRFileDesc * mFD; michael@0: uint32_t * mBitMap; // XXX future: array of bit map blocks michael@0: uint32_t mBlockSize; michael@0: uint32_t mBitMapWords; michael@0: int32_t mFileSize; michael@0: bool mBitMapDirty; michael@0: }; michael@0: michael@0: #endif // _nsDiskCacheBlockFile_h_