nsDiskCacheBlockFile.h 2.5 KB

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