Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* vim: set sw=4 ts=8 et tw=80 : */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef _nsDiskCacheBlockFile_h_ |
michael@0 | 8 | #define _nsDiskCacheBlockFile_h_ |
michael@0 | 9 | |
michael@0 | 10 | #include "mozilla/MemoryReporting.h" |
michael@0 | 11 | #include "nsIFile.h" |
michael@0 | 12 | #include "nsDiskCache.h" |
michael@0 | 13 | |
michael@0 | 14 | /****************************************************************************** |
michael@0 | 15 | * nsDiskCacheBlockFile |
michael@0 | 16 | * |
michael@0 | 17 | * The structure of a cache block file is a 4096 bytes bit map, followed by |
michael@0 | 18 | * some number of blocks of mBlockSize. The creator of a |
michael@0 | 19 | * nsDiskCacheBlockFile object must provide the block size for a given file. |
michael@0 | 20 | * |
michael@0 | 21 | *****************************************************************************/ |
michael@0 | 22 | class nsDiskCacheBlockFile { |
michael@0 | 23 | public: |
michael@0 | 24 | nsDiskCacheBlockFile() |
michael@0 | 25 | : mFD(nullptr) |
michael@0 | 26 | , mBitMap(nullptr) |
michael@0 | 27 | , mBlockSize(0) |
michael@0 | 28 | , mBitMapWords(0) |
michael@0 | 29 | , mFileSize(0) |
michael@0 | 30 | , mBitMapDirty(false) |
michael@0 | 31 | {} |
michael@0 | 32 | ~nsDiskCacheBlockFile() { (void) Close(true); } |
michael@0 | 33 | |
michael@0 | 34 | nsresult Open( nsIFile * blockFile, uint32_t blockSize, |
michael@0 | 35 | uint32_t bitMapSize, nsDiskCache::CorruptCacheInfo * corruptInfo); |
michael@0 | 36 | nsresult Close(bool flush); |
michael@0 | 37 | |
michael@0 | 38 | /* |
michael@0 | 39 | * Trim |
michael@0 | 40 | * Truncates the block file to the end of the last allocated block. |
michael@0 | 41 | */ |
michael@0 | 42 | nsresult Trim() { return nsDiskCache::Truncate(mFD, CalcBlockFileSize()); } |
michael@0 | 43 | nsresult DeallocateBlocks( int32_t startBlock, int32_t numBlocks); |
michael@0 | 44 | nsresult WriteBlocks( void * buffer, uint32_t size, int32_t numBlocks, |
michael@0 | 45 | int32_t * startBlock); |
michael@0 | 46 | nsresult ReadBlocks( void * buffer, int32_t startBlock, int32_t numBlocks, |
michael@0 | 47 | int32_t * bytesRead); |
michael@0 | 48 | |
michael@0 | 49 | size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf); |
michael@0 | 50 | |
michael@0 | 51 | private: |
michael@0 | 52 | nsresult FlushBitMap(); |
michael@0 | 53 | int32_t AllocateBlocks( int32_t numBlocks); |
michael@0 | 54 | nsresult VerifyAllocation( int32_t startBlock, int32_t numBLocks); |
michael@0 | 55 | uint32_t CalcBlockFileSize(); |
michael@0 | 56 | bool Write(int32_t offset, const void *buf, int32_t amount); |
michael@0 | 57 | |
michael@0 | 58 | /** |
michael@0 | 59 | * Data members |
michael@0 | 60 | */ |
michael@0 | 61 | PRFileDesc * mFD; |
michael@0 | 62 | uint32_t * mBitMap; // XXX future: array of bit map blocks |
michael@0 | 63 | uint32_t mBlockSize; |
michael@0 | 64 | uint32_t mBitMapWords; |
michael@0 | 65 | int32_t mFileSize; |
michael@0 | 66 | bool mBitMapDirty; |
michael@0 | 67 | }; |
michael@0 | 68 | |
michael@0 | 69 | #endif // _nsDiskCacheBlockFile_h_ |