nsMemoryCacheDevice.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* -*- Mode: C++; tab-width: 8; 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 _nsMemoryCacheDevice_h_
  6. #define _nsMemoryCacheDevice_h_
  7. #include "nsCacheDevice.h"
  8. #include "PLDHashTable.h"
  9. #include "nsCacheEntry.h"
  10. class nsMemoryCacheDeviceInfo;
  11. /******************************************************************************
  12. * nsMemoryCacheDevice
  13. ******************************************************************************/
  14. class nsMemoryCacheDevice : public nsCacheDevice
  15. {
  16. public:
  17. nsMemoryCacheDevice();
  18. virtual ~nsMemoryCacheDevice();
  19. virtual nsresult Init();
  20. virtual nsresult Shutdown();
  21. virtual const char * GetDeviceID(void);
  22. virtual nsresult BindEntry( nsCacheEntry * entry );
  23. virtual nsCacheEntry * FindEntry( nsCString * key, bool *collision );
  24. virtual void DoomEntry( nsCacheEntry * entry );
  25. virtual nsresult DeactivateEntry( nsCacheEntry * entry );
  26. virtual nsresult OpenInputStreamForEntry(nsCacheEntry * entry,
  27. nsCacheAccessMode mode,
  28. uint32_t offset,
  29. nsIInputStream ** result);
  30. virtual nsresult OpenOutputStreamForEntry(nsCacheEntry * entry,
  31. nsCacheAccessMode mode,
  32. uint32_t offset,
  33. nsIOutputStream ** result);
  34. virtual nsresult GetFileForEntry( nsCacheEntry * entry,
  35. nsIFile ** result );
  36. virtual nsresult OnDataSizeChange( nsCacheEntry * entry, int32_t deltaSize );
  37. virtual nsresult Visit( nsICacheVisitor * visitor );
  38. virtual nsresult EvictEntries(const char * clientID);
  39. nsresult EvictPrivateEntries();
  40. void SetCapacity(int32_t capacity);
  41. void SetMaxEntrySize(int32_t maxSizeInKilobytes);
  42. bool EntryIsTooBig(int64_t entrySize);
  43. size_t TotalSize();
  44. private:
  45. friend class nsMemoryCacheDeviceInfo;
  46. enum { DELETE_ENTRY = true,
  47. DO_NOT_DELETE_ENTRY = false };
  48. void AdjustMemoryLimits( int32_t softLimit, int32_t hardLimit);
  49. void EvictEntry( nsCacheEntry * entry , bool deleteEntry);
  50. void EvictEntriesIfNecessary();
  51. int EvictionList(nsCacheEntry * entry, int32_t deltaSize);
  52. typedef bool (*EvictionMatcherFn)(nsCacheEntry* entry, void* args);
  53. nsresult DoEvictEntries(EvictionMatcherFn matchFn, void* args);
  54. #ifdef DEBUG
  55. void CheckEntryCount();
  56. #endif
  57. /*
  58. * Data members
  59. */
  60. enum {
  61. kQueueCount = 24 // entries > 2^23 (8Mb) start in last queue
  62. };
  63. nsCacheEntryHashTable mMemCacheEntries;
  64. bool mInitialized;
  65. PRCList mEvictionList[kQueueCount];
  66. int32_t mHardLimit;
  67. int32_t mSoftLimit;
  68. int32_t mTotalSize;
  69. int32_t mInactiveSize;
  70. int32_t mEntryCount;
  71. int32_t mMaxEntryCount;
  72. int32_t mMaxEntrySize; // internal unit is bytes
  73. // XXX what other stats do we want to keep?
  74. };
  75. /******************************************************************************
  76. * nsMemoryCacheDeviceInfo - used to call nsIVisitor for about:cache
  77. ******************************************************************************/
  78. class nsMemoryCacheDeviceInfo : public nsICacheDeviceInfo {
  79. public:
  80. NS_DECL_ISUPPORTS
  81. NS_DECL_NSICACHEDEVICEINFO
  82. explicit nsMemoryCacheDeviceInfo(nsMemoryCacheDevice* device)
  83. : mDevice(device)
  84. {
  85. }
  86. private:
  87. virtual ~nsMemoryCacheDeviceInfo() {}
  88. nsMemoryCacheDevice* mDevice;
  89. };
  90. #endif // _nsMemoryCacheDevice_h_